Repository navigation
Expand file tree
/
Copy pathmobile-kv-store.ts
More file actions
67 lines (56 loc) · 2.3 KB
/
Copy pathmobile-kv-store.ts
File metadata and controls
67 lines (56 loc) · 2.3 KB
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
import { inject } from '@angular/core';
import { DatabaseService } from './database.service';
import { KeyValueCollection, KeyValueKey, KeyValuePair, KVStorageProvider } from './kv-types';
export class MobileKVStore implements KVStorageProvider {
private database = inject(DatabaseService);
constructor(private collection: KeyValueCollection) {}
async clear(): Promise<void> {
const handle = await this.database.getHandle();
if (handle) {
await handle.run('DELETE FROM KeyValuePairs WHERE collection = ?', [this.collection]);
}
}
async getAll(): Promise<KeyValuePair[]> {
const handle = await this.database.getHandle();
if (!handle) return [];
const result = await handle.query('SELECT id, value FROM KeyValuePairs WHERE collection = ? ORDER BY id', [
this.collection,
]);
return (result.values ?? []).map(({ id, value }) => ({ key: JSON.parse(id), value: JSON.parse(value) }));
}
async getValue(key: KeyValueKey): Promise<any | undefined> {
const handle = await this.database.getHandle();
if (!handle) return undefined;
const result = await handle.query('SELECT value FROM KeyValuePairs WHERE id = ? AND collection = ?', [
JSON.stringify(key),
this.collection,
]);
return result.values?.length ? JSON.parse(result.values[0].value) : undefined;
}
async removeValue(key: KeyValueKey): Promise<void> {
const handle = await this.database.getHandle();
if (handle) {
await handle.run('DELETE FROM KeyValuePairs WHERE id = ? AND collection = ?', [
JSON.stringify(key),
this.collection,
]);
}
}
async setValue(key: KeyValueKey, value: any): Promise<void> {
const handle = await this.database.getHandle();
if (handle) {
await handle.run(
'INSERT INTO KeyValuePairs (id, collection, value) VALUES (?, ?, ?) ON CONFLICT(id, collection) DO UPDATE SET value = excluded.value',
[JSON.stringify(key), this.collection, JSON.stringify(value)],
);
}
}
async getKeys(): Promise<KeyValueKey[]> {
const handle = await this.database.getHandle();
if (!handle) return [];
const result = await handle.query('SELECT id FROM KeyValuePairs WHERE collection = ? ORDER BY id', [
this.collection,
]);
return (result.values ?? []).map(({ id }) => JSON.parse(id));
}
}