bun-sqlite-key-value
Advanced tools
Comparing version 1.9.6 to 1.9.7
@@ -43,2 +43,3 @@ import { Database } from "bun:sqlite"; | ||
private renameStatement; | ||
private touchStatement; | ||
constructor(filename?: string, options?: Options); | ||
@@ -95,2 +96,3 @@ deleteExpired(): void; | ||
rename(oldKey: string, newKey: string): boolean; | ||
touch(key: string, ttlMs?: number): boolean; | ||
hSet<T = any>(key: string, field: string, value: T, ttlMs?: number): boolean; | ||
@@ -97,0 +99,0 @@ hGet<T = any>(key: string, field: string): T | undefined; |
@@ -31,2 +31,3 @@ // src/index.ts | ||
renameStatement; | ||
touchStatement; | ||
constructor(filename, options) { | ||
@@ -91,2 +92,3 @@ const { | ||
this.renameStatement = this.db.query("UPDATE items SET key = $newKey WHERE key = $oldKey"); | ||
this.touchStatement = this.db.query("UPDATE items SET expires = $expires WHERE key = $key"); | ||
this.deleteExpired(); | ||
@@ -386,2 +388,10 @@ } | ||
} | ||
touch(key, ttlMs) { | ||
let expires; | ||
ttlMs = ttlMs ?? this.ttlMs; | ||
if (ttlMs !== undefined && ttlMs > 0) { | ||
expires = Date.now() + ttlMs; | ||
} | ||
return this.touchStatement.run({ key, expires }).changes === 1; | ||
} | ||
hSet(key, field, value, ttlMs) { | ||
@@ -388,0 +398,0 @@ return this.db.transaction(() => { |
{ | ||
"name": "bun-sqlite-key-value", | ||
"version": "1.9.6", | ||
"version": "1.9.7", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Gerold Penz", |
@@ -834,2 +834,41 @@ # Bun SQLite Key Value | ||
## Renew TTL | ||
```typescript | ||
touch(key: string, ttlMs?: number): boolean | ||
``` | ||
Renews or deletes the TTL of the database row. | ||
Returns `true` if the `key` exists. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#touch | ||
### key | ||
The key must be a string. | ||
### ttlMs (optional) | ||
"Time to live" in milliseconds. After this time, | ||
the item becomes invalid and is deleted from the database | ||
the next time it is accessed or when the application is started. | ||
Uses the global `ttlMs` as default value. | ||
Set the value to 0 if you want to delete the TTL. | ||
### Example | ||
```typescript | ||
import { BunSqliteKeyValue } from "bun-sqlite-key-value" | ||
const store = new BunSqliteKeyValue() | ||
store.set("my-key", "my-value", 10000) | ||
// Update TTL | ||
store.touch("my-key", 10000) // --> true | ||
// Delete TTL | ||
store.touch("my-key", 0) // --> true | ||
``` | ||
## Hash (Map Object) - Write Value | ||
@@ -836,0 +875,0 @@ ```typescript |
@@ -61,2 +61,3 @@ import { Database, type Statement } from "bun:sqlite" | ||
private renameStatement: Statement | ||
private touchStatement: Statement | ||
@@ -153,2 +154,3 @@ | ||
this.renameStatement = this.db.query("UPDATE items SET key = $newKey WHERE key = $oldKey") | ||
this.touchStatement = this.db.query("UPDATE items SET expires = $expires WHERE key = $key") | ||
@@ -610,5 +612,13 @@ // Delete expired items | ||
// ToDo: touch(key, ttlMs) | ||
// Renews the TTL | ||
// Renews or deletes the TTL of the database row. | ||
// Returns `true` if the `key` exists. | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#touch | ||
touch(key: string, ttlMs?: number): boolean { | ||
let expires: number | undefined | ||
ttlMs = ttlMs ?? this.ttlMs | ||
if (ttlMs !== undefined && ttlMs > 0) { | ||
expires = Date.now() + ttlMs | ||
} | ||
return this.touchStatement.run({key, expires}).changes === 1 | ||
} | ||
@@ -615,0 +625,0 @@ |
71426
1127
1070