@firtoz/idb-collections 0.2.0 → 0.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firtoz/idb-collections",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "IndexedDB collection utilities for TanStack DB — key-value adapter, query optimization",
5
5
  "main": "./src/index.ts",
6
6
  "module": "./src/index.ts",
@@ -18,7 +18,7 @@
18
18
  "README.md"
19
19
  ],
20
20
  "scripts": {
21
- "typecheck": "tsc --noEmit -p ./tsconfig.json",
21
+ "typecheck": "tsgo --noEmit -p ./tsconfig.json",
22
22
  "test": "bun test",
23
23
  "lint": "biome check --write src",
24
24
  "lint:ci": "biome ci src",
@@ -51,15 +51,15 @@
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@standard-schema/spec": ">=1.1.0",
54
- "@tanstack/db": ">=0.5.33"
54
+ "@tanstack/db": ">=0.6.1"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@standard-schema/spec": "^1.1.0",
58
- "@tanstack/db": "^0.5.33",
59
- "bun-types": "^1.3.10",
58
+ "@tanstack/db": "^0.6.1",
59
+ "bun-types": "^1.3.11",
60
60
  "zod": "^4.3.6"
61
61
  },
62
62
  "dependencies": {
63
- "@firtoz/db-helpers": "^2.0.0"
63
+ "@firtoz/db-helpers": "^2.1.0"
64
64
  }
65
65
  }
@@ -33,7 +33,18 @@ export function tryExtractIndexedQuery(
33
33
  }
34
34
 
35
35
  try {
36
- const comparisons = extractSimpleComparisons(expression);
36
+ let comparisons: ReturnType<typeof extractSimpleComparisons>;
37
+ try {
38
+ comparisons = extractSimpleComparisons(expression);
39
+ } catch {
40
+ // e.g. `like` and other ops TanStack does not decompose here — fall back to full scan + filter
41
+ if (debug) {
42
+ console.warn(
43
+ "Indexed query extraction skipped: expression not supported by extractSimpleComparisons",
44
+ );
45
+ }
46
+ return null;
47
+ }
37
48
 
38
49
  if (comparisons.length !== 1) {
39
50
  return null;
@@ -41,7 +52,10 @@ export function tryExtractIndexedQuery(
41
52
 
42
53
  const comparison = comparisons[0];
43
54
  const fieldName = comparison.field.join(".");
44
- const indexName = indexes[fieldName];
55
+ const lastIdx = comparison.field.length - 1;
56
+ const lastSegment = lastIdx >= 0 ? (comparison.field[lastIdx] ?? "") : "";
57
+ // TanStack may use nested refs (`todo.priority`); IDB indexes are keyed by column (e.g. `priority`)
58
+ const indexName = indexes[fieldName] ?? indexes[lastSegment];
45
59
 
46
60
  if (!indexName) {
47
61
  return null;
@@ -96,7 +110,9 @@ export function tryExtractIndexedQuery(
96
110
 
97
111
  return { fieldName, indexName, keyRange };
98
112
  } catch (error) {
99
- console.error("Error extracting indexed query", error, expression);
113
+ if (debug) {
114
+ console.warn("Error extracting indexed query", error, expression);
115
+ }
100
116
  return null;
101
117
  }
102
118
  }
@@ -60,7 +60,12 @@ function defaultGetKey<TSchema extends StandardSchemaV1>(
60
60
 
61
61
  export function keyvalCollectionOptions<TSchema extends StandardSchemaV1>(
62
62
  config: KeyValCollectionConfig<TSchema>,
63
- ): CollectionConfig<InferSchemaOutput<TSchema>, string, TSchema> & {
63
+ ): CollectionConfig<
64
+ InferSchemaOutput<TSchema>,
65
+ string,
66
+ TSchema,
67
+ KeyValUtils<InferSchemaOutput<TSchema>>
68
+ > & {
64
69
  utils: KeyValUtils<InferSchemaOutput<TSchema>>;
65
70
  schema: TSchema;
66
71
  } {
@@ -209,10 +214,11 @@ export function keyvalCollectionOptions<TSchema extends StandardSchemaV1>(
209
214
  },
210
215
  };
211
216
 
212
- const baseSyncConfig: GenericBaseSyncConfig = {
217
+ const baseSyncConfig: GenericBaseSyncConfig<TItem> = {
213
218
  readyPromise,
214
219
  syncMode: config.syncMode,
215
220
  debug: config.debug,
221
+ getSyncPersistKey: getKey,
216
222
  };
217
223
 
218
224
  const syncResult: GenericSyncFunctionResult<TItem> =
@@ -223,6 +229,34 @@ export function keyvalCollectionOptions<TSchema extends StandardSchemaV1>(
223
229
  getKey,
224
230
  syncResult,
225
231
  syncMode: config.syncMode,
232
+ onInsert: async (params) => {
233
+ await syncResult.onInsert?.(params);
234
+ const writes: SyncMessage<TItem, string>[] =
235
+ params.transaction.mutations.map((mutation) => ({
236
+ type: "insert",
237
+ value: mutation.modified,
238
+ }));
239
+ config.onBroadcast?.(writes);
240
+ },
241
+ onUpdate: async (params) => {
242
+ await syncResult.onUpdate?.(params);
243
+ const writes: SyncMessage<TItem, string>[] =
244
+ params.transaction.mutations.map((mutation) => ({
245
+ type: "update",
246
+ value: mutation.modified,
247
+ previousValue: mutation.original,
248
+ }));
249
+ config.onBroadcast?.(writes);
250
+ },
251
+ onDelete: async (params) => {
252
+ await syncResult.onDelete?.(params);
253
+ const writes: SyncMessage<TItem, string>[] =
254
+ params.transaction.mutations.map((mutation) => ({
255
+ type: "delete",
256
+ key: mutation.key,
257
+ }));
258
+ config.onBroadcast?.(writes);
259
+ },
226
260
  });
227
261
  }
228
262