bun-sqlite-key-value
Advanced tools
Comparing version 1.8.2 to 1.8.3
@@ -50,3 +50,3 @@ // Generated by dts-bundle-generator v9.5.1 | ||
get length(): number; | ||
getCountValid(deleteExpired?: boolean): any; | ||
getCountValid(deleteExpired?: boolean): number; | ||
set<T = any>(key: string, value: T, ttlMs?: number): void; | ||
@@ -84,2 +84,3 @@ setValue: <T = any>(key: string, value: T, ttlMs?: number) => void; | ||
append(key: string, value: string, ttlMs?: number): number; | ||
getSet<T = any>(key: string, value: T, ttlMs?: number): T | undefined; | ||
} | ||
@@ -86,0 +87,0 @@ |
@@ -315,3 +315,3 @@ // src/index.ts | ||
return newValue; | ||
})(); | ||
}).immediate(); | ||
} | ||
@@ -327,4 +327,12 @@ decr(key, decrBy = 1, ttlMs) { | ||
return newValue.length; | ||
})(); | ||
}).immediate(); | ||
} | ||
getSet(key, value, ttlMs) { | ||
const self = this; | ||
return this.db.transaction(() => { | ||
const oldValue = self.get(key); | ||
self.set(key, value, ttlMs); | ||
return oldValue; | ||
}).immediate(); | ||
} | ||
} | ||
@@ -335,2 +343,2 @@ export { | ||
//# debugId=980128A3C0A1689C64756E2164756E21 | ||
//# debugId=B64E9A0A57ED19FF64756E2164756E21 |
{ | ||
"name": "bun-sqlite-key-value", | ||
"version": "1.8.2", | ||
"version": "1.8.3", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Gerold Penz", |
@@ -144,27 +144,2 @@ # Bun SQLite Key Value | ||
## Write Multiple Items | ||
```typescript | ||
setItems(items: {key: string, value: T, ttlMs?: number}[]) { | ||
``` | ||
Adds a large number of items to the database and takes only | ||
a small fraction of the time that `set()` would take individually. | ||
### Example | ||
```typescript | ||
import { BunSqliteKeyValue } from "bun-sqlite-key-value" | ||
const store = new BunSqliteKeyValue() | ||
// Add many records | ||
store.setItems([ | ||
{key: "a:1", value: "test-value-1"}, | ||
{key: "a:2", value: "test-value-2"}, | ||
]) | ||
``` | ||
## Read Value | ||
@@ -232,2 +207,27 @@ | ||
## Write Multiple Items | ||
```typescript | ||
setItems(items: {key: string, value: T, ttlMs?: number}[]) { | ||
``` | ||
Adds a large number of items to the database and takes only | ||
a small fraction of the time that `set()` would take individually. | ||
### Example | ||
```typescript | ||
import { BunSqliteKeyValue } from "bun-sqlite-key-value" | ||
const store = new BunSqliteKeyValue() | ||
// Add many records | ||
store.setItems([ | ||
{key: "a:1", value: "test-value-1"}, | ||
{key: "a:2", value: "test-value-2"}, | ||
]) | ||
``` | ||
## Read Values | ||
@@ -738,4 +738,3 @@ | ||
If key already exists and is a string, this command appends the | ||
value at the end of the string. | ||
If key already exists, this command appends the value at the end of the string. | ||
If key does not exist it is created and set as an empty string, | ||
@@ -791,6 +790,7 @@ so `append()` will be similar to `set()` in this special case. | ||
### Get value | ||
- `get(key: string)` | ||
- `get(key: string): any` | ||
- `getValue(key: string)` --> alias for get() | ||
- `<store>.data.<key>` | ||
- `<store>.d.<key>` | ||
- `getSet(key: string, value: any): any` | ||
@@ -797,0 +797,0 @@ ### Get item |
186
src/index.ts
@@ -186,3 +186,3 @@ import { Database, type Statement } from "bun:sqlite" | ||
// Use `getCount()` if you want the fastet possible method. | ||
getCountValid(deleteExpired?: boolean) { | ||
getCountValid(deleteExpired?: boolean): number { | ||
if (deleteExpired === true) { | ||
@@ -473,2 +473,3 @@ return this.db.transaction(() => { | ||
const self = this | ||
// @ts-ignore (Transaction returns a number or NaN, not void.) | ||
return this.db.transaction(() => { | ||
@@ -479,3 +480,3 @@ const newValue = Number(self.get<number>(key) ?? 0) + incrBy | ||
return newValue | ||
})() | ||
}).immediate() | ||
} | ||
@@ -490,5 +491,5 @@ | ||
// If key already exists and is a string, this command appends the value at the end of the string. | ||
// If key already exists, this command appends the value at the end of the string. | ||
// If key does not exist it is created and set as an empty string, | ||
// so APPEND will be similar to `set()` in this special case. | ||
// so `append()` will be similar to `set()` in this special case. | ||
// Returns the length of the string after the append operation. | ||
@@ -498,2 +499,3 @@ // Inspired by: https://docs.keydb.dev/docs/commands/#append | ||
const self = this | ||
// @ts-ignore (Transaction returns a number, not void.) | ||
return this.db.transaction(() => { | ||
@@ -503,6 +505,180 @@ const newValue = String(self.get<string>(key) ?? "") + value | ||
return newValue.length | ||
})() | ||
}).immediate() | ||
} | ||
// Atomically sets key to value and returns the old value stored at key. | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#getset | ||
getSet<T = any>(key: string, value: T, ttlMs?: number): T | undefined { | ||
const self = this | ||
// @ts-ignore (Transaction returns a number, not void.) | ||
return this.db.transaction(() => { | ||
const oldValue = self.get<T>(key) | ||
self.set<T>(key, value, ttlMs) | ||
return oldValue | ||
}).immediate() | ||
} | ||
// ToDo: hDel() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hdel | ||
// ToDo: hExists() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hexists | ||
// ToDo: hGet() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hget | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hmget | ||
// ToDo: hIncrBy() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hincrby | ||
// ToDo: hKeys() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hkeys | ||
// ToDo: hLen() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hlen | ||
// ToDo: hSet() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hset | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hmset | ||
// ToDo: hStrLen() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hstrlen | ||
// ToDo: hVals() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hvals | ||
// ToDo: lIndex() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#lindex | ||
// ToDo: lLen() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#llen | ||
// ToDo: lPop() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#lpop | ||
// ToDo: lPush() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#lpush | ||
// ToDo: lRange() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#lrange | ||
// ToDo: lSet() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#lset | ||
// ToDo: lTrim() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#ltrim | ||
// 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() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#rpop | ||
// ToDo: rPopLPush() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#rpoplpush | ||
// ToDo: rPush() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#rpush | ||
// ToDo: sAdd() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sadd | ||
// ToDo: sCard() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#scard | ||
// ToDo: sDiff() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sdiff | ||
// ToDo: sDiffStore() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sdiffstore | ||
// ToDo: sInter() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sinter | ||
// ToDo: sInterStore() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sinterstore | ||
// ToDo: sIsMember() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sismember | ||
// ToDo: sMembers() alias for getSet() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#smembers | ||
// ToDo: sMove() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#smove | ||
// ToDo: sPop() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#spop | ||
// ToDo: sRandMember() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#srandmember | ||
// ToDo: sRem() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#srem | ||
// ToDo: sUnion() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sunion | ||
// ToDo: sUnionStore() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#sunionstore | ||
// 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
87197
927