|
1 | | -import type { KV, Options, Database as DB } from 'worktop/cfw.kv'; |
| 1 | +import * as Cache from './internal/cfw.cache'; |
| 2 | +import type { KV, Options, Database as DB, Entity as E } from 'worktop/cfw.kv'; |
| 3 | +import type { Promisable } from 'worktop/utils'; |
2 | 4 |
|
3 | 5 | export function Database<Models, I extends Record<keyof Models, string> = { [P in keyof Models]: string }>(binding: KV.Namespace): DB<Models, I> { |
4 | 6 | var $ = <K extends keyof I>(type: K, uid: I[K]) => `${type}__${uid}`; |
@@ -77,3 +79,117 @@ export async function until<X extends string>( |
77 | 79 | if (exists == null) return tmp; |
78 | 80 | } |
79 | 81 | } |
| 82 | + |
| 83 | +export class Entity implements E { |
| 84 | + readonly ns: KV.Namespace; |
| 85 | + readonly cache: Cache.Entity; |
| 86 | + |
| 87 | + prefix = ''; |
| 88 | + ttl = 0; |
| 89 | + |
| 90 | + onread?(key: string, value: unknown): Promisable<void>; |
| 91 | + onwrite?(key: string, value: unknown): Promisable<void>; |
| 92 | + ondelete?(key: string, value: unknown): Promisable<void>; |
| 93 | + |
| 94 | + constructor(ns: KV.Namespace) { |
| 95 | + this.cache = new Cache.Entity; |
| 96 | + this.ns = ns; |
| 97 | + } |
| 98 | + |
| 99 | + async list(options?: KV.Options.List): Promise<string[]> { |
| 100 | + options = options || {}; |
| 101 | + let { limit, prefix='' } = options; |
| 102 | + |
| 103 | + if (this.prefix) { |
| 104 | + options.prefix = prefix.startsWith(this.prefix) ? prefix : (this.prefix + prefix); |
| 105 | + } |
| 106 | + |
| 107 | + if (limit) { |
| 108 | + options.limit = Math.min(1000, limit); |
| 109 | + } |
| 110 | + |
| 111 | + let iter = list(this.ns, { |
| 112 | + ...options, |
| 113 | + metadata: false, |
| 114 | + }); |
| 115 | + |
| 116 | + let output: string[] = []; |
| 117 | + |
| 118 | + for await (let chunk of iter) { |
| 119 | + for (let i=0, len=this.prefix.length; i < chunk.keys.length; i++) { |
| 120 | + output.push((chunk.keys[i] as string).substring(len)); |
| 121 | + if (limit && output.length === limit) return output; |
| 122 | + } |
| 123 | + if (chunk.done) break; |
| 124 | + } |
| 125 | + |
| 126 | + return output; |
| 127 | + } |
| 128 | + |
| 129 | + async get<T>(key: string, format: Exclude<KV.GetFormat, 'stream'> = 'json'): Promise<T|null> { |
| 130 | + if (this.prefix) key = this.prefix + key; |
| 131 | + |
| 132 | + let value: T|null; |
| 133 | + let res = this.ttl && await this.cache.get(key); |
| 134 | + |
| 135 | + if (res) { |
| 136 | + value = await res[format](); |
| 137 | + } else { |
| 138 | + // @ts-ignore - TODO fix overload types |
| 139 | + value = await this.ns.get<T>(key, format); |
| 140 | + if (this.ttl) await this.cache.put(key, value, this.ttl); |
| 141 | + } |
| 142 | + |
| 143 | + if (this.onread) { |
| 144 | + await this.onread(key, value); |
| 145 | + } |
| 146 | + |
| 147 | + return value; |
| 148 | + } |
| 149 | + |
| 150 | + async put<T>(key: string, value: T|null): Promise<boolean> { |
| 151 | + if (this.prefix) key = this.prefix + key; |
| 152 | + |
| 153 | + let input = Cache.normalize(value); |
| 154 | + let bool = await this.ns.put(key, input).then( |
| 155 | + () => true, |
| 156 | + () => false |
| 157 | + ); |
| 158 | + |
| 159 | + if (bool && this.ttl) { |
| 160 | + // allow cache to see `null` value |
| 161 | + let x = value == null ? null : input; |
| 162 | + bool = await this.cache.put(key, x, this.ttl); |
| 163 | + } |
| 164 | + |
| 165 | + if (bool && this.onwrite) { |
| 166 | + await this.onwrite(key, value); |
| 167 | + } |
| 168 | + |
| 169 | + return bool; |
| 170 | + } |
| 171 | + |
| 172 | + async delete(key: string, format: Exclude<KV.GetFormat, 'stream'> = 'json'): Promise<boolean> { |
| 173 | + if (this.prefix) key = this.prefix + key; |
| 174 | + |
| 175 | + let hasHook = typeof this.ondelete === 'function'; |
| 176 | + |
| 177 | + // @ts-ignore - TODO fix overload types |
| 178 | + let value = hasHook && await this.ns.get(key, format); |
| 179 | + |
| 180 | + let bool = await this.ns.delete(key).then( |
| 181 | + () => true, |
| 182 | + () => false |
| 183 | + ); |
| 184 | + |
| 185 | + if (bool && this.ttl) { |
| 186 | + bool = await this.cache.delete(key); |
| 187 | + } |
| 188 | + |
| 189 | + if (bool && hasHook) { |
| 190 | + await this.ondelete!(key, value); |
| 191 | + } |
| 192 | + |
| 193 | + return bool; |
| 194 | + } |
| 195 | +} |
0 commit comments