/
BaseKVStore.ts
85 lines (76 loc) · 3.08 KB
/
BaseKVStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// tslint:disable:unified-signatures
/**
* A value type that is safe to write to any storage option
*/
export type Value = string | number | boolean | null | ValueHash;
/**
* An object with some restrictions. An interface that follows the ValueHash interface
* can be safely written to any storage option.
*/
export interface ValueHash {
[field: string]: Value | Value[] | undefined;
}
/**
* Used when patching data.
* The previous value is provided as the first parameter.
* The return value will be used to update the record.
* WARNING: A patch updater may be called multiple times until the update is successful.
*/
export type PatchUpdater<T = ValueHash> = (previous: T) => T;
/**
* The base interface all key-value stores implement
*/
export interface BaseKVStore<T = ValueHash, R = true> {
/**
* Retrieve an object from the store given a key.
* @async
* @param key of the stored object
* @param fields to retrieve from the stored object, or undefined to retrieve the full object
* @returns hash of the complete object or only the specified fields, if supplied.
* An empty object is returned if the object, or all specified fields, does not exist.
*/
get<V extends T>(key: string, fields?: string[]): Promise<V>;
/**
* Write an object to the store at a given key. Overwrites the entire object.
* @async
* @param key of the stored object
* @param value complete hash to write
* @returns true if successful. Otherwise throws an error.
*/
put<V extends T>(key: string, value: V): Promise<R extends T ? V : R>;
/**
* Write a set of fields to an object in the store at a given key. Does not overwrite the entire object.
* @async
* @param key of the stored object
* @param value hash of fields and values to update the object with. Leaves all other fields untouched.
* @returns the complete object from before the update
* An empty object is returned if the object previously did not exist.
*/
patch<V extends T>(key: string, value: V): Promise<V>;
/**
* Update a stored object using a callback to make changes.
* @async
* @param key of the stored object
* @param updater function to manipulate the existing object (may be called multiple times to ensure an atomic change)
* @returns the complete object from before the update
* An empty object is returned if the object previously did not exist.
*/
// eslint-disable-next-line @typescript-eslint/unified-signatures
patch<V extends T>(key: string, updater: PatchUpdater<V>): Promise<V>;
/**
* Delete an object or a single field from the store at a given key.
* If fields is undefined, the entire object will be deleted.
* @async
* @param key of the stored object
* @param fields to delete or undefined to delete all fields
* @returns true if successful. Otherwise throws an error.
*/
delete<V extends T>(key: string, fields?: string[]): Promise<R extends T ? V : R>;
/**
* Check if an object exists at a given key.
* @async
* @param key of the stored object
* @returns true if the object exists
*/
exists(key: string): Promise<boolean>;
}