-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathInMemoryStore.ts
50 lines (39 loc) · 1.03 KB
/
InMemoryStore.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
export class InMemoryStore {
db = new Map<string, string>();
getAllKeys = async () => {
return Array.from(this.db.keys());
};
multiGet = async (keys: string[]) => {
return keys.reduce(
(res, k) => {
res.push([k, this.db.get(k)!]);
return res;
},
[] as [string, string][],
);
};
multiRemove = async (keys: string[], callback?) => {
keys.forEach(k => this.db.delete(k));
typeof callback === 'function' && callback();
};
multiSet = async (entries: string[][], callback?) => {
entries.forEach(([key, value]) => {
this.setItem(key, value);
});
typeof callback === 'function' && callback();
};
setItem = async (key: string, value: string) => {
return this.db.set(key, value);
};
removeItem = async (key: string) => {
return this.db.delete(key);
};
getItem = async (key: string) => {
return this.db.get(key);
};
}
export function createInMemoryStore() {
return new InMemoryStore();
}