@spfn/core 0.3.0-beta.2 → 0.3.0-beta.21
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 +343 -16
- package/dist/app-config/index.d.ts +134 -0
- package/dist/app-config/index.js +58 -0
- package/dist/app-config/index.js.map +1 -0
- package/dist/authz/index.js +1 -381
- package/dist/authz/index.js.map +1 -1
- package/dist/{boss-D16fO2oG.d.ts → boss-BiDCbbAQ.d.ts} +60 -7
- package/dist/codegen/index.d.ts +93 -15
- package/dist/codegen/index.js +626 -165
- package/dist/codegen/index.js.map +1 -1
- package/dist/config/index.d.ts +0 -63
- package/dist/config/index.js +15 -15
- package/dist/config/index.js.map +1 -1
- package/dist/contract/index.d.ts +40 -17
- package/dist/contract/index.js +147 -23
- package/dist/contract/index.js.map +1 -1
- package/dist/db/index.d.ts +201 -34
- package/dist/db/index.js +294 -158
- package/dist/db/index.js.map +1 -1
- package/dist/{define-middleware-DfDP39Nq.d.ts → define-middleware-CVKgqo8S.d.ts} +1 -1
- package/dist/env/index.d.ts +6 -442
- package/dist/env/index.js +13 -16
- package/dist/env/index.js.map +1 -1
- package/dist/env/loader.js +24 -1
- package/dist/env/loader.js.map +1 -1
- package/dist/error-TsUIjp_x.d.ts +157 -0
- package/dist/errors/index.js +1 -381
- package/dist/errors/index.js.map +1 -1
- package/dist/event/index.d.ts +5 -5
- package/dist/event/index.js +32 -1
- package/dist/event/index.js.map +1 -1
- package/dist/event/sse/client.d.ts +18 -4
- package/dist/event/sse/client.js +38 -2
- package/dist/event/sse/client.js.map +1 -1
- package/dist/event/sse/index.d.ts +5 -4
- package/dist/event/ws/client.d.ts +3 -2
- package/dist/event/ws/index.d.ts +4 -3
- package/dist/job/index.d.ts +48 -3
- package/dist/job/index.js +131 -52
- package/dist/job/index.js.map +1 -1
- package/dist/logger/index.js +0 -12
- package/dist/logger/index.js.map +1 -1
- package/dist/middleware/index.d.ts +6 -2
- package/dist/middleware/index.js +7 -388
- package/dist/middleware/index.js.map +1 -1
- package/dist/nextjs/index.d.ts +88 -8
- package/dist/nextjs/index.js +138 -62
- package/dist/nextjs/index.js.map +1 -1
- package/dist/nextjs/server.d.ts +45 -7
- package/dist/nextjs/server.js +14 -0
- package/dist/nextjs/server.js.map +1 -1
- package/dist/ops/index.d.ts +62 -43
- package/dist/ops/index.js +638 -97
- package/dist/ops/index.js.map +1 -1
- package/dist/registry-C2KQCh0m.d.ts +473 -0
- package/dist/route/index.d.ts +6 -3
- package/dist/route/index.js +97 -28
- package/dist/route/index.js.map +1 -1
- package/dist/route/types.d.ts +7 -1
- package/dist/{router-Qbssr11H.d.ts → route-builder-DRW-MSCJ.d.ts} +52 -127
- package/dist/router-4YoIy5fN.d.ts +172 -0
- package/dist/{token-manager-BT5EnUAR.d.ts → router-CysBmgv3.d.ts} +81 -127
- package/dist/server/index.d.ts +185 -14
- package/dist/server/index.js +883 -78
- package/dist/server/index.js.map +1 -1
- package/dist/surface/index.d.ts +115 -0
- package/dist/surface/index.js +1003 -0
- package/dist/surface/index.js.map +1 -0
- package/dist/token-manager-Ckjt2-qq.d.ts +122 -0
- package/dist/{types-D1c57Ko-.d.ts → types-5Rl11_Nb.d.ts} +43 -4
- package/dist/{types-ZQODsBft.d.ts → types-B8zAxFsE.d.ts} +2 -1
- package/dist/types-CxhPH4s_.d.ts +375 -0
- package/dist/{types-2AbaW4Ie.d.ts → types-W4u_S0lF.d.ts} +4 -3
- package/docs/file-upload.md +195 -333
- package/package.json +20 -9
- package/src/cache/README.md +330 -0
- package/src/codegen/README.md +634 -0
- package/src/config/README.md +333 -0
- package/src/contract/README.md +440 -0
- package/src/db/README.md +589 -0
- package/src/db/manager/README.md +500 -0
- package/src/db/schema/README.md +354 -0
- package/src/db/transaction/README.md +822 -0
- package/src/env/README.md +787 -0
- package/src/errors/README.md +429 -0
- package/src/event/README.md +789 -0
- package/src/job/README.md +613 -0
- package/src/logger/README.md +321 -0
- package/src/middleware/README.md +634 -0
- package/src/nextjs/README.md +688 -0
- package/src/route/README.md +763 -0
- package/src/security/README.md +100 -0
- package/src/server/README.md +704 -0
- package/dist/types-9oszaJqp.d.ts +0 -250
package/dist/db/index.d.ts
CHANGED
|
@@ -246,7 +246,7 @@ declare function getDatabase<TDatabase extends DrizzleDatabase = DefaultDatabase
|
|
|
246
246
|
* // Set custom database instances (testing)
|
|
247
247
|
* const writeClient = postgres('postgresql://primary:5432/mydb');
|
|
248
248
|
* const readClient = postgres('postgresql://replica:5432/mydb');
|
|
249
|
-
* setDatabase(drizzle(writeClient), drizzle(readClient));
|
|
249
|
+
* setDatabase(drizzle({ client: writeClient }), drizzle({ client: readClient }));
|
|
250
250
|
*
|
|
251
251
|
* // Clear instances (not recommended - use closeDatabase() instead)
|
|
252
252
|
* setDatabase(undefined, undefined);
|
|
@@ -397,27 +397,6 @@ declare function getDatabaseInfo(): {
|
|
|
397
397
|
providerKind?: string;
|
|
398
398
|
};
|
|
399
399
|
|
|
400
|
-
/**
|
|
401
|
-
* Reconnect Trigger — Query-error driven pool rebuild
|
|
402
|
-
*
|
|
403
|
-
* Complements the periodic health check with a fast-path: when application
|
|
404
|
-
* queries start failing with connection-level errors, we do not wait up to
|
|
405
|
-
* DB_HEALTH_CHECK_INTERVAL (default 60s) to notice. A sliding-window counter
|
|
406
|
-
* trips a force-reconnect as soon as the failure rate crosses a threshold.
|
|
407
|
-
*
|
|
408
|
-
* Why this exists:
|
|
409
|
-
* - postgres.js transparently drops dead sockets and opens new ones on the
|
|
410
|
-
* next query. A single `SELECT 1` on the periodic interval can therefore
|
|
411
|
-
* false-pass while user-facing queries keep hitting the remaining dead
|
|
412
|
-
* sockets in the pool.
|
|
413
|
-
* - This module observes real query errors and, when it sees a burst of
|
|
414
|
-
* connection-level failures, calls triggerForceReconnect() which performs
|
|
415
|
-
* the same atomic-swap rebuild as the health check.
|
|
416
|
-
*
|
|
417
|
-
* Configuration (env vars, hardcoded defaults):
|
|
418
|
-
* - DB_RECONNECT_ERROR_THRESHOLD (default 3): errors needed in window
|
|
419
|
-
* - DB_RECONNECT_ERROR_WINDOW_MS (default 10000): sliding window size
|
|
420
|
-
*/
|
|
421
400
|
/**
|
|
422
401
|
* Determine whether an error looks like a pool/connection failure
|
|
423
402
|
*
|
|
@@ -505,7 +484,7 @@ declare function checkConnection(client: Sql): Promise<boolean>;
|
|
|
505
484
|
interface DrizzleConfigOptions {
|
|
506
485
|
/** Database connection URL (defaults to process.env.DATABASE_URL) */
|
|
507
486
|
databaseUrl?: string;
|
|
508
|
-
/** Schema
|
|
487
|
+
/** Schema entry file(s), directories or glob pattern(s). Default: './src/server/entities/\*\*\/*.ts', or the registry `DRIZZLE_SCHEMA_PATH` (./src/server/entities/config.ts) when that scan finds no entity file; `schemaRegistry` reports the registry when both exist */
|
|
509
488
|
schema?: string | string[];
|
|
510
489
|
/** Migration output directory (defaults to './src/server/drizzle') */
|
|
511
490
|
out?: string;
|
|
@@ -521,8 +500,6 @@ interface DrizzleConfigOptions {
|
|
|
521
500
|
expandGlobs?: boolean;
|
|
522
501
|
/** PostgreSQL schema filter for push/introspect commands */
|
|
523
502
|
schemaFilter?: string[];
|
|
524
|
-
/** Auto-detect PostgreSQL schemas from entity files (requires expandGlobs: true) */
|
|
525
|
-
autoDetectSchemas?: boolean;
|
|
526
503
|
/** Migration prefix strategy (default: 'timestamp') */
|
|
527
504
|
migrationPrefix?: 'index' | 'timestamp' | 'unix' | 'none';
|
|
528
505
|
}
|
|
@@ -551,6 +528,8 @@ declare function detectDialect(url: string): 'postgresql' | 'mysql' | 'sqlite';
|
|
|
551
528
|
*/
|
|
552
529
|
declare function getDrizzleConfig(options?: DrizzleConfigOptions): {
|
|
553
530
|
schema: string | string[];
|
|
531
|
+
schemaSource: string;
|
|
532
|
+
schemaRegistry: string | undefined;
|
|
554
533
|
out: string;
|
|
555
534
|
dialect: "postgresql" | "mysql" | "sqlite";
|
|
556
535
|
dbCredentials: {
|
|
@@ -562,6 +541,8 @@ declare function getDrizzleConfig(options?: DrizzleConfigOptions): {
|
|
|
562
541
|
schemaFilter?: undefined;
|
|
563
542
|
} | {
|
|
564
543
|
schema: string | string[];
|
|
544
|
+
schemaSource: string;
|
|
545
|
+
schemaRegistry: string | undefined;
|
|
565
546
|
out: string;
|
|
566
547
|
dialect: "postgresql" | "mysql" | "sqlite";
|
|
567
548
|
dbCredentials: {
|
|
@@ -573,10 +554,29 @@ declare function getDrizzleConfig(options?: DrizzleConfigOptions): {
|
|
|
573
554
|
};
|
|
574
555
|
};
|
|
575
556
|
/**
|
|
576
|
-
*
|
|
557
|
+
* The schema fields a rendered drizzle.config.ts needs
|
|
558
|
+
*/
|
|
559
|
+
interface RenderableDrizzleConfig {
|
|
560
|
+
schema: string | string[];
|
|
561
|
+
out: string;
|
|
562
|
+
dialect: string;
|
|
563
|
+
dbCredentials: Record<string, unknown>;
|
|
564
|
+
schemaFilter?: string[];
|
|
565
|
+
migrations?: Record<string, unknown>;
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Render an already-resolved config as drizzle.config.ts source, schema
|
|
569
|
+
* paths made absolute for Drizzle Studio
|
|
577
570
|
*
|
|
578
|
-
* @param
|
|
579
|
-
* @
|
|
571
|
+
* @param config - Result of getDrizzleConfig, possibly with the schema replaced
|
|
572
|
+
* @param cwd - Base for relative schema paths (default: process.cwd())
|
|
573
|
+
*/
|
|
574
|
+
declare function renderDrizzleConfig(config: RenderableDrizzleConfig, cwd?: string): string;
|
|
575
|
+
/**
|
|
576
|
+
* Generate drizzle.config.ts source from options: resolve the config, then render it
|
|
577
|
+
*
|
|
578
|
+
* @param options - Configuration options (see getDrizzleConfig)
|
|
579
|
+
* @returns drizzle.config.ts source
|
|
580
580
|
*/
|
|
581
581
|
declare function generateDrizzleConfigFile(options?: DrizzleConfigOptions): string;
|
|
582
582
|
|
|
@@ -1139,6 +1139,23 @@ type TransactionDB<TDatabase extends DrizzleDatabase = DefaultDatabase> = Databa
|
|
|
1139
1139
|
* afterCommit callback type
|
|
1140
1140
|
*/
|
|
1141
1141
|
type AfterCommitCallback = () => void | Promise<void>;
|
|
1142
|
+
/**
|
|
1143
|
+
* beforeCommit callback type
|
|
1144
|
+
*/
|
|
1145
|
+
type BeforeCommitCallback = () => void | Promise<void>;
|
|
1146
|
+
/**
|
|
1147
|
+
* afterRollback callback type
|
|
1148
|
+
*/
|
|
1149
|
+
type AfterRollbackCallback = () => void | Promise<void>;
|
|
1150
|
+
/**
|
|
1151
|
+
* Serializes the savepoint frames opened directly off one transaction context
|
|
1152
|
+
*
|
|
1153
|
+
* @see createNestedFrameGate
|
|
1154
|
+
*/
|
|
1155
|
+
type NestedFrameGate = {
|
|
1156
|
+
/** Run `frame` once every frame queued before it on this context has finished */
|
|
1157
|
+
run<T>(frame: () => Promise<T>): Promise<T>;
|
|
1158
|
+
};
|
|
1142
1159
|
/**
|
|
1143
1160
|
* Transaction context stored in AsyncLocalStorage
|
|
1144
1161
|
*/
|
|
@@ -1148,8 +1165,14 @@ type TransactionContext<TDatabase extends DrizzleDatabase = DrizzleDatabase> = {
|
|
|
1148
1165
|
/** Unique transaction ID for logging and tracing */
|
|
1149
1166
|
txId: string;
|
|
1150
1167
|
level: number;
|
|
1168
|
+
/** Callbacks to execute before the root transaction commits (still inside it) */
|
|
1169
|
+
beforeCommitCallbacks: BeforeCommitCallback[];
|
|
1151
1170
|
/** Callbacks to execute after root transaction commits */
|
|
1152
1171
|
afterCommitCallbacks: AfterCommitCallback[];
|
|
1172
|
+
/** Callbacks to execute after the root transaction rolled back */
|
|
1173
|
+
afterRollbackCallbacks: AfterRollbackCallback[];
|
|
1174
|
+
/** Serializes the savepoint frames opened directly off THIS context */
|
|
1175
|
+
nestedFrames: NestedFrameGate;
|
|
1153
1176
|
};
|
|
1154
1177
|
/**
|
|
1155
1178
|
* Get current transaction object and metadata from AsyncLocalStorage
|
|
@@ -1187,7 +1210,7 @@ callback: () => Promise<T>): Promise<T>;
|
|
|
1187
1210
|
*
|
|
1188
1211
|
* @example
|
|
1189
1212
|
* ```typescript
|
|
1190
|
-
* import { onAfterCommit } from '@spfn/core/db
|
|
1213
|
+
* import { onAfterCommit } from '@spfn/core/db';
|
|
1191
1214
|
*
|
|
1192
1215
|
* async function submit(spaceId: string, chatId: string)
|
|
1193
1216
|
* {
|
|
@@ -1201,6 +1224,77 @@ callback: () => Promise<T>): Promise<T>;
|
|
|
1201
1224
|
* ```
|
|
1202
1225
|
*/
|
|
1203
1226
|
declare function onAfterCommit(callback: AfterCommitCallback): void;
|
|
1227
|
+
/**
|
|
1228
|
+
* Register a callback to run just before the current transaction commits
|
|
1229
|
+
*
|
|
1230
|
+
* - Inside a transaction: queued and executed after the root callback resolves,
|
|
1231
|
+
* while the transaction is still open — the callback MAY run statements and
|
|
1232
|
+
* they are part of the same commit
|
|
1233
|
+
* - Outside a transaction: executed immediately with a WARNING (same rationale as
|
|
1234
|
+
* onAfterCommit — there is nothing left to commit), and the abort semantics
|
|
1235
|
+
* below do not apply: there is no transaction for a throw to roll back
|
|
1236
|
+
* - Nested transactions: callbacks bubble up to root transaction
|
|
1237
|
+
* - Callbacks run in registration order, inside the transaction context
|
|
1238
|
+
* (getTransaction() returns the live tx)
|
|
1239
|
+
* - The queue is snapshot before the pass, so a callback registered BY a
|
|
1240
|
+
* beforeCommit callback does not run for this commit — it would otherwise
|
|
1241
|
+
* grow the queue mid-iteration and loop forever inside the open transaction
|
|
1242
|
+
* - A throw ABORTS: later callbacks are skipped, the transaction rolls back,
|
|
1243
|
+
* the error propagates to the caller, and afterRollback callbacks fire
|
|
1244
|
+
*
|
|
1245
|
+
* @example
|
|
1246
|
+
* ```typescript
|
|
1247
|
+
* import { runInTransaction, onBeforeCommit } from '@spfn/core/db';
|
|
1248
|
+
*
|
|
1249
|
+
* async function transfer(fromId: string, toId: string, amount: number)
|
|
1250
|
+
* {
|
|
1251
|
+
* // The transaction is what gives the check teeth — registered outside one,
|
|
1252
|
+
* // the callback runs immediately and its throw aborts nothing.
|
|
1253
|
+
* await runInTransaction(async () =>
|
|
1254
|
+
* {
|
|
1255
|
+
* await accountRepo.debit(fromId, amount);
|
|
1256
|
+
* await accountRepo.credit(toId, amount);
|
|
1257
|
+
*
|
|
1258
|
+
* // Last-moment invariant check: a throw here rolls the whole transfer back.
|
|
1259
|
+
* onBeforeCommit(() => assertNoNegativeBalance(fromId));
|
|
1260
|
+
* });
|
|
1261
|
+
* }
|
|
1262
|
+
* ```
|
|
1263
|
+
*/
|
|
1264
|
+
declare function onBeforeCommit(callback: BeforeCommitCallback): void;
|
|
1265
|
+
/**
|
|
1266
|
+
* Register a callback to run after the current transaction rolled back
|
|
1267
|
+
*
|
|
1268
|
+
* - Inside a transaction: queued and executed after the ROOT transaction rolled
|
|
1269
|
+
* back, before the causing error leaves runInTransaction / the middleware
|
|
1270
|
+
* - Outside a transaction: no-op with a warning — there is no rollback to wait for
|
|
1271
|
+
* - Nested transactions: callbacks bubble up to root transaction. A nested
|
|
1272
|
+
* rollback that the root survives does NOT fire them; these hooks are about
|
|
1273
|
+
* the root transaction's fate, not a savepoint's
|
|
1274
|
+
* - Callbacks run outside the transaction context (new connection for DB access)
|
|
1275
|
+
* - Errors are logged but never thrown: the original error keeps propagating
|
|
1276
|
+
* unchanged, never replaced by a callback failure
|
|
1277
|
+
*
|
|
1278
|
+
* @example
|
|
1279
|
+
* ```typescript
|
|
1280
|
+
* import { runInTransaction, onAfterRollback } from '@spfn/core/db';
|
|
1281
|
+
*
|
|
1282
|
+
* async function importAvatar(userId: string, file: Blob)
|
|
1283
|
+
* {
|
|
1284
|
+
* // Upload first: external I/O never belongs inside the transaction.
|
|
1285
|
+
* const key = await objectStore.put(file);
|
|
1286
|
+
*
|
|
1287
|
+
* await runInTransaction(async () =>
|
|
1288
|
+
* {
|
|
1289
|
+
* await userRepo.updateAvatar(userId, key);
|
|
1290
|
+
*
|
|
1291
|
+
* // The upload cannot roll back on its own — undo it if the write never lands.
|
|
1292
|
+
* onAfterRollback(() => objectStore.delete(key));
|
|
1293
|
+
* });
|
|
1294
|
+
* }
|
|
1295
|
+
* ```
|
|
1296
|
+
*/
|
|
1297
|
+
declare function onAfterRollback(callback: AfterRollbackCallback): void;
|
|
1204
1298
|
|
|
1205
1299
|
/**
|
|
1206
1300
|
* Transaction middleware options
|
|
@@ -1245,6 +1339,21 @@ interface TransactionalOptions {
|
|
|
1245
1339
|
* @default 30000 (30s) or TRANSACTION_IDLE_TIMEOUT environment variable
|
|
1246
1340
|
*/
|
|
1247
1341
|
idleTimeout?: number;
|
|
1342
|
+
/**
|
|
1343
|
+
* Run in an independent transaction instead of joining an ambient one.
|
|
1344
|
+
*
|
|
1345
|
+
* Only bites when the middleware itself runs nested — a sub-app mounted
|
|
1346
|
+
* under a route that already applied `Transactional()`, or a handler invoked
|
|
1347
|
+
* from inside `runInTransaction`. By default that inner run takes a SAVEPOINT
|
|
1348
|
+
* on the outer transaction; `requiresNew: true` gives it a real `BEGIN` on a
|
|
1349
|
+
* second pooled connection, with its own timeouts and its own hook queues.
|
|
1350
|
+
*
|
|
1351
|
+
* See `RunInTransactionOptions.requiresNew` for the pool and self-deadlock
|
|
1352
|
+
* costs — they apply here unchanged.
|
|
1353
|
+
*
|
|
1354
|
+
* @default false
|
|
1355
|
+
*/
|
|
1356
|
+
requiresNew?: boolean;
|
|
1248
1357
|
}
|
|
1249
1358
|
/**
|
|
1250
1359
|
* Transaction middleware for Hono routes
|
|
@@ -1282,6 +1391,9 @@ interface TransactionalOptions {
|
|
|
1282
1391
|
* - Success: Auto-commit
|
|
1283
1392
|
* - Error: Auto-rollback
|
|
1284
1393
|
* - Detects context.error to trigger rollback
|
|
1394
|
+
* - Hooks: this delegates to runInTransaction, so onBeforeCommit, onAfterCommit
|
|
1395
|
+
* and onAfterRollback behave exactly as they do there. afterRollback callbacks
|
|
1396
|
+
* have already run by the time the error reaches the conversion below.
|
|
1285
1397
|
*
|
|
1286
1398
|
* 📊 Transaction logging:
|
|
1287
1399
|
* - Auto-logs transaction start/commit/rollback
|
|
@@ -1317,8 +1429,12 @@ interface RunInTransactionOptions {
|
|
|
1317
1429
|
* - `timeout: undefined` - Uses default (30s or TRANSACTION_TIMEOUT env var)
|
|
1318
1430
|
* - `timeout: N` - Sets timeout to N milliseconds (1 to 2147483647)
|
|
1319
1431
|
*
|
|
1320
|
-
* Note: Timeout is only applied to root transactions.
|
|
1321
|
-
*
|
|
1432
|
+
* Note: Timeout is only applied to root transactions. A nested call takes a
|
|
1433
|
+
* SAVEPOINT on the outer transaction's connection, where the outer
|
|
1434
|
+
* transaction's `SET LOCAL statement_timeout` is already in force — so the
|
|
1435
|
+
* nested call genuinely inherits it, and its own `timeout` is ignored (a
|
|
1436
|
+
* warning is logged when the caller passed one explicitly). A
|
|
1437
|
+
* `requiresNew: true` call is a root and gets its own.
|
|
1322
1438
|
*
|
|
1323
1439
|
* @default 30000 (30 seconds) or TRANSACTION_TIMEOUT environment variable
|
|
1324
1440
|
*
|
|
@@ -1355,6 +1471,48 @@ interface RunInTransactionOptions {
|
|
|
1355
1471
|
* @default 'transaction'
|
|
1356
1472
|
*/
|
|
1357
1473
|
context?: string;
|
|
1474
|
+
/**
|
|
1475
|
+
* Run in an independent transaction instead of joining an ambient one.
|
|
1476
|
+
*
|
|
1477
|
+
* By default a call made while another transaction is open takes a SAVEPOINT
|
|
1478
|
+
* on that transaction's connection: its writes commit or roll back with the
|
|
1479
|
+
* outer transaction. `requiresNew: true` opens a real `BEGIN` on a SECOND
|
|
1480
|
+
* pooled connection instead, so the work commits on its own and survives an
|
|
1481
|
+
* outer rollback — an audit trail or a failed-attempt record, for example.
|
|
1482
|
+
*
|
|
1483
|
+
* Being a root transaction, it gets its own `statement_timeout`,
|
|
1484
|
+
* `idle_in_transaction_session_timeout`, and its OWN hook queues:
|
|
1485
|
+
* `onBeforeCommit` / `onAfterCommit` / `onAfterRollback` registered inside it
|
|
1486
|
+
* fire on ITS outcome, not the outer transaction's.
|
|
1487
|
+
*
|
|
1488
|
+
* Two costs, both consequences of the second connection:
|
|
1489
|
+
* - It holds a second connection for its whole duration, so it counts twice
|
|
1490
|
+
* against the pool. Keep it short and don't fan it out.
|
|
1491
|
+
* - It cannot see the outer transaction's uncommitted writes, and it BLOCKS
|
|
1492
|
+
* on any row the outer transaction has locked. Since the outer transaction
|
|
1493
|
+
* is waiting for this call to return, that block is a self-deadlock that
|
|
1494
|
+
* only `statement_timeout` breaks. Never touch rows the outer transaction
|
|
1495
|
+
* wrote.
|
|
1496
|
+
*
|
|
1497
|
+
* @default false
|
|
1498
|
+
*
|
|
1499
|
+
* @example
|
|
1500
|
+
* ```typescript
|
|
1501
|
+
* await runInTransaction(async () =>
|
|
1502
|
+
* {
|
|
1503
|
+
* await orderRepo.create(order);
|
|
1504
|
+
*
|
|
1505
|
+
* // Lands even if the order below rolls the outer transaction back.
|
|
1506
|
+
* await runInTransaction(
|
|
1507
|
+
* () => auditRepo.record('order.attempted', order.id),
|
|
1508
|
+
* { requiresNew: true },
|
|
1509
|
+
* );
|
|
1510
|
+
*
|
|
1511
|
+
* await inventoryRepo.reserve(order.items); // may throw
|
|
1512
|
+
* });
|
|
1513
|
+
* ```
|
|
1514
|
+
*/
|
|
1515
|
+
requiresNew?: boolean;
|
|
1358
1516
|
}
|
|
1359
1517
|
/**
|
|
1360
1518
|
* Run a callback function within a database transaction
|
|
@@ -1366,6 +1524,15 @@ interface RunInTransactionOptions {
|
|
|
1366
1524
|
* - Warns about slow transactions
|
|
1367
1525
|
* - Enforces timeout if configured
|
|
1368
1526
|
*
|
|
1527
|
+
* Called with a transaction already open on the async call chain, it takes a
|
|
1528
|
+
* SAVEPOINT on that transaction rather than opening an independent one: the work
|
|
1529
|
+
* runs on the same connection, sees the outer transaction's uncommitted writes,
|
|
1530
|
+
* and commits with it. A throw that the caller catches unwinds to the SAVEPOINT
|
|
1531
|
+
* and leaves the outer transaction healthy; a throw that propagates rolls the
|
|
1532
|
+
* whole thing back. Nested calls off one transaction are serialized — see
|
|
1533
|
+
* `openTransaction` — so `Promise.all` over them runs them one at a time. Pass
|
|
1534
|
+
* `requiresNew: true` for an independent transaction on its own connection.
|
|
1535
|
+
*
|
|
1369
1536
|
* Errors are propagated to the caller without modification.
|
|
1370
1537
|
* Caller is responsible for error handling and conversion.
|
|
1371
1538
|
*
|
|
@@ -1680,11 +1847,11 @@ declare function count<T extends PgTable>(table: T, where?: WhereObject<InferSel
|
|
|
1680
1847
|
*
|
|
1681
1848
|
* @example With Transactions
|
|
1682
1849
|
* ```typescript
|
|
1683
|
-
* import {
|
|
1850
|
+
* import { runInTransaction } from '@spfn/core/db';
|
|
1684
1851
|
*
|
|
1685
1852
|
* const userRepo = new UserRepository();
|
|
1686
1853
|
*
|
|
1687
|
-
* await
|
|
1854
|
+
* await runInTransaction(async () => {
|
|
1688
1855
|
* // Both db and readDb automatically use the transaction context
|
|
1689
1856
|
* const user = await userRepo.create({ name: 'John' });
|
|
1690
1857
|
* await userRepo.findById(user.id); // Uses same transaction
|
|
@@ -2006,4 +2173,4 @@ declare abstract class BaseRepository<TRelations extends AnyRelations = EmptyRel
|
|
|
2006
2173
|
protected _count<T extends PgTable>(table: T, where?: Record<string, any> | SQL | undefined): Promise<number>;
|
|
2007
2174
|
}
|
|
2008
2175
|
|
|
2009
|
-
export { type AfterCommitCallback, BaseRepository, type DatabaseClients, type DatabaseInitOptions, type DatabaseOptions, type DatabaseProvider, type DatabaseTransaction, type DefaultDatabase, type DrizzleConfigOptions, type DrizzleDatabase, type FunctionMigrationEntry, type FunctionMigrationInfo, type MigrationStatus, type MigrationStatusDb, type MigrationTargetStatus, PROJECT_MIGRATIONS_TABLE, PROJECT_TARGET_NAME, type PoolConfig, RUN_MIGRATIONS_HINT, type RepositoryDatabase, RepositoryError, type RetryConfig, type RunInTransactionOptions, type TransactionContext, type TransactionDB, Transactional, type TransactionalOptions, auditFields, checkConnection, closeDatabase, collectMigrationStatus, count, countPendingMigrations, create, createDatabaseConnection, createDatabaseFromEnv, createMany, createSchema, deleteMany, deleteOne, detectDialect, discoverFunctionMigrations, enumText, filterPendingEntries, findMany, findOne, forceReconnectDatabase, foreignKey, formatPendingMigrations, fromPostgresError, functionMigrationsTable, generateDrizzleConfigFile, getDatabase, getDatabaseInfo, getDrizzleConfig, getSchemaInfo, getTransaction, getTransactionContext, hasMigrationTargets, id, initDatabase, isConnectionLevelError, migrationTargets, onAfterCommit, optionalForeignKey, packageNameToSchema, pendingMigrationTargets, pendingMigrationsSummary, projectMigrationsDir, publishingFields, readMigrationEntries, reportDatabaseError, resetConnectionErrorCounter, runInTransaction, runWithTransaction, setDatabase, setDatabaseProvider, softDelete, timestamps, typedJsonb, updateMany, updateOne, upsert, utcTimestamp, uuid, verificationTimestamp };
|
|
2176
|
+
export { type AfterCommitCallback, type AfterRollbackCallback, BaseRepository, type BeforeCommitCallback, type DatabaseClients, type DatabaseInitOptions, type DatabaseOptions, type DatabaseProvider, type DatabaseTransaction, type DefaultDatabase, type DrizzleConfigOptions, type DrizzleDatabase, type FunctionMigrationEntry, type FunctionMigrationInfo, type MigrationStatus, type MigrationStatusDb, type MigrationTargetStatus, type NestedFrameGate, PROJECT_MIGRATIONS_TABLE, PROJECT_TARGET_NAME, type PoolConfig, RUN_MIGRATIONS_HINT, type RenderableDrizzleConfig, type RepositoryDatabase, RepositoryError, type RetryConfig, type RunInTransactionOptions, type TransactionContext, type TransactionDB, Transactional, type TransactionalOptions, auditFields, checkConnection, closeDatabase, collectMigrationStatus, count, countPendingMigrations, create, createDatabaseConnection, createDatabaseFromEnv, createMany, createSchema, deleteMany, deleteOne, detectDialect, discoverFunctionMigrations, enumText, filterPendingEntries, findMany, findOne, forceReconnectDatabase, foreignKey, formatPendingMigrations, fromPostgresError, functionMigrationsTable, generateDrizzleConfigFile, getDatabase, getDatabaseInfo, getDrizzleConfig, getSchemaInfo, getTransaction, getTransactionContext, hasMigrationTargets, id, initDatabase, isConnectionLevelError, migrationTargets, onAfterCommit, onAfterRollback, onBeforeCommit, optionalForeignKey, packageNameToSchema, pendingMigrationTargets, pendingMigrationsSummary, projectMigrationsDir, publishingFields, readMigrationEntries, renderDrizzleConfig, reportDatabaseError, resetConnectionErrorCounter, runInTransaction, runWithTransaction, setDatabase, setDatabaseProvider, softDelete, timestamps, typedJsonb, updateMany, updateOne, upsert, utcTimestamp, uuid, verificationTimestamp };
|