Skip to content

Commit

Permalink
hSet(), hGet(), hmSet()
Browse files Browse the repository at this point in the history
  • Loading branch information
gerold-penz committed Aug 9, 2024
1 parent 88c7692 commit 5a24ec0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,12 +508,11 @@ export class BunSqliteKeyValue {

// Inspired by: https://docs.keydb.dev/docs/commands/#incrby
incr(key: string, incrBy: number = 1, ttlMs?: number): number {
const self = this
// @ts-ignore (Transaction returns a number or NaN, not void.)
return this.db.transaction(() => {
const newValue = Number(self.get<number>(key) ?? 0) + incrBy
const newValue = Number(this.get<number>(key) ?? 0) + incrBy
if (isNaN(newValue)) return NaN
self.set<number>(key, newValue, ttlMs)
this.set<number>(key, newValue, ttlMs)
return newValue
}).immediate()
}
Expand All @@ -531,11 +530,10 @@ export class BunSqliteKeyValue {
// Returns the length of the string after the append operation.
// Inspired by: https://docs.keydb.dev/docs/commands/#append
append(key: string, value: string, ttlMs?: number): number {
const self = this
// @ts-ignore (Transaction returns a number, not void.)
return this.db.transaction(() => {
const newValue = String(self.get<string>(key) ?? "") + value
self.set<string>(key, newValue, ttlMs)
const newValue = String(this.get<string>(key) ?? "") + value
this.set<string>(key, newValue, ttlMs)
return newValue.length
}).immediate()
}
Expand All @@ -544,11 +542,10 @@ export class BunSqliteKeyValue {
// 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)
const oldValue = this.get<T>(key)
this.set<T>(key, value, ttlMs)
return oldValue
}).immediate()
}
Expand Down Expand Up @@ -595,6 +592,7 @@ export class BunSqliteKeyValue {
// If `newKey` already exists it is deleted first.
// Inspired by: https://docs.keydb.dev/docs/commands/#rename
rename(oldKey: string, newKey: string): boolean {
// @ts-ignore (Transaction returns boolean, not void.)
return this.db.transaction(() => {
if (this.has(oldKey)) {
this.delete(newKey)
Expand All @@ -603,7 +601,7 @@ export class BunSqliteKeyValue {
} else {
return false
}
})()
}).immediate()
}


Expand All @@ -617,18 +615,21 @@ export class BunSqliteKeyValue {
// Inspired by: https://docs.keydb.dev/docs/commands/#pttl


// Don't use it with large values or blobs.
// Inspired by: https://docs.keydb.dev/docs/commands/#hset
hSet<T = any>(key: string, field: string, value: T, ttlMs?: number): boolean {
// @ts-ignore (Transaction returns boolean, not void.)
return this.db.transaction(() => {
const internalValue = this.get<Map<string, T>>(key) ?? new Map<string, T>()
const isNewField: boolean = !internalValue.has(field)
internalValue.set(field, value)
this.set(key, internalValue, ttlMs)
return isNewField
})()
}).immediate()
}


// Don't use it with large values or blobs.
// Inspired by: https://docs.keydb.dev/docs/commands/#hget
hGet<T = any>(key: string, field: string): T | undefined {
const internalValue = this.get<Map<string, T>>(key)
Expand All @@ -637,6 +638,7 @@ export class BunSqliteKeyValue {
}


// Don't use it with large values or blobs.
// Inspired by: https://docs.keydb.dev/docs/commands/#hmset
hmSet<T = any>(key: string, fields: {[field: string]: T}, ttlMs?: number) {
this.db.transaction(() => {
Expand All @@ -645,11 +647,12 @@ export class BunSqliteKeyValue {
internalValue.set(field, value)
})
this.set(key, internalValue, ttlMs)
})()
}).immediate()
}


// ToDo: hmGet()
// Don't use it with large values or blobs.
// Inspired by: https://docs.keydb.dev/docs/commands/#hmget


Expand All @@ -670,6 +673,7 @@ export class BunSqliteKeyValue {


// ToDo: hVals()
// Don't use it with large values or blobs.
// Inspired by: https://docs.keydb.dev/docs/commands/#hvals


Expand Down
2 changes: 1 addition & 1 deletion tests/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ test("rename()", async () => {
})


test("hSet(), hGet(), hmSet()", async () => {
test("hSet(), hGet(), hmSet(), hmGet()", async () => {
const store = new BunSqliteKeyValue()

store.hSet(KEY_1, FIELD_1, STRING_VALUE_1)
Expand Down

0 comments on commit 5a24ec0

Please sign in to comment.