Skip to content

Commit

Permalink
chore: kv fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Nov 7, 2024
1 parent bc575b1 commit ed712c9
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions packages/kv-store/src/keyv/keyv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,12 @@ export class Keyv<Value = any> extends EventEmitter implements KeyvStore<Value>
)
}

_getKeyPrefix(key: string, forMany = false): string {
if (forMany) {
// We can list all keys on the default namespace using :key
return key.startsWith(':') ? `${this.opts.namespace}${key}:` : `${key}:`
}

return key.includes(':') ? key : `${this.opts.namespace}:${key}`
_getKeyPrefix(key: string): string {
return `${this.opts.namespace}:${key}`
}

_getKeyPrefixArray(keys: string[]): string[] {
return keys.map((key) => this._getKeyPrefix(key, true))
return keys.map((key) => this._getKeyPrefix(key))
}

_getKeyUnprefix(key: string): string {
Expand All @@ -135,7 +130,7 @@ export class Keyv<Value = any> extends EventEmitter implements KeyvStore<Value>
promise = this.store.getMany(keyPrefixed, options) as Promise<KeyvStoredData<Value>[]>
} else if (this.isMapWithEntries(this.store)) {
// Handle Map-based stores with prefix matching
promise = this.getFromMapWithPrefix(keyPrefixed[0], options)
promise = this.getFromMapWithPrefix(keyPrefixed, options)
} else {
promise = Promise.all(keyPrefixed.map((k) => this.store.get(k, options) as Promise<KeyvStoredData<Value>>))
}
Expand Down Expand Up @@ -167,20 +162,34 @@ export class Keyv<Value = any> extends EventEmitter implements KeyvStore<Value>
return store instanceof Map && typeof store.entries === 'function'
}

private async getFromMapWithPrefix(prefix: string, options?: { raw?: boolean }): Promise<Array<KeyvStoredData<Value>>> {
private async getFromMapWithPrefix(
prefixes: Array<string>,
options?: {
raw?: boolean
},
): Promise<Array<KeyvStoredData<Value>>> {
if (!this.isMapWithEntries(this.store)) {
return []
}

const results: KeyvStoredData<Value>[] = []

for (const [key, value] of this.store.entries()) {
if (key.startsWith(prefix)) {
results.push(value as KeyvStoredData<Value>)
const map = this.store as Map<string, Value>
const entries = Array.from(map.entries())

const result = []
for (const prefix of prefixes) {
let found: boolean = false
for (const [key, value] of entries) {
console.debug('key, value', key, value)
if (key.startsWith(prefix)) {
found = true
result.push(value as KeyvStoredData<Value>)
}
}
if (!found) {
result.push(undefined)
}
}

return results
return result
}

async get(
Expand Down

0 comments on commit ed712c9

Please sign in to comment.