bun-sqlite-key-value
Advanced tools
Comparing version 1.3.12 to 1.3.13
{ | ||
"name": "bun-sqlite-key-value", | ||
"version": "1.3.12", | ||
"version": "1.3.13", | ||
"author": "Gerold Penz<gerold@gp-softwaretechnik.at>", | ||
@@ -46,5 +46,6 @@ "repository": { | ||
"prepublishOnly": "bun run test && bun run increment_package_version", | ||
"npm:publish": "npm publish --access public" | ||
"npm:publish": "npm publish --access public", | ||
"jsr:publish": "bunx jsr publish" | ||
}, | ||
"type": "module" | ||
} |
@@ -56,3 +56,3 @@ # Bun SQLite Key Value | ||
The full path of the SQLite database to open. | ||
Pass an empty string (`""`) or `":memory:"` or undefined for an in-memory database. | ||
Pass an empty string (`""`) or `":memory:"` or `undefined` for an in-memory database. | ||
@@ -409,3 +409,23 @@ ### options (optional) | ||
## Read Keys | ||
```typescript | ||
getKeys(): string[] | ||
``` | ||
Returns the unexpired keys as array. | ||
### Example | ||
```typescript | ||
import { BunSqliteKeyValue } from "bun-sqlite-key-value" | ||
const store = new BunSqliteKeyValue() | ||
store.getKeys() --> ["key1", "key2"] | ||
``` | ||
## All Methods | ||
@@ -474,5 +494,5 @@ | ||
- `has(key: string)` --> Boolean | ||
- [ ] `getKeys()` --> Array with all Keys | ||
- `getKeys()` --> Array with all Keys | ||
- [ ] `getKeys(startsWith: string)` --> Array | ||
- [ ] `getKeys(keys: string[])` --> Array | ||
@@ -30,2 +30,3 @@ import { Database, type Statement } from "bun:sqlite" | ||
const MIN_UTF8_CHAR: string = String.fromCodePoint(1) | ||
@@ -49,3 +50,3 @@ const MAX_UTF8_CHAR: string = String.fromCodePoint(1_114_111) | ||
private getKeyStatement: Statement<Omit<Record, "value">> | ||
// private getAllKeysStatement: Statement<Omit<Record, "value">> | ||
private getAllKeysStatement: Statement<{key: string}> | ||
// private getKeysStartsWithStatement: Statement<Omit<Record, "value">> | ||
@@ -90,3 +91,3 @@ | ||
this.getKeyStatement = this.db.query("SELECT key, expires FROM items WHERE key = $key") | ||
// this.getAllKeysStatement = this.db.query("SELECT key, expires FROM items") | ||
this.getAllKeysStatement = this.db.query("SELECT key FROM items WHERE expires IS NULL OR expires > $now") | ||
// this.getKeysStartsWithStatement = this.db.query("SELECT key, expires FROM items WHERE key LIKE $startsWith") | ||
@@ -299,2 +300,10 @@ | ||
// Returns all unexpired keys as an Array | ||
getKeys(): string[] | undefined { | ||
const records = this.getAllKeysStatement.all({now: Date.now()}) | ||
if (!records?.length) return | ||
return records.map((record) => record.key) | ||
} | ||
} |
@@ -345,1 +345,15 @@ import { expect, test } from "bun:test" | ||
}) | ||
test("Get keys", async () => { | ||
const store: BunSqliteKeyValue = new BunSqliteKeyValue() | ||
store.set<string>(KEY_1, STRING_VALUE_1) | ||
store.set<string>(KEY_2, STRING_VALUE_2, 50) | ||
// All keys | ||
expect(store.getKeys()).toHaveLength(2) | ||
await Bun.sleep(100) | ||
expect(store.getKeys()).toEqual([KEY_1]) | ||
}) | ||
52541
24
814
496