@datafn/server 0.2.0 → 0.2.1

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 CHANGED
@@ -435,6 +435,8 @@ The mutation is rejected with `CONFLICT` if the guard condition is not met.
435
435
 
436
436
  Atomic multi-step transactions mixing queries and mutations.
437
437
 
438
+ Atomic execution needs database transactions. When the adapter reports `capabilities.transactions.supported === false` (for example the Drizzle adapter on SQLite or Cloudflare D1), an atomic transact with any mutation step returns HTTP 400 with `DFQL_UNSUPPORTED` (`details.path: "atomic"`) before anything is written. Send `"atomic": false` to apply steps one at a time.
439
+
438
440
  **Request:**
439
441
 
440
442
  ```json
package/dist/index.cjs CHANGED
@@ -8540,8 +8540,9 @@ async function recordChangesWithRetry(changeTracking, entries, maxRetries = 2, l
8540
8540
  }
8541
8541
  }
8542
8542
  }
8543
+ var preparedNamespaces = /* @__PURE__ */ new WeakMap();
8543
8544
  var ChangeTrackingService = class _ChangeTrackingService {
8544
- constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false) {
8545
+ constructor(db, namespace = "datafn", sequenceStore, onChange, logger, metaEnsured = false, changesEnsured = false, namespaceMetaEnsured = false) {
8545
8546
  this.db = db;
8546
8547
  this.namespace = namespace;
8547
8548
  this.sequenceStore = sequenceStore;
@@ -8549,8 +8550,10 @@ var ChangeTrackingService = class _ChangeTrackingService {
8549
8550
  this.logger = logger;
8550
8551
  this.metaEnsured = false;
8551
8552
  this.changesEnsured = false;
8553
+ this.namespaceMetaEnsured = false;
8552
8554
  this.metaEnsured = metaEnsured;
8553
8555
  this.changesEnsured = changesEnsured;
8556
+ this.namespaceMetaEnsured = namespaceMetaEnsured;
8554
8557
  }
8555
8558
  /** Initialize every durable table before an enclosing transaction begins. */
8556
8559
  async ensureReady() {
@@ -8565,6 +8568,48 @@ var ChangeTrackingService = class _ChangeTrackingService {
8565
8568
  await ensureInternalTable(this.db, "__datafn_meta");
8566
8569
  this.metaEnsured = true;
8567
8570
  }
8571
+ await this.ensureNamespaceMeta();
8572
+ }
8573
+ /**
8574
+ * Creates this namespace's `__datafn_meta` row before an enclosing
8575
+ * transaction begins. Sequence allocation otherwise creates the row inside
8576
+ * the transaction on a namespace's first write, and when two first writes
8577
+ * race, the losing INSERT fails with a unique-key conflict. On PostgreSQL that
8578
+ * aborts the whole transaction, so the recovery read that follows fails with
8579
+ * "current transaction is aborted". Outside a transaction the conflict is
8580
+ * harmless: the row exists either way, and later allocations only read and
8581
+ * CAS-update it. Transaction-bound instances (from `withDb`) never prepare
8582
+ * the row; the base instance does so before the transaction opens.
8583
+ */
8584
+ async ensureNamespaceMeta() {
8585
+ if (this.namespaceMetaEnsured) {
8586
+ return;
8587
+ }
8588
+ let prepared = preparedNamespaces.get(this.db);
8589
+ if (!prepared) {
8590
+ prepared = /* @__PURE__ */ new Set();
8591
+ preparedNamespaces.set(this.db, prepared);
8592
+ }
8593
+ if (prepared.has(this.namespace)) {
8594
+ this.namespaceMetaEnsured = true;
8595
+ return;
8596
+ }
8597
+ const where = [{ field: "namespace", op: "eq", value: this.namespace }];
8598
+ if (!await this.db.internal.findOne("__datafn_meta", where)) {
8599
+ try {
8600
+ await this.db.internal.create("__datafn_meta", {
8601
+ id: `meta:${this.namespace}`,
8602
+ namespace: this.namespace,
8603
+ next_server_seq: 1
8604
+ });
8605
+ } catch (error) {
8606
+ if (!await this.db.internal.findOne("__datafn_meta", where)) {
8607
+ throw error;
8608
+ }
8609
+ }
8610
+ }
8611
+ prepared.add(this.namespace);
8612
+ this.namespaceMetaEnsured = true;
8568
8613
  }
8569
8614
  /**
8570
8615
  * REL-011: Create a new ChangeTrackingService with a different db adapter.
@@ -8580,7 +8625,8 @@ var ChangeTrackingService = class _ChangeTrackingService {
8580
8625
  emitChanges ? this.onChange : void 0,
8581
8626
  this.logger,
8582
8627
  this.metaEnsured,
8583
- this.changesEnsured
8628
+ this.changesEnsured,
8629
+ true
8584
8630
  );
8585
8631
  }
8586
8632
  /**
@@ -13757,6 +13803,16 @@ async function executeTransaction(request, schema, db, idempotencyStore, limits,
13757
13803
  const steps = request.steps;
13758
13804
  const isAtomic = request.atomic !== false;
13759
13805
  const hasMutations = steps.some((step) => Boolean(step.mutation));
13806
+ if (isAtomic && hasMutations && db.capabilities?.transactions?.supported === false) {
13807
+ return {
13808
+ ok: false,
13809
+ error: {
13810
+ code: "DFQL_UNSUPPORTED",
13811
+ message: "Atomic transact with mutations requires database transactions, which this adapter does not support; send atomic: false to apply steps one at a time",
13812
+ details: { path: "atomic" }
13813
+ }
13814
+ };
13815
+ }
13760
13816
  const multiRegionRuntime = getDatafnMultiRegionRuntimeConfig(plugins);
13761
13817
  const needsPermissionDirectoryOutbox = Boolean(
13762
13818
  hasMutations && multiRegionRuntime && steps.some(
@@ -13879,7 +13935,7 @@ async function executeTransaction(request, schema, db, idempotencyStore, limits,
13879
13935
  }
13880
13936
  return null;
13881
13937
  };
13882
- if (isAtomic && typeof db.transaction === "function") {
13938
+ if (isAtomic && db.capabilities?.transactions?.supported !== false && typeof db.transaction === "function") {
13883
13939
  const deferredPermissionDirectorySyncs = [];
13884
13940
  const reconcilePermissionDirectoryAfterSettlement = async () => {
13885
13941
  await drainPrequeuedUnshareTasks();