-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstorage-repository.ts
539 lines (488 loc) · 14.7 KB
/
localstorage-repository.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
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
535
536
537
538
539
/**
* Error thrown when a filter operation cannot be found or is invalid.
* @extends {Error}
*
* @example
* ```typescript
* throw new FilterNotFoundError("Invalid filter criteria");
* ```
*/
export class FilterNotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = "FilterNotFoundError";
}
}
/**
* Error thrown when a record is expected but none is found.
* @extends Error
*
* @class EmptyRecordError
* @throws {EmptyRecordError} When attempting to access a record that doesn't exist
*
* @example
* throw new EmptyRecordError("No record found with id: 123");
*/
export class EmptyRecordError extends Error {
constructor(message: string) {
super(message);
this.name = "EmptyRecordError";
}
}
/**
* Represents the type of ID generator to be used.
* @enum {string}
* @property {string} Increment - Uses an auto-incrementing number as the ID.
* @property {string} UUID - Uses a Universally Unique Identifier (UUID/GUID) as the ID.
*/
export enum IdGeneratorType {
Increment = "increment",
UUID = "uuid",
}
/**
* Represents an identifier interface for database entities.
*
* @template G - The type of the identifier field.
* @property _id - The unique identifier for the database entity.
*/
export interface DatabaseId<G> {
_id: G;
}
/**
* Interface for a generic database implementation
* @template T The type of data stored in the database
*/
export interface IDatabase<T> {
/**
* Retrieves all records matching the optional filter
* @param filter - Optional partial object to filter records
* @returns Array of matching records
*/
findAll: (filter?: Partial<T>) => T[];
/**
* Finds a single record matching the filter
* @param filter - Partial object to filter records
* @returns Matching record or undefined if not found
*/
findOne: (filter: Partial<T>) => T | undefined;
/**
* Inserts a new record into the database
* @param data - Data to insert (excluding _id field)
* @returns The inserted record with generated _id
*/
insertOne: (data: Omit<T, "_id">) => T;
/**
* Inserts multiple records into the database
* @param data - Array of data to insert (excluding _id field)
* @returns Array of inserted records with generated _ids
*/
bulkInsert: (data: Omit<T, "_id">[]) => T[];
/**
* Deletes records matching the filter
* @param filter - Partial object to filter records for deletion
* @returns Array of deleted records
*/
delete: (filter: Partial<T>) => T[];
/**
* Updates a single record matching the filter
* @param filter - Partial object to filter record
* @param data - Data to update (excluding _id field)
* @returns Updated record or undefined if not found
*/
updateOne: (
filter: Partial<T>,
data: Omit<Partial<T>, "_id">,
) => T | undefined;
/**
* Updates all records matching the filter
* @param filter - Partial object to filter records
* @param data - Data to update (excluding _id field)
* @returns Array of updated records
*/
updateAll: (filter: Partial<T>, data: Omit<Partial<T>, "_id">) => T[];
}
/**
* A generic class that implements a local storage-based database system.
*
* @template T - The type of the entity being stored
* @template G - The type of the database ID (string for UUID, number for increment)
*
* @example
* ```typescript
* // Create a database for users with UUID as ID
* interface User {
* name: string;
* email: string;
* }
* const userDb = new Database<User, string>('users');
*
* // Create a database with incremental IDs
* interface Product {
* name: string;
* price: number;
* }
* const productDb = new Database<Product, number>('products', IdGeneratorType.Increment);
* ```
*
* @remarks
* - All items in the database will automatically have an `_id` field added
* - Supports two types of ID generation: UUID (default) and Increment
* - Data persists in localStorage under the provided dbKey
*
* @throws {FilterNotFoundError} When operations are attempted with empty filters
* @throws {EmptyRecordError} When operations are attempted with empty data
*/
export class Database<T, G> implements IDatabase<T & DatabaseId<G>> {
private dbKey: string;
private db: string | null;
private initIncrementId: number = 0;
private idGeneratorType: IdGeneratorType = IdGeneratorType.UUID;
constructor(
dbKey: string,
idGeneratorType: IdGeneratorType = IdGeneratorType.UUID,
) {
this.dbKey = dbKey;
this.db = localStorage.getItem(this.dbKey);
this.idGeneratorType = idGeneratorType;
if (!this.db) {
localStorage.setItem(this.dbKey, JSON.stringify([]));
}
}
/**
* Retrieves all items from the database that match the given filter criteria.
*
* @template T - The type of the stored items
* @template G - The type of the database ID
*
* @param {Partial<T & DatabaseId<G>>} [filter] - Optional filter criteria to match against items
* @returns {(T & DatabaseId<G>)[]} An array of items that match the filter criteria. If no filter is provided, returns all items
*
* @example
* ```typescript
* interface User {
* name: string;
* email: string;
* }
* const userRepo = new Database<User, string>("users");
*
* // Get all items
* const users = userRepo.findAll();
*
* // Get items matching specific criteria
* const filteredItems = ruserRepo.findAll({ name: "john Doe" });
* ```
*/
findAll(filter?: Partial<T & DatabaseId<G>>): (T & DatabaseId<G>)[] {
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
if (!filter) {
return items;
}
const filteredItems = items.filter((e) => {
return this.isObjectMatch(filter, e);
});
return filteredItems;
}
/**
* Finds and returns a single item from the database that matches the given filter criteria.
*
* @param filter - A partial object containing the search criteria. Must contain at least one property to match against.
* @returns The first matching item from the database, or undefined if no match is found.
* @throws {FilterNotFoundError} When the filter parameter is empty or undefined.
*
* @example
* ```typescript
* // Assuming a User type with { id: string, name: string, age: number }
* const userRepo = new Database<User, string>("users");
*
* // Find a user by name
* const user = userRepo.findOne({ name: "John Doe" });
*
* // Find a user by multiple criteria
* const specificUser = userRepo.findOne({ name: "John Doe", age: 30 });
* ```
*/
findOne(
filter: Partial<T & DatabaseId<G>>,
): (T & DatabaseId<G>) | undefined {
if (!filter) {
throw new FilterNotFoundError(
"Empty filter not allowed, please provide filter criteria to get the data.",
);
}
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
const item = items.find((e) => {
return this.isObjectMatch(filter, e);
});
return item;
}
/**
* Inserts a new record into the local storage database.
*
* @template T - The type of the data object
* @template G - The type of the ID field
* @param data - The data to insert, excluding the _id field
* @returns The inserted record including the generated _id
* @throws {EmptyRecordError} When no data is provided
*
* @example
* ```typescript
* interface User {
* name: string;
* age: number;
* }
*
* const repository = new Database<User, string>("users");
* const newUser = repository.insertOne({
* name: "John Doe",
* age: 30
* });
* // Result: { name: "John Doe", age: 30, _id: "generated-id" }
* ```
*/
insertOne(data: Omit<T & DatabaseId<G>, "_id">): T & DatabaseId<G> {
if (!data) {
throw new EmptyRecordError(
"Please provide data to insert!",
);
}
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
const newItem = {
...data,
_id: this.generateId(),
} as T & DatabaseId<G>;
items.push(newItem);
this.refreshLocalStorage(items);
return newItem;
}
/**
* Inserts multiple records into the database simultaneously
* @param data - An array of objects to be inserted, excluding the '_id' field
* @returns An array of the inserted records with generated '_id' fields
* @throws {EmptyRecordError} When the input array is empty or null
*
* @example
* ```typescript
* // Insert multiple users
* const newUsers = repository.bulkInsert([
* { name: "John", age: 25 },
* { name: "Jane", age: 30 }
* ]);
* ```
*/
bulkInsert(data: Omit<T & DatabaseId<G>, "_id">[]): (T & DatabaseId<G>)[] {
if (!data || !data.length) {
throw new EmptyRecordError(
"Please provide data to insert!",
);
}
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
const itemsInserted = data.map((item) => {
const newItem = {
...item,
_id: this.generateId(),
} as T & DatabaseId<G>;
items.push(newItem);
return newItem;
});
this.refreshLocalStorage(items);
return itemsInserted;
}
/**
* Deletes items from local storage that match the given filter criteria
*
* @param filter - Partial object containing key-value pairs to match against stored items
* @returns Array of deleted items that matched the filter criteria
* @throws {FilterNotFoundError} When filter parameter is empty/undefined
*
* @example
* ```typescript
* // Delete all items where name is "John"
* const deletedItems = repository.delete({ name: "John" });
*
* // Delete item with specific ID
* const deleted = repository.delete({ _id: "123abc" });
*
* // Delete items matching multiple criteria
* const deletedMultiple = repository.delete({ age: 25, city: "New York" });
* ```
*/
delete(filter: Partial<T & DatabaseId<G>>): (T & DatabaseId<G>)[] {
if (!filter) {
throw new FilterNotFoundError(
"Empty filter not allowed, please provide filter criteria to delete the data.",
);
}
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
const itemsDeleted: (T & DatabaseId<G>)[] = [];
for (const item of items) {
if (this.isObjectMatch(filter, item)) {
const itemIndex = items.findIndex((e) => e._id === item._id);
if (itemIndex !== -1) {
items.splice(itemIndex, 1);
itemsDeleted.push(item);
}
}
}
this.refreshLocalStorage(items);
return itemsDeleted;
}
/**
* Updates a single record in the local storage database that matches the filter criteria.
*
* @param filter - Partial object containing the search criteria to find the record to update
* @param data - Partial object containing the fields to update (excluding _id)
* @returns The updated record if found, undefined otherwise
* @throws {FilterNotFoundError} When filter parameter is empty
* @throws {EmptyRecordError} When data parameter is empty
*
* @example
* ```typescript
* // Assuming a User interface with {name: string, age: number, _id: string}
* const repo = new Database<User, string>("users");
*
* // Update user's age where name is "John"
* const updatedUser = repo.updateOne(
* { name: "John" },
* { age: 31 }
* );
* // Returns updated user object if found, undefined if not found
* ```
*/
updateOne(
filter: Partial<T & DatabaseId<G>>,
data: Omit<Partial<T & DatabaseId<G>>, "_id">,
): (T & DatabaseId<G>) | undefined {
if (!filter) {
throw new FilterNotFoundError(
"Empty filter not allowed, please provide filter criteria to update the data.",
);
}
if (!data) {
throw new EmptyRecordError(
"Please provide data to update!",
);
}
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
const itemIndex = items.findIndex((e) => {
return this.isObjectMatch(filter, e);
});
if (itemIndex === -1) {
return;
}
items[itemIndex] = {
...items[itemIndex],
...(data as T & DatabaseId<G>),
};
this.refreshLocalStorage(items);
return items[itemIndex];
}
/**
* Updates multiple records in the local storage database that match the given filter criteria.
*
* @param filter - Partial object containing the criteria to match records for update
* @param data - Partial object containing the fields to update (excluding _id field)
* @returns Array of updated records that matched the filter criteria
* @throws {FilterNotFoundError} When filter parameter is empty/undefined
* @throws {EmptyRecordError} When data parameter is empty/undefined
*
* @example
* ```typescript
* // Update all users with role 'admin' to have isActive = true
* const repository = new Database<User, string>("users");
* const updatedUsers = repository.updateAll(
* { role: 'admin' },
* { isActive: true }
* );
* // Returns array of updated user records
* ```
*/
updateAll(
filter: Partial<T & DatabaseId<G>>,
data: Omit<Partial<T & DatabaseId<G>>, "_id">,
): (T & DatabaseId<G>)[] {
if (!filter) {
throw new FilterNotFoundError(
"Empty filter not allowed, please provide filter criteria to update the data.",
);
}
if (!data) {
throw new EmptyRecordError(
"Please provide data to update!",
);
}
const items = JSON.parse(this.db as string) as (T & DatabaseId<G>)[];
const itemsUpdated: (T & DatabaseId<G>)[] = [];
for (const item of items) {
if (this.isObjectMatch(filter, item)) {
const itemIndex = items.findIndex((e) => e._id === item._id);
if (itemIndex !== -1) {
items[itemIndex] = {
...items[itemIndex],
...(data as T),
};
itemsUpdated.push(items[itemIndex]);
}
}
}
this.refreshLocalStorage(items);
return itemsUpdated;
}
/**
* Initializes the local storage with the provided data array.
* Each item in the array will be assigned a unique identifier.
*
* @param data - An array of objects without _id property to be stored
* @throws {EmptyRecordError} When the data array is empty or undefined
*
* @example
* ```typescript
* interface User {
* name: string;
* age: number;
* }
*
* const repository = new Database<User, "string">("users");
* repository.seed([
* { name: "John", age: 30 },
* { name: "Jane", age: 25 }
* ]);
* ```
*/
seed(data: Omit<T, "_id">[]) {
if (!data || !data.length) {
throw new EmptyRecordError(
"Please provide data to initialize!",
);
}
const items = data.map((e) => ({
...e,
_id: this.generateId(),
})) as T[];
this.refreshLocalStorage(items);
}
private refreshLocalStorage(data: T[]) {
localStorage.setItem(this.dbKey, JSON.stringify(data));
this.db = localStorage.getItem(this.dbKey);
}
private isObjectMatch(filter: Partial<T>, data: T) {
return Object.keys(filter).every((
fKey,
) => ((filter as Record<string, unknown>)[fKey] ===
(data as Record<string, unknown>)[fKey])
);
}
private incrementId(): void {
this.initIncrementId++;
}
private generateId() {
switch (this.idGeneratorType) {
case IdGeneratorType.Increment:
this.incrementId();
return this.initIncrementId;
case IdGeneratorType.UUID:
return crypto.randomUUID();
default:
return crypto.randomUUID();
}
}
}