@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/dist/index.js CHANGED
@@ -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
  /**
@@ -13756,6 +13802,16 @@ async function executeTransaction(request, schema, db, idempotencyStore, limits,
13756
13802
  const steps = request.steps;
13757
13803
  const isAtomic = request.atomic !== false;
13758
13804
  const hasMutations = steps.some((step) => Boolean(step.mutation));
13805
+ if (isAtomic && hasMutations && db.capabilities?.transactions?.supported === false) {
13806
+ return {
13807
+ ok: false,
13808
+ error: {
13809
+ code: "DFQL_UNSUPPORTED",
13810
+ message: "Atomic transact with mutations requires database transactions, which this adapter does not support; send atomic: false to apply steps one at a time",
13811
+ details: { path: "atomic" }
13812
+ }
13813
+ };
13814
+ }
13759
13815
  const multiRegionRuntime = getDatafnMultiRegionRuntimeConfig(plugins);
13760
13816
  const needsPermissionDirectoryOutbox = Boolean(
13761
13817
  hasMutations && multiRegionRuntime && steps.some(
@@ -13878,7 +13934,7 @@ async function executeTransaction(request, schema, db, idempotencyStore, limits,
13878
13934
  }
13879
13935
  return null;
13880
13936
  };
13881
- if (isAtomic && typeof db.transaction === "function") {
13937
+ if (isAtomic && db.capabilities?.transactions?.supported !== false && typeof db.transaction === "function") {
13882
13938
  const deferredPermissionDirectorySyncs = [];
13883
13939
  const reconcilePermissionDirectoryAfterSettlement = async () => {
13884
13940
  await drainPrequeuedUnshareTasks();