@foretag/tanstack-db-surrealdb 0.6.3 → 0.6.4

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
@@ -192,6 +192,44 @@ var toNativeRecordIdLikeValue = (value) => {
192
192
  }
193
193
  return getNativeRecordId(canonical);
194
194
  };
195
+ var preferRecordIdLikeIdentity = (value) => {
196
+ const canonical = asCanonicalRecordIdString(value);
197
+ if (!canonical) return normalizeRecordIdLikeValue(value);
198
+ if (value instanceof surrealdb.RecordId) {
199
+ recordIdIdentityPool.set(canonical, value);
200
+ return value;
201
+ }
202
+ if (isCrossRuntimeRecordIdObject(value)) {
203
+ recordIdIdentityPool.set(canonical, value);
204
+ return value;
205
+ }
206
+ const wrappedId = unwrapIdWrapper(value);
207
+ if (wrappedId instanceof surrealdb.RecordId) {
208
+ recordIdIdentityPool.set(canonical, wrappedId);
209
+ return wrappedId;
210
+ }
211
+ if (isCrossRuntimeRecordIdObject(wrappedId)) {
212
+ recordIdIdentityPool.set(canonical, wrappedId);
213
+ return wrappedId;
214
+ }
215
+ return internRecordIdIdentity(canonical);
216
+ };
217
+ var preferRecordIdLikeIdentityDeep = (value) => {
218
+ const preferred = preferRecordIdLikeIdentity(value);
219
+ if (Array.isArray(preferred)) {
220
+ return preferred.map(
221
+ (item) => preferRecordIdLikeIdentityDeep(item)
222
+ );
223
+ }
224
+ if (isPlainObject(preferred)) {
225
+ const out = {};
226
+ for (const [k, v] of Object.entries(preferred)) {
227
+ out[k] = preferRecordIdLikeIdentityDeep(v);
228
+ }
229
+ return out;
230
+ }
231
+ return preferred;
232
+ };
195
233
  var normalizeRecordIdLikeValue = (value) => {
196
234
  if (value instanceof surrealdb.RecordId) {
197
235
  const canonical2 = asCanonicalRecordIdString(value);
@@ -718,6 +756,27 @@ var subsetCacheKey = (subset) => {
718
756
  return value;
719
757
  }) ?? "";
720
758
  };
759
+ var normalizeSubsetValuesInPlace = (value, seen = /* @__PURE__ */ new WeakSet()) => {
760
+ if (Array.isArray(value)) {
761
+ for (const entry of value) normalizeSubsetValuesInPlace(entry, seen);
762
+ return;
763
+ }
764
+ if (!value || typeof value !== "object") return;
765
+ if (seen.has(value)) return;
766
+ seen.add(value);
767
+ const obj = value;
768
+ if (obj.type === "val" && "value" in obj) {
769
+ obj.value = preferRecordIdLikeIdentityDeep(obj.value);
770
+ }
771
+ for (const child of Object.values(obj)) {
772
+ normalizeSubsetValuesInPlace(child, seen);
773
+ }
774
+ };
775
+ var primeRecordIdIdentityFromSubset = (subset) => {
776
+ if (!subset) return;
777
+ normalizeSubsetValuesInPlace(subset);
778
+ preferRecordIdLikeIdentityDeep(subset);
779
+ };
721
780
  function modernSurrealCollectionOptions(config) {
722
781
  const {
723
782
  db,
@@ -735,7 +794,8 @@ function modernSurrealCollectionOptions(config) {
735
794
  const syncMode = syncModeFrom(inputSyncMode);
736
795
  const isOnDemandLike = syncMode === "on-demand" || syncMode === "progressive";
737
796
  const isStrictOnDemand = syncMode === "on-demand";
738
- const queryDrivenSyncMode = isOnDemandLike ? "on-demand" : "eager";
797
+ const queryDrivenSyncMode = "on-demand";
798
+ const queryDrivenUsesSubsets = queryDrivenSyncMode === "on-demand";
739
799
  const tableOptions = toTableOptions(table);
740
800
  const tableName = tableOptions.name;
741
801
  const tableResource = toTableResource(table);
@@ -1028,7 +1088,7 @@ function modernSurrealCollectionOptions(config) {
1028
1088
  };
1029
1089
  };
1030
1090
  const loadSubset = async (subset) => {
1031
- if (!isOnDemandLike) return;
1091
+ primeRecordIdIdentityFromSubset(subset);
1032
1092
  const key = subsetCacheKey(subset);
1033
1093
  const rows = await tableAccess.loadSubset(subset);
1034
1094
  const ids = new Set(
@@ -1061,7 +1121,7 @@ function modernSurrealCollectionOptions(config) {
1061
1121
  await ensureUpdateLive();
1062
1122
  };
1063
1123
  const unloadSubset = (subset) => {
1064
- if (!isOnDemandLike) return;
1124
+ primeRecordIdIdentityFromSubset(subset);
1065
1125
  const key = subsetCacheKey(subset);
1066
1126
  subsetIds.delete(key);
1067
1127
  updateActiveOnDemandIds();
@@ -1099,21 +1159,12 @@ function modernSurrealCollectionOptions(config) {
1099
1159
  syncMode: queryDrivenSyncMode,
1100
1160
  queryFn: async ({ meta }) => {
1101
1161
  try {
1102
- if (isOnDemandLike && !meta?.loadSubsetOptions) {
1162
+ primeRecordIdIdentityFromSubset(meta?.loadSubsetOptions);
1163
+ if (queryDrivenUsesSubsets && !meta?.loadSubsetOptions) {
1103
1164
  return [];
1104
1165
  }
1105
1166
  if (!crdtEnabled) {
1106
- if (!isOnDemandLike) {
1107
- const rows2 = await toRecordArray(
1108
- await db.select(tableResource)
1109
- );
1110
- const decoded2 = await Promise.all(
1111
- rows2.map(
1112
- (row) => decodeBaseRow(row)
1113
- )
1114
- );
1115
- return decoded2;
1116
- }
1167
+ if (!queryDrivenUsesSubsets) ;
1117
1168
  const rows = await tableAccess.loadSubset(
1118
1169
  meta?.loadSubsetOptions
1119
1170
  );
@@ -1124,7 +1175,7 @@ function modernSurrealCollectionOptions(config) {
1124
1175
  );
1125
1176
  return decoded;
1126
1177
  }
1127
- if (isOnDemandLike) return [];
1178
+ if (queryDrivenUsesSubsets) return [];
1128
1179
  if (!updatesTableName) return [];
1129
1180
  const updates = await queryRows(
1130
1181
  db,