Skip to content

Commit

Permalink
New tags-Table added
Browse files Browse the repository at this point in the history
  • Loading branch information
gerold-penz committed Sep 15, 2024
1 parent 83d92bb commit c83236e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ Writes a value into the database and returns the key.

### key

The key can be a string or `undefined`.
If the `key` is `undefined`, a UUID is generated as the key.
The `key` can be a string or `undefined`.
If the `key` is `undefined`, a UUID is used instead.

### value

Expand Down
4 changes: 0 additions & 4 deletions examples/binaryExampleSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,3 @@ const targetBuffer = store.get("my-image")
writeFileSync("<Target File Path>", targetBuffer)






6 changes: 6 additions & 0 deletions examples/createTestDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BunSqliteKeyValue } from "../src"


const store = new BunSqliteKeyValue("test_database.sqlite")
store.close()

24 changes: 20 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,28 @@ export class BunSqliteKeyValue {
// Open database
this.db = new Database(filename, dbOptions)
this.db.run("PRAGMA journal_mode = WAL")

// Create table and indexes
this.db.run("CREATE TABLE IF NOT EXISTS items (key TEXT PRIMARY KEY, value BLOB, expires INT)")
this.db.run("CREATE UNIQUE INDEX IF NOT EXISTS ix_items_key ON items (key)")
this.db.run("PRAGMA foreign_keys = ON")

// Create items table and indexes
this.db.run(`
CREATE TABLE IF NOT EXISTS items (
key TEXT NOT NULL PRIMARY KEY,
value BLOB,
expires INT
) STRICT`)
// Not required: this.db.run("CREATE UNIQUE INDEX IF NOT EXISTS ix_items_key ON items (key)")
this.db.run("CREATE INDEX IF NOT EXISTS ix_items_expires ON items (expires)")

// Create tags table and indexes
this.db.run(`
CREATE TABLE IF NOT EXISTS tags (
tag TEXT NOT NULL,
item_key TEXT NOT NULL REFERENCES items ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (tag, item_key)
) STRICT`)
// Not required: this.db.run("CREATE UNIQUE INDEX IF NOT EXISTS ix_tags_unique ON tags (tag, item_key)")
this.db.run("CREATE INDEX IF NOT EXISTS ix_tags_item_key ON tags (item_key)")

// Prepare and cache statements
this.clearStatement = this.db.query("DELETE FROM items")
this.deleteStatement = this.db.query("DELETE FROM items WHERE key = $key")
Expand Down

0 comments on commit c83236e

Please sign in to comment.