bun-sqlite-key-value
Advanced tools
Comparing version 1.9.4 to 1.9.5
@@ -42,2 +42,3 @@ import { Database } from "bun:sqlite"; | ||
private getRandomItemStatement; | ||
private renameStatement; | ||
constructor(filename?: string, options?: Options); | ||
@@ -93,2 +94,3 @@ deleteExpired(): void; | ||
randomValue: <T = any>() => T | undefined; | ||
rename(oldKey: string, newKey: string): boolean; | ||
} |
@@ -30,2 +30,3 @@ // src/index.ts | ||
getRandomItemStatement; | ||
renameStatement; | ||
constructor(filename, options) { | ||
@@ -89,2 +90,3 @@ const { | ||
)`); | ||
this.renameStatement = this.db.query("UPDATE items SET key = $newKey WHERE key = $oldKey"); | ||
this.deleteExpired(); | ||
@@ -376,2 +378,13 @@ } | ||
randomValue = this.getRandomValue; | ||
rename(oldKey, newKey) { | ||
return this.db.transaction(() => { | ||
if (this.has(oldKey)) { | ||
this.delete(newKey); | ||
this.renameStatement.run({ oldKey, newKey }); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
})(); | ||
} | ||
} | ||
@@ -378,0 +391,0 @@ export { |
{ | ||
"name": "bun-sqlite-key-value", | ||
"version": "1.9.4", | ||
"version": "1.9.5", | ||
"author": { | ||
@@ -19,3 +19,3 @@ "name": "Gerold Penz", | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
"typescript": "^5.5.3" | ||
}, | ||
@@ -22,0 +22,0 @@ "bugs": { |
@@ -79,3 +79,3 @@ # Bun SQLite Key Value | ||
// In-memory with 30 seconds default expiration timeout | ||
const store2 = new BunSqliteKeyValue(undefined, {ttlMs: 30000}) | ||
const store2 = new BunSqliteKeyValue(":memory:", {ttlMs: 30000}) | ||
// Store items in file system | ||
@@ -536,2 +536,14 @@ const store3 = new BunSqliteKeyValue("./store3.sqlite") | ||
## Rename Key | ||
```typescript | ||
rename(oldKey: string, newKey: string): boolean | ||
``` | ||
Renames `oldKey` to `newKey`. | ||
It returns `false` when `oldKey` does not exist. | ||
If `newKey` already exists it is deleted first. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#rename | ||
## Delete Items | ||
@@ -792,2 +804,34 @@ | ||
## Database Transactions | ||
Transactions can be used to combine several database statements. | ||
These combined database statements are processed much faster than | ||
if they were executed individually. | ||
The more database statements are combined, the greater the speed advantage. | ||
You can find more infos in the | ||
[Bun documentation](https://bun.sh/docs/api/sqlite#transactions). | ||
### Example | ||
```typescript | ||
import { BunSqliteKeyValue } from "bun-sqlite-key-value" | ||
const store = new BunSqliteKeyValue() | ||
store.db.transaction(() => { | ||
store.set("key1", "100") | ||
store.set("key2", "200") | ||
store.set("key3", "300") | ||
})() | ||
store.db.transaction(() => { | ||
const value1 = store.get("key1") | ||
const value2 = store.get("key2") | ||
const value3 = store.get("key3") | ||
const total = value1 + value2 + value3 | ||
store.set("total1", total) | ||
})() | ||
``` | ||
## All Functions | ||
@@ -863,10 +907,11 @@ | ||
- `getCount()` --> Number | ||
- `count()` | ||
- `length` --> alias for getCount() | ||
- `getCountValid(deleteExpired?: boolean)` --> Number | ||
### Get keys | ||
- `has(key: string)` --> Boolean | ||
### Keys | ||
- `has(key: string): boolean` | ||
- `getKeys()` --> Array with all Keys | ||
- `getKeys(startsWith: string)` --> Array | ||
- `getKeys(keys: string[])` --> Array | ||
- `getKeys(startsWith: string): string[]` | ||
- `getKeys(keys: string[]): string[]` | ||
- `keys` --> alias for getKeys() | ||
@@ -876,2 +921,3 @@ - `<key> in <store>.data` | ||
- `randomKey()` --> alias for getRandomKey() | ||
- `rename(oldKey: string, newKey: string): boolean` | ||
@@ -878,0 +924,0 @@ ### Math operations |
@@ -60,2 +60,3 @@ import { Database, type Statement } from "bun:sqlite" | ||
private getRandomItemStatement: Statement<Omit<Record, "expires">> | ||
private renameStatement: Statement | ||
@@ -151,2 +152,3 @@ | ||
)`) | ||
this.renameStatement = this.db.query("UPDATE items SET key = $newKey WHERE key = $oldKey") | ||
@@ -593,4 +595,17 @@ // Delete expired items | ||
// ToDo: rename() | ||
// Renames `oldKey` to `newKey`. | ||
// It returns `false` when `oldKey` does not exist. | ||
// If `newKey` already exists it is deleted first. | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#rename | ||
rename(oldKey: string, newKey: string): boolean { | ||
return this.db.transaction(() => { | ||
if (this.has(oldKey)) { | ||
this.delete(newKey) | ||
this.renameStatement.run({oldKey, newKey}) | ||
return true | ||
} else { | ||
return false | ||
} | ||
})() | ||
} | ||
@@ -597,0 +612,0 @@ |
64466
1048
941