@foretag/tanstack-db-surrealdb 0.6.5 → 0.6.7

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.mts CHANGED
@@ -161,7 +161,7 @@ declare function surrealCollectionOptions<T extends SyncedTable<object>>(config:
161
161
  };
162
162
  declare module '@tanstack/db' {
163
163
  interface Collection<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = UtilsRecord, TSchema extends StandardSchemaV1 = StandardSchemaV1, TInsertInput extends object = T> {
164
- delete(keys: Array<TKey | RecordId | string> | TKey | RecordId | string, config?: OperationConfig): Transaction<any>;
164
+ delete(keys: Array<TKey | RecordId | string> | TKey | RecordId | string, config?: OperationConfig): Transaction<Record<string, never>>;
165
165
  }
166
166
  }
167
167
 
package/dist/index.d.ts CHANGED
@@ -161,7 +161,7 @@ declare function surrealCollectionOptions<T extends SyncedTable<object>>(config:
161
161
  };
162
162
  declare module '@tanstack/db' {
163
163
  interface Collection<T extends object = Record<string, unknown>, TKey extends string | number = string | number, TUtils extends UtilsRecord = UtilsRecord, TSchema extends StandardSchemaV1 = StandardSchemaV1, TInsertInput extends object = T> {
164
- delete(keys: Array<TKey | RecordId | string> | TKey | RecordId | string, config?: OperationConfig): Transaction<any>;
164
+ delete(keys: Array<TKey | RecordId | string> | TKey | RecordId | string, config?: OperationConfig): Transaction<Record<string, never>>;
165
165
  }
166
166
  }
167
167
 
package/dist/index.js CHANGED
@@ -640,6 +640,30 @@ var ENVELOPE_FIELDS = [
640
640
  ];
641
641
  var NOOP = () => {
642
642
  };
643
+ var patchedCollections = /* @__PURE__ */ new WeakSet();
644
+ var normalizeDeleteKeyAgainstState = (state, key) => {
645
+ if (state.has(key)) return key;
646
+ const canonical = asCanonicalRecordIdString(key);
647
+ if (!canonical) return key;
648
+ return state.has(canonical) ? canonical : key;
649
+ };
650
+ var patchCollectionDeleteForRecordIds = (collection) => {
651
+ if (!collection || typeof collection !== "object") return;
652
+ if (patchedCollections.has(collection)) return;
653
+ const candidate = collection;
654
+ if (typeof candidate.delete !== "function") return;
655
+ const originalDelete = candidate.delete.bind(collection);
656
+ Object.defineProperty(collection, "delete", {
657
+ configurable: true,
658
+ writable: true,
659
+ value: (keys, config) => {
660
+ const state = collection.state ?? /* @__PURE__ */ new Map();
661
+ const normalizedKeys = Array.isArray(keys) ? keys.map((key) => normalizeDeleteKeyAgainstState(state, key)) : normalizeDeleteKeyAgainstState(state, keys);
662
+ return originalDelete(normalizedKeys, config);
663
+ }
664
+ });
665
+ patchedCollections.add(collection);
666
+ };
643
667
  var isTableObject = (value) => typeof value === "object" && value !== null && "name" in value && typeof value.name === "string";
644
668
  var toTableOptions = (table) => {
645
669
  if (typeof table === "string") return { name: table };
@@ -746,7 +770,8 @@ var subsetCacheKey = (subset) => {
746
770
  if (value instanceof Date) return value.toISOString();
747
771
  if (value instanceof surrealdb.RecordId) return toRecordIdString(value);
748
772
  if (typeof value === "bigint") return value.toString();
749
- if (typeof value === "function") return `[fn:${value.name || "anonymous"}]`;
773
+ if (typeof value === "function")
774
+ return `[fn:${value.name || "anonymous"}]`;
750
775
  if (value && typeof value === "object") {
751
776
  const canonical = asCanonicalRecordIdString(value);
752
777
  if (canonical) return canonical;
@@ -1162,7 +1187,10 @@ function modernSurrealCollectionOptions(config) {
1162
1187
  };
1163
1188
  const loadSubset = async (subset) => {
1164
1189
  const preferredFromSubset = primeRecordIdIdentityFromSubset(subset);
1165
- applyPreferredRecordIdIdentityToCollection(ctx, preferredFromSubset);
1190
+ applyPreferredRecordIdIdentityToCollection(
1191
+ ctx,
1192
+ preferredFromSubset
1193
+ );
1166
1194
  const key = subsetCacheKey(subset);
1167
1195
  const rows = await tableAccess.loadSubset(subset);
1168
1196
  const ids = new Set(
@@ -1412,6 +1440,7 @@ function modernSurrealCollectionOptions(config) {
1412
1440
  const baseSync = base.sync?.sync;
1413
1441
  const sync = baseSync ? {
1414
1442
  sync: (ctx) => {
1443
+ patchCollectionDeleteForRecordIds(ctx.collection);
1415
1444
  const canRunBaseSync = typeof ctx.collection?.on === "function";
1416
1445
  const baseResult = canRunBaseSync ? baseSync(ctx) : void 0;
1417
1446
  const baseCleanup = typeof baseResult === "function" ? baseResult : typeof baseResult === "object" && baseResult && "cleanup" in baseResult && typeof baseResult.cleanup === "function" ? baseResult.cleanup : NOOP;