@getstrata/core 1.1.8 → 1.1.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @getstrata/core changelog
2
2
 
3
+ ## 1.1.9
4
+
5
+ - Defer model events until the surrounding transaction commits
6
+
7
+ ## Unreleased
8
+
9
+ - Model events from `BaseRepository` writes wait for the surrounding `runInTransaction` / `runWithTenantDatabase` / `runWithMigrationBypass` callback to commit. A rollback drops the queued events. Nested transactions promote to the outer commit. Listeners run with the tenant that was active when the write happened.
10
+
3
11
  ## 1.1.8
4
12
 
5
13
  - Exclude HTML and SPA routes from generated OpenAPI specs
@@ -0,0 +1,3 @@
1
+ declare function dispatchModelEvent(event: string, payload: unknown): Promise<void>;
2
+ declare function runWithDeferredModelEvents<T>(callback: () => T | Promise<T>): Promise<T>;
3
+ export { dispatchModelEvent, runWithDeferredModelEvents };
@@ -1,3 +1,4 @@
1
+ export { dispatchModelEvent, runWithDeferredModelEvents } from "./deferredModelEvents";
1
2
  export type { EventListener } from "./eventBus";
2
3
  export { EventBus, eventBus, readSharedEventBus } from "./eventBus";
3
4
  declare function modelEventName(tableName: string, action: string): string;
@@ -1,4 +1,239 @@
1
1
  // @bun
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
13
+
14
+ // ../../src/core/runtime/asyncContextStore.ts
15
+ import { AsyncLocalStorage } from "async_hooks";
16
+ function createAsyncContextStore(key) {
17
+ const symbol = Symbol.for(key);
18
+ const globalRecord = globalThis;
19
+ const existing = globalRecord[symbol];
20
+ if (existing) {
21
+ return existing;
22
+ }
23
+ const store = new AsyncLocalStorage;
24
+ globalRecord[symbol] = store;
25
+ return store;
26
+ }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/database/connectionContext.ts
30
+ function runWithDatabaseConnection(connection, callback) {
31
+ return activeConnection.run(connection, callback);
32
+ }
33
+ function getActiveDatabaseConnection(fallback) {
34
+ return activeConnection.getStore() ?? fallback;
35
+ }
36
+ function hasActiveDatabaseConnection() {
37
+ return activeConnection.getStore() !== undefined;
38
+ }
39
+ var activeConnection;
40
+ var init_connectionContext = __esm(() => {
41
+ init_asyncContextStore();
42
+ activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
43
+ });
44
+
45
+ // ../../src/core/tenant/tenantContext.ts
46
+ function runWithTenant(tenant, callback) {
47
+ return tenantContext.run(tenant, callback);
48
+ }
49
+ function currentTenant() {
50
+ return tenantContext.getStore() ?? null;
51
+ }
52
+ var tenantContext;
53
+ var init_tenantContext = __esm(() => {
54
+ init_asyncContextStore();
55
+ tenantContext = createAsyncContextStore("@getstrata/tenantContext");
56
+ });
57
+
58
+ // ../../src/core/events/eventBus.ts
59
+ class EventBus {
60
+ constructor() {}
61
+ listeners = new Map;
62
+ on(event, listener) {
63
+ return this.listen(event, listener);
64
+ }
65
+ listen(event, listener) {
66
+ const handlers = this.listeners.get(event) ?? new Set;
67
+ handlers.add(listener);
68
+ this.listeners.set(event, handlers);
69
+ return () => {
70
+ handlers.delete(listener);
71
+ if (handlers.size === 0) {
72
+ this.listeners.delete(event);
73
+ }
74
+ };
75
+ }
76
+ async emit(event, payload) {
77
+ await this.dispatch(event, payload);
78
+ }
79
+ async dispatch(event, payload) {
80
+ const handlers = this.listeners.get(event);
81
+ if (!handlers || handlers.size === 0) {
82
+ return;
83
+ }
84
+ for (const handler of handlers) {
85
+ await handler(payload);
86
+ }
87
+ }
88
+ }
89
+ function readSharedEventBus() {
90
+ const globalBus = globalThis[EVENT_BUS_KEY];
91
+ if (globalBus) {
92
+ return globalBus;
93
+ }
94
+ const bus = new EventBus;
95
+ globalThis[EVENT_BUS_KEY] = bus;
96
+ return bus;
97
+ }
98
+ var EVENT_BUS_KEY, eventBus;
99
+ var init_eventBus = __esm(() => {
100
+ EVENT_BUS_KEY = Symbol.for("@getstrata/eventBus");
101
+ eventBus = readSharedEventBus();
102
+ });
103
+
104
+ // ../../src/core/tenant/tenancyConfig.ts
105
+ function readTenancyDriver(env = process.env) {
106
+ const raw = env.TENANCY_DRIVER?.trim();
107
+ if (raw === undefined || raw === "") {
108
+ return "rls";
109
+ }
110
+ if (TENANCY_DRIVERS.includes(raw)) {
111
+ return raw;
112
+ }
113
+ throw new Error(`TENANCY_DRIVER must be one of ${TENANCY_DRIVERS.join(", ")}; received "${raw}".`);
114
+ }
115
+ function isTenancyEnabled(env = process.env) {
116
+ return readTenancyDriver(env) !== "none";
117
+ }
118
+ function isRlsTenancy(env = process.env) {
119
+ return readTenancyDriver(env) === "rls";
120
+ }
121
+ var TENANCY_DRIVERS;
122
+ var init_tenancyConfig = __esm(() => {
123
+ TENANCY_DRIVERS = ["rls", "column", "none"];
124
+ });
125
+
126
+ // ../../src/core/tenant/tenantDatabaseScope.ts
127
+ import { getDefaultDatabasePool } from "@getstrata/core/database/defaultConnection";
128
+ async function applyTenantContextToTransaction(transaction, tenantId) {
129
+ await transaction.unsafe(`SELECT set_config('app.tenant_id', $1, true)`, [String(tenantId)]);
130
+ await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
131
+ }
132
+ async function runWithTenantDatabase(tenant, callback) {
133
+ if (!isTenancyEnabled() || !isRlsTenancy()) {
134
+ return await runWithTenant(tenant, callback);
135
+ }
136
+ if (hasActiveDatabaseConnection()) {
137
+ const activeConnection = getActiveDatabaseConnection(getDefaultDatabasePool());
138
+ await applyTenantContextToTransaction(activeConnection, tenant.id);
139
+ return await runWithTenant(tenant, callback);
140
+ }
141
+ const pool = getDefaultDatabasePool();
142
+ const begin = pool.begin;
143
+ if (typeof begin !== "function") {
144
+ throw new Error("RLS tenant scope requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
145
+ }
146
+ return await runWithDeferredModelEvents(async () => {
147
+ return await begin(async (transaction) => {
148
+ await applyTenantContextToTransaction(transaction, tenant.id);
149
+ return await runWithDatabaseConnection(transaction, async () => {
150
+ return await runWithTenant(tenant, callback);
151
+ });
152
+ });
153
+ });
154
+ }
155
+ function isInsideTenantDatabaseScope(tenantId = currentTenant()?.id) {
156
+ return hasActiveDatabaseConnection() && currentTenant()?.id === tenantId;
157
+ }
158
+ var init_tenantDatabaseScope = __esm(() => {
159
+ init_connectionContext();
160
+ init_deferredModelEvents();
161
+ init_tenancyConfig();
162
+ init_tenantContext();
163
+ });
164
+
165
+ // ../../src/core/events/deferredModelEvents.ts
166
+ function sameTenant(left, right) {
167
+ if (left === null && right === null) {
168
+ return true;
169
+ }
170
+ if (left === null || right === null) {
171
+ return false;
172
+ }
173
+ return left.id === right.id;
174
+ }
175
+ function groupQueuedModelEvents(queued) {
176
+ const groups = [];
177
+ for (const item of queued) {
178
+ const lastGroup = groups[groups.length - 1];
179
+ const last = lastGroup?.[lastGroup.length - 1];
180
+ if (!lastGroup || !last || !sameTenant(last.tenant, item.tenant)) {
181
+ groups.push([item]);
182
+ } else {
183
+ lastGroup.push(item);
184
+ }
185
+ }
186
+ return groups;
187
+ }
188
+ async function dispatchEventBatch(batch) {
189
+ for (const item of batch) {
190
+ await eventBus.dispatch(item.event, item.payload);
191
+ }
192
+ }
193
+ async function dispatchQueuedModelEvents(queued) {
194
+ if (queued.length === 0) {
195
+ return;
196
+ }
197
+ await Promise.resolve().then(() => init_tenantDatabaseScope());
198
+ for (const batch of groupQueuedModelEvents(queued)) {
199
+ const tenant = batch[0]?.tenant ?? null;
200
+ if (tenant) {
201
+ await runWithTenantDatabase(tenant, () => dispatchEventBatch(batch));
202
+ } else {
203
+ await dispatchEventBatch(batch);
204
+ }
205
+ }
206
+ }
207
+ async function runWithDeferredModelEvents(callback) {
208
+ const existing = deferredModelEvents.getStore();
209
+ if (existing) {
210
+ existing.frames.push([]);
211
+ try {
212
+ const result = await callback();
213
+ const nested = existing.frames.pop() ?? [];
214
+ const parent = existing.frames[existing.frames.length - 1];
215
+ if (parent) {
216
+ parent.push(...nested);
217
+ }
218
+ return result;
219
+ } catch (error) {
220
+ existing.frames.pop();
221
+ throw error;
222
+ }
223
+ }
224
+ const state = { frames: [[]] };
225
+ const result = await deferredModelEvents.run(state, callback);
226
+ await dispatchQueuedModelEvents(state.frames[0] ?? []);
227
+ return result;
228
+ }
229
+ var deferredModelEvents;
230
+ var init_deferredModelEvents = __esm(() => {
231
+ init_asyncContextStore();
232
+ init_tenantContext();
233
+ init_eventBus();
234
+ deferredModelEvents = createAsyncContextStore("@getstrata/deferredModelEvents");
235
+ });
236
+
2
237
  // ../../src/core/audit/exportAuditLogs.ts
3
238
  import { repositoryConnection as db } from "@getstrata/core/database/repositoryConnection";
4
239
 
@@ -376,54 +611,10 @@ async function safeFetch(input, init = {}, options = {}) {
376
611
  }
377
612
 
378
613
  // ../../src/core/tenant/databaseTenantContext.ts
379
- import { getDefaultDatabasePool } from "@getstrata/core/database/defaultConnection";
380
-
381
- // ../../src/core/runtime/asyncContextStore.ts
382
- import { AsyncLocalStorage } from "async_hooks";
383
- function createAsyncContextStore(key) {
384
- const symbol = Symbol.for(key);
385
- const globalRecord = globalThis;
386
- const existing = globalRecord[symbol];
387
- if (existing) {
388
- return existing;
389
- }
390
- const store = new AsyncLocalStorage;
391
- globalRecord[symbol] = store;
392
- return store;
393
- }
394
-
395
- // ../../src/core/database/connectionContext.ts
396
- var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
397
- function runWithDatabaseConnection(connection, callback) {
398
- return activeConnection.run(connection, callback);
399
- }
400
- function getActiveDatabaseConnection(fallback) {
401
- return activeConnection.getStore() ?? fallback;
402
- }
403
- function hasActiveDatabaseConnection() {
404
- return activeConnection.getStore() !== undefined;
405
- }
406
-
407
- // ../../src/core/tenant/tenancyConfig.ts
408
- var TENANCY_DRIVERS = ["rls", "column", "none"];
409
- function readTenancyDriver(env = process.env) {
410
- const raw = env.TENANCY_DRIVER?.trim();
411
- if (raw === undefined || raw === "") {
412
- return "rls";
413
- }
414
- if (TENANCY_DRIVERS.includes(raw)) {
415
- return raw;
416
- }
417
- throw new Error(`TENANCY_DRIVER must be one of ${TENANCY_DRIVERS.join(", ")}; received "${raw}".`);
418
- }
419
- function isTenancyEnabled(env = process.env) {
420
- return readTenancyDriver(env) !== "none";
421
- }
422
- function isRlsTenancy(env = process.env) {
423
- return readTenancyDriver(env) === "rls";
424
- }
425
-
426
- // ../../src/core/tenant/databaseTenantContext.ts
614
+ init_connectionContext();
615
+ init_deferredModelEvents();
616
+ init_tenancyConfig();
617
+ import { getDefaultDatabasePool as getDefaultDatabasePool2 } from "@getstrata/core/database/defaultConnection";
427
618
  async function applyBypassToTransaction(transaction, bypass) {
428
619
  await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, [
429
620
  bypass ? "true" : "false"
@@ -456,13 +647,16 @@ async function runWithScopedTenantTransaction(apply, callback) {
456
647
  if (!isRlsTenancy()) {
457
648
  return await callback();
458
649
  }
459
- const pool = getDefaultDatabasePool();
460
- if (typeof pool.begin !== "function") {
650
+ const pool = getDefaultDatabasePool2();
651
+ const begin = pool.begin;
652
+ if (typeof begin !== "function") {
461
653
  throw new Error("RLS migration bypass requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
462
654
  }
463
- return await pool.begin(async (transaction) => {
464
- await apply(transaction);
465
- return await runWithDatabaseConnection(transaction, callback);
655
+ return await runWithDeferredModelEvents(async () => {
656
+ return await begin(async (transaction) => {
657
+ await apply(transaction);
658
+ return await runWithDatabaseConnection(transaction, callback);
659
+ });
466
660
  });
467
661
  }
468
662
  async function runWithMigrationBypass(callback) {
@@ -1,6 +1,15 @@
1
1
  // @bun
2
- // ../../src/core/auth/accessControl.ts
3
- import { ForbiddenError } from "@getstrata/core/errors/http";
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
4
13
 
5
14
  // ../../src/core/runtime/asyncContextStore.ts
6
15
  import { AsyncLocalStorage } from "async_hooks";
@@ -15,8 +24,13 @@ function createAsyncContextStore(key) {
15
24
  globalRecord[symbol] = store;
16
25
  return store;
17
26
  }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/auth/accessControl.ts
30
+ import { ForbiddenError } from "@getstrata/core/errors/http";
18
31
 
19
32
  // ../../src/core/auth/authContext.ts
33
+ init_asyncContextStore();
20
34
  var authContext = createAsyncContextStore("@getstrata/authContext");
21
35
  var credentialSourceContext = createAsyncContextStore("@getstrata/credentialSource");
22
36
  function currentAuthUser() {
@@ -1,4 +1,47 @@
1
1
  // @bun
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
13
+
14
+ // ../../src/core/runtime/asyncContextStore.ts
15
+ import { AsyncLocalStorage } from "async_hooks";
16
+ function createAsyncContextStore(key) {
17
+ const symbol = Symbol.for(key);
18
+ const globalRecord = globalThis;
19
+ const existing = globalRecord[symbol];
20
+ if (existing) {
21
+ return existing;
22
+ }
23
+ const store = new AsyncLocalStorage;
24
+ globalRecord[symbol] = store;
25
+ return store;
26
+ }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/database/connectionContext.ts
30
+ function runWithDatabaseConnection(connection, callback) {
31
+ return activeConnection.run(connection, callback);
32
+ }
33
+ function getActiveDatabaseConnection(fallback) {
34
+ return activeConnection.getStore() ?? fallback;
35
+ }
36
+ function hasActiveDatabaseConnection() {
37
+ return activeConnection.getStore() !== undefined;
38
+ }
39
+ var activeConnection;
40
+ var init_connectionContext = __esm(() => {
41
+ init_asyncContextStore();
42
+ activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
43
+ });
44
+
2
45
  // ../../src/core/contracts/serviceTokens.ts
3
46
  var CORE_CONFIG_TOKEN = "core.config";
4
47
  var CORE_CACHE_TOKEN = "core.cache";
@@ -60,33 +103,8 @@ function getBoundDatabaseConnection() {
60
103
  return boundConnectionState().connection;
61
104
  }
62
105
 
63
- // ../../src/core/runtime/asyncContextStore.ts
64
- import { AsyncLocalStorage } from "async_hooks";
65
- function createAsyncContextStore(key) {
66
- const symbol = Symbol.for(key);
67
- const globalRecord = globalThis;
68
- const existing = globalRecord[symbol];
69
- if (existing) {
70
- return existing;
71
- }
72
- const store = new AsyncLocalStorage;
73
- globalRecord[symbol] = store;
74
- return store;
75
- }
76
-
77
- // ../../src/core/database/connectionContext.ts
78
- var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
79
- function runWithDatabaseConnection(connection, callback) {
80
- return activeConnection.run(connection, callback);
81
- }
82
- function getActiveDatabaseConnection(fallback) {
83
- return activeConnection.getStore() ?? fallback;
84
- }
85
- function hasActiveDatabaseConnection() {
86
- return activeConnection.getStore() !== undefined;
87
- }
88
-
89
106
  // ../../src/core/database/queryProxy.ts
107
+ init_connectionContext();
90
108
  var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
91
109
 
92
110
  // ../../src/core/database/defaultConnection.ts
@@ -289,6 +307,9 @@ function revealMfaSecret(stored) {
289
307
  return decryptField(stored, key);
290
308
  }
291
309
 
310
+ // ../../src/core/database/dialect.ts
311
+ init_asyncContextStore();
312
+
292
313
  // ../../src/core/database/schema/driver.ts
293
314
  function normalizeConnectionName(connection) {
294
315
  const normalized = connection.trim().toLowerCase();
@@ -1,4 +1,16 @@
1
1
  // @bun
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
13
+
2
14
  // ../../src/core/runtime/asyncContextStore.ts
3
15
  import { AsyncLocalStorage } from "async_hooks";
4
16
  function createAsyncContextStore(key) {
@@ -12,11 +24,16 @@ function createAsyncContextStore(key) {
12
24
  globalRecord[symbol] = store;
13
25
  return store;
14
26
  }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/auth/membershipContext.ts
30
+ init_asyncContextStore();
15
31
 
16
32
  // ../../src/core/auth/accessControl.ts
17
33
  import { ForbiddenError } from "@getstrata/core/errors/http";
18
34
 
19
35
  // ../../src/core/auth/authContext.ts
36
+ init_asyncContextStore();
20
37
  var authContext = createAsyncContextStore("@getstrata/authContext");
21
38
  var credentialSourceContext = createAsyncContextStore("@getstrata/credentialSource");
22
39
  function currentAuthUser() {
@@ -1,10 +1,15 @@
1
1
  // @bun
2
- // ../../src/core/auth/membershipScope.ts
3
- import { NotFoundError } from "@getstrata/core/errors/http";
4
- import { currentTenantId } from "@getstrata/core/tenant/tenantContext";
5
-
6
- // ../../src/core/auth/accessControl.ts
7
- import { ForbiddenError } from "@getstrata/core/errors/http";
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
8
13
 
9
14
  // ../../src/core/runtime/asyncContextStore.ts
10
15
  import { AsyncLocalStorage } from "async_hooks";
@@ -19,8 +24,17 @@ function createAsyncContextStore(key) {
19
24
  globalRecord[symbol] = store;
20
25
  return store;
21
26
  }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/auth/membershipScope.ts
30
+ import { NotFoundError } from "@getstrata/core/errors/http";
31
+ import { currentTenantId } from "@getstrata/core/tenant/tenantContext";
32
+
33
+ // ../../src/core/auth/accessControl.ts
34
+ import { ForbiddenError } from "@getstrata/core/errors/http";
22
35
 
23
36
  // ../../src/core/auth/authContext.ts
37
+ init_asyncContextStore();
24
38
  var authContext = createAsyncContextStore("@getstrata/authContext");
25
39
  var credentialSourceContext = createAsyncContextStore("@getstrata/credentialSource");
26
40
  function currentAuthUser() {
@@ -58,6 +72,7 @@ function resolveUserId(user) {
58
72
  }
59
73
 
60
74
  // ../../src/core/auth/membershipContext.ts
75
+ init_asyncContextStore();
61
76
  var membershipContext = createAsyncContextStore("@getstrata/membershipContext");
62
77
  var uninitializedMembershipLookup = {
63
78
  async listForUser() {
@@ -1,9 +1,15 @@
1
1
  // @bun
2
- // ../../src/core/auth/membershipService.ts
3
- import { ForbiddenError as ForbiddenError2 } from "@getstrata/core/errors/http";
4
-
5
- // ../../src/core/auth/accessControl.ts
6
- import { ForbiddenError } from "@getstrata/core/errors/http";
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
7
13
 
8
14
  // ../../src/core/runtime/asyncContextStore.ts
9
15
  import { AsyncLocalStorage } from "async_hooks";
@@ -18,8 +24,16 @@ function createAsyncContextStore(key) {
18
24
  globalRecord[symbol] = store;
19
25
  return store;
20
26
  }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/auth/membershipService.ts
30
+ import { ForbiddenError as ForbiddenError2 } from "@getstrata/core/errors/http";
31
+
32
+ // ../../src/core/auth/accessControl.ts
33
+ import { ForbiddenError } from "@getstrata/core/errors/http";
21
34
 
22
35
  // ../../src/core/auth/authContext.ts
36
+ init_asyncContextStore();
23
37
  var authContext = createAsyncContextStore("@getstrata/authContext");
24
38
  var credentialSourceContext = createAsyncContextStore("@getstrata/credentialSource");
25
39
  function currentAuthUser() {
@@ -57,6 +71,7 @@ function resolveUserId(user) {
57
71
  }
58
72
 
59
73
  // ../../src/core/auth/membershipContext.ts
74
+ init_asyncContextStore();
60
75
  var membershipContext = createAsyncContextStore("@getstrata/membershipContext");
61
76
  var uninitializedMembershipLookup = {
62
77
  async listForUser() {
@@ -1,4 +1,47 @@
1
1
  // @bun
2
+ var __esm = (fn, res, err) => () => {
3
+ if (fn)
4
+ try {
5
+ res = fn(fn = 0);
6
+ } catch (e) {
7
+ err = [e];
8
+ }
9
+ if (err)
10
+ throw err[0];
11
+ return res;
12
+ };
13
+
14
+ // ../../src/core/runtime/asyncContextStore.ts
15
+ import { AsyncLocalStorage } from "async_hooks";
16
+ function createAsyncContextStore(key) {
17
+ const symbol = Symbol.for(key);
18
+ const globalRecord = globalThis;
19
+ const existing = globalRecord[symbol];
20
+ if (existing) {
21
+ return existing;
22
+ }
23
+ const store = new AsyncLocalStorage;
24
+ globalRecord[symbol] = store;
25
+ return store;
26
+ }
27
+ var init_asyncContextStore = () => {};
28
+
29
+ // ../../src/core/database/connectionContext.ts
30
+ function runWithDatabaseConnection(connection, callback) {
31
+ return activeConnection.run(connection, callback);
32
+ }
33
+ function getActiveDatabaseConnection(fallback) {
34
+ return activeConnection.getStore() ?? fallback;
35
+ }
36
+ function hasActiveDatabaseConnection() {
37
+ return activeConnection.getStore() !== undefined;
38
+ }
39
+ var activeConnection;
40
+ var init_connectionContext = __esm(() => {
41
+ init_asyncContextStore();
42
+ activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
43
+ });
44
+
2
45
  // ../../src/core/runtime/optionalPeer.ts
3
46
  function missingOptionalPeer(packageName, reason, error) {
4
47
  return new Error(`Install ${packageName} ${reason} (\`bun add ${packageName}\`).`, {
@@ -33,33 +76,8 @@ function getBoundDatabaseConnection() {
33
76
  return boundConnectionState().connection;
34
77
  }
35
78
 
36
- // ../../src/core/runtime/asyncContextStore.ts
37
- import { AsyncLocalStorage } from "async_hooks";
38
- function createAsyncContextStore(key) {
39
- const symbol = Symbol.for(key);
40
- const globalRecord = globalThis;
41
- const existing = globalRecord[symbol];
42
- if (existing) {
43
- return existing;
44
- }
45
- const store = new AsyncLocalStorage;
46
- globalRecord[symbol] = store;
47
- return store;
48
- }
49
-
50
- // ../../src/core/database/connectionContext.ts
51
- var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
52
- function runWithDatabaseConnection(connection, callback) {
53
- return activeConnection.run(connection, callback);
54
- }
55
- function getActiveDatabaseConnection(fallback) {
56
- return activeConnection.getStore() ?? fallback;
57
- }
58
- function hasActiveDatabaseConnection() {
59
- return activeConnection.getStore() !== undefined;
60
- }
61
-
62
79
  // ../../src/core/database/queryProxy.ts
80
+ init_connectionContext();
63
81
  var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
64
82
 
65
83
  // ../../src/core/database/defaultConnection.ts
@@ -82,6 +100,9 @@ function getDefaultDatabasePool() {
82
100
  return pool;
83
101
  }
84
102
 
103
+ // ../../src/core/database/dialect.ts
104
+ init_asyncContextStore();
105
+
85
106
  // ../../src/core/database/schema/driver.ts
86
107
  function normalizeConnectionName(connection) {
87
108
  const normalized = connection.trim().toLowerCase();