@getstrata/core 2.2.9 → 2.2.11

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.
@@ -129,6 +129,9 @@ function unwrapTransactionRollback(error, depth = 0) {
129
129
  }
130
130
  return unwrapTransactionRollback(error.cause, depth + 1);
131
131
  }
132
+ function hasActiveTransaction() {
133
+ return hasActiveDatabaseConnection() && (transactionScopes.getStore()?.rollback.length ?? 0) > 0;
134
+ }
132
135
  function requestTransactionRollback() {
133
136
  const state = transactionScopes.getStore();
134
137
  const index = state ? state.rollback.length - 1 : -1;
@@ -344,6 +347,55 @@ var init_deferredModelEvents = __esm(() => {
344
347
  deferredModelEvents = createAsyncContextStore("@getstrata/deferredModelEvents");
345
348
  });
346
349
 
350
+ // ../../src/core/database/acquisitionMetrics.ts
351
+ async function observeTransactionAcquisition(metrics, acquire, signal) {
352
+ const state = states.get(metrics);
353
+ if (!state)
354
+ throw new TypeError("Transaction acquisition metrics must be created by the framework factory.");
355
+ const started = performance.now();
356
+ state.inflight++;
357
+ let outcome = "acquired";
358
+ try {
359
+ return await acquire();
360
+ } catch (error) {
361
+ outcome = signal?.aborted ? "aborted" : "failed";
362
+ throw error;
363
+ } finally {
364
+ state.inflight--;
365
+ const elapsed = Math.max(0, performance.now() - started) / 1000;
366
+ const histogram = state.outcomes[outcomes.indexOf(outcome)];
367
+ if (histogram) {
368
+ histogram.count++;
369
+ histogram.sumSeconds += elapsed;
370
+ for (let index = 0;index < ACQUISITION_BUCKETS_SECONDS.length; index++) {
371
+ const bound = ACQUISITION_BUCKETS_SECONDS[index];
372
+ if (bound !== undefined && elapsed <= bound)
373
+ histogram.buckets[index] = (histogram.buckets[index] ?? 0) + 1;
374
+ }
375
+ }
376
+ }
377
+ }
378
+ var ACQUISITION_BUCKETS_SECONDS, outcomes, metricsBrand, states;
379
+ var init_acquisitionMetrics = __esm(() => {
380
+ ACQUISITION_BUCKETS_SECONDS = [
381
+ 0.001,
382
+ 0.005,
383
+ 0.01,
384
+ 0.025,
385
+ 0.05,
386
+ 0.1,
387
+ 0.25,
388
+ 0.5,
389
+ 1,
390
+ 2.5,
391
+ 5,
392
+ 10
393
+ ];
394
+ outcomes = ["acquired", "failed", "aborted"];
395
+ metricsBrand = Symbol("transaction acquisition metrics");
396
+ states = new WeakMap;
397
+ });
398
+
347
399
  // ../../src/core/database/connection.ts
348
400
  function createDatabaseConnection(source) {
349
401
  return {
@@ -712,9 +764,13 @@ async function runInTransaction(operation, options = {}) {
712
764
  throw new Error("Active database connection does not support transactions. Bind a client with begin() via bindDatabaseConnection.");
713
765
  }
714
766
  return await settleTransaction(() => runWithDeferredModelEvents(async () => {
715
- if (signal && !pool.reserve)
716
- throw new Error("Active database connection does not support cancellable transaction acquisition.");
717
- const reserved = signal ? await pool.reserve?.({ signal }) : undefined;
767
+ let reserved;
768
+ if (signal || options.acquisitionMetrics) {
769
+ if (!pool.reserve)
770
+ throw new Error(signal ? "Active database connection does not support cancellable transaction acquisition." : "Active database connection does not support observable transaction acquisition.");
771
+ const reserve = pool.reserve.bind(pool);
772
+ reserved = options.acquisitionMetrics ? await observeTransactionAcquisition(options.acquisitionMetrics, () => reserve({ signal }), signal) : await reserve({ signal });
773
+ }
718
774
  const connection = reserved ?? pool;
719
775
  try {
720
776
  signal?.throwIfAborted();
@@ -732,10 +788,12 @@ var transactionFrame, outerFrames;
732
788
  var init_transaction = __esm(() => {
733
789
  init_deferredModelEvents();
734
790
  init_asyncContextStore();
791
+ init_acquisitionMetrics();
735
792
  init_connection();
736
793
  init_connectionContext();
737
794
  init_repositoryConnection();
738
795
  init_transactionControl();
796
+ init_acquisitionMetrics();
739
797
  transactionFrame = createAsyncContextStore("@getstrata/transactionFrame");
740
798
  outerFrames = new WeakMap;
741
799
  });