-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDBObject.ts
228 lines (206 loc) · 5.89 KB
/
IDBObject.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { IDBPDatabase } from "idb";
import { IDBORM, ObjectStoreInitializer, IDBObjectKey, Entry, EntriesIteratorCallbackfn } from ".";
export class IDBObject {
private db: Promise<IDBPDatabase<unknown>>;
private readonly storeName: string;
private readonly storeOptions: IDBObjectStoreParameters | undefined;
/** @ignore */
constructor(db: Promise<IDBPDatabase<unknown>>, { name, options }: ObjectStoreInitializer) {
this.db = db;
this.storeName = name;
this.storeOptions = options;
}
/**
* Put a record in the object store
* Replaces items with the same keys
*
* @param value - item's value
*
* @param key - item's key **when no option (keyPath or autoIncrement) specified this argument is required**
*
* @typeparam Value - value's type
*
* @returns Recorded value
*
* Using `id` as objectStore keyPath option :
* ```ts
* await Todo.put({ id: 1, content: 'Pet my cat' })
* ```
* Using autoIncrement option :
* ```ts
* await Todo.put({ content: 'Pet my cat' })
* ```
* No option (keyPath or autoIncrement) provided :
* ```ts
* await Todo.put({ content: 'Pet my cat' }, 'task one')
* ```
*/
public put = <Value = any>(value: Value, key?: IDBObjectKey): Promise<Value> => {
const { db, storeName } = this;
return db
.then(_db => {
const tx = _db.transaction(storeName, "readwrite");
const store = tx.objectStore(storeName);
store.put(value, key);
return tx.done;
})
.then(() => value)
.catch(err => {
throw new Error(`${IDBORM}: ${storeName}.put(${value}${key ? `, ${key}` : ""}): ${err}`);
});
};
/**
* Get a record from object store
*
* @param key - item's key
*
* @typeparam Value - value's type
*
* @returns Recorded value
*
* Get record with key `task one`
* ```ts
* await Todo.get('task one')
* ```
*/
public get = async <Value = any>(key: IDBObjectKey): Promise<Value | undefined> => {
const { db, storeName } = this;
return db
.then(_db => {
const tx = _db.transaction(storeName, "readwrite");
const store = tx.objectStore(storeName);
return store.get(key);
})
.catch(err => {
throw new Error(`${IDBORM}: ${storeName}.get(${key}): ${err}`);
});
};
/**
* Delete a record from object store
*
* @param key - item's key
*
* Delete record with key `task one`
* ```ts
* await Todo.delete('task one')
* ```
*/
public delete = async (key: IDBObjectKey): Promise<void> => {
const { db, storeName } = this;
return db
.then(async _db => {
const tx = _db.transaction(storeName, "readwrite");
const store = tx.objectStore(storeName);
store.delete(key);
return tx.done;
})
.catch(err => {
throw new Error(`${IDBORM}: ${storeName}.get(${key}): ${err}`);
});
};
/**
* Retrieves the keys of records in an object store
*
* @returns A list containing all object store keys
*
* ```ts
* await Todo.keys()
* ```
*/
public keys = async (): Promise<IDBObjectKey[]> => {
const { db, storeName } = this;
return db
.then(async _db => {
const tx = _db.transaction(storeName, "readwrite");
const store = tx.objectStore(storeName);
return store.getAllKeys();
})
.catch(err => {
throw new Error(`${IDBORM}: ${storeName}.keys(): ${err}`);
});
};
/**
* Retrieves the values of records in an object store
*
* @typeparam Value - value's type
*
* @returns An list containing all object store values
*
* ```ts
* await Todo.values()
* ```
*/
public values = <Value = any>(): Promise<Value[]> => {
const { db, storeName } = this;
return db
.then(async _db => {
const tx = _db.transaction(storeName, "readwrite");
const store = tx.objectStore(storeName);
return store.getAll();
})
.catch(err => {
throw new Error(`${IDBORM}: ${storeName}.values(): ${err}`);
});
};
/**
* Retrieves an array of a given object's own enumerable string-keyed property `[key, value]` pairs
*
* @typeparam Value - value's type
*
* @returns an array of a given object's own enumerable string-keyed property `[key, value]` pairs
*
* ```ts
* await Todo.entries()
* ```
*/
public entries = async <Value = any>(): Promise<Entry<Value>[]> => {
const { storeName } = this;
try {
const keyAndValues = await Promise.all([this.keys(), this.values()]);
return keyAndValues?.[0].map((key: IDBObjectKey, idx: number) => [key, keyAndValues?.[1][idx]]) || [];
} catch (err) {
throw new Error(`${IDBORM}: ${storeName}.entries(): ${err}`);
}
};
/**
* Delete all records stored in an object store
*
* ```ts
* await Todo.clear()
* ```
*/
public clear = async (): Promise<void> => {
const { db, storeName } = this;
return db
.then(_db => {
const tx = _db.transaction(storeName, "readwrite");
const store = tx.objectStore(storeName);
store.clear();
return tx.done;
})
.catch(err => {
throw new Error(`${IDBORM}: ${storeName}.clear(): ${err}`);
});
};
/**
* Iterate over all the record in an object store and perform an async action on each one
*
* @param callbackfn - async callback(action)
*
* @typeparam Value - async callback returns type
*
* @returns A list contains async action results
*
* Delete all completed task :
* ```ts
* await Todo.iterate(([key, value], index, entries) => {
* if (value.completed) return Todo.delete(key);
* })
* ```
*/
public iterate = async <Value = any>(callbackfn: EntriesIteratorCallbackfn<Value>): Promise<Value[]> => {
const { entries } = this;
const _entries = await entries();
return Promise.all(_entries.map(callbackfn));
};
}