Skip to content

Commit

Permalink
able to push path into the table
Browse files Browse the repository at this point in the history
  • Loading branch information
shibisuriya committed Aug 11, 2024
1 parent 11de56b commit 60e0e10
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ dist-ssr
# Don't push the deploy keys to the public...
.npmrc

test_database.db
test-database.db
46 changes: 45 additions & 1 deletion SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
To experiment with SQLite,

```bash
docker run -it --rm -v "$(pwd)":/data keinos/sqlite3 sqlite3 /data/test_database.db
docker run -it --rm -v "$(pwd)":/data keinos/sqlite3 sqlite3 /data/test-database.db
```

## Basic commands
Expand All @@ -31,3 +31,47 @@ CREATE TABLE users (
age INTEGER
);
```

# DB design

```sql
CREATE TABLE undo_stack (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL
);
```

```sql
CREATE TABLE redo_stack (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL
);
```

## Push operation

```sql
INSERT INTO undo_stack (path) VALUES ('directory path');
```

## Pop operation

```sql
BEGIN TRANSACTION;

-- Retrieve the item with the highest id
WITH item_to_pop AS (
SELECT id, value
FROM stack
ORDER BY id DESC
LIMIT 1
)
-- Select the value of the item to pop (before deleting it)
SELECT value FROM item_to_pop;

-- Delete the item
DELETE FROM stack
WHERE id = (SELECT id FROM item_to_pop);

COMMIT;
```
16 changes: 16 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sqlite3 from 'sqlite3'
import path from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const dbPath = path.join(__dirname, './test-database.db')

const db = new sqlite3.Database(dbPath, (err) => {
if (err) {
return
}
})

export { db }
68 changes: 62 additions & 6 deletions src/push.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,66 @@
import { db } from './db.js'

function push(sessionId, path) {
// Clear the redo stack!
console.log('session id => ', sessionId)
console.log('path => ', path)
// Create a table in the db for each shell instance, so
// that we can simply drop the table if a shell instance is
// killed.
// Clear the redo stack if present!

db.serialize(() => {
const tableName = `undo_stack_${sessionId}`
// Create a table in the db for each shell instance, so
// that we can simply drop the table if a shell instance is
// killed.
db.run(
`CREATE TABLE IF NOT EXISTS ${tableName} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL
)`,
(err) => {
if (err) {
console.error('Error creating table:', err.message)
} else {
const stmt = db.prepare(
`INSERT INTO ${tableName} (path) VALUES (?)`
)
stmt.run(path, (err) => {
if (err) {
console.error('Error inserting data:', err.message)
} else {
console.log('Data inserted successfully.')

// Retrieve and print the contents of the table
db.all(
`SELECT * FROM ${tableName};`,
(err, rows) => {
if (err) {
console.error(
`Error retrieving data from ${tableName}:`,
err.message
)
} else {
console.log(
`Contents of table ${tableName}:`
)
console.log(rows)
}

// Finalize the statement and close the database
stmt.finalize(() => {
db.close((err) => {
if (err) {
console.error(
'Error closing the database:',
err.message
)
}
})
})
}
)
}
})
}
}
)
})
}

export { push }

0 comments on commit 60e0e10

Please sign in to comment.