@foretag/tanstack-db-surrealdb 0.6.4 → 0.6.5

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
@@ -754,26 +754,99 @@ var subsetCacheKey = (subset) => {
754
754
  return value;
755
755
  }) ?? "";
756
756
  };
757
- var normalizeSubsetValuesInPlace = (value, seen = /* @__PURE__ */ new WeakSet()) => {
757
+ var isPlainObject2 = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
758
+ var normalizeSubsetValuesInPlace = (value, seen = /* @__PURE__ */ new WeakSet(), preferredByCanonical = /* @__PURE__ */ new Map()) => {
758
759
  if (Array.isArray(value)) {
759
- for (const entry of value) normalizeSubsetValuesInPlace(entry, seen);
760
- return;
760
+ for (const entry of value) {
761
+ normalizeSubsetValuesInPlace(entry, seen, preferredByCanonical);
762
+ }
763
+ return preferredByCanonical;
761
764
  }
762
- if (!value || typeof value !== "object") return;
763
- if (seen.has(value)) return;
765
+ if (!value || typeof value !== "object") return preferredByCanonical;
766
+ if (seen.has(value)) return preferredByCanonical;
764
767
  seen.add(value);
765
768
  const obj = value;
766
769
  if (obj.type === "val" && "value" in obj) {
767
- obj.value = preferRecordIdLikeIdentityDeep(obj.value);
770
+ const canonical = asCanonicalRecordIdString(obj.value);
771
+ if (canonical) {
772
+ const preferred = preferRecordIdLikeIdentity(obj.value);
773
+ obj.value = preferred;
774
+ preferredByCanonical.set(canonical, preferred);
775
+ } else {
776
+ obj.value = preferRecordIdLikeIdentityDeep(obj.value);
777
+ }
768
778
  }
769
779
  for (const child of Object.values(obj)) {
770
- normalizeSubsetValuesInPlace(child, seen);
780
+ normalizeSubsetValuesInPlace(child, seen, preferredByCanonical);
771
781
  }
782
+ return preferredByCanonical;
772
783
  };
773
784
  var primeRecordIdIdentityFromSubset = (subset) => {
774
- if (!subset) return;
775
- normalizeSubsetValuesInPlace(subset);
776
- preferRecordIdLikeIdentityDeep(subset);
785
+ if (!subset) return /* @__PURE__ */ new Map();
786
+ return normalizeSubsetValuesInPlace(subset);
787
+ };
788
+ var rebindRecordIdIdentityDeep = (value, preferredByCanonical) => {
789
+ const canonical = asCanonicalRecordIdString(value);
790
+ if (canonical && preferredByCanonical.has(canonical)) {
791
+ const preferred = preferredByCanonical.get(canonical);
792
+ return {
793
+ value: preferred,
794
+ changed: value !== preferred
795
+ };
796
+ }
797
+ if (Array.isArray(value)) {
798
+ let changed2 = false;
799
+ const out2 = value.map((entry) => {
800
+ const rebound = rebindRecordIdIdentityDeep(
801
+ entry,
802
+ preferredByCanonical
803
+ );
804
+ changed2 = changed2 || rebound.changed;
805
+ return rebound.value;
806
+ });
807
+ return changed2 ? { value: out2, changed: true } : { value, changed: false };
808
+ }
809
+ if (!isPlainObject2(value)) return { value, changed: false };
810
+ let changed = false;
811
+ const out = {};
812
+ for (const [key, entry] of Object.entries(value)) {
813
+ const rebound = rebindRecordIdIdentityDeep(entry, preferredByCanonical);
814
+ if (rebound.changed) changed = true;
815
+ out[key] = rebound.value;
816
+ }
817
+ return changed ? { value: out, changed: true } : { value, changed: false };
818
+ };
819
+ var applyPreferredRecordIdIdentityToCollection = (ctx, preferredByCanonical) => {
820
+ if (!preferredByCanonical.size) return;
821
+ const collection = ctx.collection;
822
+ if (!collection || typeof collection.entries !== "function") return;
823
+ const rebound = [];
824
+ for (const [, row] of collection.entries()) {
825
+ const updated = rebindRecordIdIdentityDeep(row, preferredByCanonical);
826
+ if (!updated.changed) continue;
827
+ rebound.push(updated.value);
828
+ }
829
+ if (!rebound.length) return;
830
+ ctx.begin();
831
+ try {
832
+ for (const row of rebound) {
833
+ ctx.write({
834
+ type: "delete",
835
+ value: { id: row.id }
836
+ });
837
+ ctx.write({ type: "insert", value: row });
838
+ }
839
+ } finally {
840
+ ctx.commit();
841
+ }
842
+ const collectionWithInternals = collection;
843
+ const entries = collectionWithInternals._state?.entries?.();
844
+ const indexes = collectionWithInternals._indexes?.indexes;
845
+ if (entries && indexes) {
846
+ for (const index of indexes.values()) {
847
+ index.build?.(entries);
848
+ }
849
+ }
777
850
  };
778
851
  function modernSurrealCollectionOptions(config) {
779
852
  const {
@@ -1086,7 +1159,8 @@ function modernSurrealCollectionOptions(config) {
1086
1159
  };
1087
1160
  };
1088
1161
  const loadSubset = async (subset) => {
1089
- primeRecordIdIdentityFromSubset(subset);
1162
+ const preferredFromSubset = primeRecordIdIdentityFromSubset(subset);
1163
+ applyPreferredRecordIdIdentityToCollection(ctx, preferredFromSubset);
1090
1164
  const key = subsetCacheKey(subset);
1091
1165
  const rows = await tableAccess.loadSubset(subset);
1092
1166
  const ids = new Set(