@blaze-chat/tanstack-db-idb-collection 0.0.3 → 0.0.4

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.js CHANGED
@@ -12,7 +12,7 @@ function createDatabase(name, version, objectStores) {
12
12
  idb: openDB(name, version, { async upgrade(database) {
13
13
  for (const [key, { idPath: keyPath, indicies }] of Object.entries(objectStores)) {
14
14
  const objectStore = database.createObjectStore(key, { keyPath });
15
- if (indicies) await Promise.all(indicies.map((index) => objectStore.createIndex(name, index)));
15
+ if (indicies) await Promise.all(indicies.map((index) => objectStore.createIndex(index, index)));
16
16
  }
17
17
  } }),
18
18
  objectStores
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, 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,kBAGd,aAAuE;AACvE,QAAO;;AAWT,SAAgB,eACd,MACA,SACA,cACa;AAcb,QAAO;EAAE,KAbG,OAAO,MAAM,SAAS,EAChC,MAAM,QAAQ,UAAU;AACtB,QAAK,MAAM,CAAC,KAAK,EAAE,QAAQ,SAAS,eAAe,OAAO,QAAQ,aAAa,EAAE;IAC/E,MAAM,cAAc,SAAS,kBAAkB,KAAK,EAAE,SAAS,CAAC;AAEhE,QAAI,SAEF,OAAM,QAAQ,IAAI,SAAS,KAAK,UAAU,YAAY,YAAY,MAAM,MAAM,CAAC,CAAC;;KAIvF,CAAC;EAEY;EAAc;;;;;AC3B9B,IAAa,sBAAb,MAIE;CACA;CACA;CACA;CACA;CAEA;CACA;CACA;CAEA,AAAQ;CACR,AAAQ;CACR,AAAQ;CAER,YAAY,KAA4B,MAAc,EAAE,QAAQ,UAAiB;AAC/E,OAAK,KAAK;AACV,OAAK,SAAS;AACd,OAAK,UAAU,WAAW,OAAO;AACjC,OAAK,OAAO,EACV,OAAO,EAAE,OAAO,OAAO,QAAQ,gBAAgB;AAC7C,QAAK,QAAQ;AACb,QAAK,QAAQ;AACb,QAAK,SAAS;AAGd,IAAC,YAAY;AACX,QAAI;AACF,YAAO;AAEP,UAAK,MAAM,UAAU,OAAO,MAAM,KAAK,OAAO,KAAK,CACjD,OAAM;MAAE,MAAM;MAAU,OAAO;MAAQ,CAAC;AAG1C,aAAQ;cACA;AACR,gB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":["../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(index, 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,kBAGd,aAAuE;AACvE,QAAO;;AAWT,SAAgB,eACd,MACA,SACA,cACa;AAcb,QAAO;EAAE,KAbG,OAAO,MAAM,SAAS,EAChC,MAAM,QAAQ,UAAU;AACtB,QAAK,MAAM,CA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KAAK,YAAY,MAAM,YAAY;AAErD,SAAM,QAAQ,IAAI,SAAS,KAAK,WAAW,GAAG,MAAM,IAAI,OAAO,CAAC,CAAC;AAEjE,SAAM,GAAG;AAET,QAAK,OAAO;AACZ,QAAK,MAAM,UAAU,SACnB,MAAK,MAAM;IAAE,MAAM;IAAU,OAAO;IAAQ,CAAC;AAE/C,QAAK,QAAQ;;AAGf,OAAK,WAAW,OAAO,EAAE,kBAAkB;GACzC,MAAM,OAAO,YAAY,UAAU,KAAK,MAAM,EAAE,IAAI;GAEpD,MAAM,MAAM,MAAM,KAAK,YAAY,MAAM,YAAY;AAErD,SAAM,QAAQ,IAAI,KAAK,KAAK,QAAQ,GAAG,MAAM,OAAO,IAAI,CAAC,CAAC;AAE1D,SAAM,GAAG;AAET,QAAK,OAAO;AACZ,QAAK,MAAM,OAAO,KAChB,MAAK,MAAM;IAAE,MAAM;IAAU;IAAK,CAAC;AAErC,QAAK,QAAQ;;;;AAKnB,SAAgB,qBAGd,EAAE,UAAU,EAAE,KAAK,gBAAgB,QAA+C;AAClF,QAAO,IAAI,oBAAoB,KAAK,MAAM,aAAa,MAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaze-chat/tanstack-db-idb-collection",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "license": "Apache-2.0",
5
5
  "author": "MioYi Sama",
6
6
  "repository": "https://github.com/MioYiSama/blaze-chat/tree/main/packages/tanstack-db-idb-collection",