@blaze-chat/tanstack-db-idb-collection 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +31 -24
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,31 +3,35 @@ import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
|
3
3
|
import { DeleteMutationFn, InsertMutationFn, SyncConfig, UpdateMutationFn } from "@tanstack/db";
|
|
4
4
|
|
|
5
5
|
//#region src/types.d.ts
|
|
6
|
-
type
|
|
6
|
+
type ValidId = number | string;
|
|
7
7
|
/**
|
|
8
|
-
* Extract Valid
|
|
8
|
+
* Extract Valid ID Type from Entity
|
|
9
9
|
*/
|
|
10
|
-
type ValidIdPath<Entity extends object> = { [K in keyof Entity]: Entity[K] extends
|
|
11
|
-
type ObjectStore<Entity extends object = any> = {
|
|
10
|
+
type ValidIdPath<Entity extends object> = { [K in keyof Entity]: Entity[K] extends ValidId ? K : never }[keyof Entity];
|
|
11
|
+
type ObjectStore<Entity extends object = any, IdPath extends ValidIdPath<Entity> = any> = {
|
|
12
12
|
schema: StandardSchemaV1<Entity>;
|
|
13
|
-
idPath:
|
|
13
|
+
idPath: IdPath;
|
|
14
14
|
indicies?: (keyof Entity & string)[];
|
|
15
15
|
};
|
|
16
|
+
type ObjectStores = Record<string, ObjectStore>;
|
|
16
17
|
/**
|
|
17
|
-
* Extract
|
|
18
|
+
* Extract `Entity` from ObjectStore
|
|
18
19
|
*/
|
|
19
|
-
type ObjectStoreEntity<
|
|
20
|
+
type ObjectStoreEntity<Store extends ObjectStore> = Store extends ObjectStore<infer Entity> ? Entity : never;
|
|
20
21
|
/**
|
|
21
|
-
* Extract
|
|
22
|
+
* Extract `IdPath` from ObjectStore
|
|
22
23
|
*/
|
|
23
|
-
type
|
|
24
|
-
|
|
24
|
+
type ObjectStoreIdPath<Store extends ObjectStore> = Store extends ObjectStore<any, infer IdPath> ? IdPath : never;
|
|
25
|
+
/**
|
|
26
|
+
* Get IdType from ObjectStore
|
|
27
|
+
*/
|
|
28
|
+
type ObjectStoreIdType<Store extends ObjectStore> = Store extends ObjectStore<infer Entity, infer IdPath> ? Entity[IdPath] : never;
|
|
25
29
|
//#endregion
|
|
26
30
|
//#region src/database.d.ts
|
|
27
31
|
/**
|
|
28
32
|
* Helper function for IDE auto completion
|
|
29
33
|
*/
|
|
30
|
-
declare function defineObjectStore<Entity extends object
|
|
34
|
+
declare function defineObjectStore<const Entity extends object, const IdPath extends ValidIdPath<Entity>>(objectStore: ObjectStore<Entity, IdPath>): ObjectStore<Entity, IdPath>;
|
|
31
35
|
type Database<T extends ObjectStores> = {
|
|
32
36
|
/**
|
|
33
37
|
* Note: Safari does not support Top-Level Await
|
|
@@ -38,29 +42,32 @@ type Database<T extends ObjectStores> = {
|
|
|
38
42
|
declare function createDatabase<const T extends ObjectStores>(name: string, version: number, objectStores: T): Database<T>;
|
|
39
43
|
//#endregion
|
|
40
44
|
//#region src/options.d.ts
|
|
41
|
-
declare class IDBCollectionConfig<const
|
|
45
|
+
declare class IDBCollectionConfig<const Store extends ObjectStore, const Entity extends ObjectStoreEntity<Store>, const IdType extends ObjectStoreIdType<Store>> {
|
|
42
46
|
id: string;
|
|
43
47
|
schema: StandardSchemaV1<Entity>;
|
|
44
|
-
getKey: (entity: Entity) => IdType
|
|
45
|
-
sync: SyncConfig<Entity>;
|
|
46
|
-
onInsert: InsertMutationFn<Entity, IdType
|
|
47
|
-
onUpdate: UpdateMutationFn<Entity, IdType
|
|
48
|
-
onDelete: DeleteMutationFn<Entity, IdType
|
|
48
|
+
getKey: (entity: Entity) => IdType;
|
|
49
|
+
sync: SyncConfig<Entity, IdType>;
|
|
50
|
+
onInsert: InsertMutationFn<Entity, IdType>;
|
|
51
|
+
onUpdate: UpdateMutationFn<Entity, IdType>;
|
|
52
|
+
onDelete: DeleteMutationFn<Entity, IdType>;
|
|
49
53
|
private begin;
|
|
50
54
|
private write;
|
|
51
55
|
private commit;
|
|
52
|
-
constructor({
|
|
56
|
+
constructor(idb: Promise<IDBPDatabase>, name: string, {
|
|
57
|
+
idPath,
|
|
58
|
+
schema
|
|
59
|
+
}: Store);
|
|
60
|
+
}
|
|
61
|
+
declare function idbCollectionOptions<const T extends ObjectStores, const Name extends keyof T & string>({
|
|
62
|
+
database: {
|
|
53
63
|
idb,
|
|
54
64
|
objectStores
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
declare function idbCollectionOptions<const T extends ObjectStores, Name extends keyof T & string>({
|
|
58
|
-
database,
|
|
65
|
+
},
|
|
59
66
|
name
|
|
60
67
|
}: {
|
|
61
68
|
database: Database<T>;
|
|
62
69
|
name: Name;
|
|
63
|
-
}): IDBCollectionConfig<T
|
|
70
|
+
}): IDBCollectionConfig<NonNullable<T[Name]>, ObjectStoreEntity<NonNullable<T[Name]>>, ObjectStoreIdType<NonNullable<T[Name]>>>;
|
|
64
71
|
//#endregion
|
|
65
|
-
export { Database,
|
|
72
|
+
export { Database, IDBCollectionConfig, ObjectStore, ObjectStoreEntity, ObjectStoreIdPath, ObjectStoreIdType, ObjectStores, ValidId, ValidIdPath, createDatabase, defineObjectStore, idbCollectionOptions };
|
|
66
73
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/database.ts","../src/options.ts"],"mappings":";;;;;KAEY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/database.ts","../src/options.ts"],"mappings":";;;;;KAEY,OAAA;AAAA;AAKZ;;AALY,KAKA,WAAA,wCACE,MAAA,GAAS,MAAA,CAAO,CAAA,UAAW,OAAA,GAAU,CAAA,iBAC3C,MAAA;AAAA,KAEI,WAAA,6CAAwD,WAAA,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YAAA,GAAA,EAMlB,OAAA,CAAQ,YAAA,GAAA,IAAA;IAAA,MAAA;IAAA;EAAA,GAAiD,KAAA;AAAA;AAAA,iBA6E5D,oBAAA,iBACE,YAAA,2BACS,CAAA,UAAA,CAAA;EAAA,QAAA;IAAA,GAAA;IAAA;EAAA;EAAA;AAAA;EAAA,QAAA,EAC8B,QAAA,CAAS,CAAA;EAAA,IAAA,EAAU,IAAA;AAAA,IAAM,mBAAA,CAAA,WAAA,CAAA,CAAA,CAAA,IAAA,IAAA,iBAAA,CAAA,WAAA,CAAA,CAAA,CAAA,IAAA,KAAA,iBAAA,CAAA,WAAA,CAAA,CAAA,CAAA,IAAA"}
|
package/dist/index.js
CHANGED
|
@@ -32,8 +32,7 @@ var IDBCollectionConfig = class {
|
|
|
32
32
|
begin;
|
|
33
33
|
write;
|
|
34
34
|
commit;
|
|
35
|
-
constructor({
|
|
36
|
-
const { schema, idPath } = objectStores[name];
|
|
35
|
+
constructor(idb, name, { idPath, schema }) {
|
|
37
36
|
this.id = name;
|
|
38
37
|
this.schema = schema;
|
|
39
38
|
this.getKey = (entity) => entity[idPath];
|
|
@@ -92,10 +91,10 @@ var IDBCollectionConfig = class {
|
|
|
92
91
|
};
|
|
93
92
|
}
|
|
94
93
|
};
|
|
95
|
-
function idbCollectionOptions({ database, name }) {
|
|
96
|
-
return new IDBCollectionConfig(
|
|
94
|
+
function idbCollectionOptions({ database: { idb, objectStores }, name }) {
|
|
95
|
+
return new IDBCollectionConfig(idb, name, objectStores[name]);
|
|
97
96
|
}
|
|
98
97
|
|
|
99
98
|
//#endregion
|
|
100
|
-
export { createDatabase, defineObjectStore, idbCollectionOptions };
|
|
99
|
+
export { IDBCollectionConfig, createDatabase, defineObjectStore, idbCollectionOptions };
|
|
101
100
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/database.ts","../src/options.ts"],"sourcesContent":["import { openDB, type IDBPDatabase } from \"idb\";\nimport type { ObjectStore, ObjectStores } from \"./types\";\n\n/**\n * Helper function for IDE auto completion\n */\nexport function defineObjectStore
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/database.ts","../src/options.ts"],"sourcesContent":["import { openDB, type IDBPDatabase } from \"idb\";\nimport type { ObjectStore, ObjectStores, ValidIdPath } from \"./types\";\n\n/**\n * Helper function for IDE auto completion\n */\nexport function defineObjectStore<\n const Entity extends object,\n const IdPath extends ValidIdPath<Entity>,\n>(objectStore: ObjectStore<Entity, IdPath>): ObjectStore<Entity, IdPath> {\n return objectStore;\n}\n\nexport type Database<T extends ObjectStores> = {\n /**\n * Note: Safari does not support Top-Level Await\n */\n idb: Promise<IDBPDatabase>;\n objectStores: T;\n};\n\nexport function createDatabase<const T extends ObjectStores>(\n name: string,\n version: number,\n objectStores: T,\n): Database<T> {\n const idb = openDB(name, version, {\n async upgrade(database) {\n for (const [key, { idPath: keyPath, indicies }] of Object.entries(objectStores)) {\n const objectStore = database.createObjectStore(key, { keyPath });\n\n if (indicies) {\n // oxlint-disable-next-line no-await-in-loop\n await Promise.all(indicies.map((index) => objectStore.createIndex(name, index)));\n }\n }\n },\n });\n\n return { idb, objectStores };\n}\n","import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type {\n ChangeMessageOrDeleteKeyMessage,\n DeleteMutationFn,\n InsertMutationFn,\n SyncConfig,\n UpdateMutationFn,\n} from \"@tanstack/db\";\nimport type { IDBPDatabase } from \"idb\";\nimport type { Database } from \"./database\";\nimport type { ObjectStore, ObjectStoreEntity, ObjectStoreIdType, ObjectStores } from \"./types\";\n\nexport class IDBCollectionConfig<\n const Store extends ObjectStore,\n const Entity extends ObjectStoreEntity<Store>,\n const IdType extends ObjectStoreIdType<Store>,\n> {\n id: string;\n schema: StandardSchemaV1<Entity>;\n getKey: (entity: Entity) => IdType;\n sync: SyncConfig<Entity, IdType>;\n\n onInsert: InsertMutationFn<Entity, IdType>;\n onUpdate: UpdateMutationFn<Entity, IdType>;\n onDelete: DeleteMutationFn<Entity, IdType>;\n\n private begin!: () => void;\n private write!: (message: ChangeMessageOrDeleteKeyMessage<Entity, IdType>) => void;\n private commit!: () => void;\n\n constructor(idb: Promise<IDBPDatabase>, name: string, { idPath, schema }: Store) {\n this.id = name;\n this.schema = schema;\n this.getKey = (entity) => entity[idPath];\n this.sync = {\n sync: ({ begin, write, commit, markReady }) => {\n this.begin = begin;\n this.write = write;\n this.commit = commit;\n\n // oxlint-disable-next-line typescript/no-floating-promises\n (async () => {\n try {\n begin();\n\n for (const entity of await (await idb).getAll(name)) {\n write({ type: \"insert\", value: entity });\n }\n\n commit();\n } finally {\n markReady();\n }\n })();\n },\n };\n\n this.onInsert = async ({ transaction }) => {\n const entities = transaction.mutations.map((m) => m.modified);\n\n const tx = (await idb).transaction(name, \"readwrite\");\n\n await Promise.all(entities.map((entity) => tx.store.add(entity)));\n\n await tx.done;\n\n this.begin();\n for (const entity of entities) {\n this.write({ type: \"insert\", value: entity });\n }\n this.commit();\n };\n\n this.onUpdate = async ({ transaction }) => {\n const entities = transaction.mutations.map((m) => m.modified);\n\n const tx = (await idb).transaction(name, \"readwrite\");\n\n await Promise.all(entities.map((entity) => tx.store.put(entity)));\n\n await tx.done;\n\n this.begin();\n for (const entity of entities) {\n this.write({ type: \"update\", value: entity });\n }\n this.commit();\n };\n\n this.onDelete = async ({ transaction }) => {\n const keys = transaction.mutations.map((m) => m.key);\n\n const tx = (await idb).transaction(name, \"readwrite\");\n\n await Promise.all(keys.map((key) => tx.store.delete(key)));\n\n await tx.done;\n\n this.begin();\n for (const key of keys) {\n this.write({ type: \"delete\", key });\n }\n this.commit();\n };\n }\n}\n\nexport function idbCollectionOptions<\n const T extends ObjectStores,\n const Name extends keyof T & string,\n>({ database: { idb, objectStores }, name }: { database: Database<T>; name: Name }) {\n return new IDBCollectionConfig(idb, name, objectStores[name]!);\n}\n"],"mappings":";;;;;;AAMA,SAAgB,kBAG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