forked from Keyri-Co/EZindexDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
534 lines (455 loc) · 15.8 KB
/
index.mjs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
* A class to simplify interactions with IndexedDB.
*/
class EZindexDB{
#database;
/**
* Initializes a connection to the database or creates it if it doesn't exist.
*
* @param {string} database - The name of the database.
* @param {string} table - The name of the table (object store).
* @param {Array<string>} [indexes] - An array of index names to be created.
* @returns {Promise<boolean>} Resolves to true if successful.
*/
start = (database, table, indexes) => {
return new Promise((resolve, reject) => {
// start connection to DB, then, listen for events
const openRequest = indexedDB.open(database, 1);
// handle error
openRequest.onerror = event => {
reject(event.target.error);
};
// upgradeNeeded ???
openRequest.onupgradeneeded = event => {
this.#database = event.target.result;
const store = this.#database.createObjectStore(table, {"keyPath": "id"});
// If we're taking indexes, let's create indexes
if(indexes){
indexes.forEach((index) => store.createIndex(index,index));
}
};
openRequest.onsuccess = event => {
this.#database = event.target.result;
resolve(true);
};
});
}
/**
* Creates a transaction for internal use.
*
* @private
* @param {string} table - The name of the table (object store).
* @returns {IDBObjectStore} The object store for the specified table.
*/
#transaction = async(table) => {
const transaction = await this.#database.transaction(table, 'readwrite');
const store = transaction.objectStore(table);
return store;
}
/**
* Adds a record to the database if it doesn't exist.
* Throws an error if the record already exists.
*
* @param {string} table - The name of the table (object store).
* @param {Object} data - The data to be added.
* @returns {Promise<IDBValidKey>} Resolves to the key of the added record.
*/
creates = (table, data) => {
return new Promise((resolve, reject) => {
// start a transaction
const transaction = this.#database.transaction(table, 'readwrite');
const store = transaction.objectStore(table);
// Try adding data to the store
const request = store.add(data);
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = (event) => {
console.error("Error adding data to IndexedDB:", event.target.error);
reject(event.target.error);
};
// Handle transaction errors
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Retrieves a record from the database by its ID.
*
* @param {string} table - The name of the table (object store).
* @param {IDBValidKey} id - The ID of the record to retrieve.
* @returns {Promise<Object>} Resolves to the retrieved record.
*/
reads = (table, id) => {
return new Promise((resolve, reject) => {
// start a transaction
const transaction = this.#database.transaction(table, 'readonly');
const store = transaction.objectStore(table);
// Try getting some information out of the database
const request = store.get(id);
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = (event) => {
console.error("Error reading data from IndexedDB:", event.target.error);
reject(event.target.error);
};
// Handle transaction errors
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Updates an existing record in the database.
* Throws an error if the record doesn't exist.
*
* @param {string} table - The name of the table (object store).
* @param {Object} data - The data to update.
* @returns {Promise<IDBValidKey>} Resolves to the key of the updated record.
*/
updates = (table, data) => {
return new Promise(async (resolve, reject) => {
// see if the thing exists first.
// if not, fail it
let test_data = await this.reads(table, data.id);
if(!test_data){
reject(new Error("A record must exist before you can update it"));
}
// start a transaction
const transaction = this.#database.transaction(table, 'readwrite');
const store = transaction.objectStore(table);
// Try updating data in the store
const request = store.put({...test_data, ...data});
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = (event) => {
console.error("Error updating data in IndexedDB:", event.target.error);
reject(event.target.error);
};
// Handle transaction errors
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Inserts or updates a record in the database.
*
* @param {string} table - The name of the table (object store).
* @param {Object} data - The data to insert or update.
* @returns {Promise<IDBValidKey>} Resolves to the key of the inserted or updated record.
*/
upserts = (table, data) => {
return new Promise(async (resolve, reject) => {
// see if the thing exists first.
// if not, fail it
let test_data = await this.reads(table, data.id);
// start a transaction
const transaction = this.#database.transaction(table, 'readwrite');
const store = transaction.objectStore(table);
let request;
// Try updating data in the store
if(test_data){
request = store.put({...test_data, ...data});
} else {
request = store.add(data);
}
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = (event) => {
console.error("Error updating data in IndexedDB:", event.target.error);
reject(event.target.error);
};
// Handle transaction errors
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Deletes a record from the database by its ID.
*
* @param {string} table - The name of the table (object store).
* @param {IDBValidKey} id - The ID of the record to delete.
* @returns {Promise<boolean>} Resolves to true if the deletion was successful.
*/
deletes = (table, id) => {
return new Promise((resolve, reject) => {
// start a transaction
const transaction = this.#database.transaction(table, 'readwrite');
const store = transaction.objectStore(table);
// Try deleting the record from the store
const request = store.delete(id);
request.onsuccess = () => {
resolve(true);
};
request.onerror = (event) => {
console.error("Error deleting record from IndexedDB:", event.target.error);
reject(event.target.error);
};
// Handle transaction errors
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Searches for records in the database by a specified field and value.
*
* @param {string} table - The name of the table (object store).
* @param {string} field - The name of the field to search by.
* @param {any} value - The value to search for.
* @returns {Promise<Array<Object>>} Resolves to an array of matching records.
*/
searches = (table, field, value) => {
return new Promise((resolve, reject) => {
// start a transaction
const transaction = this.#database.transaction(table, 'readonly');
const store = transaction.objectStore(table);
// Set Reference to our Index
let ndx = store.index(field);
// Try getting some information out of the database
const request = ndx.getAll(value);
request.onsuccess = () => {
// The result of the request will be in request.result
resolve(request.result);
};
request.onerror = (event) => {
console.error("Error reading from IndexedDB:", event.target.error);
reject(event.target.error);
};
// Handle transaction errors
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Retrieves all records from a table.
*
* @param {string} table - The name of the table (object store).
* @returns {Promise<Array<Object>>} Resolves to an array of all records.
*/
getAll = (table) => {
return new Promise((resolve, reject) => {
const transaction = this.#database.transaction(table, 'readonly');
const store = transaction.objectStore(table);
const request = store.openCursor();
const allRecords = [];
request.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
allRecords.push(cursor.value);
cursor.continue();
} else {
resolve(allRecords);
}
};
request.onerror = (event) => {
console.error("Error retrieving all records from IndexedDB:", event.target.error);
reject(event.target.error);
};
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
/**
* Counts the number of records in a table.
*
* @param {string} table - The name of the table (object store).
* @returns {Promise<number>} Resolves to the count of records.
*/
countRecords = (table) => {
return new Promise((resolve, reject) => {
const transaction = this.#database.transaction(table, 'readonly');
const store = transaction.objectStore(table);
const request = store.count();
request.onsuccess = () => {
resolve(request.result);
};
request.onerror = (event) => {
console.error("Error counting records in IndexedDB:", event.target.error);
reject(event.target.error);
};
transaction.onerror = (event) => {
console.error("Transaction error:", event.target.error);
};
});
}
}
/////////////////////////////////
// DEMO:
/////////////////////////////////
/*
// Instantiate the DB
let ez = new EZindexDB();
//
// List any of the fields we might want to search on
// that aren't "id"
//
await ez.start("company","people",["name"]);
//
// Demonstration of adding people to our DB
//
await ez.creates("people",{"id": "1", "salary": 12, "name": "STEVE"});
await ez.creates("people",{"id": "2", "salary": 12, "name": "EDDY"});
await ez.creates("people",{"id": "3", "salary": 12, "name": "JOE"});
await ez.creates("people",{"id": "4", "salary": 13, "name": "JOE"});
//
// Find everybody named "JOE"
//
let data = await ez.searches("people","name", "JOE");
//
// Set Joe's Salary to 12_000
//
await ez.updates("people",{"id": "3", "salary": 12_000});
//
// Make sure we can't 'upsert' a record
//
await ez.updates("people",{"id": "newb", "salary": 12_000}); // this one fails
*/
/**
* A class to simulate interactions with IndexedDB using in-memory storage.
*/
export default class SynthEzIndexDB {
/**
* In-memory storage object.
* @type {Object}
* @private
*/
#inMemoryStorage = {};
/**
* Initializes a connection to the in-memory database or creates it if it doesn't exist.
*
* @param {string} database - The name of the database.
* @param {string} table - The name of the table (object store).
* @param {Array<string>} [indexes] - An array of index names to be created (ignored in this implementation).
* @returns {Promise<boolean>} Resolves to true if successful.
*/
start = async (database, table, indexes) => {
if (!this.#inMemoryStorage[database]) {
this.#inMemoryStorage[database] = {};
}
if (!this.#inMemoryStorage[database][table]) {
this.#inMemoryStorage[database][table] = {};
}
return true;
};
/**
* Adds a record to the in-memory database if it doesn't exist.
* Throws an error if the record already exists.
*
* @param {string} table - The name of the table (object store).
* @param {Object} data - The data to be added.
* @returns {Promise<string>} Resolves to the key of the added record.
*/
creates = async (table, data) => {
const db = this.#inMemoryStorage;
if (!db[table]) {
db[table] = {};
}
if (db[table][data.id]) {
throw new Error("Record already exists");
}
db[table][data.id] = data;
return data.id;
};
/**
* Retrieves a record from the in-memory database by its ID.
*
* @param {string} table - The name of the table (object store).
* @param {string} id - The ID of the record to retrieve.
* @returns {Promise<Object>} Resolves to the retrieved record.
*/
reads = async (table, id) => {
const db = this.#inMemoryStorage;
return db[table] ? db[table][id] : undefined;
};
/**
* Updates an existing record in the in-memory database.
* Throws an error if the record doesn't exist.
*
* @param {string} table - The name of the table (object store).
* @param {Object} data - The data to update.
* @returns {Promise<string>} Resolves to the key of the updated record.
*/
updates = async (table, data) => {
const db = this.#inMemoryStorage;
if (!db[table] || !db[table][data.id]) {
throw new Error("A record must exist before you can update it");
}
db[table][data.id] = { ...db[table][data.id], ...data };
return data.id;
};
/**
* Inserts or updates a record in the in-memory database.
*
* @param {string} table - The name of the table (object store).
* @param {Object} data - The data to insert or update.
* @returns {Promise<string>} Resolves to the key of the inserted or updated record.
*/
upserts = async (table, data) => {
const db = this.#inMemoryStorage;
if (!db[table]) {
db[table] = {};
}
db[table][data.id] = { ...db[table][data.id], ...data };
return data.id;
};
/**
* Deletes a record from the in-memory database by its ID.
*
* @param {string} table - The name of the table (object store).
* @param {string} id - The ID of the record to delete.
* @returns {Promise<boolean>} Resolves to true if the deletion was successful.
*/
deletes = async (table, id) => {
const db = this.#inMemoryStorage;
if (!db[table] || !db[table][id]) {
throw new Error("Record not found");
}
delete db[table][id];
return true;
};
/**
* Searches for records in the in-memory database by a specified field and value.
*
* @param {string} table - The name of the table (object store).
* @param {string} field - The name of the field to search by.
* @param {any} value - The value to search for.
* @returns {Promise<Array<Object>>} Resolves to an array of matching records.
*/
searches = async (table, field, value) => {
const db = this.#inMemoryStorage;
if (!db[table]) {
return [];
}
return Object.values(db[table]).filter(record => record[field] === value);
};
/**
* Retrieves all records from a table in the in-memory database.
*
* @param {string} table - The name of the table (object store).
* @returns {Promise<Array<Object>>} Resolves to an array of all records.
*/
getAll = async (table) => {
const db = this.#inMemoryStorage;
return db[table] ? Object.values(db[table]) : [];
}
/**
* Counts the number of records in a table in the in-memory database.
*
* @param {string} table - The name of the table (object store).
* @returns {Promise<number>} Resolves to the count of records.
*/
countRecords = async (table) => {
const db = this.#inMemoryStorage;
return db[table] ? Object.keys(db[table]).length : 0;
}
}
export {EZindexDB, SynthEzIndexDB}