@datafn/server 0.2.0 → 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/README.md +2 -0
- package/dist/index.cjs +302 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +303 -25
- package/dist/index.js.map +1 -1
- package/package.json +17 -9
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';
|
|
@@ -8539,8 +8539,9 @@ async function recordChangesWithRetry(changeTracking, entries, maxRetries = 2, l
|
|
|
8539
8539
|
}
|
|
8540
8540
|
}
|
|
8541
8541
|
}
|
|
8542
|
+
var preparedNamespaces = /* @__PURE__ */ new WeakMap();
|
|
8542
8543
|
var ChangeTrackingService = class _ChangeTrackingService {
|
|
8543
|
-
constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false) {
|
|
8544
|
+
constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false, namespaceMetaEnsured = false) {
|
|
8544
8545
|
this.db = db;
|
|
8545
8546
|
this.namespace = namespace;
|
|
8546
8547
|
this.sequenceStore = sequenceStore;
|
|
@@ -8548,8 +8549,10 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8548
8549
|
this.logger = logger;
|
|
8549
8550
|
this.metaEnsured = false;
|
|
8550
8551
|
this.changesEnsured = false;
|
|
8552
|
+
this.namespaceMetaEnsured = false;
|
|
8551
8553
|
this.metaEnsured = metaEnsured;
|
|
8552
8554
|
this.changesEnsured = changesEnsured;
|
|
8555
|
+
this.namespaceMetaEnsured = namespaceMetaEnsured;
|
|
8553
8556
|
}
|
|
8554
8557
|
/** Initialize every durable table before an enclosing transaction begins. */
|
|
8555
8558
|
async ensureReady() {
|
|
@@ -8564,6 +8567,48 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8564
8567
|
await ensureInternalTable(this.db, "__datafn_meta");
|
|
8565
8568
|
this.metaEnsured = true;
|
|
8566
8569
|
}
|
|
8570
|
+
await this.ensureNamespaceMeta();
|
|
8571
|
+
}
|
|
8572
|
+
/**
|
|
8573
|
+
* Creates this namespace's `__datafn_meta` row before an enclosing
|
|
8574
|
+
* transaction begins. Sequence allocation otherwise creates the row inside
|
|
8575
|
+
* the transaction on a namespace's first write, and when two first writes
|
|
8576
|
+
* race, the losing INSERT fails with a unique-key conflict. On PostgreSQL that
|
|
8577
|
+
* aborts the whole transaction, so the recovery read that follows fails with
|
|
8578
|
+
* "current transaction is aborted". Outside a transaction the conflict is
|
|
8579
|
+
* harmless: the row exists either way, and later allocations only read and
|
|
8580
|
+
* CAS-update it. Transaction-bound instances (from `withDb`) never prepare
|
|
8581
|
+
* the row; the base instance does so before the transaction opens.
|
|
8582
|
+
*/
|
|
8583
|
+
async ensureNamespaceMeta() {
|
|
8584
|
+
if (this.namespaceMetaEnsured) {
|
|
8585
|
+
return;
|
|
8586
|
+
}
|
|
8587
|
+
let prepared = preparedNamespaces.get(this.db);
|
|
8588
|
+
if (!prepared) {
|
|
8589
|
+
prepared = /* @__PURE__ */ new Set();
|
|
8590
|
+
preparedNamespaces.set(this.db, prepared);
|
|
8591
|
+
}
|
|
8592
|
+
if (prepared.has(this.namespace)) {
|
|
8593
|
+
this.namespaceMetaEnsured = true;
|
|
8594
|
+
return;
|
|
8595
|
+
}
|
|
8596
|
+
const where = [{ field: "namespace", op: "eq", value: this.namespace }];
|
|
8597
|
+
if (!await this.db.internal.findOne("__datafn_meta", where)) {
|
|
8598
|
+
try {
|
|
8599
|
+
await this.db.internal.create("__datafn_meta", {
|
|
8600
|
+
id: `meta:${this.namespace}`,
|
|
8601
|
+
namespace: this.namespace,
|
|
8602
|
+
next_server_seq: 1
|
|
8603
|
+
});
|
|
8604
|
+
} catch (error) {
|
|
8605
|
+
if (!await this.db.internal.findOne("__datafn_meta", where)) {
|
|
8606
|
+
throw error;
|
|
8607
|
+
}
|
|
8608
|
+
}
|
|
8609
|
+
}
|
|
8610
|
+
prepared.add(this.namespace);
|
|
8611
|
+
this.namespaceMetaEnsured = true;
|
|
8567
8612
|
}
|
|
8568
8613
|
/**
|
|
8569
8614
|
* REL-011: Create a new ChangeTrackingService with a different db adapter.
|
|
@@ -8579,7 +8624,8 @@ var ChangeTrackingService = class _ChangeTrackingService {
|
|
|
8579
8624
|
emitChanges ? this.onChange : void 0,
|
|
8580
8625
|
this.logger,
|
|
8581
8626
|
this.metaEnsured,
|
|
8582
|
-
this.changesEnsured
|
|
8627
|
+
this.changesEnsured,
|
|
8628
|
+
true
|
|
8583
8629
|
);
|
|
8584
8630
|
}
|
|
8585
8631
|
/**
|
|
@@ -10503,6 +10549,9 @@ function isRecord(value) {
|
|
|
10503
10549
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10504
10550
|
}
|
|
10505
10551
|
function isNotFoundError(error) {
|
|
10552
|
+
if (error?.name === "KvMergeContentionError" || error?.name === "KvMergeUnsupportedError") {
|
|
10553
|
+
return false;
|
|
10554
|
+
}
|
|
10506
10555
|
const message = String(error?.message || "").toLowerCase();
|
|
10507
10556
|
return error?.name === "NotFoundError" || message.includes("not found") || message.includes("zero rows");
|
|
10508
10557
|
}
|
|
@@ -10596,6 +10645,136 @@ async function executeSchemaAwareMerge(params) {
|
|
|
10596
10645
|
return { ok: false, code: "INTERNAL", message: "Internal error", path: "$", error: retryError };
|
|
10597
10646
|
}
|
|
10598
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
|
+
}
|
|
10599
10778
|
|
|
10600
10779
|
// src/execution/mutation/share.ts
|
|
10601
10780
|
function getShareableConfig(resolvedCapabilities) {
|
|
@@ -12419,7 +12598,7 @@ function hasShareableCapability(resolvedCapabilities) {
|
|
|
12419
12598
|
(entry) => typeof entry === "object" && entry !== null && "shareable" in entry
|
|
12420
12599
|
);
|
|
12421
12600
|
}
|
|
12422
|
-
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) {
|
|
12423
12602
|
const resolvedCapabilities = resolveCapabilitiesForResource(schema, mutation.resource);
|
|
12424
12603
|
let opResult;
|
|
12425
12604
|
let resolvedRecord;
|
|
@@ -12493,12 +12672,25 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12493
12672
|
actorId
|
|
12494
12673
|
);
|
|
12495
12674
|
const strictUpdateNotFound = db.capabilities?.operations?.strictUpdateNotFound === true;
|
|
12675
|
+
const kvValueMerge = isKvValueMerge(mutation);
|
|
12676
|
+
let kvMergedData;
|
|
12496
12677
|
const mergeResult = await executeSchemaAwareMerge({
|
|
12497
12678
|
schema,
|
|
12498
12679
|
resource: mutation.resource,
|
|
12499
12680
|
id: mutation.id,
|
|
12500
12681
|
delta: mergeData,
|
|
12501
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
|
+
}
|
|
12502
12694
|
const updated = await db.update({
|
|
12503
12695
|
model: mutation.resource,
|
|
12504
12696
|
where: [{ field: "id", operator: "eq", value: mutation.id }],
|
|
@@ -12525,7 +12717,7 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12525
12717
|
}
|
|
12526
12718
|
});
|
|
12527
12719
|
if (mergeResult.ok) {
|
|
12528
|
-
resolvedRecord = mergeData;
|
|
12720
|
+
resolvedRecord = mergeResult.mode !== "create" && kvMergedData ? kvMergedData : mergeData;
|
|
12529
12721
|
additionalChanges.push(...await applyInactivePropagation(
|
|
12530
12722
|
db,
|
|
12531
12723
|
schema,
|
|
@@ -12541,11 +12733,19 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
12541
12733
|
resource: mutation.resource
|
|
12542
12734
|
});
|
|
12543
12735
|
}
|
|
12544
|
-
opResult = {
|
|
12736
|
+
opResult = isKvMergeUnsupportedError(mergeResult.error) ? {
|
|
12737
|
+
ok: false,
|
|
12738
|
+
code: "DFQL_UNSUPPORTED",
|
|
12739
|
+
message: mergeResult.error.message,
|
|
12740
|
+
path: "record.value"
|
|
12741
|
+
} : {
|
|
12545
12742
|
ok: false,
|
|
12546
12743
|
code: mergeResult.code,
|
|
12547
12744
|
message: mergeResult.message,
|
|
12548
|
-
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)
|
|
12549
12749
|
};
|
|
12550
12750
|
}
|
|
12551
12751
|
}
|
|
@@ -13060,7 +13260,12 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
13060
13260
|
ok: false,
|
|
13061
13261
|
mutationId: effectiveMutationId,
|
|
13062
13262
|
affectedIds: [],
|
|
13063
|
-
errors: [{
|
|
13263
|
+
errors: [{
|
|
13264
|
+
code: opResult.code,
|
|
13265
|
+
message: opResult.message,
|
|
13266
|
+
path: opResult.path,
|
|
13267
|
+
retryable: opResult.retryable === true
|
|
13268
|
+
}],
|
|
13064
13269
|
deduped: false
|
|
13065
13270
|
};
|
|
13066
13271
|
if (failedShareCompensation) {
|
|
@@ -13103,8 +13308,21 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
13103
13308
|
primaryChange,
|
|
13104
13309
|
...additionalChanges
|
|
13105
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
|
+
};
|
|
13106
13323
|
if (allChanges.length > 1) {
|
|
13107
13324
|
const seqs = await changeTracking.getNextServerSeqBatch(allChanges.length);
|
|
13325
|
+
await snapshotPrimaryKvChange();
|
|
13108
13326
|
await recordChangesWithRetry(
|
|
13109
13327
|
changeTracking,
|
|
13110
13328
|
allChanges.map((change, i) => ({
|
|
@@ -13119,6 +13337,7 @@ async function executeMutationCore(mutation, db, schema, namespace, effectiveMut
|
|
|
13119
13337
|
);
|
|
13120
13338
|
} else {
|
|
13121
13339
|
const serverSeq = await changeTracking.getNextServerSeq();
|
|
13340
|
+
await snapshotPrimaryKvChange();
|
|
13122
13341
|
await recordChangeWithRetry(changeTracking, {
|
|
13123
13342
|
serverSeq,
|
|
13124
13343
|
...primaryChange
|
|
@@ -13352,7 +13571,8 @@ async function executeMutation(mutation, schema, db, idempotencyStore, changeTra
|
|
|
13352
13571
|
changeTracking.withDb(txDb),
|
|
13353
13572
|
actorId,
|
|
13354
13573
|
logger,
|
|
13355
|
-
multiRegionRuntime
|
|
13574
|
+
multiRegionRuntime,
|
|
13575
|
+
true
|
|
13356
13576
|
);
|
|
13357
13577
|
if (txMutationResult.ok && shouldQueuePermissionDirectorySync) {
|
|
13358
13578
|
await queuePermissionDirectorySync(txDb);
|
|
@@ -13458,7 +13678,8 @@ async function executeMutation(mutation, schema, db, idempotencyStore, changeTra
|
|
|
13458
13678
|
changeTracking.withDb(txDb),
|
|
13459
13679
|
actorId,
|
|
13460
13680
|
logger,
|
|
13461
|
-
multiRegionRuntime
|
|
13681
|
+
multiRegionRuntime,
|
|
13682
|
+
true
|
|
13462
13683
|
);
|
|
13463
13684
|
if (txMutationResult.ok && shouldQueuePermissionDirectorySync) {
|
|
13464
13685
|
await queuePermissionDirectorySync(txDb);
|
|
@@ -13508,7 +13729,8 @@ async function executeMutation(mutation, schema, db, idempotencyStore, changeTra
|
|
|
13508
13729
|
changeTracking.withDb(db),
|
|
13509
13730
|
actorId,
|
|
13510
13731
|
logger,
|
|
13511
|
-
multiRegionRuntime
|
|
13732
|
+
multiRegionRuntime,
|
|
13733
|
+
insideTransaction
|
|
13512
13734
|
);
|
|
13513
13735
|
} catch (error) {
|
|
13514
13736
|
await releasePermissionDirectoryTask();
|
|
@@ -13756,6 +13978,16 @@ async function executeTransaction(request, schema, db, idempotencyStore, limits,
|
|
|
13756
13978
|
const steps = request.steps;
|
|
13757
13979
|
const isAtomic = request.atomic !== false;
|
|
13758
13980
|
const hasMutations = steps.some((step) => Boolean(step.mutation));
|
|
13981
|
+
if (isAtomic && hasMutations && db.capabilities?.transactions?.supported === false) {
|
|
13982
|
+
return {
|
|
13983
|
+
ok: false,
|
|
13984
|
+
error: {
|
|
13985
|
+
code: "DFQL_UNSUPPORTED",
|
|
13986
|
+
message: "Atomic transact with mutations requires database transactions, which this adapter does not support; send atomic: false to apply steps one at a time",
|
|
13987
|
+
details: { path: "atomic" }
|
|
13988
|
+
}
|
|
13989
|
+
};
|
|
13990
|
+
}
|
|
13759
13991
|
const multiRegionRuntime = getDatafnMultiRegionRuntimeConfig(plugins);
|
|
13760
13992
|
const needsPermissionDirectoryOutbox = Boolean(
|
|
13761
13993
|
hasMutations && multiRegionRuntime && steps.some(
|
|
@@ -13878,7 +14110,7 @@ async function executeTransaction(request, schema, db, idempotencyStore, limits,
|
|
|
13878
14110
|
}
|
|
13879
14111
|
return null;
|
|
13880
14112
|
};
|
|
13881
|
-
if (isAtomic && typeof db.transaction === "function") {
|
|
14113
|
+
if (isAtomic && db.capabilities?.transactions?.supported !== false && typeof db.transaction === "function") {
|
|
13882
14114
|
const deferredPermissionDirectorySyncs = [];
|
|
13883
14115
|
const reconcilePermissionDirectoryAfterSettlement = async () => {
|
|
13884
14116
|
await drainPrequeuedUnshareTasks();
|
|
@@ -15531,6 +15763,26 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15531
15763
|
}
|
|
15532
15764
|
return allocation.pool.shift();
|
|
15533
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
|
+
}
|
|
15534
15786
|
async function collectPropagationChanges(targetDb, seeds) {
|
|
15535
15787
|
return (await applyInactivePropagation(targetDb, schema, seeds, namespace)).map((change) => ({
|
|
15536
15788
|
resource: change.resource,
|
|
@@ -15620,12 +15872,25 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15620
15872
|
actorId
|
|
15621
15873
|
);
|
|
15622
15874
|
const strictUpdateNotFound = targetDb.capabilities?.operations?.strictUpdateNotFound === true;
|
|
15875
|
+
const kvValueMerge = isKvValueMerge(mut);
|
|
15876
|
+
let kvMergedData;
|
|
15623
15877
|
const mergeResult = await executeSchemaAwareMerge({
|
|
15624
15878
|
schema,
|
|
15625
15879
|
resource: mut.resource,
|
|
15626
15880
|
id: mut.id,
|
|
15627
15881
|
delta: createData,
|
|
15628
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
|
+
}
|
|
15629
15894
|
const updated = await targetDb.update({ model: mut.resource, where: [{ field: "id", operator: "eq", value: mut.id }], data: updateData, namespace });
|
|
15630
15895
|
if (strictUpdateNotFound && updated === void 0) {
|
|
15631
15896
|
throw { name: "NotFoundError", message: "Record not found after update" };
|
|
@@ -15639,7 +15904,7 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15639
15904
|
}
|
|
15640
15905
|
});
|
|
15641
15906
|
if (mergeResult.ok) {
|
|
15642
|
-
const changeRecordData = mergeResult.mode === "create" ? createData : updateData;
|
|
15907
|
+
const changeRecordData = mergeResult.mode === "create" ? createData : kvMergedData ?? updateData;
|
|
15643
15908
|
const propagationChanges = await collectPropagationChanges(
|
|
15644
15909
|
targetDb,
|
|
15645
15910
|
[{ resource: mut.resource, id: mut.id }]
|
|
@@ -15647,12 +15912,20 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
15647
15912
|
return {
|
|
15648
15913
|
ok: true,
|
|
15649
15914
|
changes: [
|
|
15650
|
-
{
|
|
15915
|
+
{
|
|
15916
|
+
resource: mut.resource,
|
|
15917
|
+
id: mut.id,
|
|
15918
|
+
op: "merge",
|
|
15919
|
+
record: { id: mut.id, ...changeRecordData }
|
|
15920
|
+
},
|
|
15651
15921
|
...propagationChanges
|
|
15652
15922
|
]
|
|
15653
15923
|
};
|
|
15654
15924
|
}
|
|
15655
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
|
+
}
|
|
15656
15929
|
logger?.error("Push merge failed", { error: String(mergeResult.error), operation: mut.operation, resource: mut.resource, id: mut.id });
|
|
15657
15930
|
return { ok: false, code: "INTERNAL", message: "Internal error", path: "$" };
|
|
15658
15931
|
}
|
|
@@ -16240,7 +16513,12 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
16240
16513
|
const changes = opResult.changes;
|
|
16241
16514
|
const txChangeTracking = changeTracking.withDb(txDb, false);
|
|
16242
16515
|
for (const change of changes) {
|
|
16243
|
-
const seq = await
|
|
16516
|
+
const { seq, op, record } = await sequenceChange(
|
|
16517
|
+
change,
|
|
16518
|
+
txSequenceAllocation,
|
|
16519
|
+
txDb,
|
|
16520
|
+
txChangeTracking
|
|
16521
|
+
);
|
|
16244
16522
|
mutationLatestSeq = Math.max(mutationLatestSeq, seq);
|
|
16245
16523
|
mutationResourceSeqs[change.resource] = Math.max(
|
|
16246
16524
|
mutationResourceSeqs[change.resource] || 0,
|
|
@@ -16250,8 +16528,8 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
16250
16528
|
serverSeq: seq,
|
|
16251
16529
|
resource: change.resource,
|
|
16252
16530
|
id: change.id,
|
|
16253
|
-
op
|
|
16254
|
-
record
|
|
16531
|
+
op,
|
|
16532
|
+
record
|
|
16255
16533
|
}, 2, logger);
|
|
16256
16534
|
pendingChangeEvents.push(seq);
|
|
16257
16535
|
}
|
|
@@ -16330,15 +16608,15 @@ async function executePush(request, schema, db, idempotencyStore, namespace, seq
|
|
|
16330
16608
|
let changeTrackingFailed = false;
|
|
16331
16609
|
try {
|
|
16332
16610
|
for (const change of changes) {
|
|
16333
|
-
const seq = await
|
|
16611
|
+
const { seq, op, record } = await sequenceChange(change, sequenceAllocation, db);
|
|
16334
16612
|
latestSeq = Math.max(latestSeq, seq);
|
|
16335
16613
|
resourceSeqs[change.resource] = Math.max(resourceSeqs[change.resource] || 0, seq);
|
|
16336
16614
|
await recordChangeWithRetry(changeTracking, {
|
|
16337
16615
|
serverSeq: seq,
|
|
16338
16616
|
resource: change.resource,
|
|
16339
16617
|
id: change.id,
|
|
16340
|
-
op
|
|
16341
|
-
record
|
|
16618
|
+
op,
|
|
16619
|
+
record
|
|
16342
16620
|
}, 2, logger);
|
|
16343
16621
|
}
|
|
16344
16622
|
} catch (error) {
|
|
@@ -17342,7 +17620,7 @@ function analyzeFilterComplexity(node, depth = 1) {
|
|
|
17342
17620
|
}
|
|
17343
17621
|
return { depth: maxDepth, conditions };
|
|
17344
17622
|
}
|
|
17345
|
-
function
|
|
17623
|
+
function isPlainObject2(value) {
|
|
17346
17624
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
17347
17625
|
return false;
|
|
17348
17626
|
}
|
|
@@ -17350,9 +17628,9 @@ function isPlainObject(value) {
|
|
|
17350
17628
|
return proto === Object.prototype || proto === null;
|
|
17351
17629
|
}
|
|
17352
17630
|
async function normalizeSearchTemporalFilters(input) {
|
|
17353
|
-
const temporalByResource =
|
|
17631
|
+
const temporalByResource = isPlainObject2(input.raw.temporalByResource) ? input.raw.temporalByResource : void 0;
|
|
17354
17632
|
const temporal = input.raw.temporal;
|
|
17355
|
-
const baseFilters =
|
|
17633
|
+
const baseFilters = isPlainObject2(input.raw.filters) ? input.raw.filters : {};
|
|
17356
17634
|
if (!temporalByResource && !temporal) {
|
|
17357
17635
|
return Object.keys(baseFilters).length > 0 ? baseFilters : void 0;
|
|
17358
17636
|
}
|
|
@@ -17390,7 +17668,7 @@ async function normalizeSearchTemporalFilters(input) {
|
|
|
17390
17668
|
},
|
|
17391
17669
|
temporalConfig
|
|
17392
17670
|
);
|
|
17393
|
-
if (
|
|
17671
|
+
if (isPlainObject2(normalized.filters)) {
|
|
17394
17672
|
normalizedFilters[resource] = normalized.filters;
|
|
17395
17673
|
}
|
|
17396
17674
|
}
|
|
@@ -17513,7 +17791,7 @@ function createSearchHandler(validatedSchema, extractNamespace, extractActorId,
|
|
|
17513
17791
|
});
|
|
17514
17792
|
}
|
|
17515
17793
|
if (raw.fieldBoosts !== void 0) {
|
|
17516
|
-
if (!
|
|
17794
|
+
if (!isPlainObject2(raw.fieldBoosts)) {
|
|
17517
17795
|
return errorResponse({
|
|
17518
17796
|
code: "DFQL_INVALID",
|
|
17519
17797
|
message: "Invalid request: fieldBoosts must be an object",
|