bun-sqlite-key-value
Advanced tools
Comparing version 1.10.9 to 1.10.10
@@ -155,2 +155,4 @@ import { Database } from "bun:sqlite"; | ||
hDelete(key: string, field: string): boolean | undefined; | ||
hIncr(key: string, field: string, incrBy?: number, ttlMs?: number): number; | ||
hDecr(key: string, field: string, decrBy?: number, ttlMs?: number): number; | ||
} |
@@ -486,2 +486,25 @@ // src/index.ts | ||
} | ||
hIncr(key, field, incrBy = 1, ttlMs) { | ||
return this.db.transaction(() => { | ||
const map = this.get(key) ?? new Map; | ||
let newValue; | ||
try { | ||
newValue = Number(map.get(field) ?? 0) + incrBy; | ||
} catch (error) { | ||
if (error.toString().includes("TypeError")) { | ||
return NaN; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
if (isNaN(newValue)) | ||
return NaN; | ||
map.set(field, newValue); | ||
this.set(key, map, ttlMs); | ||
return newValue; | ||
}).immediate(); | ||
} | ||
hDecr(key, field, decrBy = 1, ttlMs) { | ||
return this.hIncr(key, field, decrBy * -1, ttlMs); | ||
} | ||
} | ||
@@ -488,0 +511,0 @@ export { |
{ | ||
"name": "bun-sqlite-key-value", | ||
"version": "1.10.9", | ||
"version": "1.10.10", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Gerold Penz", |
159
README.md
@@ -6,2 +6,8 @@ # Bun SQLite Key Value | ||
[](LICENSE) | ||
[](https://www.npmjs.com/package/bun-sqlite-key-value) | ||
[](https://www.npmjs.com/package/bun-sqlite-key-value) | ||
[](https://bun.sh/docs/api/sqlite) | ||
[Bun's](https://bun.sh/) lightning-fast | ||
@@ -29,3 +35,2 @@ [SQLite implementation](https://bun.sh/docs/api/sqlite) makes Bun-SQLite-Key-Value | ||
- [`getValues()`](#read-multiple-values) | ||
- [`getRandomValue()`](#read-random-value) | ||
- `getValuesSet()` --> Set with values | ||
@@ -36,3 +41,2 @@ - Write and Read Items | ||
- [`getItems()`](#read-multiple-items) | ||
- [`getRandomItem()`](#read-random-item) | ||
- `getItemsObject()` --> Object with items | ||
@@ -44,3 +48,2 @@ - `getItemsMap()` --> Map with items | ||
- [`getKeys()`](#read-multiple-keys) | ||
- [`getRandomKey()`](#random-key) | ||
- [`rename()`](#rename-key) | ||
@@ -58,2 +61,6 @@ - Delete Items | ||
- [`getTtl()`](#get-ttl) | ||
- Random | ||
- [`getRandomValue()`](#random-value) | ||
- [`getRandomItem()`](#random-item) | ||
- [`getRandomKey()`](#random-key) | ||
- Math | ||
@@ -73,3 +80,5 @@ - [`incr()`](#increment) | ||
- [`hGetValues()`](#hash-map-object---get-all-values) | ||
- [`hDelete()`]() | ||
- [`hDelete()`](#hash-map-object---delete-field) | ||
- [`hIncr()`]() | ||
- [`hDecr()`]() | ||
- Extended database topics | ||
@@ -245,3 +254,3 @@ - [Multiple Databases](#multiple-databases) | ||
Atomically sets key to value and returns the old value stored at key. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#getset | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#getset](https://docs.keydb.dev/docs/commands/#getset) | ||
@@ -293,5 +302,5 @@ ### key | ||
`undefined`: Returns all values in an array. | ||
`undefined`: Returns an array with all values. | ||
`string`: Returns all values in an array whose keys begin with the passed string. | ||
`string`: Returns an array with all values whose keys begin with the passed string. | ||
If you plan the names of the keys well, more complex data can be stored. | ||
@@ -325,14 +334,2 @@ It is advisable to divide keys into ranges using separators. | ||
## Read 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 | ||
## Write Multiple Items | ||
@@ -439,14 +436,2 @@ | ||
## Read 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 | ||
## Read and Write Binary Files (Images) | ||
@@ -568,14 +553,2 @@ | ||
## 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 | ||
## Rename Key | ||
@@ -590,3 +563,3 @@ | ||
If `newKey` already exists it is deleted first. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#rename | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#rename](https://docs.keydb.dev/docs/commands/#rename) | ||
@@ -811,3 +784,3 @@ | ||
Inspired by: https://docs.keydb.dev/docs/commands/#ttl | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#ttl](https://docs.keydb.dev/docs/commands/#ttl) | ||
@@ -831,2 +804,38 @@ ### key | ||
## 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](https://docs.keydb.dev/docs/commands/#randomkey) | ||
## 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](https://docs.keydb.dev/docs/commands/#randomkey) | ||
## 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](https://docs.keydb.dev/docs/commands/#randomkey) | ||
## Increment | ||
@@ -924,3 +933,3 @@ | ||
so `append()` will be similar to `set()` in this special case. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#append | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#append](https://docs.keydb.dev/docs/commands/#append) | ||
@@ -970,7 +979,7 @@ Returns the length of the string after the append operation. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hset | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hset](https://docs.keydb.dev/docs/commands/#hset) | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
@@ -1030,7 +1039,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hget | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hget](https://docs.keydb.dev/docs/commands/#hget) | ||
@@ -1068,7 +1077,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hmset | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hmset](https://docs.keydb.dev/docs/commands/#hmset) | ||
@@ -1113,7 +1122,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hmget | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hmget](https://docs.keydb.dev/docs/commands/#hmget) | ||
@@ -1156,7 +1165,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hexists | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hexists](https://docs.keydb.dev/docs/commands/#hexists) | ||
@@ -1193,7 +1202,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hlen | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hlen](https://docs.keydb.dev/docs/commands/#hlen) | ||
@@ -1226,7 +1235,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hkeys | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hkeys](https://docs.keydb.dev/docs/commands/#hkeys) | ||
@@ -1261,7 +1270,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hvals | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hvals](https://docs.keydb.dev/docs/commands/#hvals) | ||
@@ -1299,7 +1308,7 @@ ### key | ||
> Do not use the hash functions with several very large amounts of data or blobs. | ||
> Do not use the hash functions with several very large amounts (megabytes) of data or blobs. | ||
> This is because the entire data record with all fields is always read and written. | ||
> It is better to use `setValues()` and `getValues()` for large amounts (megabytes) of data. | ||
> It is better to use `setValues()` and `getValues()` for large amounts of data. | ||
Inspired by: https://docs.keydb.dev/docs/commands/#hdel | ||
Inspired by: [https://docs.keydb.dev/docs/commands/#hdel](https://docs.keydb.dev/docs/commands/#hdel) | ||
@@ -1315,2 +1324,6 @@ ### key | ||
## Multiple Databases | ||
@@ -1317,0 +1330,0 @@ |
@@ -187,3 +187,3 @@ import { Database, type Statement } from "bun:sqlite" | ||
// Alias for clear | ||
// Alias for delete | ||
// Alias inspired by: https://docs.keydb.dev/docs/commands/#del | ||
@@ -738,2 +738,3 @@ del = this.delete | ||
/** | ||
@@ -801,6 +802,32 @@ * Returns the *values* contained in the hash stored at `key`. | ||
// ToDo: hIncrBy() | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hincrby | ||
hIncr(key: string, field: string, incrBy: number = 1, ttlMs?: number): number { | ||
// @ts-ignore (Transaction returns boolean, not void.) | ||
return this.db.transaction(() => { | ||
const map = this.get<Map<string, number>>(key) ?? new Map<string, number>() | ||
let newValue: number | ||
try { | ||
newValue = Number(map.get(field) ?? 0) + incrBy | ||
} catch (error: any) { | ||
if (error.toString().includes("TypeError")) { | ||
return NaN | ||
} else { | ||
throw error | ||
} | ||
} | ||
if (isNaN(newValue)) return NaN | ||
map.set(field, newValue) | ||
this.set(key, map, ttlMs) | ||
return newValue | ||
}).immediate() | ||
} | ||
// Inspired by: https://docs.keydb.dev/docs/commands/#hincrby | ||
hDecr(key: string, field: string, decrBy: number = 1, ttlMs?: number): number { | ||
return this.hIncr(key, field, decrBy * -1, ttlMs) | ||
} | ||
// ToDo: lIndex() | ||
@@ -807,0 +834,0 @@ // Inspired by: https://docs.keydb.dev/docs/commands/#lindex |
89993
1393
1400