@foretag/tanstack-db-surrealdb 0.6.4 → 0.6.6

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