@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 +93 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +93 -40
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -122,20 +122,14 @@ var isRecordIdString = (value) => {
|
|
|
122
122
|
return idx > 0 && idx < value.length - 1;
|
|
123
123
|
};
|
|
124
124
|
var looksLikeTableName = (value) => /^[A-Za-z_][A-Za-z0-9_-]*$/.test(value);
|
|
125
|
-
var getCtorName = (value) => {
|
|
126
|
-
if (!value || typeof value !== "object") return void 0;
|
|
127
|
-
const ctor = value.constructor;
|
|
128
|
-
return typeof ctor?.name === "string" ? ctor.name : void 0;
|
|
129
|
-
};
|
|
130
125
|
var isCrossRuntimeRecordIdObject = (value) => {
|
|
131
126
|
if (!value || typeof value !== "object" || value instanceof RecordId) {
|
|
132
127
|
return false;
|
|
133
128
|
}
|
|
134
129
|
const obj = value;
|
|
135
130
|
if (typeof obj.toString !== "function") return false;
|
|
131
|
+
if (obj.toString === Object.prototype.toString) return false;
|
|
136
132
|
if (!("table" in obj) || !("id" in obj)) return false;
|
|
137
|
-
const ctorName = getCtorName(value);
|
|
138
|
-
if (!ctorName || !/^RecordId\d*$/.test(ctorName)) return false;
|
|
139
133
|
const tableValue = obj.table;
|
|
140
134
|
const table = typeof tableValue === "string" ? tableValue : tableValue != null ? String(tableValue) : "";
|
|
141
135
|
const idValue = obj.id;
|
|
@@ -161,11 +155,11 @@ var unwrapIdWrapper = (value) => {
|
|
|
161
155
|
var asCanonicalRecordIdFromCrossRuntimeObject = (value) => {
|
|
162
156
|
if (!isCrossRuntimeRecordIdObject(value)) return void 0;
|
|
163
157
|
const obj = value;
|
|
164
|
-
const
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
158
|
+
const table = typeof obj.table === "string" ? stripOuterQuotes(obj.table).trim() : void 0;
|
|
159
|
+
const id = typeof obj.id === "string" || typeof obj.id === "number" || typeof obj.id === "bigint" ? String(obj.id) : void 0;
|
|
160
|
+
if (table && id && looksLikeTableName(table)) {
|
|
161
|
+
return toCanonicalRecordIdString(`${table}:${id}`);
|
|
162
|
+
}
|
|
169
163
|
return toCanonicalRecordIdString(String(obj.toString()));
|
|
170
164
|
};
|
|
171
165
|
var asCanonicalRecordIdString = (value) => {
|
|
@@ -185,12 +179,55 @@ var toNativeRecordIdLikeValue = (value) => {
|
|
|
185
179
|
if (value instanceof RecordId) {
|
|
186
180
|
const canonical2 = asCanonicalRecordIdString(value);
|
|
187
181
|
if (!canonical2) return value;
|
|
188
|
-
|
|
182
|
+
recordIdIdentityPool.set(canonical2, value);
|
|
183
|
+
nativeRecordIdPool.set(canonical2, value);
|
|
184
|
+
return value;
|
|
189
185
|
}
|
|
190
186
|
const canonical = asCanonicalRecordIdString(value);
|
|
191
187
|
if (!canonical) return value;
|
|
188
|
+
if (isCrossRuntimeRecordIdObject(value)) {
|
|
189
|
+
recordIdIdentityPool.set(canonical, value);
|
|
190
|
+
}
|
|
192
191
|
return getNativeRecordId(canonical);
|
|
193
192
|
};
|
|
193
|
+
var preferRecordIdLikeIdentity = (value) => {
|
|
194
|
+
const canonical = asCanonicalRecordIdString(value);
|
|
195
|
+
if (!canonical) return normalizeRecordIdLikeValue(value);
|
|
196
|
+
if (value instanceof RecordId) {
|
|
197
|
+
recordIdIdentityPool.set(canonical, value);
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
if (isCrossRuntimeRecordIdObject(value)) {
|
|
201
|
+
recordIdIdentityPool.set(canonical, value);
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
const wrappedId = unwrapIdWrapper(value);
|
|
205
|
+
if (wrappedId instanceof RecordId) {
|
|
206
|
+
recordIdIdentityPool.set(canonical, wrappedId);
|
|
207
|
+
return wrappedId;
|
|
208
|
+
}
|
|
209
|
+
if (isCrossRuntimeRecordIdObject(wrappedId)) {
|
|
210
|
+
recordIdIdentityPool.set(canonical, wrappedId);
|
|
211
|
+
return wrappedId;
|
|
212
|
+
}
|
|
213
|
+
return internRecordIdIdentity(canonical);
|
|
214
|
+
};
|
|
215
|
+
var preferRecordIdLikeIdentityDeep = (value) => {
|
|
216
|
+
const preferred = preferRecordIdLikeIdentity(value);
|
|
217
|
+
if (Array.isArray(preferred)) {
|
|
218
|
+
return preferred.map(
|
|
219
|
+
(item) => preferRecordIdLikeIdentityDeep(item)
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
if (isPlainObject(preferred)) {
|
|
223
|
+
const out = {};
|
|
224
|
+
for (const [k, v] of Object.entries(preferred)) {
|
|
225
|
+
out[k] = preferRecordIdLikeIdentityDeep(v);
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
return preferred;
|
|
230
|
+
};
|
|
194
231
|
var normalizeRecordIdLikeValue = (value) => {
|
|
195
232
|
if (value instanceof RecordId) {
|
|
196
233
|
const canonical2 = asCanonicalRecordIdString(value);
|
|
@@ -701,19 +738,43 @@ function defaultAad(ctx) {
|
|
|
701
738
|
return toBytes(`${ctx.table}:${base}:${ctx.id}`);
|
|
702
739
|
}
|
|
703
740
|
var syncModeFrom = (syncMode) => syncMode ?? "eager";
|
|
704
|
-
var subsetCacheKey = (subset) =>
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
741
|
+
var subsetCacheKey = (subset) => {
|
|
742
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
743
|
+
return JSON.stringify(subset, (_key, value) => {
|
|
744
|
+
if (value instanceof Date) return value.toISOString();
|
|
745
|
+
if (value instanceof RecordId) return toRecordIdString(value);
|
|
746
|
+
if (typeof value === "bigint") return value.toString();
|
|
747
|
+
if (typeof value === "function") return `[fn:${value.name || "anonymous"}]`;
|
|
748
|
+
if (value && typeof value === "object") {
|
|
749
|
+
const canonical = asCanonicalRecordIdString(value);
|
|
750
|
+
if (canonical) return canonical;
|
|
751
|
+
if (seen.has(value)) return "[Circular]";
|
|
752
|
+
seen.add(value);
|
|
713
753
|
}
|
|
754
|
+
return value;
|
|
755
|
+
}) ?? "";
|
|
756
|
+
};
|
|
757
|
+
var normalizeSubsetValuesInPlace = (value, seen = /* @__PURE__ */ new WeakSet()) => {
|
|
758
|
+
if (Array.isArray(value)) {
|
|
759
|
+
for (const entry of value) normalizeSubsetValuesInPlace(entry, seen);
|
|
760
|
+
return;
|
|
714
761
|
}
|
|
715
|
-
|
|
716
|
-
|
|
762
|
+
if (!value || typeof value !== "object") return;
|
|
763
|
+
if (seen.has(value)) return;
|
|
764
|
+
seen.add(value);
|
|
765
|
+
const obj = value;
|
|
766
|
+
if (obj.type === "val" && "value" in obj) {
|
|
767
|
+
obj.value = preferRecordIdLikeIdentityDeep(obj.value);
|
|
768
|
+
}
|
|
769
|
+
for (const child of Object.values(obj)) {
|
|
770
|
+
normalizeSubsetValuesInPlace(child, seen);
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
var primeRecordIdIdentityFromSubset = (subset) => {
|
|
774
|
+
if (!subset) return;
|
|
775
|
+
normalizeSubsetValuesInPlace(subset);
|
|
776
|
+
preferRecordIdLikeIdentityDeep(subset);
|
|
777
|
+
};
|
|
717
778
|
function modernSurrealCollectionOptions(config) {
|
|
718
779
|
const {
|
|
719
780
|
db,
|
|
@@ -731,7 +792,8 @@ function modernSurrealCollectionOptions(config) {
|
|
|
731
792
|
const syncMode = syncModeFrom(inputSyncMode);
|
|
732
793
|
const isOnDemandLike = syncMode === "on-demand" || syncMode === "progressive";
|
|
733
794
|
const isStrictOnDemand = syncMode === "on-demand";
|
|
734
|
-
const queryDrivenSyncMode =
|
|
795
|
+
const queryDrivenSyncMode = "on-demand";
|
|
796
|
+
const queryDrivenUsesSubsets = queryDrivenSyncMode === "on-demand";
|
|
735
797
|
const tableOptions = toTableOptions(table);
|
|
736
798
|
const tableName = tableOptions.name;
|
|
737
799
|
const tableResource = toTableResource(table);
|
|
@@ -1024,7 +1086,7 @@ function modernSurrealCollectionOptions(config) {
|
|
|
1024
1086
|
};
|
|
1025
1087
|
};
|
|
1026
1088
|
const loadSubset = async (subset) => {
|
|
1027
|
-
|
|
1089
|
+
primeRecordIdIdentityFromSubset(subset);
|
|
1028
1090
|
const key = subsetCacheKey(subset);
|
|
1029
1091
|
const rows = await tableAccess.loadSubset(subset);
|
|
1030
1092
|
const ids = new Set(
|
|
@@ -1057,7 +1119,7 @@ function modernSurrealCollectionOptions(config) {
|
|
|
1057
1119
|
await ensureUpdateLive();
|
|
1058
1120
|
};
|
|
1059
1121
|
const unloadSubset = (subset) => {
|
|
1060
|
-
|
|
1122
|
+
primeRecordIdIdentityFromSubset(subset);
|
|
1061
1123
|
const key = subsetCacheKey(subset);
|
|
1062
1124
|
subsetIds.delete(key);
|
|
1063
1125
|
updateActiveOnDemandIds();
|
|
@@ -1095,21 +1157,12 @@ function modernSurrealCollectionOptions(config) {
|
|
|
1095
1157
|
syncMode: queryDrivenSyncMode,
|
|
1096
1158
|
queryFn: async ({ meta }) => {
|
|
1097
1159
|
try {
|
|
1098
|
-
|
|
1160
|
+
primeRecordIdIdentityFromSubset(meta?.loadSubsetOptions);
|
|
1161
|
+
if (queryDrivenUsesSubsets && !meta?.loadSubsetOptions) {
|
|
1099
1162
|
return [];
|
|
1100
1163
|
}
|
|
1101
1164
|
if (!crdtEnabled) {
|
|
1102
|
-
if (!
|
|
1103
|
-
const rows2 = await toRecordArray(
|
|
1104
|
-
await db.select(tableResource)
|
|
1105
|
-
);
|
|
1106
|
-
const decoded2 = await Promise.all(
|
|
1107
|
-
rows2.map(
|
|
1108
|
-
(row) => decodeBaseRow(row)
|
|
1109
|
-
)
|
|
1110
|
-
);
|
|
1111
|
-
return decoded2;
|
|
1112
|
-
}
|
|
1165
|
+
if (!queryDrivenUsesSubsets) ;
|
|
1113
1166
|
const rows = await tableAccess.loadSubset(
|
|
1114
1167
|
meta?.loadSubsetOptions
|
|
1115
1168
|
);
|
|
@@ -1120,7 +1173,7 @@ function modernSurrealCollectionOptions(config) {
|
|
|
1120
1173
|
);
|
|
1121
1174
|
return decoded;
|
|
1122
1175
|
}
|
|
1123
|
-
if (
|
|
1176
|
+
if (queryDrivenUsesSubsets) return [];
|
|
1124
1177
|
if (!updatesTableName) return [];
|
|
1125
1178
|
const updates = await queryRows(
|
|
1126
1179
|
db,
|