-
Notifications
You must be signed in to change notification settings - Fork 773
/
db.ts
82 lines (72 loc) · 2.07 KB
/
db.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
export type DBObject = {
[key: string]: string | string[] | number
}
export type BatchDBOp<
TKey extends Uint8Array | string | number = Uint8Array,
TValue extends Uint8Array | string | DBObject = Uint8Array,
> = PutBatch<TKey, TValue> | DelBatch<TKey>
export enum KeyEncoding {
String = 'string',
Bytes = 'view',
Number = 'number',
}
export enum ValueEncoding {
String = 'string',
Bytes = 'view',
JSON = 'json',
}
export type EncodingOpts = {
keyEncoding?: KeyEncoding
valueEncoding?: ValueEncoding
}
export interface PutBatch<
TKey extends Uint8Array | string | number = Uint8Array,
TValue extends Uint8Array | string | DBObject = Uint8Array,
> {
type: 'put'
key: TKey
value: TValue
opts?: EncodingOpts
}
export interface DelBatch<TKey extends Uint8Array | string | number = Uint8Array> {
type: 'del'
key: TKey
opts?: EncodingOpts
}
export interface DB<
TKey extends Uint8Array | string | number = Uint8Array,
TValue extends Uint8Array | string | DBObject = Uint8Array,
> {
/**
* Retrieves a raw value from db.
* @param key
* @returns A Promise that resolves to `Uint8Array` if a value is found or `undefined` if no value is found.
*/
get(key: TKey, opts?: EncodingOpts): Promise<TValue | undefined>
/**
* Writes a value directly to db.
* @param key The key as a `TValue`
* @param value The value to be stored
*/
put(key: TKey, val: TValue, opts?: EncodingOpts): Promise<void>
/**
* Removes a raw value in the underlying db.
* @param keys
*/
del(key: TKey, opts?: EncodingOpts): Promise<void>
/**
* Performs a batch operation on db.
* @param opStack A stack of levelup operations
*/
batch(opStack: BatchDBOp<TKey, TValue>[]): Promise<void>
/**
* Returns a copy of the DB instance, with a reference
* to the **same** underlying db instance.
*/
shallowCopy(): DB<TKey, TValue>
/**
* Opens the database -- if applicable
*/
open(): Promise<void>
// TODO - decide if we actually need open/close - it's not required for maps and Level automatically opens the DB when you instantiate it
}