bun-sqlite-key-value
Advanced tools
Comparing version 1.8.3 to 1.9.1
@@ -43,8 +43,12 @@ // Generated by dts-bundle-generator v9.5.1 | ||
private deleteExpiringStatement; | ||
private getRandomKeyStatement; | ||
private getRandomItemStatement; | ||
constructor(filename?: string, options?: Options); | ||
deleteExpired(): void; | ||
delete(keyOrKeys?: string | string[]): void; | ||
del: (keyOrKeys?: string | string[]) => void; | ||
clear(): void; | ||
close(): void; | ||
getCount(): number; | ||
count: () => number; | ||
get length(): number; | ||
@@ -75,2 +79,3 @@ getCountValid(deleteExpired?: boolean): number; | ||
has(key: string): boolean; | ||
exists: (key: string) => boolean; | ||
getKeys(startsWithOrKeys?: string | string[]): string[] | undefined; | ||
@@ -86,2 +91,8 @@ get keys(): string[] | undefined; | ||
getSet<T = any>(key: string, value: T, ttlMs?: number): T | undefined; | ||
getRandomKey(): string | undefined; | ||
randomKey: () => string | undefined; | ||
getRandomItem<T = any>(): Item<T> | undefined; | ||
randomItem: <T = any>() => Item<T> | undefined; | ||
getRandomValue<T = any>(): T | undefined; | ||
randomValue: <T = any>() => T | undefined; | ||
} | ||
@@ -88,0 +99,0 @@ |
@@ -28,2 +28,4 @@ // src/index.ts | ||
deleteExpiringStatement; | ||
getRandomKeyStatement; | ||
getRandomItemStatement; | ||
constructor(filename, options) { | ||
@@ -64,7 +66,6 @@ const { | ||
this.getKeyStatement = this.db.query("SELECT expires FROM items WHERE key = $key"); | ||
this.getKeysStartsWithStatement = this.db.query("SELECT key, expires FROM items WHERE (key = $key OR key >= $gte AND key < $lt)"); | ||
this.getKeysStartsWithStatement = this.db.query("SELECT key, expires FROM items WHERE key = $key OR key >= $gte AND key < $lt"); | ||
this.countExpiringStatement = this.db.query("SELECT COUNT(*) as count FROM items WHERE expires IS NOT NULL"); | ||
this.deleteExpiringStatement = this.db.query(` | ||
DELETE FROM items | ||
WHERE key IN ( | ||
DELETE FROM items WHERE key IN ( | ||
SELECT key FROM items | ||
@@ -75,2 +76,16 @@ WHERE expires IS NOT NULL | ||
)`); | ||
this.getRandomKeyStatement = this.db.query(` | ||
SELECT key FROM items | ||
WHERE expires IS NULL OR expires > \$now | ||
ORDER BY RANDOM() | ||
LIMIT 1 | ||
`); | ||
this.getRandomItemStatement = this.db.query(` | ||
SELECT key, value from items | ||
WHERE key = ( | ||
SELECT key FROM items | ||
WHERE expires IS NULL OR expires > \$now | ||
ORDER BY RANDOM() | ||
LIMIT 1 | ||
)`); | ||
this.deleteExpired(); | ||
@@ -94,2 +109,3 @@ } | ||
} | ||
del = this.delete; | ||
clear() { | ||
@@ -104,2 +120,3 @@ this.delete(); | ||
} | ||
count = this.getCount; | ||
get length() { | ||
@@ -238,2 +255,3 @@ return this.getCount(); | ||
} | ||
exists = this.has; | ||
getKeys(startsWithOrKeys) { | ||
@@ -342,2 +360,22 @@ let records; | ||
} | ||
getRandomKey() { | ||
return this.getRandomKeyStatement.get({ now: Date.now() })?.key ?? undefined; | ||
} | ||
randomKey = this.getRandomKey; | ||
getRandomItem() { | ||
const record = this.getRandomItemStatement.get({ now: Date.now() }); | ||
if (!record) | ||
return; | ||
return { | ||
key: record.key, | ||
value: record.value ? deserialize(record.value) : undefined | ||
}; | ||
} | ||
randomItem = this.getRandomItem; | ||
getRandomValue() { | ||
const item = this.randomItem(); | ||
if (item) | ||
return item.value; | ||
} | ||
randomValue = this.getRandomValue; | ||
} | ||
@@ -348,2 +386,2 @@ export { | ||
//# debugId=B64E9A0A57ED19FF64756E2164756E21 | ||
//# debugId=1A018DFF5686F1D564756E2164756E21 |
{ | ||
"name": "bun-sqlite-key-value", | ||
"version": "1.8.3", | ||
"version": "1.9.1", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Gerold Penz", |
@@ -181,2 +181,14 @@ # Bun SQLite Key Value | ||
## Random Value | ||
```typescript | ||
getRandomValue(): any // --> random value | ||
randomValue() // --> alias for getRandomValue() | ||
``` | ||
Returns a random value or `undefined` if no valid item was found. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
## Read Item | ||
@@ -207,2 +219,14 @@ | ||
## Random Item | ||
```typescript | ||
getRandomItem() // --> random item | ||
randomItem() // --> alias for getRandomItem() | ||
``` | ||
Returns a random item or `undefined` if no valid item was found. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
## Write Multiple Items | ||
@@ -476,3 +500,3 @@ | ||
<store>.keys | ||
<store>.keys // --> all keys | ||
``` | ||
@@ -517,2 +541,14 @@ | ||
## Random Key | ||
```typescript | ||
getRandomKey() // --> random key | ||
randomKey() // --> alias for getRandomKey() | ||
``` | ||
Returns a random key or `undefined` if no valid item was found. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
## Delete Items | ||
@@ -796,5 +832,9 @@ | ||
- `getSet(key: string, value: any): any` | ||
- `getRandomValue(): any` | ||
- `randomValue(): any` --> alias for getRandomValue() | ||
### Get item | ||
- `getItem(key: string)` --> Object | ||
- `getRandomItem()` --> Object | ||
- `randomItem()` --> Alias for getRandomItem() | ||
@@ -856,2 +896,4 @@ ### Get items as Array | ||
- `<key> in <store>.d` | ||
- `getRandomKey()` | ||
- `randomKey()` --> alias for getRandomKey() | ||
@@ -858,0 +900,0 @@ ### Math operations |
131
src/index.ts
@@ -59,2 +59,4 @@ import { Database, type Statement } from "bun:sqlite" | ||
private deleteExpiringStatement: Statement | ||
private getRandomKeyStatement: Statement<Omit<Record, "value" | "expires">> | ||
private getRandomItemStatement: Statement<Omit<Record, "expires">> | ||
@@ -105,9 +107,15 @@ | ||
this.setItemStatement = this.db.query("INSERT OR REPLACE INTO items (key, value, expires) VALUES ($key, $value, $expires)") | ||
this.setItemStatement = this.db.query( | ||
"INSERT OR REPLACE INTO items (key, value, expires) VALUES ($key, $value, $expires)" | ||
) | ||
this.countStatement = this.db.query("SELECT COUNT(*) AS count FROM items") | ||
this.countValidStatement = this.db.query("SELECT COUNT(*) AS count FROM items WHERE expires IS NULL OR expires > $now") | ||
this.countValidStatement = this.db.query( | ||
"SELECT COUNT(*) AS count FROM items WHERE expires IS NULL OR expires > $now" | ||
) | ||
this.getAllItemsStatement = this.db.query("SELECT key, value, expires FROM items") | ||
this.getItemStatement = this.db.query("SELECT value, expires FROM items WHERE key = $key") | ||
this.getItemsStartsWithStatement = this.db.query("SELECT key, value, expires FROM items WHERE key = $key OR key >= $gte AND key < $lt") | ||
this.getItemsStartsWithStatement = this.db.query( | ||
"SELECT key, value, expires FROM items WHERE key = $key OR key >= $gte AND key < $lt" | ||
) | ||
// gte = key + MIN_UTF8_CHAR | ||
@@ -118,8 +126,10 @@ // lt = key + MAX_UTF8_CHAR | ||
this.getKeyStatement = this.db.query("SELECT expires FROM items WHERE key = $key") | ||
this.getKeysStartsWithStatement = this.db.query("SELECT key, expires FROM items WHERE (key = $key OR key >= $gte AND key < $lt)") | ||
this.countExpiringStatement = this.db.query("SELECT COUNT(*) as count FROM items WHERE expires IS NOT NULL") | ||
this.getKeysStartsWithStatement = this.db.query( | ||
"SELECT key, expires FROM items WHERE key = $key OR key >= $gte AND key < $lt" | ||
) | ||
this.countExpiringStatement = this.db.query( | ||
"SELECT COUNT(*) as count FROM items WHERE expires IS NOT NULL" | ||
) | ||
this.deleteExpiringStatement = this.db.query(` | ||
DELETE FROM items | ||
WHERE key IN ( | ||
DELETE FROM items WHERE key IN ( | ||
SELECT key FROM items | ||
@@ -130,2 +140,16 @@ WHERE expires IS NOT NULL | ||
)`) | ||
this.getRandomKeyStatement = this.db.query(` | ||
SELECT key FROM items | ||
WHERE expires IS NULL OR expires > $now | ||
ORDER BY RANDOM() | ||
LIMIT 1 | ||
`) | ||
this.getRandomItemStatement = this.db.query(` | ||
SELECT key, value from items | ||
WHERE key = ( | ||
SELECT key FROM items | ||
WHERE expires IS NULL OR expires > $now | ||
ORDER BY RANDOM() | ||
LIMIT 1 | ||
)`) | ||
@@ -162,2 +186,7 @@ // Delete expired items | ||
// Alias for clear | ||
// Alias inspired by: https://docs.keydb.dev/docs/commands/#del | ||
del = this.delete | ||
// Delete all items | ||
@@ -183,2 +212,6 @@ clear() { | ||
// Alias for getCount() | ||
count = this.getCount | ||
// Getter for getCount() | ||
@@ -193,2 +226,3 @@ get length() { | ||
getCountValid(deleteExpired?: boolean): number { | ||
if (deleteExpired === true) { | ||
@@ -375,2 +409,7 @@ return this.db.transaction(() => { | ||
// Alias for has() | ||
// Alias inspired by: https://docs.keydb.dev/docs/commands/#exists | ||
exists = this.has | ||
// Get multiple keys as array | ||
@@ -525,2 +564,51 @@ getKeys(startsWithOrKeys?: string | string[]): string[] | undefined { | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
getRandomKey(): string | undefined { | ||
return this.getRandomKeyStatement.get({now: Date.now()})?.key ?? undefined | ||
} | ||
// Alias for getRandomKey() | ||
randomKey = this.getRandomKey | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
getRandomItem<T = any>(): Item<T> | undefined { | ||
const record = this.getRandomItemStatement.get({now: Date.now()}) | ||
if (!record) return | ||
return { | ||
key: record.key, | ||
value: record.value ? deserialize(record.value) as T : undefined | ||
} | ||
} | ||
// Alias for getRandomItem() | ||
randomItem = this.getRandomItem | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
getRandomValue<T = any>(): T | undefined { | ||
const item = this.randomItem<T>() | ||
if (item) return item.value | ||
} | ||
// Alias for getRandomValue() | ||
randomValue = this.getRandomValue | ||
// ToDo: rename() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#rename | ||
// ToDo: touch(key, ttlMs) | ||
// Renews the TTL | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#touch | ||
// ToDo: ttl() milliseconds like pTtl() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#ttl | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#pttl | ||
// ToDo: hDel() | ||
@@ -592,20 +680,2 @@ // Inspired by: https://docs.keydb.dev/docs/commands/#hdel | ||
// ToDo: randomKey() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
// ToDo: randomValue() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
// ToDo: randomItem() | ||
// --> SELECT * FROM items OFFSET abs(random() % (SELECT COUNT(*) from items)) LIMIT 1 | ||
// --> SELECT * FROM items WHERE key IN (SELECT key FROM items ORDER BY RANDOM() LIMIT 1) | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#randomkey | ||
// ToDo: rename() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#rename | ||
// ToDo: rPop() | ||
@@ -679,11 +749,2 @@ // Inspired by: https://docs.keydb.dev/docs/commands/#rpop | ||
// ToDo: touch(key, ttlMs) | ||
// Renews the TTL | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#touch | ||
// ToDo: ttl() milliseconds like pTtl() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#ttl | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#pttl | ||
} |
Sorry, the diff of this file is not supported yet
93393
1025
916