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