-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
orm.ts
974 lines (850 loc) · 29 KB
/
orm.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
type AutoIncrementDefault = { type: "autoincrement" };
type NowDefault = { type: "now" };
type StaticDefault<T> = { type: "static"; value: T };
type FunctionDefault<T> = { type: "function"; fn: () => T };
type FieldType = "string" | "number" | "boolean" | "date";
type FieldDefinition<T extends FieldType> = {
type: T;
primaryKey?: boolean;
unique?: boolean;
default?: DefaultValueForType<T>;
};
const fieldSymbol = Symbol("__isField");
type FieldDefinitionWithMeta<
T extends FieldType,
HasDefault extends boolean,
IsPrimaryKey extends boolean
> = FieldDefinition<T> & {
[fieldSymbol]: true;
hasDefault: HasDefault;
isPrimaryKey: IsPrimaryKey;
};
type ObjectStoreSchema = {
fields: {
[fieldName: string]: FieldDefinitionWithMeta<FieldType, boolean, boolean>;
};
};
export type Schema = {
[storeName: string]: ObjectStoreSchema;
};
type InferFieldType<T extends FieldType> = T extends "string"
? string
: T extends "number"
? number
: T extends "boolean"
? boolean
: T extends "date"
? Date
: never;
type DefaultValueForType<T extends FieldType> = T extends "number"
? AutoIncrementDefault | StaticDefault<number> | FunctionDefault<number>
: T extends "date"
? NowDefault | StaticDefault<Date> | FunctionDefault<Date>
: T extends "string"
? StaticDefault<string> | FunctionDefault<string>
: T extends "boolean"
? StaticDefault<boolean> | FunctionDefault<boolean>
: never;
type InferModelShape<S extends ObjectStoreSchema> = {
[K in keyof S["fields"]]: InferFieldType<S["fields"][K]["type"]>;
};
type PrimaryKey<T extends ObjectStoreSchema> = {
[K in keyof T["fields"] as T["fields"][K]["isPrimaryKey"] extends true
? K
: never]: InferFieldType<T["fields"][K]["type"]>;
};
type RequiredFields<T extends ObjectStoreSchema> = {
[K in keyof T["fields"] as T["fields"][K]["hasDefault"] extends false
? K
: never]: InferFieldType<T["fields"][K]["type"]>;
};
type OptionalFields<T extends ObjectStoreSchema> = {
[K in keyof T["fields"] as T["fields"][K]["hasDefault"] extends true
? K
: never]?: InferFieldType<T["fields"][K]["type"]>;
};
type CreateInput<T extends ObjectStoreSchema> = RequiredFields<T> &
OptionalFields<T>;
type UpdateInput<T extends ObjectStoreSchema> = Partial<
Omit<InferModelShape<T>, keyof PrimaryKey<T>>
>;
type IdType<T extends ObjectStoreSchema> = PrimaryKey<T>[keyof PrimaryKey<T>];
export type ModelMethods<T extends ObjectStoreSchema> = {
create: (data: CreateInput<T>) => Promise<InferModelShape<T>>;
findAll: () => Promise<InferModelShape<T>[]>;
findById: (id: IdType<T>) => Promise<InferModelShape<T> | undefined>;
update: (id: IdType<T>, data: UpdateInput<T>) => Promise<InferModelShape<T>>;
delete: (id: IdType<T>) => Promise<void>;
};
type Versioning = { type: "auto" } | { type: "manual"; version: number };
type WhereOperator = "equals" | "startsWith" | "endsWith";
type OrderByOperator = "asc" | "desc";
class QueryBuilder<T> {
private filters: { field: keyof T; operator: string; value: any }[] = [];
private sortField?: keyof T;
private sortOrder: OrderByOperator = "asc";
private limitCount?: number;
private offsetCount?: number;
constructor(private db: IDBDatabase, private storeName: string) {}
// Add a filter condition (where clause)
where(field: keyof T, operator: WhereOperator, value: any) {
this.filters.push({ field, operator, value });
return this;
}
// Sort the results by a field
orderBy(field: keyof T, order: OrderByOperator = "asc") {
this.sortField = field;
this.sortOrder = order;
return this;
}
// Limit the number of results
limit(count: number) {
this.limitCount = count;
return this;
}
// Skip a number of results (pagination)
offset(count: number) {
this.offsetCount = count;
return this;
}
async run(): Promise<T[]> {
return new Promise<T[]>((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], "readonly");
const store = transaction.objectStore(this.storeName);
const request = store.openCursor();
const results: T[] = [];
let skipped = 0;
request.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
if (cursor) {
const record = cursor.value;
if (this.applyFilters(record)) {
// Pagination (skip and limit)
if (this.offsetCount && skipped < this.offsetCount) {
skipped++;
} else {
results.push(record);
if (this.limitCount && results.length >= this.limitCount) {
resolve(this.applySorting(results));
return;
}
}
}
cursor.continue();
} else {
resolve(this.applySorting(results));
}
};
request.onerror = () => reject(request.error);
});
}
private applyFilters(record: T): boolean {
return this.filters.every(({ field, operator, value }) => {
const fieldValue = record[field];
switch (operator) {
case "equals":
return fieldValue === value;
case "startsWith":
return typeof fieldValue === "string" && fieldValue.startsWith(value);
case "endsWith":
return typeof fieldValue === "string" && fieldValue.endsWith(value);
default:
return true;
}
});
}
private applySorting(results: T[]): T[] {
if (!this.sortField) {
return results; // No sorting needed
}
return results.sort((a, b) => {
const aValue = a[this.sortField!];
const bValue = b[this.sortField!];
if (aValue === bValue) return 0;
if (this.sortOrder === "asc") {
return aValue > bValue ? 1 : -1;
}
return aValue < bValue ? 1 : -1;
});
}
}
export type BackupData<S extends Schema> = {
[K in keyof S]?: S[K]["fields"][];
};
export type EventCallback<S extends Schema> = (
storeName: keyof S,
record: any
) => void;
export type InitOptions<S extends Schema> = {
schema: S;
dbName?: string;
versioning?: Versioning;
migrations?: (
oldVersion: number,
newVersion: number,
db: IDBDatabase
) => void;
debug?: boolean;
};
export class ORM<S extends Schema> {
private db!: IDBDatabase;
private schema: S;
private dbName: string;
private versioning: InitOptions<S>["versioning"];
private migrations?: InitOptions<S>["migrations"];
private debug: (...args: any[]) => void;
private currentVersion = 0;
private eventHandlers: {
[K in "create" | "update" | "delete"]: EventCallback<S>[];
} = {
create: [],
update: [],
delete: [],
};
#schemaLength = 0;
private constructor(options: InitOptions<S>) {
this.schema = options.schema;
this.dbName = options.dbName || "defaultDB";
this.versioning = options.versioning || { type: "auto" };
this.migrations = options.migrations;
// Custom loggers later on??
this.debug = options.debug ? $debug : () => {};
}
/**
* Initializes a new `ORM` instance.
*
// * @param {InitOptions<S>} options - Configuration options for initializing the ORM, including the schema, database name, versioning strategy, migrations, and debug flag.
* @param {S} options.schema - The schema of the database.
* @param {string} [options.dbName] - The name of the database.
* @param {Versioning} [options.versioning] - The versioning strategy for the database.
* @param {(oldVersion: number, newVersion: number, db: IDBDatabase) => void} [options.migrations] - The callback for the manual version migration.
* @param {boolean} [options.debug] - Wether to enable debug logs
* @returns {Promise<ORM<S>>} A promise that resolves to the initialized ORM instance.
*/
static async init<S extends Schema>(
options: InitOptions<S>
): Promise<ORM<S>> {
const instance = new ORM({ debug: true, ...options });
await instance.initializeDatabase();
return instance;
}
async migrate(targetVersion: number) {
if (this.isAutoVersioning()) {
throw new Error("Cannot manually migrate when using auto versioning");
}
await this.migrateDatabase(targetVersion);
}
async transaction(
callback: (trx: { [K in keyof S]: ModelMethods<S[K]> }) => Promise<void>
): Promise<void> {
const storeNames = Object.keys(this.schema);
const transaction = this.db.transaction(storeNames, "readwrite");
const trx = storeNames.reduce((acc, storeName) => {
acc[storeName as keyof S] = this.createModelMethods(
storeName as keyof S,
transaction
);
return acc;
}, {} as { [K in keyof S]: ModelMethods<S[K]> });
return new Promise((resolve, reject) => {
transaction.oncomplete = () => {
this.debug("Transaction committed successfully");
resolve();
};
transaction.onerror = () => {
this.debug("Transaction failed, rolling back");
reject(transaction.error);
};
// Execute the user's callback and pass in the trx object
callback(trx).catch((err) => {
this.debug("Error in transaction callback:", err);
transaction.abort();
reject(err);
});
});
}
query<K extends keyof S>(storeName: K) {
return new QueryBuilder<S[K]["fields"]>(this.db, storeName as string);
}
/**
* Seed the database with mock data. This is useful for debugging purposes or creating
* placeholder records.
*
* @param data
*/
async seed(data: { [K in keyof S]?: InferModelShape<S[K]>[] }) {
const transaction = this.db.transaction(
Object.keys(this.schema),
"readwrite"
);
return new Promise<void>((resolve, reject) => {
for (const storeName of Object.keys(data)) {
const records = data[storeName as keyof Schema]!;
const store = transaction.objectStore(storeName);
for (const record of records) {
const request = store.add(record);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
this.events.trigger("create", storeName, record);
};
}
}
transaction.oncomplete = () => {
this.debug("Seed data applied successfully");
resolve();
};
transaction.onerror = (event) => {
this.debug("Transaction failed during seeding", event);
reject(transaction.error);
};
});
}
/**
* Returns metadata about your database, including size, records and indexes.
*/
async meta(): Promise<{
version: number;
stores: {
[K in keyof S]: {
recordCount: number;
size: string;
indexes: string[];
keyRange: { lower: any; upper: any } | null;
lastUpdated: Date | null;
};
};
}> {
const transaction = this.db.transaction(
Object.keys(this.schema),
"readonly"
);
const result: {
version: number;
stores: {
[K in keyof S]: {
recordCount: number;
// In bytes
size: string;
indexes: string[];
keyRange: { lower: any; upper: any } | null;
lastUpdated: Date | null;
};
};
} = {
version: this.db.version,
stores: {} as any,
};
const storePromises = Object.keys(this.schema).map((storeName) => {
return new Promise<void>((resolve, reject) => {
const store = transaction.objectStore(storeName);
const countRequest = store.count();
const indexNames = Array.from(store.indexNames);
let size = 0;
let lower: any = null;
let upper: any = null;
let lastUpdated: Date | null = null;
countRequest.onsuccess = () => {
const recordCount = countRequest.result;
const cursorRequest = store.openCursor();
cursorRequest.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>)
.result;
if (cursor) {
const storedObject = cursor.value;
size += this.estimateSize(storedObject);
if (lower === null || cursor.key < lower) lower = cursor.key;
if (upper === null || cursor.key > upper) upper = cursor.key;
// Check for lastUpdated field, if it exists
if (
"lastUpdated" in storedObject &&
storedObject.lastUpdated instanceof Date
) {
if (!lastUpdated || storedObject.lastUpdated > lastUpdated) {
lastUpdated = storedObject.lastUpdated;
}
}
cursor.continue();
} else {
result.stores[storeName as keyof S] = {
recordCount,
size: formatBytes(size),
indexes: indexNames,
keyRange:
lower !== null && upper !== null ? { lower, upper } : null,
lastUpdated,
};
resolve();
}
};
cursorRequest.onerror = () => reject(cursorRequest.error);
};
countRequest.onerror = () => reject(countRequest.error);
});
});
await Promise.all(storePromises);
return result;
}
raw(): IDBDatabase {
return this.db;
}
get events() {
const self = this;
return {
on: (
eventName: "create" | "update" | "delete",
callback: EventCallback<S>
) => {
if (!self.eventHandlers[eventName]) {
self.eventHandlers[eventName] = [];
}
const index = self.eventHandlers[eventName].length;
self.eventHandlers[eventName].push(callback);
return () => {
self.eventHandlers[eventName].splice(index, 1);
};
},
trigger: (
eventName: "create" | "update" | "delete",
storeName: keyof Schema,
record: any
) => {
if (self.eventHandlers[eventName]) {
for (const callback of self.eventHandlers[eventName]) {
callback(storeName, record);
}
}
},
off(
eventName: "create" | "update" | "delete",
callback: EventCallback<S>
): void {
if (!self.eventHandlers[eventName]) return;
self.eventHandlers[eventName] = self.eventHandlers[eventName].filter(
(handler) => handler !== callback
);
},
once: (
eventName: "create" | "update" | "delete",
callback: EventCallback<S>
) => {
const wrappedCallback = (storeName: keyof S, record: any) => {
callback(storeName, record);
self.events.off(eventName, wrappedCallback);
};
self.events.on(eventName, wrappedCallback);
},
};
}
get models(): { [K in keyof S]: ModelMethods<S[K]> } {
return new Proxy({} as { [K in keyof S]: ModelMethods<S[K]> }, {
get: (_, prop: string) => {
if (prop in this.schema) {
return this.createModelMethods(prop as keyof S);
}
throw new Error(`Model '${prop}' not found in schema`);
},
});
}
private estimateSize(obj: any): number {
const jsonString = JSON.stringify(obj);
return new Blob([jsonString]).size;
}
// Initialize IndexedDB and handle migrations
private async initializeDatabase() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName);
// Handle the `onupgradeneeded` event (for migrations and schema updates)
request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
const oldVersion = event.oldVersion;
const newVersion = event.newVersion;
this.currentVersion = oldVersion;
// Remove this debug line!!!
this.debug(
`Database upgrade from version ${oldVersion} to ${newVersion}`
);
// Automatically create/upgrade stores based on schema
this.applySchema(db);
};
request.onsuccess = () => {
this.db = request.result;
this.currentVersion = this.db.version;
this.debug(`Database "${this.dbName}" opened successfully`);
this.checkAndApplyMigrations().then(() => resolve(this));
};
request.onerror = () => reject(request.error);
});
}
private async checkAndApplyMigrations() {
const targetVersion = this.getTargetVersion();
if (targetVersion > this.currentVersion) {
await this.migrateDatabase(targetVersion);
}
}
private getTargetVersion(): number {
if (this.versioning && this.versioning.type === "auto") {
return this.calculateSchemaVersion();
}
return this.versioning?.version || 0;
}
private calculateSchemaVersion(): number {
let currentSchemaLength = 0;
for (const storeName in this.schema) {
currentSchemaLength += Object.keys(this.schema[storeName].fields).length;
}
const previousSchemaLength = this.#schemaLength;
const currentVersion = this.currentVersion;
this.#schemaLength = currentSchemaLength;
if (previousSchemaLength !== currentSchemaLength) {
this.debug("Detected schema change, applying schema...");
this.currentVersion = currentVersion + 1;
return this.currentVersion;
}
this.debug("No schema change detected");
return currentVersion;
}
private isAutoVersioning(): boolean {
return (this.versioning && this.versioning.type === "auto") ?? false;
}
private async migrateDatabase(targetVersion: number) {
return new Promise<void>((resolve, reject) => {
const request = indexedDB.open(this.dbName, targetVersion);
request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
const oldVersion = event.oldVersion;
const newVersion = event.newVersion!;
this.debug(
`Migrating database from version ${oldVersion} to ${newVersion}`
);
this.applySchema(db);
if (this.migrations) {
this.migrations(oldVersion, newVersion, db);
}
};
request.onsuccess = () => {
this.db = request.result;
this.currentVersion = this.db.version;
this.debug(`Database migrated to version ${this.currentVersion}`);
resolve();
};
request.onerror = () => reject(request.error);
});
}
// Apply schema changes to the IndexedDB
private applySchema(db: IDBDatabase) {
for (const storeName of Object.keys(this.schema)) {
const storeSchema = this.schema[storeName];
if (!db.objectStoreNames.contains(storeName)) {
let primaryKeyField: string | undefined;
for (const fieldName of Object.keys(storeSchema.fields)) {
if (storeSchema.fields[fieldName].primaryKey) {
primaryKeyField = fieldName;
break;
}
}
if (!primaryKeyField) {
throw new Error(
`No primary key defined for object store "${storeName}"`
);
}
// Create object store with primary key
const store = db.createObjectStore(storeName, {
keyPath: primaryKeyField,
autoIncrement:
storeSchema.fields[primaryKeyField].default?.type ===
"autoincrement" || false,
});
// Create indexes if defined
for (const fieldName of Object.keys(storeSchema.fields)) {
const fieldSchema = storeSchema.fields[fieldName];
if (fieldSchema.unique) {
store.createIndex(fieldName, fieldName, { unique: true });
}
}
}
}
this.debug("Schema applied");
}
private createModelMethods<K extends keyof S>(
storeName: K,
__transaction: IDBTransaction = undefined!
): ModelMethods<S[K]> {
return {
create: async (
data: CreateInput<S[K]>
): Promise<InferModelShape<S[K]>> => {
return new Promise((resolve, reject) => {
let transaction: IDBTransaction;
if (__transaction) {
transaction = __transaction;
} else {
transaction = this.db.transaction(
[storeName as string],
"readwrite"
);
}
const store = transaction.objectStore(storeName as string);
const fullData = this.applyDefaults(storeName, data);
const primaryKeyField = Object.keys(
this.schema[storeName].fields
).find(
(fieldName) => this.schema[storeName].fields[fieldName].primaryKey
);
if (!primaryKeyField) {
throw new Error(
`No primary key defined for object store "${storeName as string}"`
);
}
const request = store.add(fullData);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
const id = request.result; // In case of autoincrement, this is the generated ID
const resultData = { ...fullData, [primaryKeyField]: id };
this.events.trigger("create", storeName as string, resultData);
resolve(resultData as InferModelShape<S[K]>);
};
});
},
findAll: async (): Promise<InferModelShape<S[K]>[]> => {
return new Promise((resolve, reject) => {
let transaction: IDBTransaction;
if (__transaction) {
transaction = __transaction;
} else {
transaction = this.db.transaction(
[storeName as string],
"readonly"
);
}
const store = transaction.objectStore(storeName as string);
const request = store.getAll();
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
});
},
findById: async (
id: IdType<S[K]>
): Promise<InferModelShape<S[K]> | undefined> => {
return new Promise((resolve, reject) => {
let transaction: IDBTransaction;
if (__transaction) {
transaction = __transaction;
} else {
transaction = this.db.transaction(
[storeName as string],
"readonly"
);
}
const store = transaction.objectStore(storeName as string);
if (typeof id === "boolean") {
reject(
new Error("Invalid ID type: boolean is not a valid IDBValidKey")
);
return;
}
const request = store.get(id as IDBValidKey);
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result || undefined);
});
},
update: async (
id: IdType<S[K]>,
data: UpdateInput<S[K]>
): Promise<InferModelShape<S[K]>> => {
return new Promise((resolve, reject) => {
let transaction: IDBTransaction;
if (__transaction) {
transaction = __transaction;
} else {
transaction = this.db.transaction(
[storeName as string],
"readwrite"
);
}
const store = transaction.objectStore(storeName as string);
if (typeof id === "boolean") {
reject(
new Error("Invalid ID type: boolean is not a valid IDBValidKey")
);
return;
}
const getRequest = store.get(id as IDBValidKey);
getRequest.onerror = () => reject(getRequest.error);
getRequest.onsuccess = () => {
if (!getRequest.result) {
reject(new Error(`Record with id ${id} not found`));
return;
}
const updatedData = { ...getRequest.result, ...data };
const putRequest = store.put(updatedData);
putRequest.onerror = () => reject(putRequest.error);
putRequest.onsuccess = () => {
const resultData = { ...updatedData };
this.events.trigger("update", storeName as string, resultData);
resolve(resultData as InferModelShape<S[K]>);
};
};
});
},
delete: async (id: IdType<S[K]>): Promise<void> => {
return new Promise((resolve, reject) => {
let transaction: IDBTransaction;
if (__transaction) {
transaction = __transaction;
} else {
transaction = this.db.transaction(
[storeName as string],
"readwrite"
);
}
const store = transaction.objectStore(storeName as string);
if (typeof id === "boolean") {
reject(
new Error("Invalid ID type: boolean is not a valid IDBValidKey")
);
return;
}
const request = store.delete(id as IDBValidKey);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
this.events.trigger("delete", storeName as string, id);
resolve();
};
});
},
};
}
private applyDefaults<K extends keyof S>(
storeName: K,
data: CreateInput<S[K]>
): InferModelShape<S[K]> {
const storeSchema = this.schema[storeName];
const result = { ...data } as unknown as InferModelShape<S[K]>;
for (const [fieldName, fieldSchema] of Object.entries(storeSchema.fields)) {
if (fieldName in data) continue;
if (fieldSchema.default) {
const key = fieldName as keyof InferModelShape<S[K]>;
switch (fieldSchema.default.type) {
case "now":
result[key] = new Date() as InferModelShape<S[K]>[typeof key];
break;
case "static":
result[key] = fieldSchema.default.value as InferModelShape<
S[K]
>[typeof key];
break;
case "function":
result[key] = fieldSchema.default.fn() as InferModelShape<
S[K]
>[typeof key];
break;
// 'autoincrement' is handled by IndexedDB, so we don't need to do anything here
}
}
}
return result;
}
}
/**
* Defines a field with metadata based on the provided field definition.
* @param definition The field definition including type, primary key, unique, and default value.
* @returns The field definition with metadata inserted.
* @throws Error if the primary key type is not "number", "string", or "date", or if an invalid default type is provided.
*/
export function field<
T extends FieldType,
U extends DefaultValueForType<T> | undefined = undefined,
V extends boolean | undefined = undefined
>(
definition: FieldDefinition<T> &
(U extends undefined ? unknown : { default: U }) &
(V extends undefined ? unknown : { primaryKey: V })
): FieldDefinitionWithMeta<
T,
U extends undefined ? false : true,
V extends undefined ? false : true
> {
if (
definition.primaryKey &&
!["number", "string", "date"].includes(definition.type)
) {
throw new Error(
`Primary key must be of type "number", "string" or "date", got "${definition.type}"`
);
}
// Runtime checks for default values
if (definition.default) {
switch (definition.type) {
case "number":
if (
!["autoincrement", "static", "function"].includes(
definition.default.type
)
) {
throw new Error(
`Invalid default type for number field: ${definition.default.type}`
);
}
break;
case "date":
if (!["now", "static", "function"].includes(definition.default.type)) {
throw new Error(
`Invalid default type for date field: ${definition.default.type}`
);
}
break;
case "string":
case "boolean":
if (!["static", "function"].includes(definition.default.type)) {
throw new Error(
`Invalid default type for ${definition.type} field: ${definition.default.type}`
);
}
break;
}
}
const hasDefault = !!definition.default;
const isPrimaryKey = !!definition.primaryKey;
return Object.assign(definition, {
[fieldSymbol]: true as const,
hasDefault,
isPrimaryKey,
}) as unknown as FieldDefinitionWithMeta<
T,
U extends undefined ? false : true,
V extends undefined ? false : true
>;
}
/**
* Utility to define a valid schema for the database.
*
* @param schema The schema to be validated and converted into a valid schema.
* @returns The validated schema.
*/
export function defineSchema<T extends Schema>(schema: T): T {
for (const [storeName, store] of Object.entries(schema)) {
for (const [fieldName, fieldDef] of Object.entries(store.fields)) {
if (!(fieldSymbol in fieldDef) || fieldDef[fieldSymbol] !== true) {
throw new Error(
`Field "${fieldName}" in store "${storeName}" must be created using the \`field\` utility function.`
);
}
}
}
return schema;
}
const $debug = (...args: any[]) => {
if (typeof self !== "undefined" || typeof window === "undefined") {
console.log(...args);
}
};
const formatBytes = (bytes: number) => {
const units = ["B", "KB", "MB", "GB", "TB"];
let l = 0;
let _bytes = bytes;
while (_bytes >= 1024 && l < units.length - 1) {
_bytes /= 1024;
l++;
}
return `${_bytes.toFixed(2)} ${units[l]}`;
};