@foretag/tanstack-db-surrealdb 0.6.6 → 0.6.8

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.mjs CHANGED
@@ -638,6 +638,30 @@ var ENVELOPE_FIELDS = [
638
638
  ];
639
639
  var NOOP = () => {
640
640
  };
641
+ var patchedCollections = /* @__PURE__ */ new WeakSet();
642
+ var normalizeDeleteKeyAgainstState = (state, key) => {
643
+ if (state.has(key)) return key;
644
+ const canonical = asCanonicalRecordIdString(key);
645
+ if (!canonical) return key;
646
+ return state.has(canonical) ? canonical : key;
647
+ };
648
+ var patchCollectionDeleteForRecordIds = (collection) => {
649
+ if (!collection || typeof collection !== "object") return;
650
+ if (patchedCollections.has(collection)) return;
651
+ const candidate = collection;
652
+ if (typeof candidate.delete !== "function") return;
653
+ const originalDelete = candidate.delete.bind(collection);
654
+ Object.defineProperty(collection, "delete", {
655
+ configurable: true,
656
+ writable: true,
657
+ value: (keys, config) => {
658
+ const state = collection.state ?? /* @__PURE__ */ new Map();
659
+ const normalizedKeys = Array.isArray(keys) ? keys.map((key) => normalizeDeleteKeyAgainstState(state, key)) : normalizeDeleteKeyAgainstState(state, keys);
660
+ return originalDelete(normalizedKeys, config);
661
+ }
662
+ });
663
+ patchedCollections.add(collection);
664
+ };
641
665
  var isTableObject = (value) => typeof value === "object" && value !== null && "name" in value && typeof value.name === "string";
642
666
  var toTableOptions = (table) => {
643
667
  if (typeof table === "string") return { name: table };
@@ -744,7 +768,8 @@ var subsetCacheKey = (subset) => {
744
768
  if (value instanceof Date) return value.toISOString();
745
769
  if (value instanceof RecordId) return toRecordIdString(value);
746
770
  if (typeof value === "bigint") return value.toString();
747
- if (typeof value === "function") return `[fn:${value.name || "anonymous"}]`;
771
+ if (typeof value === "function")
772
+ return `[fn:${value.name || "anonymous"}]`;
748
773
  if (value && typeof value === "object") {
749
774
  const canonical = asCanonicalRecordIdString(value);
750
775
  if (canonical) return canonical;
@@ -1160,7 +1185,10 @@ function modernSurrealCollectionOptions(config) {
1160
1185
  };
1161
1186
  const loadSubset = async (subset) => {
1162
1187
  const preferredFromSubset = primeRecordIdIdentityFromSubset(subset);
1163
- applyPreferredRecordIdIdentityToCollection(ctx, preferredFromSubset);
1188
+ applyPreferredRecordIdIdentityToCollection(
1189
+ ctx,
1190
+ preferredFromSubset
1191
+ );
1164
1192
  const key = subsetCacheKey(subset);
1165
1193
  const rows = await tableAccess.loadSubset(subset);
1166
1194
  const ids = new Set(
@@ -1410,6 +1438,7 @@ function modernSurrealCollectionOptions(config) {
1410
1438
  const baseSync = base.sync?.sync;
1411
1439
  const sync = baseSync ? {
1412
1440
  sync: (ctx) => {
1441
+ patchCollectionDeleteForRecordIds(ctx.collection);
1413
1442
  const canRunBaseSync = typeof ctx.collection?.on === "function";
1414
1443
  const baseResult = canRunBaseSync ? baseSync(ctx) : void 0;
1415
1444
  const baseCleanup = typeof baseResult === "function" ? baseResult : typeof baseResult === "object" && baseResult && "cleanup" in baseResult && typeof baseResult.cleanup === "function" ? baseResult.cleanup : NOOP;