Skip to content

Commit

Permalink
chore(kv): extract prefix?key helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Apr 25, 2022
1 parent 24e758d commit b85e39b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/worktop/src/cfw.kv.ts
Expand Up @@ -80,6 +80,10 @@ export async function until<X extends string>(
}
}

function keyname(prefix: string, key: string): string {
return !prefix || key.startsWith(prefix + '~') ? key : (prefix + '~' + key);
}

export class Entity implements E {
readonly ns: KV.Namespace;
readonly cache: Cache.Entity;
Expand All @@ -101,7 +105,7 @@ export class Entity implements E {
let { limit, prefix='' } = options;

if (this.prefix) {
options.prefix = prefix.startsWith(this.prefix) ? prefix : (this.prefix + prefix);
options.prefix = keyname(this.prefix, prefix);
}

if (limit) {
Expand All @@ -127,7 +131,7 @@ export class Entity implements E {
}

async get<T>(key: string, format: Exclude<KV.GetFormat, 'stream'> = 'json'): Promise<T|null> {
if (this.prefix) key = this.prefix + key;
key = keyname(this.prefix, key);

let value: T|null;
let res = this.ttl && await this.cache.get(key);
Expand All @@ -148,7 +152,7 @@ export class Entity implements E {
}

async put<T>(key: string, value: T|null): Promise<boolean> {
if (this.prefix) key = this.prefix + key;
key = keyname(this.prefix, key);

let input = Cache.normalize(value);
let bool = await this.ns.put(key, input).then(
Expand All @@ -170,7 +174,7 @@ export class Entity implements E {
}

async delete(key: string, format: Exclude<KV.GetFormat, 'stream'> = 'json'): Promise<boolean> {
if (this.prefix) key = this.prefix + key;
key = keyname(this.prefix, key);

let hasHook = typeof this.ondelete === 'function';

Expand Down

0 comments on commit b85e39b

Please sign in to comment.