@foretag/tanstack-db-surrealdb 0.6.2 → 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
@@ -124,20 +124,14 @@ var isRecordIdString = (value) => {
124
124
  return idx > 0 && idx < value.length - 1;
125
125
  };
126
126
  var looksLikeTableName = (value) => /^[A-Za-z_][A-Za-z0-9_-]*$/.test(value);
127
- var getCtorName = (value) => {
128
- if (!value || typeof value !== "object") return void 0;
129
- const ctor = value.constructor;
130
- return typeof ctor?.name === "string" ? ctor.name : void 0;
131
- };
132
127
  var isCrossRuntimeRecordIdObject = (value) => {
133
128
  if (!value || typeof value !== "object" || value instanceof surrealdb.RecordId) {
134
129
  return false;
135
130
  }
136
131
  const obj = value;
137
132
  if (typeof obj.toString !== "function") return false;
133
+ if (obj.toString === Object.prototype.toString) return false;
138
134
  if (!("table" in obj) || !("id" in obj)) return false;
139
- const ctorName = getCtorName(value);
140
- if (!ctorName || !/^RecordId\d*$/.test(ctorName)) return false;
141
135
  const tableValue = obj.table;
142
136
  const table = typeof tableValue === "string" ? tableValue : tableValue != null ? String(tableValue) : "";
143
137
  const idValue = obj.id;
@@ -163,11 +157,11 @@ var unwrapIdWrapper = (value) => {
163
157
  var asCanonicalRecordIdFromCrossRuntimeObject = (value) => {
164
158
  if (!isCrossRuntimeRecordIdObject(value)) return void 0;
165
159
  const obj = value;
166
- const tableRaw = typeof obj.table === "string" ? obj.table : String(obj.table);
167
- const table = stripOuterQuotes(tableRaw).trim();
168
- const id = String(obj.id);
169
- const fromFields = table && id && looksLikeTableName(table) ? `${table}:${id}` : void 0;
170
- if (fromFields) return toCanonicalRecordIdString(fromFields);
160
+ const table = typeof obj.table === "string" ? stripOuterQuotes(obj.table).trim() : void 0;
161
+ const id = typeof obj.id === "string" || typeof obj.id === "number" || typeof obj.id === "bigint" ? String(obj.id) : void 0;
162
+ if (table && id && looksLikeTableName(table)) {
163
+ return toCanonicalRecordIdString(`${table}:${id}`);
164
+ }
171
165
  return toCanonicalRecordIdString(String(obj.toString()));
172
166
  };
173
167
  var asCanonicalRecordIdString = (value) => {
@@ -187,12 +181,55 @@ var toNativeRecordIdLikeValue = (value) => {
187
181
  if (value instanceof surrealdb.RecordId) {
188
182
  const canonical2 = asCanonicalRecordIdString(value);
189
183
  if (!canonical2) return value;
190
- return getNativeRecordId(canonical2);
184
+ recordIdIdentityPool.set(canonical2, value);
185
+ nativeRecordIdPool.set(canonical2, value);
186
+ return value;
191
187
  }
192
188
  const canonical = asCanonicalRecordIdString(value);
193
189
  if (!canonical) return value;
190
+ if (isCrossRuntimeRecordIdObject(value)) {
191
+ recordIdIdentityPool.set(canonical, value);
192
+ }
194
193
  return getNativeRecordId(canonical);
195
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
+ };
196
233
  var normalizeRecordIdLikeValue = (value) => {
197
234
  if (value instanceof surrealdb.RecordId) {
198
235
  const canonical2 = asCanonicalRecordIdString(value);
@@ -703,19 +740,43 @@ function defaultAad(ctx) {
703
740
  return toBytes(`${ctx.table}:${base}:${ctx.id}`);
704
741
  }
705
742
  var syncModeFrom = (syncMode) => syncMode ?? "eager";
706
- var subsetCacheKey = (subset) => JSON.stringify(subset, (_key, value) => {
707
- if (value instanceof Date) return value.toISOString();
708
- if (value instanceof surrealdb.RecordId) return toRecordIdString(value);
709
- if (typeof value === "function") return void 0;
710
- if (value && typeof value === "object" && "table" in value && "id" in value) {
711
- try {
712
- return toRecordIdString(String(value));
713
- } catch {
714
- return value;
743
+ var subsetCacheKey = (subset) => {
744
+ const seen = /* @__PURE__ */ new WeakSet();
745
+ return JSON.stringify(subset, (_key, value) => {
746
+ if (value instanceof Date) return value.toISOString();
747
+ if (value instanceof surrealdb.RecordId) return toRecordIdString(value);
748
+ if (typeof value === "bigint") return value.toString();
749
+ if (typeof value === "function") return `[fn:${value.name || "anonymous"}]`;
750
+ if (value && typeof value === "object") {
751
+ const canonical = asCanonicalRecordIdString(value);
752
+ if (canonical) return canonical;
753
+ if (seen.has(value)) return "[Circular]";
754
+ seen.add(value);
715
755
  }
756
+ return value;
757
+ }) ?? "";
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;
716
763
  }
717
- return value;
718
- });
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
+ };
719
780
  function modernSurrealCollectionOptions(config) {
720
781
  const {
721
782
  db,
@@ -733,7 +794,8 @@ function modernSurrealCollectionOptions(config) {
733
794
  const syncMode = syncModeFrom(inputSyncMode);
734
795
  const isOnDemandLike = syncMode === "on-demand" || syncMode === "progressive";
735
796
  const isStrictOnDemand = syncMode === "on-demand";
736
- const queryDrivenSyncMode = isOnDemandLike ? "on-demand" : "eager";
797
+ const queryDrivenSyncMode = "on-demand";
798
+ const queryDrivenUsesSubsets = queryDrivenSyncMode === "on-demand";
737
799
  const tableOptions = toTableOptions(table);
738
800
  const tableName = tableOptions.name;
739
801
  const tableResource = toTableResource(table);
@@ -1026,7 +1088,7 @@ function modernSurrealCollectionOptions(config) {
1026
1088
  };
1027
1089
  };
1028
1090
  const loadSubset = async (subset) => {
1029
- if (!isOnDemandLike) return;
1091
+ primeRecordIdIdentityFromSubset(subset);
1030
1092
  const key = subsetCacheKey(subset);
1031
1093
  const rows = await tableAccess.loadSubset(subset);
1032
1094
  const ids = new Set(
@@ -1059,7 +1121,7 @@ function modernSurrealCollectionOptions(config) {
1059
1121
  await ensureUpdateLive();
1060
1122
  };
1061
1123
  const unloadSubset = (subset) => {
1062
- if (!isOnDemandLike) return;
1124
+ primeRecordIdIdentityFromSubset(subset);
1063
1125
  const key = subsetCacheKey(subset);
1064
1126
  subsetIds.delete(key);
1065
1127
  updateActiveOnDemandIds();
@@ -1097,21 +1159,12 @@ function modernSurrealCollectionOptions(config) {
1097
1159
  syncMode: queryDrivenSyncMode,
1098
1160
  queryFn: async ({ meta }) => {
1099
1161
  try {
1100
- if (isOnDemandLike && !meta?.loadSubsetOptions) {
1162
+ primeRecordIdIdentityFromSubset(meta?.loadSubsetOptions);
1163
+ if (queryDrivenUsesSubsets && !meta?.loadSubsetOptions) {
1101
1164
  return [];
1102
1165
  }
1103
1166
  if (!crdtEnabled) {
1104
- if (!isOnDemandLike) {
1105
- const rows2 = await toRecordArray(
1106
- await db.select(tableResource)
1107
- );
1108
- const decoded2 = await Promise.all(
1109
- rows2.map(
1110
- (row) => decodeBaseRow(row)
1111
- )
1112
- );
1113
- return decoded2;
1114
- }
1167
+ if (!queryDrivenUsesSubsets) ;
1115
1168
  const rows = await tableAccess.loadSubset(
1116
1169
  meta?.loadSubsetOptions
1117
1170
  );
@@ -1122,7 +1175,7 @@ function modernSurrealCollectionOptions(config) {
1122
1175
  );
1123
1176
  return decoded;
1124
1177
  }
1125
- if (isOnDemandLike) return [];
1178
+ if (queryDrivenUsesSubsets) return [];
1126
1179
  if (!updatesTableName) return [];
1127
1180
  const updates = await queryRows(
1128
1181
  db,