@datafn/server 0.2.1 → 0.2.3
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.cjs +243 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +244 -22
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DATAFN_ROUTE_MAX_TTL_MS, validateDatafnRegionalEndpoint, DATAFN_ROUTE_TICKET_HEADER, DATAFN_ROUTE_WS_TICKET_PREFIX, DATAFN_ROUTE_WS_PROTOCOL, validateSchema, ensureBuiltinTemporal, ensureBuiltinKv, isNamespaced, ensureBuiltinPublicLinks, assertSupportedNamespaceStorageVersions, namespaceStorageManifestEntry, DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION, DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION, getAncestorInactiveResources, ANCESTOR_INACTIVE_FIELD, parseDatafnRequest, collectStructuralResourceSelectors, hasTemporalGrouping, resolveCapabilities, getCapabilityFields, getRelationJoinTableName, normalizeDfql, buildSchemaIndex as buildSchemaIndex$1, runBeforeHook, getTemporalGroupAliases, getTemporalClauses, parseSortTerm, endpointIncludes, runAfterHook, checkPrototypePollution, validateFieldValue, toBoundsEpochMs, formatBoundEpochMs, TIMEZONE_CHANGE_RESOURCE_NAME, createTimezoneResolver, normalizeTemporalQuery, relationFkFieldForManyOne, relationFkFieldForOneMany, resolveEndpointResource, endpointList, evaluateFilter as evaluateFilter$1, findRelationMatch, relationTargetEndpoint, sortRecords, relationKeyFor, resourceNameFromId, findSystemFieldWrite, normalizeRelationFkRecord, parseSelectToken, getJoinStoreKey, OP_REMAP, parseSortTerms as parseSortTerms$1, stripNullsForNonNullableFields, getTemporalGroups, resolveTemporalBucketValue, calculateAggregation, normalizeRelationPayload, firstEndpoint, RELATION_CAPABILITY_FIELD_DEFS, coerceDateFieldsToEpoch, resourceRequiresAncestorInactive } from '@datafn/core';
|
|
1
|
+
import { DATAFN_ROUTE_MAX_TTL_MS, validateDatafnRegionalEndpoint, DATAFN_ROUTE_TICKET_HEADER, DATAFN_ROUTE_WS_TICKET_PREFIX, DATAFN_ROUTE_WS_PROTOCOL, validateSchema, ensureBuiltinTemporal, ensureBuiltinKv, isNamespaced, ensureBuiltinPublicLinks, assertSupportedNamespaceStorageVersions, namespaceStorageManifestEntry, DATAFN_NAMESPACE_STORAGE_SCHEMA_VERSION, DATAFN_NAMESPACE_STORAGE_MANIFEST_VERSION, getAncestorInactiveResources, ANCESTOR_INACTIVE_FIELD, parseDatafnRequest, collectStructuralResourceSelectors, hasTemporalGrouping, resolveCapabilities, getCapabilityFields, getRelationJoinTableName, normalizeDfql, buildSchemaIndex as buildSchemaIndex$1, runBeforeHook, getTemporalGroupAliases, getTemporalClauses, parseSortTerm, endpointIncludes, runAfterHook, checkPrototypePollution, validateFieldValue, toBoundsEpochMs, formatBoundEpochMs, TIMEZONE_CHANGE_RESOURCE_NAME, createTimezoneResolver, normalizeTemporalQuery, relationFkFieldForManyOne, relationFkFieldForOneMany, resolveEndpointResource, endpointList, evaluateFilter as evaluateFilter$1, findRelationMatch, relationTargetEndpoint, sortRecords, relationKeyFor, resourceNameFromId, findSystemFieldWrite, normalizeRelationFkRecord, parseSelectToken, getJoinStoreKey, OP_REMAP, parseSortTerms as parseSortTerms$1, stripNullsForNonNullableFields, getTemporalGroups, resolveTemporalBucketValue, calculateAggregation, normalizeRelationPayload, firstEndpoint, KV_RESOURCE_NAME, RELATION_CAPABILITY_FIELD_DEFS, coerceDateFieldsToEpoch, resourceRequiresAncestorInactive } from '@datafn/core';
|
|
2
2
|
export { DATAFN_REQUEST_ACTIONS, DATAFN_REQUEST_PROTOCOL_VERSION, DATAFN_REQUEST_PROTOCOL_VERSIONS, DATAFN_STRUCTURAL_SELECTOR_POSITIONS, collectStructuralResourceSelectors, extractStructuralResourceSelectors, isDatafnRequestAction, parseDatafnRequest } from '@datafn/core';
|
|
3
3
|
import { createRouter, createObservabilityMiddleware, executeMiddlewareChain } from '@superfunctions/http';
|
|
4
4
|
import { wrapWithRowLevelNamespace, instrumentAdapter, instrumentKVStore } from '@superfunctions/db';
|
|
@@ -10549,6 +10549,9 @@ function isRecord(value) {
|
|
|
10549
10549
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10550
10550
|
}
|
|
10551
10551
|
function isNotFoundError(error) {
|
|
10552
|
+
if (error?.name === "KvMergeContentionError" || error?.name === "KvMergeUnsupportedError") {
|
|
10553
|
+
return false;
|
|
10554
|
+
}
|
|
10552
10555
|
const message = String(error?.message || "").toLowerCase();
|
|
10553
10556
|
return error?.name === "NotFoundError" || message.includes("not found") || message.includes("zero rows");
|
|
10554
10557
|
}
|
|
@@ -10642,6 +10645,136 @@ async function executeSchemaAwareMerge(params) {
|
|
|
10642
10645
|
return { ok: false, code: "INTERNAL", message: "Internal error", path: "$", error: retryError };
|
|
10643
10646
|
}
|
|
10644
10647
|
}
|
|
10648
|
+
var KV_SET_MUTATION_ID_PREFIX = "kv-set-";
|
|
10649
|
+
var KV_MERGE_MAX_ATTEMPTS = 16;
|
|
10650
|
+
var KvMergeContentionError = class extends Error {
|
|
10651
|
+
constructor(id) {
|
|
10652
|
+
super(`KV merge of ${id} did not settle after ${KV_MERGE_MAX_ATTEMPTS} concurrent attempts`);
|
|
10653
|
+
this.retryable = true;
|
|
10654
|
+
this.name = "KvMergeContentionError";
|
|
10655
|
+
}
|
|
10656
|
+
};
|
|
10657
|
+
function isKvMergeContentionError(error) {
|
|
10658
|
+
return error?.name === "KvMergeContentionError";
|
|
10659
|
+
}
|
|
10660
|
+
var KvMergeUnsupportedError = class extends Error {
|
|
10661
|
+
constructor() {
|
|
10662
|
+
super(
|
|
10663
|
+
"KV merge needs an adapter with transactions or strictUpdateNotFound to merge an existing record atomically"
|
|
10664
|
+
);
|
|
10665
|
+
this.name = "KvMergeUnsupportedError";
|
|
10666
|
+
}
|
|
10667
|
+
};
|
|
10668
|
+
function isKvMergeUnsupportedError(error) {
|
|
10669
|
+
return error?.name === "KvMergeUnsupportedError";
|
|
10670
|
+
}
|
|
10671
|
+
function isPlainObject(value) {
|
|
10672
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10673
|
+
}
|
|
10674
|
+
function notFound() {
|
|
10675
|
+
return { name: "NotFoundError", message: "Record not found" };
|
|
10676
|
+
}
|
|
10677
|
+
function isKvValueMerge(mutation) {
|
|
10678
|
+
return mutation.resource === KV_RESOURCE_NAME && mutation.operation === "merge" && isPlainObject(mutation.record) && Object.prototype.hasOwnProperty.call(mutation.record, "value") && !(typeof mutation.mutationId === "string" && mutation.mutationId.startsWith(KV_SET_MUTATION_ID_PREFIX));
|
|
10679
|
+
}
|
|
10680
|
+
function mergeKvValue(stored, patch) {
|
|
10681
|
+
if (!isPlainObject(stored) || !isPlainObject(patch)) {
|
|
10682
|
+
return patch;
|
|
10683
|
+
}
|
|
10684
|
+
return { ...stored, ...patch };
|
|
10685
|
+
}
|
|
10686
|
+
function supportsTransactions(db) {
|
|
10687
|
+
return db.capabilities?.transactions?.supported !== false && typeof db.transaction === "function";
|
|
10688
|
+
}
|
|
10689
|
+
function isTransactionUnsupportedError(error) {
|
|
10690
|
+
if (error?.name === "OperationNotSupportedError") return true;
|
|
10691
|
+
const message = String(error?.message ?? error).toLowerCase();
|
|
10692
|
+
return message.includes("cannot return a promise") || message.includes("not supported") || message.includes("not implemented");
|
|
10693
|
+
}
|
|
10694
|
+
async function updateKvRecordMerging(params) {
|
|
10695
|
+
const { db, resource, id, namespace, data, inTransaction } = params;
|
|
10696
|
+
const where = [{ field: "id", operator: "eq", value: id }];
|
|
10697
|
+
const strictUpdateNotFound = db.capabilities?.operations?.strictUpdateNotFound === true;
|
|
10698
|
+
const readStored = async (target) => {
|
|
10699
|
+
const row = await target.findOne({
|
|
10700
|
+
model: resource,
|
|
10701
|
+
where,
|
|
10702
|
+
namespace
|
|
10703
|
+
});
|
|
10704
|
+
if (!row) throw notFound();
|
|
10705
|
+
return row;
|
|
10706
|
+
};
|
|
10707
|
+
const tryUpdate = async (target, updateWhere, updateData) => {
|
|
10708
|
+
try {
|
|
10709
|
+
const updated = await target.update({
|
|
10710
|
+
model: resource,
|
|
10711
|
+
where: updateWhere,
|
|
10712
|
+
data: updateData,
|
|
10713
|
+
namespace
|
|
10714
|
+
});
|
|
10715
|
+
return !(strictUpdateNotFound && updated === void 0);
|
|
10716
|
+
} catch (error) {
|
|
10717
|
+
if (isNotFoundError(error)) return false;
|
|
10718
|
+
throw error;
|
|
10719
|
+
}
|
|
10720
|
+
};
|
|
10721
|
+
const withMergedValue = (stored) => ({
|
|
10722
|
+
...data,
|
|
10723
|
+
value: mergeKvValue(stored.value, data.value)
|
|
10724
|
+
});
|
|
10725
|
+
const mergeLocked = async (trx) => {
|
|
10726
|
+
if (!await tryUpdate(trx, where, { id })) throw notFound();
|
|
10727
|
+
const written = withMergedValue(await readStored(trx));
|
|
10728
|
+
if (!await tryUpdate(trx, where, written)) throw notFound();
|
|
10729
|
+
return written;
|
|
10730
|
+
};
|
|
10731
|
+
if (inTransaction) {
|
|
10732
|
+
return mergeLocked(db);
|
|
10733
|
+
}
|
|
10734
|
+
if (supportsTransactions(db)) {
|
|
10735
|
+
let started = false;
|
|
10736
|
+
try {
|
|
10737
|
+
return await db.transaction(async (trx) => {
|
|
10738
|
+
started = true;
|
|
10739
|
+
return mergeLocked(trx);
|
|
10740
|
+
});
|
|
10741
|
+
} catch (error) {
|
|
10742
|
+
if (started || !isTransactionUnsupportedError(error)) throw error;
|
|
10743
|
+
}
|
|
10744
|
+
}
|
|
10745
|
+
if (!strictUpdateNotFound) {
|
|
10746
|
+
await readStored(db);
|
|
10747
|
+
throw new KvMergeUnsupportedError();
|
|
10748
|
+
}
|
|
10749
|
+
for (let attempt = 0; attempt < KV_MERGE_MAX_ATTEMPTS; attempt += 1) {
|
|
10750
|
+
const stored = await readStored(db);
|
|
10751
|
+
const current = stored.value ?? null;
|
|
10752
|
+
const written = withMergedValue(stored);
|
|
10753
|
+
const updateWhere = [
|
|
10754
|
+
...where,
|
|
10755
|
+
{ field: "value", operator: "eq", value: current }
|
|
10756
|
+
];
|
|
10757
|
+
if (await tryUpdate(db, updateWhere, written)) {
|
|
10758
|
+
return written;
|
|
10759
|
+
}
|
|
10760
|
+
}
|
|
10761
|
+
throw new KvMergeContentionError(id);
|
|
10762
|
+
}
|
|
10763
|
+
function isKvResource(resource) {
|
|
10764
|
+
return resource === KV_RESOURCE_NAME;
|
|
10765
|
+
}
|
|
10766
|
+
async function snapshotKvChange(params) {
|
|
10767
|
+
const { db, id, namespace, op, record } = params;
|
|
10768
|
+
const row = await db.findOne({
|
|
10769
|
+
model: KV_RESOURCE_NAME,
|
|
10770
|
+
where: [{ field: "id", operator: "eq", value: id }],
|
|
10771
|
+
namespace
|
|
10772
|
+
});
|
|
10773
|
+
if (!row) return { op: "delete", record: null };
|
|
10774
|
+
const value = row.value ?? null;
|
|
10775
|
+
if (op === "delete" || !record) return { op: "upsert", record: { id, value } };
|
|
10776
|
+
return { op, record: { ...record, id, value } };
|
|
10777
|
+
}
|
|
10645
10778
|
|
|
10646
10779
|
// src/execution/mutation/share.ts
|
|
10647
10780
|
function getShareableConfig(resolvedCapabilities) {
|
|
@@ -12465,7 +12598,7 @@ function hasShareableCapability(resolvedCapabilities) {
|
|
|
12465
12598
|
(entry) => typeof entry === "object" && entry !== null && "shareable" in entry
|
|
12466
12599
|
);
|
|
12467
12600
|
}
|
|
12468
|
-
async function executeMutationCore(mutation, db, schema, namespace, effectiveMutationId, changeTracking, actorId, logger, multiRegionRuntime) {
|
|
12601
|
+
async function executeMutationCore(mutation, db, schema, namespace, effectiveMutationId, changeTracking, actorId, logger, multiRegionRuntime, inTransaction = false) {
|
|
12469
12602
|
const resolvedCapabilities = resolveCapabilitiesForResource(schema, mutation.resource);
|
|
12470
12603
|
let opResult;
|
|
12471
12604
|
let resolvedRecord;
|
|
@@ -12539,12 +12672,25 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12539
12672
|
actorId
|
|
12540
12673
|
);
|
|
12541
12674
|
const strictUpdateNotFound = db.capabilities?.operations?.strictUpdateNotFound === true;
|
|
12675
|
+
const kvValueMerge = isKvValueMerge(mutation);
|
|
12676
|
+
let kvMergedData;
|
|
12542
12677
|
const mergeResult = await executeSchemaAwareMerge({
|
|
12543
12678
|
schema,
|
|
12544
12679
|
resource: mutation.resource,
|
|
12545
12680
|
id: mutation.id,
|
|
12546
12681
|
delta: mergeData,
|
|
12547
12682
|
update: async () => {
|
|
12683
|
+
if (kvValueMerge) {
|
|
12684
|
+
kvMergedData = await updateKvRecordMerging({
|
|
12685
|
+
db,
|
|
12686
|
+
resource: mutation.resource,
|
|
12687
|
+
id: mutation.id,
|
|
12688
|
+
namespace,
|
|
12689
|
+
data: mergeData,
|
|
12690
|
+
inTransaction
|
|
12691
|
+
});
|
|
12692
|
+
return;
|
|
12693
|
+
}
|
|
12548
12694
|
const updated = await db.update({
|
|
12549
12695
|
model: mutation.resource,
|
|
12550
12696
|
where: [{ field: "id", operator: "eq", value: mutation.id }],
|
|
@@ -12571,7 +12717,7 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12571
12717
|
}
|
|
12572
12718
|
});
|
|
12573
12719
|
if (mergeResult.ok) {
|
|
12574
|
-
resolvedRecord = mergeData;
|
|
12720
|
+
resolvedRecord = mergeResult.mode !== "create" && kvMergedData ? kvMergedData : mergeData;
|
|
12575
12721
|
additionalChanges.push(...await applyInactivePropagation(
|
|
12576
12722
|
db,
|
|
12577
12723
|
schema,
|
|
@@ -12587,11 +12733,19 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12587
12733
|
resource: mutation.resource
|
|
12588
12734
|
});
|
|
12589
12735
|
}
|
|
12590
|
-
opResult = {
|
|
12736
|
+
opResult = isKvMergeUnsupportedError(mergeResult.error) ? {
|
|
12737
|
+
ok: false,
|
|
12738
|
+
code: "DFQL_UNSUPPORTED",
|
|
12739
|
+
message: mergeResult.error.message,
|
|
12740
|
+
path: "record.value"
|
|
12741
|
+
} : {
|
|
12591
12742
|
ok: false,
|
|
12592
12743
|
code: mergeResult.code,
|
|
12593
12744
|
message: mergeResult.message,
|
|
12594
|
-
path: mergeResult.path
|
|
12745
|
+
path: mergeResult.path,
|
|
12746
|
+
// A KV merge that gave up under contention wrote nothing;
|
|
12747
|
+
// the same mutation can succeed when retried.
|
|
12748
|
+
retryable: isKvMergeContentionError(mergeResult.error)
|
|
12595
12749
|
};
|
|
12596
12750
|
}
|
|
12597
12751
|
}
|
|
@@ -13106,7 +13260,12 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
13106
13260
|
ok: false,
|
|
13107
13261
|
mutationId: effectiveMutationId,
|
|
13108
13262
|
affectedIds: [],
|
|
13109
|
-
errors: [{
|
|
13263
|
+
errors: [{
|
|
13264
|
+
code: opResult.code,
|
|
13265
|
+
message: opResult.message,
|
|
13266
|
+
path: opResult.path,
|
|
13267
|
+
retryable: opResult.retryable === true
|
|
13268
|
+
}],
|
|
13110
13269
|
deduped: false
|
|
13111
13270
|
};
|
|
13112
13271
|
if (failedShareCompensation) {
|
|
@@ -13149,8 +13308,21 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
13149
13308
|
primaryChange,
|
|
13150
13309
|
...additionalChanges
|
|
13151
13310
|
];
|
|
13311
|
+
const snapshotPrimaryKvChange = async () => {
|
|
13312
|
+
if (!isKvResource(primaryChange.resource)) return;
|
|
13313
|
+
const snapshot = await snapshotKvChange({
|
|
13314
|
+
db,
|
|
13315
|
+
id: primaryChange.id,
|
|
13316
|
+
namespace,
|
|
13317
|
+
op: primaryChange.op,
|
|
13318
|
+
record: primaryChange.record
|
|
13319
|
+
});
|
|
13320
|
+
primaryChange.op = snapshot.op;
|
|
13321
|
+
primaryChange.record = snapshot.record;
|
|
13322
|
+
};
|
|
13152
13323
|
if (allChanges.length > 1) {
|
|
13153
13324
|
const seqs = await changeTracking.getNextServerSeqBatch(allChanges.length);
|
|
13325
|
+
await snapshotPrimaryKvChange();
|
|
13154
13326
|
await recordChangesWithRetry(
|
|
13155
13327
|
changeTracking,
|
|
13156
13328
|
allChanges.map((change, i) => ({
|
|
@@ -13165,6 +13337,7 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
13165
13337
|
);
|
|
13166
13338
|
} else {
|
|
13167
13339
|
const serverSeq = await changeTracking.getNextServerSeq();
|
|
13340
|
+
await snapshotPrimaryKvChange();
|
|
13168
13341
|
await recordChangeWithRetry(changeTracking, {
|
|
13169
13342
|
serverSeq,
|
|
13170
13343
|
...primaryChange
|
|
@@ -13398,7 +13571,8 @@ async function executeMutation(mutation, schema, db, idempotencyStore, changeTra
|
|
|
13398
13571
|
changeTracking.withDb(txDb),
|
|
13399
13572
|
actorId,
|
|
13400
13573
|
logger,
|
|
13401
|
-
multiRegionRuntime
|
|
13574
|
+
multiRegionRuntime,
|
|
13575
|
+
true
|
|
13402
13576
|
);
|
|
13403
13577
|
if (txMutationResult.ok && shouldQueuePermissionDirectorySync) {
|
|
13404
13578
|
await queuePermissionDirectorySync(txDb);
|
|
@@ -13504,7 +13678,8 @@ async function executeMutation(mutation, schema, db, idempotencyStore, changeTra
|
|
|
13504
13678
|
changeTracking.withDb(txDb),
|
|
13505
13679
|
actorId,
|
|
13506
13680
|
logger,
|
|
13507
|
-
multiRegionRuntime
|
|
13681
|
+
multiRegionRuntime,
|
|
13682
|
+
true
|
|
13508
13683
|
);
|
|
13509
13684
|
if (txMutationResult.ok && shouldQueuePermissionDirectorySync) {
|
|
13510
13685
|
await queuePermissionDirectorySync(txDb);
|
|
@@ -13554,7 +13729,8 @@ async function executeMutation(mutation, schema, db, idempotencyStore, changeTra
|
|
|
13554
13729
|
changeTracking.withDb(db),
|
|
13555
13730
|
actorId,
|
|
13556
13731
|
logger,
|
|
13557
|
-
multiRegionRuntime
|
|
13732
|
+
multiRegionRuntime,
|
|
13733
|
+
insideTransaction
|
|
13558
13734
|
);
|
|
13559
13735
|
} catch (error) {
|
|
13560
13736
|
await releasePermissionDirectoryTask();
|
|
@@ -15587,6 +15763,26 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15587
15763
|
}
|
|
15588
15764
|
return allocation.pool.shift();
|
|
15589
15765
|
}
|
|
15766
|
+
async function sequenceChange(change, allocation, targetDb, targetChangeTracking = changeTracking) {
|
|
15767
|
+
if (!isKvResource(change.resource)) {
|
|
15768
|
+
return {
|
|
15769
|
+
seq: await takeNextServerSeq(allocation, targetChangeTracking),
|
|
15770
|
+
op: change.op,
|
|
15771
|
+
record: change.record
|
|
15772
|
+
};
|
|
15773
|
+
}
|
|
15774
|
+
allocation.initialBatchAllocated = true;
|
|
15775
|
+
allocation.pool = [];
|
|
15776
|
+
const seq = await targetChangeTracking.getNextServerSeq();
|
|
15777
|
+
const snapshot = await snapshotKvChange({
|
|
15778
|
+
db: targetDb,
|
|
15779
|
+
id: change.id,
|
|
15780
|
+
namespace,
|
|
15781
|
+
op: change.op,
|
|
15782
|
+
record: change.record
|
|
15783
|
+
});
|
|
15784
|
+
return { seq, ...snapshot };
|
|
15785
|
+
}
|
|
15590
15786
|
async function collectPropagationChanges(targetDb, seeds) {
|
|
15591
15787
|
return (await applyInactivePropagation(targetDb, schema, seeds, namespace)).map((change) => ({
|
|
15592
15788
|
resource: change.resource,
|
|
@@ -15676,12 +15872,25 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15676
15872
|
actorId
|
|
15677
15873
|
);
|
|
15678
15874
|
const strictUpdateNotFound = targetDb.capabilities?.operations?.strictUpdateNotFound === true;
|
|
15875
|
+
const kvValueMerge = isKvValueMerge(mut);
|
|
15876
|
+
let kvMergedData;
|
|
15679
15877
|
const mergeResult = await executeSchemaAwareMerge({
|
|
15680
15878
|
schema,
|
|
15681
15879
|
resource: mut.resource,
|
|
15682
15880
|
id: mut.id,
|
|
15683
15881
|
delta: createData,
|
|
15684
15882
|
update: async () => {
|
|
15883
|
+
if (kvValueMerge) {
|
|
15884
|
+
kvMergedData = await updateKvRecordMerging({
|
|
15885
|
+
db: targetDb,
|
|
15886
|
+
resource: mut.resource,
|
|
15887
|
+
id: mut.id,
|
|
15888
|
+
namespace,
|
|
15889
|
+
data: updateData,
|
|
15890
|
+
inTransaction: hasTransaction
|
|
15891
|
+
});
|
|
15892
|
+
return;
|
|
15893
|
+
}
|
|
15685
15894
|
const updated = await targetDb.update({ model: mut.resource, where: [{ field: "id", operator: "eq", value: mut.id }], data: updateData, namespace });
|
|
15686
15895
|
if (strictUpdateNotFound && updated === void 0) {
|
|
15687
15896
|
throw { name: "NotFoundError", message: "Record not found after update" };
|
|
@@ -15695,7 +15904,7 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15695
15904
|
}
|
|
15696
15905
|
});
|
|
15697
15906
|
if (mergeResult.ok) {
|
|
15698
|
-
const changeRecordData = mergeResult.mode === "create" ? createData : updateData;
|
|
15907
|
+
const changeRecordData = mergeResult.mode === "create" ? createData : kvMergedData ?? updateData;
|
|
15699
15908
|
const propagationChanges = await collectPropagationChanges(
|
|
15700
15909
|
targetDb,
|
|
15701
15910
|
[{ resource: mut.resource, id: mut.id }]
|
|
@@ -15703,12 +15912,20 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15703
15912
|
return {
|
|
15704
15913
|
ok: true,
|
|
15705
15914
|
changes: [
|
|
15706
|
-
{
|
|
15915
|
+
{
|
|
15916
|
+
resource: mut.resource,
|
|
15917
|
+
id: mut.id,
|
|
15918
|
+
op: "merge",
|
|
15919
|
+
record: { id: mut.id, ...changeRecordData }
|
|
15920
|
+
},
|
|
15707
15921
|
...propagationChanges
|
|
15708
15922
|
]
|
|
15709
15923
|
};
|
|
15710
15924
|
}
|
|
15711
15925
|
if (mergeResult.code === "NOT_FOUND") return { ok: false, code: "NOT_FOUND", message: `Record not found: ${mut.id}`, path: "id" };
|
|
15926
|
+
if (isKvMergeUnsupportedError(mergeResult.error)) {
|
|
15927
|
+
return { ok: false, code: "DFQL_UNSUPPORTED", message: mergeResult.error.message, path: "record.value" };
|
|
15928
|
+
}
|
|
15712
15929
|
logger?.error("Push merge failed", { error: String(mergeResult.error), operation: mut.operation, resource: mut.resource, id: mut.id });
|
|
15713
15930
|
return { ok: false, code: "INTERNAL", message: "Internal error", path: "$" };
|
|
15714
15931
|
}
|
|
@@ -16296,7 +16513,12 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
16296
16513
|
const changes = opResult.changes;
|
|
16297
16514
|
const txChangeTracking = changeTracking.withDb(txDb, false);
|
|
16298
16515
|
for (const change of changes) {
|
|
16299
|
-
const seq = await
|
|
16516
|
+
const { seq, op, record } = await sequenceChange(
|
|
16517
|
+
change,
|
|
16518
|
+
txSequenceAllocation,
|
|
16519
|
+
txDb,
|
|
16520
|
+
txChangeTracking
|
|
16521
|
+
);
|
|
16300
16522
|
mutationLatestSeq = Math.max(mutationLatestSeq, seq);
|
|
16301
16523
|
mutationResourceSeqs[change.resource] = Math.max(
|
|
16302
16524
|
mutationResourceSeqs[change.resource] || 0,
|
|
@@ -16306,8 +16528,8 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
16306
16528
|
serverSeq: seq,
|
|
16307
16529
|
resource: change.resource,
|
|
16308
16530
|
id: change.id,
|
|
16309
|
-
op
|
|
16310
|
-
record
|
|
16531
|
+
op,
|
|
16532
|
+
record
|
|
16311
16533
|
}, 2, logger);
|
|
16312
16534
|
pendingChangeEvents.push(seq);
|
|
16313
16535
|
}
|
|
@@ -16386,15 +16608,15 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
16386
16608
|
let changeTrackingFailed = false;
|
|
16387
16609
|
try {
|
|
16388
16610
|
for (const change of changes) {
|
|
16389
|
-
const seq = await
|
|
16611
|
+
const { seq, op, record } = await sequenceChange(change, sequenceAllocation, db);
|
|
16390
16612
|
latestSeq = Math.max(latestSeq, seq);
|
|
16391
16613
|
resourceSeqs[change.resource] = Math.max(resourceSeqs[change.resource] || 0, seq);
|
|
16392
16614
|
await recordChangeWithRetry(changeTracking, {
|
|
16393
16615
|
serverSeq: seq,
|
|
16394
16616
|
resource: change.resource,
|
|
16395
16617
|
id: change.id,
|
|
16396
|
-
op
|
|
16397
|
-
record
|
|
16618
|
+
op,
|
|
16619
|
+
record
|
|
16398
16620
|
}, 2, logger);
|
|
16399
16621
|
}
|
|
16400
16622
|
} catch (error) {
|
|
@@ -17398,7 +17620,7 @@ function analyzeFilterComplexity(node, depth = 1) {
|
|
|
17398
17620
|
}
|
|
17399
17621
|
return { depth: maxDepth, conditions };
|
|
17400
17622
|
}
|
|
17401
|
-
function
|
|
17623
|
+
function isPlainObject2(value) {
|
|
17402
17624
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
17403
17625
|
return false;
|
|
17404
17626
|
}
|
|
@@ -17406,9 +17628,9 @@ function isPlainObject(value) {
|
|
|
17406
17628
|
return proto === Object.prototype || proto === null;
|
|
17407
17629
|
}
|
|
17408
17630
|
async function normalizeSearchTemporalFilters(input) {
|
|
17409
|
-
const temporalByResource =
|
|
17631
|
+
const temporalByResource = isPlainObject2(input.raw.temporalByResource) ? input.raw.temporalByResource : void 0;
|
|
17410
17632
|
const temporal = input.raw.temporal;
|
|
17411
|
-
const baseFilters =
|
|
17633
|
+
const baseFilters = isPlainObject2(input.raw.filters) ? input.raw.filters : {};
|
|
17412
17634
|
if (!temporalByResource && !temporal) {
|
|
17413
17635
|
return Object.keys(baseFilters).length > 0 ? baseFilters : void 0;
|
|
17414
17636
|
}
|
|
@@ -17446,7 +17668,7 @@ async function normalizeSearchTemporalFilters(input) {
|
|
|
17446
17668
|
},
|
|
17447
17669
|
temporalConfig
|
|
17448
17670
|
);
|
|
17449
|
-
if (
|
|
17671
|
+
if (isPlainObject2(normalized.filters)) {
|
|
17450
17672
|
normalizedFilters[resource] = normalized.filters;
|
|
17451
17673
|
}
|
|
17452
17674
|
}
|
|
@@ -17569,7 +17791,7 @@ function createSearchHandler(validatedSchema, extractNamespace, extractActorId,
|
|
|
17569
17791
|
});
|
|
17570
17792
|
}
|
|
17571
17793
|
if (raw.fieldBoosts !== void 0) {
|
|
17572
|
-
if (!
|
|
17794
|
+
if (!isPlainObject2(raw.fieldBoosts)) {
|
|
17573
17795
|
return errorResponse({
|
|
17574
17796
|
code: "DFQL_INVALID",
|
|
17575
17797
|
message: "Invalid request: fieldBoosts must be an object",
|