@getstrata/core 1.1.9 → 2.0.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/CHANGELOG.md +19 -0
- package/dist/core/database/transaction.d.ts +2 -1
- package/dist/core/database/transactionControl.d.ts +5 -0
- package/dist/core/http/loginThrottleMiddleware.d.ts +3 -0
- package/dist/core/http/metricsMiddleware.d.ts +1 -2
- package/dist/core/http/requestMetaContext.d.ts +2 -0
- package/dist/core/http/throttleMiddleware.d.ts +11 -2
- package/dist/core/metrics/prometheus.d.ts +11 -1
- package/dist/core/queue/jobRunner.d.ts +3 -1
- package/dist/core/queue/redisQueue.d.ts +20 -3
- package/dist/core/tenant/databaseTenantContext.d.ts +5 -1
- package/dist/entries/audit/exportAuditLogs.js +522 -62
- package/dist/entries/auth/basicAuthGuard.js +245 -219
- package/dist/entries/auth/oauth/samlProvider.js +185 -161
- package/dist/entries/auth/oneTimeToken.js +130 -127
- package/dist/entries/auth/passwordLogin.js +185 -180
- package/dist/entries/auth/saml/samlServiceProvider.js +185 -161
- package/dist/entries/auth/scimAuthMiddleware.js +525 -11
- package/dist/entries/database/migrations.js +132 -129
- package/dist/entries/database/schema.js +58 -39
- package/dist/entries/http/metricsMiddleware.js +124 -27
- package/dist/entries/http/response.js +107 -0
- package/dist/entries/http/routeMiddleware.js +40 -0
- package/dist/entries/http/throttleMiddleware.js +124 -11
- package/dist/entries/jobs/exportAuditLogsJob.js +522 -62
- package/dist/entries/metrics/prometheus.js +71 -11
- package/dist/entries/queue/createAppQueue.js +215 -21
- package/dist/entries/queue/jobRunner.js +3 -2
- package/dist/entries/queue/publicQueue.js +215 -21
- package/dist/entries/queue/queueMetrics.js +215 -21
- package/dist/entries/queue/redisQueue.js +223 -22
- package/dist/entries/tenant/databaseTenantContext.js +477 -16
- package/dist/entries/tenant/tenantDatabaseScope.js +523 -9
- package/dist/framework/public-api.d.ts +2 -2
- package/dist/index.js +954 -457
- package/package.json +1 -1
|
@@ -42,6 +42,80 @@ var init_connectionContext = __esm(() => {
|
|
|
42
42
|
activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
+
// ../../src/core/database/transactionControl.ts
|
|
46
|
+
function unwrapTransactionRollback(error, depth = 0) {
|
|
47
|
+
if (error instanceof TransactionRollback) {
|
|
48
|
+
return error;
|
|
49
|
+
}
|
|
50
|
+
if (depth >= 5 || !(error instanceof Error) || error.cause === undefined) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return unwrapTransactionRollback(error.cause, depth + 1);
|
|
54
|
+
}
|
|
55
|
+
function requestTransactionRollback() {
|
|
56
|
+
const state = transactionScopes.getStore();
|
|
57
|
+
const index = state ? state.rollback.length - 1 : -1;
|
|
58
|
+
if (!state || index < 0) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const flags = state.rollback;
|
|
62
|
+
const current = flags[index];
|
|
63
|
+
if (current === undefined) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
flags[index] = true;
|
|
67
|
+
}
|
|
68
|
+
function scopeRequestsRollback() {
|
|
69
|
+
const state = transactionScopes.getStore();
|
|
70
|
+
if (!state || state.rollback.length === 0) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return state.rollback[state.rollback.length - 1] === true;
|
|
74
|
+
}
|
|
75
|
+
async function commitOrRollbackScope(result) {
|
|
76
|
+
if (scopeRequestsRollback()) {
|
|
77
|
+
throw new TransactionRollback(result);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
async function settleTransaction(work) {
|
|
82
|
+
try {
|
|
83
|
+
return await work();
|
|
84
|
+
} catch (error) {
|
|
85
|
+
const rollback = unwrapTransactionRollback(error);
|
|
86
|
+
if (rollback) {
|
|
87
|
+
return rollback.result;
|
|
88
|
+
}
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function runWithTransactionScope(callback) {
|
|
93
|
+
const existing = transactionScopes.getStore();
|
|
94
|
+
if (existing) {
|
|
95
|
+
existing.rollback.push(false);
|
|
96
|
+
try {
|
|
97
|
+
return await callback();
|
|
98
|
+
} finally {
|
|
99
|
+
existing.rollback.pop();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const state = { rollback: [false] };
|
|
103
|
+
return await transactionScopes.run(state, callback);
|
|
104
|
+
}
|
|
105
|
+
var TransactionRollback, transactionScopes;
|
|
106
|
+
var init_transactionControl = __esm(() => {
|
|
107
|
+
init_asyncContextStore();
|
|
108
|
+
TransactionRollback = class TransactionRollback extends Error {
|
|
109
|
+
result;
|
|
110
|
+
constructor(result) {
|
|
111
|
+
super("Transaction rolled back");
|
|
112
|
+
this.name = "TransactionRollback";
|
|
113
|
+
this.result = result;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
transactionScopes = createAsyncContextStore("@getstrata/transactionScopes");
|
|
117
|
+
});
|
|
118
|
+
|
|
45
119
|
// ../../src/core/tenant/tenantContext.ts
|
|
46
120
|
function runWithTenant(tenant, callback) {
|
|
47
121
|
return tenantContext.run(tenant, callback);
|
|
@@ -173,6 +247,367 @@ var init_deferredModelEvents = __esm(() => {
|
|
|
173
247
|
deferredModelEvents = createAsyncContextStore("@getstrata/deferredModelEvents");
|
|
174
248
|
});
|
|
175
249
|
|
|
250
|
+
// ../../src/core/database/connection.ts
|
|
251
|
+
function createDatabaseConnection(source) {
|
|
252
|
+
return {
|
|
253
|
+
async unsafe(query, params = []) {
|
|
254
|
+
return await source.unsafe(query, params);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
var init_connection = () => {};
|
|
259
|
+
|
|
260
|
+
// ../../src/core/database/boundConnection.ts
|
|
261
|
+
function boundConnectionState() {
|
|
262
|
+
const globalRecord = globalThis;
|
|
263
|
+
const existing = globalRecord[BOUND_CONNECTION_KEY];
|
|
264
|
+
if (existing) {
|
|
265
|
+
return existing;
|
|
266
|
+
}
|
|
267
|
+
const created = { connection: null };
|
|
268
|
+
globalRecord[BOUND_CONNECTION_KEY] = created;
|
|
269
|
+
return created;
|
|
270
|
+
}
|
|
271
|
+
function getBoundDatabaseConnection() {
|
|
272
|
+
return boundConnectionState().connection;
|
|
273
|
+
}
|
|
274
|
+
var BOUND_CONNECTION_KEY;
|
|
275
|
+
var init_boundConnection = __esm(() => {
|
|
276
|
+
BOUND_CONNECTION_KEY = Symbol.for("@getstrata/boundDatabaseConnection");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// ../../src/core/database/queryProxy.ts
|
|
280
|
+
var POOL_CONNECTION_METHODS;
|
|
281
|
+
var init_queryProxy = __esm(() => {
|
|
282
|
+
init_connectionContext();
|
|
283
|
+
POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
// ../../src/core/database/defaultConnection.ts
|
|
287
|
+
function defaultConnectionState() {
|
|
288
|
+
const globalRecord = globalThis;
|
|
289
|
+
const existing = globalRecord[DEFAULT_CONNECTION_KEY];
|
|
290
|
+
if (existing) {
|
|
291
|
+
return existing;
|
|
292
|
+
}
|
|
293
|
+
const created = { pool: null, query: null };
|
|
294
|
+
globalRecord[DEFAULT_CONNECTION_KEY] = created;
|
|
295
|
+
return created;
|
|
296
|
+
}
|
|
297
|
+
function getDefaultDatabasePool() {
|
|
298
|
+
const pool = defaultConnectionState().pool;
|
|
299
|
+
if (!pool) {
|
|
300
|
+
throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
301
|
+
}
|
|
302
|
+
return pool;
|
|
303
|
+
}
|
|
304
|
+
function getDefaultDatabaseQuery() {
|
|
305
|
+
const query = defaultConnectionState().query;
|
|
306
|
+
if (!query) {
|
|
307
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
308
|
+
}
|
|
309
|
+
return query;
|
|
310
|
+
}
|
|
311
|
+
var DEFAULT_CONNECTION_KEY;
|
|
312
|
+
var init_defaultConnection = __esm(() => {
|
|
313
|
+
init_queryProxy();
|
|
314
|
+
DEFAULT_CONNECTION_KEY = Symbol.for("@getstrata/defaultDatabaseConnection");
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// ../../src/core/database/schema/driver.ts
|
|
318
|
+
function normalizeConnectionName(connection) {
|
|
319
|
+
const normalized = connection.trim().toLowerCase();
|
|
320
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
321
|
+
return "pgsql";
|
|
322
|
+
}
|
|
323
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
324
|
+
return "mysql";
|
|
325
|
+
}
|
|
326
|
+
if (normalized === "sqlite") {
|
|
327
|
+
return "sqlite";
|
|
328
|
+
}
|
|
329
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
330
|
+
}
|
|
331
|
+
function resolveDriverFromUrl(url) {
|
|
332
|
+
const normalized = url.trim().toLowerCase();
|
|
333
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
334
|
+
return "pgsql";
|
|
335
|
+
}
|
|
336
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
337
|
+
return "mysql";
|
|
338
|
+
}
|
|
339
|
+
if (normalized.startsWith("sqlite:")) {
|
|
340
|
+
return "sqlite";
|
|
341
|
+
}
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
function resolveDatabaseDriver(options = {}) {
|
|
345
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
346
|
+
if (connection) {
|
|
347
|
+
return normalizeConnectionName(connection);
|
|
348
|
+
}
|
|
349
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
350
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
351
|
+
if (fromUrl) {
|
|
352
|
+
return fromUrl;
|
|
353
|
+
}
|
|
354
|
+
return "pgsql";
|
|
355
|
+
}
|
|
356
|
+
var init_driver = () => {};
|
|
357
|
+
|
|
358
|
+
// ../../src/core/database/dialect.ts
|
|
359
|
+
function assertSafeIdentifier(identifier) {
|
|
360
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(identifier)) {
|
|
361
|
+
throw new Error(`Invalid SQL identifier: ${identifier}`);
|
|
362
|
+
}
|
|
363
|
+
return identifier;
|
|
364
|
+
}
|
|
365
|
+
function quoteIdentifierFor(dialect, column) {
|
|
366
|
+
return dialect.quoteIdentifier(column);
|
|
367
|
+
}
|
|
368
|
+
function dialectFor(driver) {
|
|
369
|
+
return dialects[driver];
|
|
370
|
+
}
|
|
371
|
+
function currentSqlDialect() {
|
|
372
|
+
return dialectContext.getStore() ?? dialectOverride ?? dialectFor(resolveDatabaseDriver());
|
|
373
|
+
}
|
|
374
|
+
var postgresDialect, mysqlDialect, sqliteDialect, dialects, dialectContext, dialectOverride = null;
|
|
375
|
+
var init_dialect = __esm(() => {
|
|
376
|
+
init_asyncContextStore();
|
|
377
|
+
init_driver();
|
|
378
|
+
postgresDialect = {
|
|
379
|
+
driver: "pgsql",
|
|
380
|
+
placeholder(index) {
|
|
381
|
+
return `$${index}`;
|
|
382
|
+
},
|
|
383
|
+
quoteIdentifier(identifier) {
|
|
384
|
+
return `"${assertSafeIdentifier(identifier)}"`;
|
|
385
|
+
},
|
|
386
|
+
nowExpression() {
|
|
387
|
+
return "NOW()";
|
|
388
|
+
},
|
|
389
|
+
timestampValue(value) {
|
|
390
|
+
return value.toISOString();
|
|
391
|
+
},
|
|
392
|
+
returningClause(columns) {
|
|
393
|
+
return ` RETURNING ${columns}`;
|
|
394
|
+
},
|
|
395
|
+
ilikeOperator() {
|
|
396
|
+
return "ILIKE";
|
|
397
|
+
},
|
|
398
|
+
nullsLastSuffix() {
|
|
399
|
+
return " NULLS LAST";
|
|
400
|
+
},
|
|
401
|
+
castToText(expression) {
|
|
402
|
+
return `${expression}::text`;
|
|
403
|
+
},
|
|
404
|
+
upsertSuffix(conflictColumns, updateColumns) {
|
|
405
|
+
const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
|
|
406
|
+
if (updateColumns.length === 0) {
|
|
407
|
+
return ` ON CONFLICT (${target}) DO NOTHING`;
|
|
408
|
+
}
|
|
409
|
+
const assignments = updateColumns.map((column) => {
|
|
410
|
+
const quoted = quoteIdentifierFor(this, column);
|
|
411
|
+
return `${quoted} = excluded.${quoted}`;
|
|
412
|
+
}).join(", ");
|
|
413
|
+
return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
mysqlDialect = {
|
|
417
|
+
driver: "mysql",
|
|
418
|
+
placeholder() {
|
|
419
|
+
return "?";
|
|
420
|
+
},
|
|
421
|
+
quoteIdentifier(identifier) {
|
|
422
|
+
return `\`${assertSafeIdentifier(identifier)}\``;
|
|
423
|
+
},
|
|
424
|
+
nowExpression() {
|
|
425
|
+
return "CURRENT_TIMESTAMP";
|
|
426
|
+
},
|
|
427
|
+
timestampValue(value) {
|
|
428
|
+
return value.toISOString().slice(0, 19).replace("T", " ");
|
|
429
|
+
},
|
|
430
|
+
returningClause() {
|
|
431
|
+
return "";
|
|
432
|
+
},
|
|
433
|
+
ilikeOperator() {
|
|
434
|
+
return "LIKE";
|
|
435
|
+
},
|
|
436
|
+
nullsLastSuffix() {
|
|
437
|
+
return "";
|
|
438
|
+
},
|
|
439
|
+
castToText(expression) {
|
|
440
|
+
return `CAST(${expression} AS CHAR)`;
|
|
441
|
+
},
|
|
442
|
+
upsertSuffix(conflictColumns, updateColumns) {
|
|
443
|
+
if (updateColumns.length === 0) {
|
|
444
|
+
const anchor = quoteIdentifierFor(this, conflictColumns[0] ?? "");
|
|
445
|
+
return ` ON DUPLICATE KEY UPDATE ${anchor} = ${anchor}`;
|
|
446
|
+
}
|
|
447
|
+
const assignments = updateColumns.map((column) => {
|
|
448
|
+
const quoted = quoteIdentifierFor(this, column);
|
|
449
|
+
return `${quoted} = VALUES(${quoted})`;
|
|
450
|
+
}).join(", ");
|
|
451
|
+
return ` ON DUPLICATE KEY UPDATE ${assignments}`;
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
sqliteDialect = {
|
|
455
|
+
driver: "sqlite",
|
|
456
|
+
placeholder() {
|
|
457
|
+
return "?";
|
|
458
|
+
},
|
|
459
|
+
quoteIdentifier(identifier) {
|
|
460
|
+
return `"${assertSafeIdentifier(identifier)}"`;
|
|
461
|
+
},
|
|
462
|
+
nowExpression() {
|
|
463
|
+
return "strftime('%Y-%m-%dT%H:%M:%fZ', 'now')";
|
|
464
|
+
},
|
|
465
|
+
timestampValue(value) {
|
|
466
|
+
return value.toISOString();
|
|
467
|
+
},
|
|
468
|
+
returningClause(columns) {
|
|
469
|
+
return ` RETURNING ${columns}`;
|
|
470
|
+
},
|
|
471
|
+
ilikeOperator() {
|
|
472
|
+
return "LIKE";
|
|
473
|
+
},
|
|
474
|
+
nullsLastSuffix() {
|
|
475
|
+
return "";
|
|
476
|
+
},
|
|
477
|
+
castToText(expression) {
|
|
478
|
+
return `CAST(${expression} AS TEXT)`;
|
|
479
|
+
},
|
|
480
|
+
upsertSuffix(conflictColumns, updateColumns) {
|
|
481
|
+
const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
|
|
482
|
+
if (updateColumns.length === 0) {
|
|
483
|
+
return ` ON CONFLICT (${target}) DO NOTHING`;
|
|
484
|
+
}
|
|
485
|
+
const assignments = updateColumns.map((column) => {
|
|
486
|
+
const quoted = quoteIdentifierFor(this, column);
|
|
487
|
+
return `${quoted} = excluded.${quoted}`;
|
|
488
|
+
}).join(", ");
|
|
489
|
+
return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
dialects = {
|
|
493
|
+
pgsql: postgresDialect,
|
|
494
|
+
mysql: mysqlDialect,
|
|
495
|
+
sqlite: sqliteDialect
|
|
496
|
+
};
|
|
497
|
+
dialectContext = createAsyncContextStore("@getstrata/sqlDialect");
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
// ../../src/core/database/repositoryConnection.ts
|
|
501
|
+
function resolveRepositoryConnection() {
|
|
502
|
+
return getActiveDatabaseConnection(getBoundDatabaseConnection() ?? getDefaultDatabaseQuery());
|
|
503
|
+
}
|
|
504
|
+
function compileTaggedSql(strings, values) {
|
|
505
|
+
const dialect = currentSqlDialect();
|
|
506
|
+
let query = strings[0] ?? "";
|
|
507
|
+
const params = [];
|
|
508
|
+
for (let index = 0;index < values.length; index += 1) {
|
|
509
|
+
params.push(values[index]);
|
|
510
|
+
query += `${dialect.placeholder(params.length)}${strings[index + 1] ?? ""}`;
|
|
511
|
+
}
|
|
512
|
+
return { query, params };
|
|
513
|
+
}
|
|
514
|
+
function isTaggedStrings(value) {
|
|
515
|
+
if (!Array.isArray(value) || typeof value[0] !== "string") {
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
return typeof value.raw !== "undefined";
|
|
519
|
+
}
|
|
520
|
+
function runOnConnection(connection, args) {
|
|
521
|
+
if (typeof connection === "function") {
|
|
522
|
+
return connection(...args);
|
|
523
|
+
}
|
|
524
|
+
if (typeof connection.unsafe !== "function") {
|
|
525
|
+
throw new Error("Database connection cannot run SQL.");
|
|
526
|
+
}
|
|
527
|
+
const strings = args[0];
|
|
528
|
+
if (!isTaggedStrings(strings)) {
|
|
529
|
+
throw new Error("Database connection cannot run tagged SQL.");
|
|
530
|
+
}
|
|
531
|
+
const compiled = compileTaggedSql(strings, args.slice(1));
|
|
532
|
+
return connection.unsafe(compiled.query, compiled.params);
|
|
533
|
+
}
|
|
534
|
+
var repositoryConnection;
|
|
535
|
+
var init_repositoryConnection = __esm(() => {
|
|
536
|
+
init_boundConnection();
|
|
537
|
+
init_connectionContext();
|
|
538
|
+
init_defaultConnection();
|
|
539
|
+
init_dialect();
|
|
540
|
+
repositoryConnection = new Proxy(function repositoryConnection() {}, {
|
|
541
|
+
apply(_target, _thisArg, args) {
|
|
542
|
+
return runOnConnection(resolveRepositoryConnection(), args);
|
|
543
|
+
},
|
|
544
|
+
get(_target, property) {
|
|
545
|
+
const connection = resolveRepositoryConnection();
|
|
546
|
+
const value = connection[property];
|
|
547
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
// ../../src/core/database/transaction.ts
|
|
553
|
+
async function runInSavepoint(connection, operation) {
|
|
554
|
+
const inherited = transactionFrame.getStore();
|
|
555
|
+
let parent = inherited?.connection === connection ? inherited : outerFrames.get(connection);
|
|
556
|
+
if (!parent) {
|
|
557
|
+
parent = { connection, childActive: false };
|
|
558
|
+
outerFrames.set(connection, parent);
|
|
559
|
+
}
|
|
560
|
+
if (parent.childActive) {
|
|
561
|
+
throw new Error("Concurrent nested transactions on one connection are not supported. Await each transaction before starting the next.");
|
|
562
|
+
}
|
|
563
|
+
parent.childActive = true;
|
|
564
|
+
const name = `strata_${crypto.randomUUID().replaceAll("-", "")}`;
|
|
565
|
+
try {
|
|
566
|
+
return await settleTransaction(() => runWithDeferredModelEvents(async () => {
|
|
567
|
+
await connection.unsafe(`SAVEPOINT ${name}`);
|
|
568
|
+
try {
|
|
569
|
+
const result = await transactionFrame.run({ connection, childActive: false }, () => runWithTransactionScope(async () => commitOrRollbackScope(await operation(createDatabaseConnection(connection)))));
|
|
570
|
+
await connection.unsafe(`RELEASE SAVEPOINT ${name}`);
|
|
571
|
+
return result;
|
|
572
|
+
} catch (error) {
|
|
573
|
+
await connection.unsafe(`ROLLBACK TO SAVEPOINT ${name}`);
|
|
574
|
+
await connection.unsafe(`RELEASE SAVEPOINT ${name}`);
|
|
575
|
+
throw error;
|
|
576
|
+
}
|
|
577
|
+
}));
|
|
578
|
+
} finally {
|
|
579
|
+
parent.childActive = false;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
function supportsTransactions(connection) {
|
|
583
|
+
return typeof connection.begin === "function";
|
|
584
|
+
}
|
|
585
|
+
async function runInTransaction(operation) {
|
|
586
|
+
const pool = resolveRepositoryConnection();
|
|
587
|
+
if (hasActiveDatabaseConnection()) {
|
|
588
|
+
return await runInSavepoint(getActiveDatabaseConnection(pool), operation);
|
|
589
|
+
}
|
|
590
|
+
if (!supportsTransactions(pool)) {
|
|
591
|
+
throw new Error("Active database connection does not support transactions. Bind a client with begin() via bindDatabaseConnection.");
|
|
592
|
+
}
|
|
593
|
+
return await settleTransaction(() => runWithDeferredModelEvents(async () => {
|
|
594
|
+
return await pool.begin(async (transaction) => {
|
|
595
|
+
return await runWithDatabaseConnection(transaction, () => transactionFrame.run({ connection: transaction, childActive: false }, () => runWithTransactionScope(async () => commitOrRollbackScope(await operation(createDatabaseConnection(transaction))))));
|
|
596
|
+
});
|
|
597
|
+
}));
|
|
598
|
+
}
|
|
599
|
+
var transactionFrame, outerFrames;
|
|
600
|
+
var init_transaction = __esm(() => {
|
|
601
|
+
init_deferredModelEvents();
|
|
602
|
+
init_asyncContextStore();
|
|
603
|
+
init_connection();
|
|
604
|
+
init_connectionContext();
|
|
605
|
+
init_repositoryConnection();
|
|
606
|
+
init_transactionControl();
|
|
607
|
+
transactionFrame = createAsyncContextStore("@getstrata/transactionFrame");
|
|
608
|
+
outerFrames = new WeakMap;
|
|
609
|
+
});
|
|
610
|
+
|
|
176
611
|
// ../../src/core/tenant/tenancyConfig.ts
|
|
177
612
|
function readTenancyDriver(env = process.env) {
|
|
178
613
|
const raw = env.TENANCY_DRIVER?.trim();
|
|
@@ -195,41 +630,120 @@ var init_tenancyConfig = __esm(() => {
|
|
|
195
630
|
TENANCY_DRIVERS = ["rls", "column", "none"];
|
|
196
631
|
});
|
|
197
632
|
|
|
633
|
+
// ../../src/core/tenant/databaseTenantContext.ts
|
|
634
|
+
import { getDefaultDatabasePool as getDefaultDatabasePool2 } from "@getstrata/core/database/defaultConnection";
|
|
635
|
+
async function applyBypassToTransaction(transaction, bypass) {
|
|
636
|
+
await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, [
|
|
637
|
+
bypass ? "true" : "false"
|
|
638
|
+
]);
|
|
639
|
+
}
|
|
640
|
+
function stringifyBypassIdentifier(identifier) {
|
|
641
|
+
if (typeof identifier === "number") {
|
|
642
|
+
return String(identifier);
|
|
643
|
+
}
|
|
644
|
+
return identifier.trim();
|
|
645
|
+
}
|
|
646
|
+
async function applyIdentifierToTransaction(transaction, identifier) {
|
|
647
|
+
await transaction.unsafe(`SELECT set_config('app.bypass_identifier', $1, true)`, [
|
|
648
|
+
stringifyBypassIdentifier(identifier)
|
|
649
|
+
]);
|
|
650
|
+
}
|
|
651
|
+
function assertBypassIdentifier(identifier) {
|
|
652
|
+
if (typeof identifier === "number") {
|
|
653
|
+
if (!Number.isInteger(identifier) || identifier <= 0) {
|
|
654
|
+
throw new Error("RLS bypass requires a caller-supplied identifier that pins the row.");
|
|
655
|
+
}
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (typeof identifier === "string" && identifier.trim() !== "") {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
throw new Error("RLS bypass requires a caller-supplied identifier that pins the row.");
|
|
662
|
+
}
|
|
663
|
+
async function runWithScopedTenantTransaction(apply, callback) {
|
|
664
|
+
if (!isRlsTenancy()) {
|
|
665
|
+
return await callback();
|
|
666
|
+
}
|
|
667
|
+
if (hasActiveDatabaseConnection()) {
|
|
668
|
+
const connection = getActiveDatabaseConnection(getDefaultDatabasePool2());
|
|
669
|
+
return await runInTransaction(async () => {
|
|
670
|
+
const [previous] = await connection.unsafe(`SELECT current_setting('app.tenant_id', true) AS tenant_id,
|
|
671
|
+
current_setting('app.bypass_rls', true) AS bypass_rls,
|
|
672
|
+
current_setting('app.bypass_identifier', true) AS bypass_identifier`);
|
|
673
|
+
await apply(connection);
|
|
674
|
+
const result = await callback();
|
|
675
|
+
await connection.unsafe(`SELECT set_config('app.tenant_id', $1, true),
|
|
676
|
+
set_config('app.bypass_rls', $2, true), set_config('app.bypass_identifier', $3, true)`, [previous?.tenant_id ?? "", previous?.bypass_rls ?? "", previous?.bypass_identifier ?? ""]);
|
|
677
|
+
return result;
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
const pool = getDefaultDatabasePool2();
|
|
681
|
+
const begin = pool.begin;
|
|
682
|
+
if (typeof begin !== "function") {
|
|
683
|
+
throw new Error("RLS migration bypass requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
|
|
684
|
+
}
|
|
685
|
+
return await settleTransaction(() => runWithDeferredModelEvents(async () => {
|
|
686
|
+
return await begin.bind(pool)(async (transaction) => {
|
|
687
|
+
await apply(transaction);
|
|
688
|
+
return await runWithDatabaseConnection(transaction, () => runWithTransactionScope(async () => commitOrRollbackScope(await callback())));
|
|
689
|
+
});
|
|
690
|
+
}));
|
|
691
|
+
}
|
|
692
|
+
async function runWithMigrationBypass(callback) {
|
|
693
|
+
return await runWithScopedTenantTransaction((transaction) => applyBypassToTransaction(transaction, true), callback);
|
|
694
|
+
}
|
|
695
|
+
async function runWithMigrationBypassForIdentifier(identifier, callback) {
|
|
696
|
+
assertBypassIdentifier(identifier);
|
|
697
|
+
return await runWithScopedTenantTransaction((transaction) => applyIdentifierToTransaction(transaction, identifier), callback);
|
|
698
|
+
}
|
|
699
|
+
var init_databaseTenantContext = __esm(() => {
|
|
700
|
+
init_connectionContext();
|
|
701
|
+
init_transaction();
|
|
702
|
+
init_transactionControl();
|
|
703
|
+
init_deferredModelEvents();
|
|
704
|
+
init_tenancyConfig();
|
|
705
|
+
});
|
|
706
|
+
|
|
198
707
|
// ../../src/core/tenant/tenantDatabaseScope.ts
|
|
199
|
-
import { getDefaultDatabasePool } from "@getstrata/core/database/defaultConnection";
|
|
708
|
+
import { getDefaultDatabasePool as getDefaultDatabasePool3 } from "@getstrata/core/database/defaultConnection";
|
|
200
709
|
async function applyTenantContextToTransaction(transaction, tenantId) {
|
|
201
710
|
await transaction.unsafe(`SELECT set_config('app.tenant_id', $1, true)`, [String(tenantId)]);
|
|
202
711
|
await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
|
|
203
712
|
}
|
|
204
713
|
async function runWithTenantDatabase(tenant, callback) {
|
|
714
|
+
const outerTenant = currentTenant();
|
|
715
|
+
if (hasActiveDatabaseConnection() && outerTenant && outerTenant.id !== tenant.id) {
|
|
716
|
+
throw new Error("Cannot switch tenants inside an active database transaction.");
|
|
717
|
+
}
|
|
205
718
|
if (!isTenancyEnabled() || !isRlsTenancy()) {
|
|
206
719
|
return await runWithTenant(tenant, callback);
|
|
207
720
|
}
|
|
208
721
|
if (hasActiveDatabaseConnection()) {
|
|
209
|
-
const activeConnection = getActiveDatabaseConnection(
|
|
210
|
-
await applyTenantContextToTransaction(activeConnection, tenant.id);
|
|
211
|
-
return await runWithTenant(tenant, callback);
|
|
722
|
+
const activeConnection = getActiveDatabaseConnection(getDefaultDatabasePool3());
|
|
723
|
+
return await runWithScopedTenantTransaction(() => applyTenantContextToTransaction(activeConnection, tenant.id), () => runWithTenant(tenant, callback));
|
|
212
724
|
}
|
|
213
|
-
const pool =
|
|
725
|
+
const pool = getDefaultDatabasePool3();
|
|
214
726
|
const begin = pool.begin;
|
|
215
727
|
if (typeof begin !== "function") {
|
|
216
728
|
throw new Error("RLS tenant scope requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
|
|
217
729
|
}
|
|
218
|
-
return await runWithDeferredModelEvents(async () => {
|
|
219
|
-
return await begin(async (transaction) => {
|
|
730
|
+
return await settleTransaction(() => runWithDeferredModelEvents(async () => {
|
|
731
|
+
return await begin.bind(pool)(async (transaction) => {
|
|
220
732
|
await applyTenantContextToTransaction(transaction, tenant.id);
|
|
221
733
|
return await runWithDatabaseConnection(transaction, async () => {
|
|
222
|
-
return await runWithTenant(tenant, callback);
|
|
734
|
+
return await runWithTransactionScope(async () => commitOrRollbackScope(await runWithTenant(tenant, callback)));
|
|
223
735
|
});
|
|
224
736
|
});
|
|
225
|
-
});
|
|
737
|
+
}));
|
|
226
738
|
}
|
|
227
739
|
function isInsideTenantDatabaseScope(tenantId = currentTenant()?.id) {
|
|
228
740
|
return hasActiveDatabaseConnection() && currentTenant()?.id === tenantId;
|
|
229
741
|
}
|
|
230
742
|
var init_tenantDatabaseScope = __esm(() => {
|
|
231
743
|
init_connectionContext();
|
|
744
|
+
init_transactionControl();
|
|
232
745
|
init_deferredModelEvents();
|
|
746
|
+
init_databaseTenantContext();
|
|
233
747
|
init_tenancyConfig();
|
|
234
748
|
init_tenantContext();
|
|
235
749
|
});
|
|
@@ -64,7 +64,7 @@ export { loadSeedersFromDirectory, runSeedersFromDirectory, } from "../core/data
|
|
|
64
64
|
export type { Seeder, SeederDatabase } from "../core/database/seeders/types.ts";
|
|
65
65
|
export { createSqliteConnection } from "../core/database/sqliteConnection.ts";
|
|
66
66
|
export { defineTable } from "../core/database/table.ts";
|
|
67
|
-
export { runInTransaction } from "../core/database/transaction.ts";
|
|
67
|
+
export { requestTransactionRollback, runInTransaction } from "../core/database/transaction.ts";
|
|
68
68
|
export type { QueryJoin, QueryJoinOn, QueryOptions, QueryOrder, QuerySelectItem, QueryWhere, } from "../core/database/types.ts";
|
|
69
69
|
export type { WhereNode } from "../core/database/whereBuilder.ts";
|
|
70
70
|
export { WhereBuilder } from "../core/database/whereBuilder.ts";
|
|
@@ -89,7 +89,7 @@ export { FormRequest, QueryFormRequest } from "../core/http/formRequest.ts";
|
|
|
89
89
|
export { applyMiddlewareToRoutes, bindRouteModel, buildPaginationMeta, buildRequestCacheKey, composeMiddleware, createAuthMiddleware, createAuthorizeMiddleware, createRequireAuthMiddleware, DEFAULT_PER_PAGE, MAX_PER_PAGE, paginatedResponse, parsePaginationQuery, parsePositiveIntParam, requestIdMiddleware, securedBindRouteModel, securedBindRouteModelByKey, withMiddleware, wrapRouteHandler, } from "../core/http/index.ts";
|
|
90
90
|
export { createLoginThrottleMiddleware, createMemoryLoginThrottleMiddleware, resetMemoryLoginThrottleForTests, resolveLoginEmail, resolveLoginIdentity, } from "../core/http/loginThrottleMiddleware.ts";
|
|
91
91
|
export { createMemoryThrottleMiddleware, resetMemoryThrottleForTests, } from "../core/http/memoryThrottleMiddleware.ts";
|
|
92
|
-
export { createMetricsMiddleware
|
|
92
|
+
export { createMetricsMiddleware } from "../core/http/metricsMiddleware.ts";
|
|
93
93
|
export type { Middleware, RouteHandler } from "../core/http/middleware.ts";
|
|
94
94
|
export { type ParsedUpload, parseMultipartUpload, sanitizeUploadFileName, validateUploadFile, } from "../core/http/parseMultipartUpload.ts";
|
|
95
95
|
export type { RequestMeta } from "../core/http/requestMetaContext.ts";
|