-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Related #2
- Loading branch information
Showing
7 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ node_modules | |
*-lock.* | ||
*.lock | ||
|
||
/kv | ||
/cache | ||
/router | ||
/request | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export namespace KV { | ||
type Value = string | ReadableStream | ArrayBuffer; | ||
type WriteOptions = { expiration?: number; expirationTtl?: number }; | ||
type ListOptions = { prefix?: string; limit?: number; cursor?: string }; | ||
type GetOptions = 'text' | 'json' | 'arrayBuffer' | 'stream'; | ||
|
||
interface KeyList { | ||
keys: Array<{ name: string; expiration?: number }>; | ||
list_complete: boolean; | ||
cursor: string; | ||
} | ||
|
||
interface Namespace { | ||
get<T>(key: string, type: 'json'): Promise<T>; | ||
get<T>(key: string, type: 'stream'): Promise<ReadableStream>; | ||
get<T>(key: string, type: 'arrayBuffer'): Promise<ArrayBuffer>; | ||
get<T>(key: string, type: 'text'): Promise<string>; | ||
get<T>(key: string, type: GetOptions): Promise<T>; | ||
get<T>(key: string): Promise<string>; // "text" | ||
|
||
put(key: string, value: Value, options?: WriteOptions): Promise<void>; | ||
list(options?: ListOptions): Promise<KeyList>; | ||
delete(key: string): Promise<void>; | ||
} | ||
} | ||
|
||
export declare class Database<Models, Identifiers extends Record<keyof Models, string> = { [P in keyof Models]: string}> { | ||
constructor(binding: KV.Namespace); | ||
get<K extends keyof Models>(type: K, uid: Identifiers[K], format?: KV.GetOptions): Promise<Models[K] | false>; | ||
put<K extends keyof Models>(type: K, uid: Identifiers[K], value: Models[K], isJSON?: boolean): Promise<boolean>; | ||
del<K extends keyof Models>(type: K, uid: Identifiers[K]): Promise<boolean>; | ||
} | ||
|
||
export function read<T>(binding: KV.Namespace, key: string, format?: KV.GetOptions): Promise<T | false>; | ||
export function write<T>(binding: KV.Namespace, key: string, value: T, isJSON?: boolean): Promise<boolean>; | ||
export function remove(binding: KV.Namespace, key: string): Promise<boolean>; | ||
|
||
export function until<X extends string>( | ||
toMake: () => X, | ||
toSearch: (val: X) => Promise<unknown | false> | ||
): Promise<X>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { KV, Database as DB } from 'worktop/kv'; | ||
|
||
export function Database<M, I extends Record<keyof M, string> = { [P in keyof M]: string }>(binding: KV.Namespace): DB<M, I> { | ||
var $ = <K extends keyof I>(type: K, uid: I[K]) => `${type}__${uid}`; | ||
|
||
return { | ||
get<K extends keyof M>(type: K, uid: I[K], format?: KV.GetOptions) { | ||
return read<M[K]>(binding, $(type, uid), format); | ||
}, | ||
put<K extends keyof M>(type: K, uid: I[K], value: M[K], isJSON = true) { | ||
return write<M[K]>(binding, $(type, uid), value, isJSON); | ||
}, | ||
del<K extends keyof M>(type: K, uid: I[K]) { | ||
return remove(binding, $(type, uid)); | ||
} | ||
}; | ||
} | ||
|
||
export function read<T>(binding: KV.Namespace, key: string, format: KV.GetOptions = 'json'): Promise<T | false> { | ||
return binding.get<T>(key, format).then(x => x !== void 0 ? x : false); | ||
} | ||
|
||
export function write<T=any>(binding: KV.Namespace, key: string, value: T, isJSON: boolean = true): Promise<boolean> { | ||
return binding.put(key, (!isJSON && (typeof value === 'string' || value instanceof ArrayBuffer || value instanceof ReadableStream)) ? value : JSON.stringify(value)).then(() => true, () => false); | ||
} | ||
|
||
export function remove(binding: KV.Namespace, key: string): Promise<boolean> { | ||
return binding.delete(key).then(() => true, () => false); | ||
} | ||
|
||
export async function until<X extends string>( | ||
toMake: () => X, | ||
toSearch: (val: X) => Promise<unknown | false> | ||
): Promise<X> { | ||
let exists, tmp = '' as X; | ||
while (exists !== false) { | ||
exists = await toSearch(tmp = toMake()); | ||
} | ||
return tmp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters