@getstrata/core 2.2.60 → 3.0.4
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 +26 -0
- package/dist/core/audit/exportAuditLogs.d.ts +12 -3
- package/dist/core/audit/exportStore.d.ts +6 -2
- package/dist/core/audit/siemFormatter.d.ts +2 -2
- package/dist/core/auth/abilityCatalog.d.ts +1 -9
- package/dist/core/auth/authContext.d.ts +2 -0
- package/dist/core/auth/authOutcome.d.ts +32 -0
- package/dist/core/auth/authReader.d.ts +22 -0
- package/dist/core/auth/basicAuthGuard.d.ts +19 -3
- package/dist/core/auth/emailVerification.d.ts +1 -1
- package/dist/core/auth/guard.d.ts +27 -6
- package/dist/core/auth/intendedUrlCookie.d.ts +13 -7
- package/dist/core/auth/jwt.d.ts +5 -4
- package/dist/core/auth/jwtGuard.d.ts +7 -6
- package/dist/core/auth/membershipContext.d.ts +10 -7
- package/dist/core/auth/membershipContextMiddleware.d.ts +3 -3
- package/dist/core/auth/membershipMiddleware.d.ts +1 -2
- package/dist/core/auth/membershipServiceImplementation.d.ts +1 -1
- package/dist/core/auth/oauth/oidcIdToken.d.ts +25 -16
- package/dist/core/auth/oauth/oidcProvider.d.ts +6 -4
- package/dist/core/auth/oauth/providers.d.ts +3 -1
- package/dist/core/auth/oneTimeToken.d.ts +2 -3
- package/dist/core/auth/passwordConfirmCookie.d.ts +15 -6
- package/dist/core/auth/passwordLogin.d.ts +5 -5
- package/dist/core/auth/pendingMfaCookie.d.ts +2 -2
- package/dist/core/auth/policy.d.ts +23 -10
- package/dist/core/auth/requireAuthCapability.d.ts +3 -0
- package/dist/core/auth/resolveMembershipService.d.ts +3 -2
- package/dist/core/auth/saml/samlAssertionReplay.d.ts +4 -4
- package/dist/core/auth/saml/samlServiceProvider.d.ts +11 -7
- package/dist/core/auth/scimAuthMiddleware.d.ts +10 -3
- package/dist/core/auth/sessionCookie.d.ts +20 -9
- package/dist/core/auth/sessionGuard.d.ts +14 -3
- package/dist/core/cache/createCacheStore.d.ts +6 -3
- package/dist/core/cache/redisCacheStore.d.ts +5 -1
- package/dist/core/cache/repository.d.ts +5 -1
- package/dist/core/cache/simpleCache.d.ts +2 -1
- package/dist/core/cache/tokens.d.ts +8 -0
- package/dist/core/config/applicationConfig.d.ts +21 -0
- package/dist/core/config/configSnapshot.d.ts +3 -0
- package/dist/core/config/envSchema.d.ts +3 -2
- package/dist/core/contracts/application.d.ts +153 -0
- package/dist/core/contracts/authUserDirectory.d.ts +24 -7
- package/dist/core/contracts/container.d.ts +20 -23
- package/dist/core/contracts/di.d.ts +4 -2
- package/dist/core/contracts/executionContext.d.ts +5 -0
- package/dist/core/contracts/executionOperations.d.ts +10 -0
- package/dist/core/contracts/executionOwnership.d.ts +38 -0
- package/dist/core/contracts/persistence.d.ts +20 -0
- package/dist/core/contracts/serviceTokens.d.ts +32 -18
- package/dist/core/contracts/services.d.ts +63 -0
- package/dist/core/contracts/token.d.ts +19 -0
- package/dist/core/crypto/fieldEncryption.d.ts +19 -7
- package/dist/core/crypto/fieldEncryptionRotation.d.ts +21 -5
- package/dist/core/crypto/mfaSecret.d.ts +10 -3
- package/dist/core/database/baseRepository.d.ts +14 -10
- package/dist/core/database/bindings.d.ts +6 -0
- package/dist/core/database/bunSql.d.ts +3 -12
- package/dist/core/database/definitions.d.ts +209 -0
- package/dist/core/database/dialect.d.ts +5 -24
- package/dist/core/database/driverPorts.d.ts +116 -0
- package/dist/core/database/errors.d.ts +47 -7
- package/dist/core/database/factory.d.ts +51 -31
- package/dist/core/database/index.d.ts +9 -10
- package/dist/core/database/keyset.d.ts +2 -1
- package/dist/core/database/legacyNativeTransaction.d.ts +5 -0
- package/dist/core/database/manager.d.ts +12 -0
- package/dist/core/database/model.d.ts +91 -97
- package/dist/core/database/modelQuery.d.ts +9 -5
- package/dist/core/database/modelRegistry.d.ts +52 -33
- package/dist/core/database/mysqlConnection.d.ts +1 -19
- package/dist/core/database/mysqlNative.d.ts +38 -0
- package/dist/core/database/nativeTransactions.d.ts +16 -0
- package/dist/core/database/ormInternal.d.ts +85 -0
- package/dist/core/database/ormRuntime.d.ts +4 -0
- package/dist/core/database/ownedReadOnlyObservation.d.ts +16 -0
- package/dist/core/database/postgresNative.d.ts +11 -0
- package/dist/core/database/query.d.ts +24 -23
- package/dist/core/database/queryPlanSnapshot.d.ts +7 -0
- package/dist/core/database/rawMysql.d.ts +7 -0
- package/dist/core/database/rawPostgres.d.ts +23 -0
- package/dist/core/database/rawSqlite.d.ts +6 -0
- package/dist/core/database/relationQueries.d.ts +3 -2
- package/dist/core/database/relationQuery.d.ts +24 -6
- package/dist/core/database/repositoryQuery.d.ts +6 -1
- package/dist/core/database/schema/driver.d.ts +1 -1
- package/dist/core/database/seeders/types.d.ts +1 -1
- package/dist/core/database/session.d.ts +13 -0
- package/dist/core/database/sessionFactory.d.ts +9 -0
- package/dist/core/database/sqlDialects.d.ts +22 -0
- package/dist/core/database/tokens.d.ts +7 -0
- package/dist/core/database/transactionSession.d.ts +43 -0
- package/dist/core/errors/http.d.ts +4 -1
- package/dist/core/events/{eventBus.d.ts → eventBusClass.d.ts} +1 -3
- package/dist/core/events/index.d.ts +2 -3
- package/dist/core/events/outbox/index.d.ts +44 -13
- package/dist/core/events/outbox/metrics.d.ts +4 -4
- package/dist/core/events/outbox/workerTransport.d.ts +17 -0
- package/dist/core/http/authMiddleware.d.ts +2 -2
- package/dist/core/http/authorizeMiddleware.d.ts +2 -2
- package/dist/core/http/bodySizeLimitMiddleware.d.ts +1 -1
- package/dist/core/http/conditionalResponse.d.ts +7 -2
- package/dist/core/http/contentSecurityPolicy.d.ts +4 -3
- package/dist/core/http/corsMiddleware.d.ts +2 -1
- package/dist/core/http/csrfMiddleware.d.ts +6 -1
- package/dist/core/http/csrfToken.d.ts +17 -7
- package/dist/core/http/databaseError.d.ts +4 -0
- package/dist/core/http/etag.d.ts +5 -2
- package/dist/core/http/flashMiddleware.d.ts +1 -1
- package/dist/core/http/flashSession.d.ts +13 -7
- package/dist/core/http/index.d.ts +1 -1
- package/dist/core/http/loginThrottleMiddleware.d.ts +3 -3
- package/dist/core/http/memoryThrottleMiddleware.d.ts +2 -2
- package/dist/core/http/parseMultipartUpload.d.ts +3 -2
- package/dist/core/http/requestMetaContext.d.ts +5 -0
- package/dist/core/http/requireAbilityMiddleware.d.ts +1 -1
- package/dist/core/http/requireAuthMiddleware.d.ts +2 -2
- package/dist/core/http/requirePasswordConfirmMiddleware.d.ts +2 -1
- package/dist/core/http/requireVerifiedMiddleware.d.ts +2 -2
- package/dist/core/http/requireWebAuthMiddleware.d.ts +2 -2
- package/dist/core/http/response.d.ts +4 -3
- package/dist/core/http/scimThrottleMiddleware.d.ts +4 -3
- package/dist/core/http/securedRouteModelBinding.d.ts +10 -3
- package/dist/core/http/securityHeadersMiddleware.d.ts +1 -2
- package/dist/core/http/signedUrl.d.ts +8 -6
- package/dist/core/http/throttleMiddleware.d.ts +9 -5
- package/dist/core/http/uploads.d.ts +9 -3
- package/dist/core/http/webErrorResponse.d.ts +8 -2
- package/dist/core/jobs/exportAuditLogsJob.d.ts +5 -2
- package/dist/core/jobs/invalidateCacheTagsJob.d.ts +5 -4
- package/dist/core/lifecycle/gracefulShutdown.d.ts +2 -5
- package/dist/core/logging/requestLoggingMiddleware.d.ts +1 -1
- package/dist/core/mail/mailer.d.ts +7 -5
- package/dist/core/openapi/generator.d.ts +12 -4
- package/dist/core/openapi/registeredRoute.d.ts +4 -4
- package/dist/core/queue/createAppQueue.d.ts +9 -4
- package/dist/core/queue/failedJobMetrics.d.ts +11 -2
- package/dist/core/queue/failedJobRepository.d.ts +6 -3
- package/dist/core/queue/failedJobService.d.ts +1 -0
- package/dist/core/queue/index.d.ts +16 -6
- package/dist/core/queue/jobRegistry.d.ts +3 -2
- package/dist/core/queue/jobRunner.d.ts +10 -4
- package/dist/core/queue/publicQueue.d.ts +18 -9
- package/dist/core/queue/queueConfig.d.ts +20 -10
- package/dist/core/queue/queueHandoff.d.ts +31 -0
- package/dist/core/queue/queueMetrics.d.ts +10 -3
- package/dist/core/queue/queueServices.d.ts +28 -0
- package/dist/core/queue/queueWorkerTransport.d.ts +28 -0
- package/dist/core/queue/redisQueue.d.ts +19 -10
- package/dist/core/queue/redisQueueKeys.d.ts +5 -5
- package/dist/core/queue/redisStreams.d.ts +17 -10
- package/dist/core/queue/resilientQueue.d.ts +10 -4
- package/dist/core/queue/types.d.ts +1 -0
- package/dist/core/runtime/appKeyPrefix.d.ts +5 -5
- package/dist/core/runtime/application.d.ts +7 -0
- package/dist/core/runtime/executionContext.d.ts +9 -0
- package/dist/core/runtime/executionTasks.d.ts +49 -0
- package/dist/core/runtime/resourceJournal.d.ts +67 -0
- package/dist/core/runtime/serviceGraph.d.ts +14 -0
- package/dist/core/security/oauthState.d.ts +13 -9
- package/dist/core/security/publicReads.d.ts +2 -2
- package/dist/core/security/safeFetch.d.ts +1 -0
- package/dist/core/security/safeUrl.d.ts +3 -0
- package/dist/core/security/scimTenantTokens.d.ts +1 -1
- package/dist/core/security/tokenExpiry.d.ts +1 -1
- package/dist/core/security/webhookSignature.d.ts +1 -1
- package/dist/core/storage/storage.d.ts +24 -16
- package/dist/core/tenant/databaseTenantContext.d.ts +5 -7
- package/dist/core/tenant/resolveTenant.d.ts +2 -2
- package/dist/core/tenant/scopedDatabasePolicy.d.ts +8 -0
- package/dist/core/tenant/tenantDatabaseScope.d.ts +3 -7
- package/dist/core/tenant/tenantMiddleware.d.ts +15 -4
- package/dist/core/tracing/otel.d.ts +4 -6
- package/dist/core/tracing/tracingMiddleware.d.ts +3 -3
- package/dist/core/view/etaViewEngine.d.ts +1 -1
- package/dist/core/view/index.d.ts +5 -4
- package/dist/core/view/tokens.d.ts +8 -0
- package/dist/core/view/viewRenderer.d.ts +2 -2
- package/dist/core/view/webErrorView.d.ts +8 -7
- package/dist/core/view/webLayoutData.d.ts +6 -4
- package/dist/entries/audit/exportAuditLogs.js +87 -1024
- package/dist/entries/audit/siemFormatter.js +4 -90
- package/dist/entries/auth/abilityCatalog.js +28 -0
- package/dist/entries/auth/accessControl.js +5 -16
- package/dist/entries/auth/accountStateMigration.js +216 -236
- package/dist/entries/auth/authOutcome.js +80 -0
- package/dist/entries/auth/authReader.js +68 -0
- package/dist/entries/auth/basicAuthGuard.js +144 -1249
- package/dist/entries/auth/emailVerification.js +2 -2
- package/dist/entries/auth/intendedUrlCookie.js +35 -59
- package/dist/entries/auth/jwt.js +22 -47
- package/dist/entries/auth/jwtGuard.js +99 -231
- package/dist/entries/auth/membershipMiddleware.js +71 -10
- package/dist/entries/auth/membershipScope.js +16 -22
- package/dist/entries/auth/membershipService.js +11 -28
- package/dist/entries/auth/oauth/oidcProvider.js +59 -64
- package/dist/entries/auth/oauth/providers.js +12 -95
- package/dist/entries/auth/oauth/samlProvider.js +53 -1080
- package/dist/entries/auth/oneTimeToken.js +27 -289
- package/dist/entries/auth/passwordConfirmCookie.js +40 -63
- package/dist/entries/auth/passwordLogin.js +94 -399
- package/dist/entries/auth/pendingMfaCookie.js +6 -6
- package/dist/entries/auth/policy.js +22 -56
- package/dist/entries/auth/saml/samlServiceProvider.js +55 -1084
- package/dist/entries/auth/scimAuthMiddleware.js +16 -3358
- package/dist/entries/auth/sessionCookie.js +58 -73
- package/dist/entries/auth/sessionGuard.js +95 -248
- package/dist/entries/auth/tokenHash.js +11 -43
- package/dist/entries/cache/createCacheStore.js +126 -117
- package/dist/entries/cache/repository.js +25 -0
- package/dist/entries/cache/simpleCache.js +12 -7
- package/dist/entries/cache/tokens.js +9 -0
- package/dist/entries/config/applicationConfig.js +163 -0
- package/dist/entries/config/envSchema.js +21 -4
- package/dist/entries/contracts/application.js +1 -0
- package/dist/entries/contracts/authUserDirectory.js +10 -0
- package/dist/entries/contracts/container.js +99 -26
- package/dist/entries/contracts/di.js +3 -0
- package/dist/entries/contracts/executionContext.js +7 -0
- package/dist/entries/contracts/executionOperations.js +7 -0
- package/dist/entries/contracts/executionOwnership.js +7 -0
- package/dist/entries/contracts/membership.js +1 -0
- package/dist/entries/contracts/persistence.js +1 -0
- package/dist/entries/contracts/serviceTokens.js +26 -116
- package/dist/entries/contracts/services.js +98 -0
- package/dist/entries/contracts/token.js +41 -0
- package/dist/entries/crypto/fieldEncryption.js +49 -50
- package/dist/entries/crypto/mfaSecret.js +86 -82
- package/dist/entries/database/acquisitionMetrics.js +107 -0
- package/dist/entries/database/baseRepository.d.ts +2 -0
- package/dist/entries/database/bindings.js +2082 -0
- package/dist/entries/database/definitions.js +175 -0
- package/dist/entries/database/errors.js +114 -89
- package/dist/entries/database/factory.js +67 -28
- package/dist/entries/database/migrations.js +172 -224
- package/dist/entries/database/model.d.ts +1 -0
- package/dist/entries/database/ownedReadOnlyObservation.js +1282 -0
- package/dist/entries/database/query.js +159 -161
- package/dist/entries/database/repositoryQuery.d.ts +1 -0
- package/dist/entries/database/repositoryQuery.js +1 -1373
- package/dist/entries/database/schema.js +216 -236
- package/dist/entries/database/session.js +1 -0
- package/dist/entries/database/sqliteConnection.js +525 -83
- package/dist/entries/database/tokens.js +9 -0
- package/dist/entries/errors/http.js +118 -1
- package/dist/entries/http/authMiddleware.js +8 -5
- package/dist/entries/http/authorizeMiddleware.js +1 -1
- package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
- package/dist/entries/http/clientIp.js +2 -2
- package/dist/entries/http/conditionalResponse.js +19 -7
- package/dist/entries/http/corsMiddleware.js +6 -101
- package/dist/entries/http/csrfMiddleware.js +92 -106
- package/dist/entries/http/csrfToken.js +58 -94
- package/dist/entries/http/etag.js +7 -4
- package/dist/entries/http/flashMiddleware.js +57 -99
- package/dist/entries/http/flashSession.js +41 -68
- package/dist/entries/http/memoryThrottleMiddleware.js +43 -186
- package/dist/entries/http/metricsMiddleware.js +2 -16
- package/dist/entries/http/parseMultipartUpload.js +15 -29
- package/dist/entries/http/requireAbilityMiddleware.js +2 -2
- package/dist/entries/http/requirePasswordConfirmMiddleware.js +19 -31
- package/dist/entries/http/requireVerifiedMiddleware.js +4 -7
- package/dist/entries/http/requireWebAuthMiddleware.js +2 -2
- package/dist/entries/http/response.js +67 -233
- package/dist/entries/http/routeMiddleware.js +0 -14
- package/dist/entries/http/scimThrottleMiddleware.js +43 -237
- package/dist/entries/http/securedRouteModelBinding.js +37 -25
- package/dist/entries/http/securityHeadersMiddleware.js +8 -25
- package/dist/entries/http/signedUrl.js +20 -110
- package/dist/entries/http/throttleMiddleware.js +41 -185
- package/dist/entries/http/uploads.js +9 -24
- package/dist/entries/jobs/exportAuditLogsJob.js +17 -1498
- package/dist/entries/jobs/invalidateCacheTagsJob.js +12 -47
- package/dist/entries/logging/requestLoggingMiddleware.js +4 -4
- package/dist/entries/mail/mailer.js +68 -78
- package/dist/entries/openapi/generator.js +33 -100
- package/dist/entries/queue/createAppQueue.js +416 -256
- package/dist/entries/queue/failedJobRepository.js +17 -8
- package/dist/entries/queue/failedJobService.js +1 -0
- package/dist/entries/queue/jobRunner.js +141 -66
- package/dist/entries/queue/publicQueue.js +411 -257
- package/dist/entries/queue/queueConfig.js +61 -0
- package/dist/entries/queue/queueHandoff.js +318 -0
- package/dist/entries/queue/queueMetrics.js +989 -1386
- package/dist/entries/queue/queueServices.js +133 -0
- package/dist/entries/queue/queueWorkerTransport.js +1703 -0
- package/dist/entries/queue/redisQueue.js +282 -180
- package/dist/entries/queue.js +6 -45
- package/dist/entries/runtime/application.js +1585 -0
- package/dist/entries/runtime/asyncContextStore.js +0 -16
- package/dist/entries/security/oauthState.js +35 -67
- package/dist/entries/security/publicReads.js +4 -4
- package/dist/entries/security/safeFetch.js +1 -0
- package/dist/entries/security/scimTenantTokens.js +3 -3
- package/dist/entries/security/tokenExpiry.js +2 -2
- package/dist/entries/security/totp.js +1 -87
- package/dist/entries/security/webhookSignature.js +2 -90
- package/dist/entries/storage/storage.js +120 -45
- package/dist/entries/tenant/databaseTenantContext.js +6 -907
- package/dist/entries/tenant/publicTenantHost.js +78 -0
- package/dist/entries/tenant/resolveTenant.js +14 -0
- package/dist/entries/tenant/scopedDatabasePolicy.js +75 -0
- package/dist/entries/tenant/tenancyConfig.js +1 -19
- package/dist/entries/tenant/tenantDatabaseScope.js +3 -892
- package/dist/entries/tracing/tracingMiddleware.js +104 -173
- package/dist/entries/view/tokens.js +11 -0
- package/dist/framework/public-api.d.ts +62 -44
- package/dist/index.js +10435 -11351
- package/dist/internal/ormInternal.js +117 -0
- package/dist/internal/ormRuntime.js +5000 -0
- package/package.json +158 -46
- package/dist/core/database/bindConnection.d.ts +0 -1
- package/dist/core/database/boundConnection.d.ts +0 -5
- package/dist/core/database/defaultConnection.d.ts +0 -7
- package/dist/core/database/namedConnections.d.ts +0 -15
- package/dist/core/database/readOnlyCollector.d.ts +0 -7
- package/dist/core/database/repositoryConnection.d.ts +0 -4
- package/dist/core/database/transaction.d.ts +0 -23
- package/dist/core/database/transactionControl.d.ts +0 -6
- package/dist/core/events/deferredModelEvents.d.ts +0 -7
- package/dist/core/facades/index.d.ts +0 -10
- package/dist/core/runtime/applicationRegistry.d.ts +0 -18
- package/dist/entries/database/bindConnection.js +0 -1
- package/dist/entries/database/boundConnection.js +0 -1
- package/dist/entries/database/defaultConnection.js +0 -1
- package/dist/entries/database/namedConnections.js +0 -1
- package/dist/entries/database/repositoryConnection.js +0 -1
- package/dist/entries/database/transaction.js +0 -1
- package/dist/entries/facades.js +0 -711
- package/dist/entries/runtime/applicationRegistry.js +0 -1
|
@@ -1,16 +1,4 @@
|
|
|
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
2
|
// ../../src/core/runtime/asyncContextStore.ts
|
|
15
3
|
import { AsyncLocalStorage } from "async_hooks";
|
|
16
4
|
function createAsyncContextStore(key) {
|
|
@@ -24,28 +12,15 @@ function createAsyncContextStore(key) {
|
|
|
24
12
|
globalRecord[symbol] = store;
|
|
25
13
|
return store;
|
|
26
14
|
}
|
|
27
|
-
var init_asyncContextStore = () => {};
|
|
28
15
|
|
|
29
16
|
// ../../src/core/database/connectionContext.ts
|
|
17
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
30
18
|
function runWithDatabaseConnection(connection, callback) {
|
|
31
19
|
return activeConnection.run(connection, callback);
|
|
32
20
|
}
|
|
33
21
|
function getActiveDatabaseConnection(fallback) {
|
|
34
22
|
return activeConnection.getStore() ?? fallback;
|
|
35
23
|
}
|
|
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/database/sqliteConnection.ts
|
|
46
|
-
init_connectionContext();
|
|
47
|
-
import { Database } from "bun:sqlite";
|
|
48
|
-
import { resolve } from "path";
|
|
49
24
|
|
|
50
25
|
// ../../src/core/database/isolatedReadOnlyObservation.ts
|
|
51
26
|
var childSource = `
|
|
@@ -164,102 +139,569 @@ async function observeIsolatedReadOnly(profile, operation, options) {
|
|
|
164
139
|
}
|
|
165
140
|
}
|
|
166
141
|
|
|
167
|
-
// ../../src/core/database/
|
|
168
|
-
function
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
142
|
+
// ../../src/core/database/legacyNativeTransaction.ts
|
|
143
|
+
async function legacyNativeTransaction(pin, callback, transactions) {
|
|
144
|
+
let terminal;
|
|
145
|
+
let unexpected;
|
|
146
|
+
let disposal;
|
|
147
|
+
try {
|
|
148
|
+
terminal = await pin.begin(async (raw) => {
|
|
149
|
+
let active = true;
|
|
150
|
+
const transaction = {
|
|
151
|
+
async unsafe(text, params = []) {
|
|
152
|
+
if (!active)
|
|
153
|
+
throw new Error("Database transaction is no longer active.");
|
|
154
|
+
return [...await raw.execute({ text, params })];
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
transactions.add(transaction);
|
|
158
|
+
try {
|
|
159
|
+
const result = await runWithDatabaseConnection(transaction, () => callback(transaction));
|
|
160
|
+
return { finish: "commit", result };
|
|
161
|
+
} finally {
|
|
162
|
+
active = false;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
unexpected = { error };
|
|
167
|
+
} finally {
|
|
168
|
+
try {
|
|
169
|
+
disposal = await (terminal?.nativeState === "idle" ? pin.release() : pin.discard());
|
|
170
|
+
} catch (error) {
|
|
171
|
+
disposal = { ok: false, error, ownership: "unknown" };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
let primary = unexpected;
|
|
175
|
+
if (!primary && terminal) {
|
|
176
|
+
if (terminal.stage.kind === "callback-failed") {
|
|
177
|
+
primary = {
|
|
178
|
+
error: terminal.failure ? new AggregateError([terminal.stage.error, terminal.failure.error], "Native rollback failed; connection discarded.") : terminal.stage.error
|
|
179
|
+
};
|
|
180
|
+
} else if (terminal.failure)
|
|
181
|
+
primary = terminal.failure;
|
|
182
|
+
else if (terminal.stage.kind !== "decided" || terminal.data !== "committed")
|
|
183
|
+
primary = { error: new Error("Native transaction did not commit.") };
|
|
184
|
+
}
|
|
185
|
+
if (!disposal.ok) {
|
|
186
|
+
if (primary)
|
|
187
|
+
throw new AggregateError([primary.error, disposal.error], "Native transaction and disposal failed.");
|
|
188
|
+
throw disposal.error;
|
|
172
189
|
}
|
|
173
|
-
|
|
190
|
+
if (primary)
|
|
191
|
+
throw primary.error;
|
|
192
|
+
if (terminal?.stage.kind !== "decided")
|
|
193
|
+
throw new Error("Native transaction has no decision.");
|
|
194
|
+
return terminal.stage.decision.result;
|
|
174
195
|
}
|
|
175
|
-
|
|
176
|
-
|
|
196
|
+
|
|
197
|
+
// ../../src/core/database/rawSqlite.ts
|
|
198
|
+
import { Database } from "bun:sqlite";
|
|
199
|
+
import { resolve } from "path";
|
|
200
|
+
|
|
201
|
+
// ../../src/core/database/nativeTransactions.ts
|
|
202
|
+
var failure = (error) => Object.freeze({ error });
|
|
203
|
+
function nativeTransaction(control) {
|
|
204
|
+
let state = "idle";
|
|
205
|
+
let sequence = 0;
|
|
206
|
+
let running = false;
|
|
207
|
+
const knownState = () => control.state?.() ?? "unknown";
|
|
208
|
+
async function run(work, savepoint) {
|
|
209
|
+
const phases = [];
|
|
210
|
+
let stage = Object.freeze({ kind: "callback-not-entered" });
|
|
211
|
+
let terminalFailure = null;
|
|
212
|
+
let data = "not-started";
|
|
213
|
+
let open = false;
|
|
214
|
+
let childActive = false;
|
|
215
|
+
const phase = async (name, sql) => {
|
|
216
|
+
try {
|
|
217
|
+
const reply = await control.control(sql);
|
|
218
|
+
phases.push(Object.freeze({ phase: name, acknowledgement: "confirmed-reply", failure: null }));
|
|
219
|
+
return reply;
|
|
220
|
+
} catch (error) {
|
|
221
|
+
const proof = knownState();
|
|
222
|
+
phases.push(Object.freeze({
|
|
223
|
+
phase: name,
|
|
224
|
+
acknowledgement: name === "commit" && proof === "transaction-active" || name === "begin" && proof === "idle" ? "proven-not-applied" : "unknown",
|
|
225
|
+
failure: failure(error)
|
|
226
|
+
}));
|
|
227
|
+
state = proof;
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const result = () => Object.freeze({
|
|
232
|
+
data,
|
|
233
|
+
nativeState: state,
|
|
234
|
+
stage,
|
|
235
|
+
phases: Object.freeze([...phases]),
|
|
236
|
+
failure: terminalFailure
|
|
237
|
+
});
|
|
238
|
+
try {
|
|
239
|
+
await phase(savepoint ? "savepoint" : "begin", savepoint ? `SAVEPOINT ${savepoint}` : control.beginSql ?? "BEGIN");
|
|
240
|
+
state = "transaction-active";
|
|
241
|
+
open = true;
|
|
242
|
+
} catch (error) {
|
|
243
|
+
terminalFailure = failure(error);
|
|
244
|
+
data = state === "idle" && !savepoint ? "not-started" : "unknown";
|
|
245
|
+
return result();
|
|
246
|
+
}
|
|
247
|
+
const transaction = Object.freeze({
|
|
248
|
+
execute(statement) {
|
|
249
|
+
if (!open || childActive)
|
|
250
|
+
return Promise.reject(new Error("Native transaction branch is not active."));
|
|
251
|
+
return control.execute(statement);
|
|
252
|
+
},
|
|
253
|
+
async savepoint(callback) {
|
|
254
|
+
if (!open || childActive)
|
|
255
|
+
throw new Error("Native transaction branch is not active.");
|
|
256
|
+
childActive = true;
|
|
257
|
+
try {
|
|
258
|
+
return await run(callback, `strata_sp_${++sequence}`);
|
|
259
|
+
} finally {
|
|
260
|
+
childActive = false;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
stage = Object.freeze({ kind: "callback-running" });
|
|
265
|
+
try {
|
|
266
|
+
const decision = await work(transaction);
|
|
267
|
+
if (!decision || decision.finish !== "commit" && decision.finish !== "rollback")
|
|
268
|
+
throw new TypeError("Invalid native transaction decision.");
|
|
269
|
+
stage = Object.freeze({
|
|
270
|
+
kind: "decided",
|
|
271
|
+
decision: Object.freeze({ finish: decision.finish, result: decision.result })
|
|
272
|
+
});
|
|
273
|
+
} catch (error) {
|
|
274
|
+
stage = Object.freeze({ kind: "callback-failed", error });
|
|
275
|
+
} finally {
|
|
276
|
+
open = false;
|
|
277
|
+
}
|
|
278
|
+
const rollback = async () => {
|
|
279
|
+
if (savepoint) {
|
|
280
|
+
await phase("rollback-to", `ROLLBACK TO SAVEPOINT ${savepoint}`);
|
|
281
|
+
await phase("release-savepoint", `RELEASE SAVEPOINT ${savepoint}`);
|
|
282
|
+
state = "transaction-active";
|
|
283
|
+
} else {
|
|
284
|
+
await phase("rollback", "ROLLBACK");
|
|
285
|
+
state = "idle";
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
if (stage.kind === "decided" && stage.decision.finish === "commit") {
|
|
289
|
+
try {
|
|
290
|
+
const reply = await phase(savepoint ? "release-savepoint" : "commit", savepoint ? `RELEASE SAVEPOINT ${savepoint}` : "COMMIT");
|
|
291
|
+
data = savepoint ? "released" : reply.command === "ROLLBACK" ? "rolled-back" : "committed";
|
|
292
|
+
state = savepoint ? "transaction-active" : "idle";
|
|
293
|
+
} catch (error) {
|
|
294
|
+
terminalFailure = failure(error);
|
|
295
|
+
const commitNotApplied = !savepoint && state === "transaction-active" && control.state !== undefined;
|
|
296
|
+
data = "unknown";
|
|
297
|
+
try {
|
|
298
|
+
await rollback();
|
|
299
|
+
if (savepoint || commitNotApplied)
|
|
300
|
+
data = "rolled-back";
|
|
301
|
+
} catch {}
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
try {
|
|
305
|
+
await rollback();
|
|
306
|
+
data = "rolled-back";
|
|
307
|
+
} catch (error) {
|
|
308
|
+
terminalFailure = failure(error);
|
|
309
|
+
data = "unknown";
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return result();
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
get state() {
|
|
316
|
+
return state;
|
|
317
|
+
},
|
|
318
|
+
async begin(work) {
|
|
319
|
+
if (running)
|
|
320
|
+
throw new Error("Native pin already has an active transaction.");
|
|
321
|
+
running = true;
|
|
322
|
+
try {
|
|
323
|
+
return await run(work);
|
|
324
|
+
} finally {
|
|
325
|
+
running = false;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ../../src/core/database/sqlDialects.ts
|
|
332
|
+
function assertSafeIdentifier(identifier) {
|
|
333
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(identifier)) {
|
|
334
|
+
throw new Error(`Invalid SQL identifier: ${identifier}`);
|
|
335
|
+
}
|
|
336
|
+
return identifier;
|
|
337
|
+
}
|
|
338
|
+
function quoteIdentifierFor(dialect, column) {
|
|
339
|
+
return dialect.quoteIdentifier(column);
|
|
340
|
+
}
|
|
341
|
+
var postgresDialect = Object.freeze({
|
|
342
|
+
driver: "pgsql",
|
|
343
|
+
placeholder(index) {
|
|
344
|
+
return `$${index}`;
|
|
345
|
+
},
|
|
346
|
+
quoteIdentifier(identifier) {
|
|
347
|
+
return `"${assertSafeIdentifier(identifier)}"`;
|
|
348
|
+
},
|
|
349
|
+
nowExpression() {
|
|
350
|
+
return "NOW()";
|
|
351
|
+
},
|
|
352
|
+
epochMillisecondsExpression() {
|
|
353
|
+
return "CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT)";
|
|
354
|
+
},
|
|
355
|
+
rowLockClause(skipLocked = false) {
|
|
356
|
+
return ` FOR UPDATE${skipLocked ? " SKIP LOCKED" : ""}`;
|
|
357
|
+
},
|
|
358
|
+
timestampValue(value) {
|
|
359
|
+
return value.toISOString();
|
|
360
|
+
},
|
|
361
|
+
returningClause(columns) {
|
|
362
|
+
return ` RETURNING ${columns}`;
|
|
363
|
+
},
|
|
364
|
+
ilikeOperator() {
|
|
365
|
+
return "ILIKE";
|
|
366
|
+
},
|
|
367
|
+
nullsLastSuffix() {
|
|
368
|
+
return " NULLS LAST";
|
|
369
|
+
},
|
|
370
|
+
castToText(expression) {
|
|
371
|
+
return `${expression}::text`;
|
|
372
|
+
},
|
|
373
|
+
upsertSuffix(conflictColumns, updateColumns) {
|
|
374
|
+
const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
|
|
375
|
+
if (updateColumns.length === 0) {
|
|
376
|
+
return ` ON CONFLICT (${target}) DO NOTHING`;
|
|
377
|
+
}
|
|
378
|
+
const assignments = updateColumns.map((column) => {
|
|
379
|
+
const quoted = quoteIdentifierFor(this, column);
|
|
380
|
+
return `${quoted} = excluded.${quoted}`;
|
|
381
|
+
}).join(", ");
|
|
382
|
+
return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
var mysqlDialect = Object.freeze({
|
|
386
|
+
driver: "mysql",
|
|
387
|
+
placeholder() {
|
|
388
|
+
return "?";
|
|
389
|
+
},
|
|
390
|
+
quoteIdentifier(identifier) {
|
|
391
|
+
return `\`${assertSafeIdentifier(identifier)}\``;
|
|
392
|
+
},
|
|
393
|
+
nowExpression() {
|
|
394
|
+
return "CURRENT_TIMESTAMP";
|
|
395
|
+
},
|
|
396
|
+
epochMillisecondsExpression() {
|
|
397
|
+
return "CAST(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000 AS UNSIGNED)";
|
|
398
|
+
},
|
|
399
|
+
rowLockClause(skipLocked = false) {
|
|
400
|
+
return ` FOR UPDATE${skipLocked ? " SKIP LOCKED" : ""}`;
|
|
401
|
+
},
|
|
402
|
+
timestampValue(value) {
|
|
403
|
+
return value.toISOString().slice(0, 19).replace("T", " ");
|
|
404
|
+
},
|
|
405
|
+
returningClause() {
|
|
406
|
+
return "";
|
|
407
|
+
},
|
|
408
|
+
ilikeOperator() {
|
|
409
|
+
return "LIKE";
|
|
410
|
+
},
|
|
411
|
+
nullsLastSuffix() {
|
|
412
|
+
return "";
|
|
413
|
+
},
|
|
414
|
+
castToText(expression) {
|
|
415
|
+
return `CAST(${expression} AS CHAR)`;
|
|
416
|
+
},
|
|
417
|
+
upsertSuffix(conflictColumns, updateColumns) {
|
|
418
|
+
if (updateColumns.length === 0) {
|
|
419
|
+
const anchor = quoteIdentifierFor(this, conflictColumns[0] ?? "");
|
|
420
|
+
return ` ON DUPLICATE KEY UPDATE ${anchor} = ${anchor}`;
|
|
421
|
+
}
|
|
422
|
+
const assignments = updateColumns.map((column) => {
|
|
423
|
+
const quoted = quoteIdentifierFor(this, column);
|
|
424
|
+
return `${quoted} = VALUES(${quoted})`;
|
|
425
|
+
}).join(", ");
|
|
426
|
+
return ` ON DUPLICATE KEY UPDATE ${assignments}`;
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
var sqliteDialect = Object.freeze({
|
|
430
|
+
driver: "sqlite",
|
|
431
|
+
placeholder() {
|
|
432
|
+
return "?";
|
|
433
|
+
},
|
|
434
|
+
quoteIdentifier(identifier) {
|
|
435
|
+
return `"${assertSafeIdentifier(identifier)}"`;
|
|
436
|
+
},
|
|
437
|
+
nowExpression() {
|
|
438
|
+
return "strftime('%Y-%m-%dT%H:%M:%fZ', 'now')";
|
|
439
|
+
},
|
|
440
|
+
epochMillisecondsExpression() {
|
|
441
|
+
return "CAST((julianday('now') - 2440587.5) * 86400000 AS INTEGER)";
|
|
442
|
+
},
|
|
443
|
+
rowLockClause() {
|
|
444
|
+
return "";
|
|
445
|
+
},
|
|
446
|
+
timestampValue(value) {
|
|
447
|
+
return value.toISOString();
|
|
448
|
+
},
|
|
449
|
+
returningClause(columns) {
|
|
450
|
+
return ` RETURNING ${columns}`;
|
|
451
|
+
},
|
|
452
|
+
ilikeOperator() {
|
|
453
|
+
return "LIKE";
|
|
454
|
+
},
|
|
455
|
+
nullsLastSuffix() {
|
|
456
|
+
return "";
|
|
457
|
+
},
|
|
458
|
+
castToText(expression) {
|
|
459
|
+
return `CAST(${expression} AS TEXT)`;
|
|
460
|
+
},
|
|
461
|
+
upsertSuffix(conflictColumns, updateColumns) {
|
|
462
|
+
const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
|
|
463
|
+
if (updateColumns.length === 0) {
|
|
464
|
+
return ` ON CONFLICT (${target}) DO NOTHING`;
|
|
465
|
+
}
|
|
466
|
+
const assignments = updateColumns.map((column) => {
|
|
467
|
+
const quoted = quoteIdentifierFor(this, column);
|
|
468
|
+
return `${quoted} = excluded.${quoted}`;
|
|
469
|
+
}).join(", ");
|
|
470
|
+
return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
var dialects = Object.freeze({
|
|
474
|
+
pgsql: postgresDialect,
|
|
475
|
+
mysql: mysqlDialect,
|
|
476
|
+
sqlite: sqliteDialect
|
|
477
|
+
});
|
|
478
|
+
function dialectFor(driver) {
|
|
479
|
+
if (!Object.hasOwn(dialects, driver))
|
|
480
|
+
throw new TypeError("Unsupported SQL driver.");
|
|
481
|
+
return dialects[driver];
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// ../../src/core/database/rawSqlite.ts
|
|
485
|
+
function sqliteFilename(filename) {
|
|
486
|
+
if (!filename.trim())
|
|
177
487
|
throw new Error("SQLite path is not configured. Pass a filename or :memory:.");
|
|
488
|
+
return filename === ":memory:" ? filename : resolve(filename);
|
|
489
|
+
}
|
|
490
|
+
function isRowReturning(sql) {
|
|
491
|
+
const upper = sql.replace(/\s+/g, " ").trim().toUpperCase();
|
|
492
|
+
return upper.includes(" RETURNING ") || upper.startsWith("SELECT") || upper.startsWith("WITH") || upper.startsWith("PRAGMA") || upper.startsWith("EXPLAIN");
|
|
493
|
+
}
|
|
494
|
+
function openRawSqlitePool(options) {
|
|
495
|
+
let filename;
|
|
496
|
+
try {
|
|
497
|
+
filename = sqliteFilename(options.filename);
|
|
498
|
+
} catch (error) {
|
|
499
|
+
return { ok: false, error, disposition: "not-acquired" };
|
|
178
500
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
501
|
+
let db;
|
|
502
|
+
try {
|
|
503
|
+
db = new Database(filename, { create: true });
|
|
504
|
+
} catch (error) {
|
|
505
|
+
return { ok: false, error, disposition: "unknown" };
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
db.exec("PRAGMA foreign_keys = ON");
|
|
509
|
+
db.exec("PRAGMA busy_timeout = 5000");
|
|
510
|
+
if (filename !== ":memory:") {
|
|
511
|
+
db.exec("PRAGMA journal_mode = WAL");
|
|
512
|
+
db.exec("PRAGMA synchronous = NORMAL");
|
|
513
|
+
}
|
|
514
|
+
} catch (error) {
|
|
515
|
+
try {
|
|
516
|
+
db.close();
|
|
517
|
+
return { ok: false, error, disposition: "cleaned" };
|
|
518
|
+
} catch (cleanupError) {
|
|
519
|
+
return { ok: false, error, disposition: "unknown", cleanupErrors: [cleanupError] };
|
|
520
|
+
}
|
|
186
521
|
}
|
|
187
522
|
let tail = Promise.resolve();
|
|
188
523
|
let pending = 0;
|
|
189
524
|
let closed = false;
|
|
190
|
-
|
|
191
|
-
function
|
|
192
|
-
const statement = db.query(query);
|
|
193
|
-
const args = params.map((value) => value instanceof Date ? value.toISOString() : value);
|
|
194
|
-
if (isRowReturning(query))
|
|
195
|
-
return statement.all(...args);
|
|
196
|
-
statement.run(...args);
|
|
197
|
-
return [];
|
|
198
|
-
}
|
|
199
|
-
function enqueue(operation) {
|
|
525
|
+
let quarantined = false;
|
|
526
|
+
function check() {
|
|
200
527
|
if (closed)
|
|
201
|
-
|
|
528
|
+
throw new Error("SQLite connection is closed.");
|
|
529
|
+
if (quarantined)
|
|
530
|
+
throw new Error("SQLite connection is quarantined after uncertain native state.");
|
|
531
|
+
}
|
|
532
|
+
function execute(statement) {
|
|
533
|
+
try {
|
|
534
|
+
check();
|
|
535
|
+
const query = db.query(statement.text);
|
|
536
|
+
const params = statement.params.map((value) => value instanceof Date ? value.toISOString() : value);
|
|
537
|
+
if (isRowReturning(statement.text))
|
|
538
|
+
return Promise.resolve(query.all(...params));
|
|
539
|
+
query.run(...params);
|
|
540
|
+
return Promise.resolve([]);
|
|
541
|
+
} catch (error) {
|
|
542
|
+
return Promise.reject(error);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
function enqueue(work) {
|
|
546
|
+
try {
|
|
547
|
+
check();
|
|
548
|
+
} catch (error) {
|
|
549
|
+
return Promise.reject(error);
|
|
550
|
+
}
|
|
202
551
|
pending++;
|
|
203
|
-
const result = tail.then(
|
|
552
|
+
const result = tail.then(() => {
|
|
553
|
+
check();
|
|
554
|
+
return work();
|
|
555
|
+
}).finally(() => {
|
|
204
556
|
pending--;
|
|
205
557
|
});
|
|
206
558
|
tail = result.then(() => {}, () => {});
|
|
207
559
|
return result;
|
|
208
560
|
}
|
|
561
|
+
const pool = Object.freeze({
|
|
562
|
+
dialect: dialectFor("sqlite"),
|
|
563
|
+
capabilities: Object.freeze({
|
|
564
|
+
driver: "sqlite",
|
|
565
|
+
transactions: true,
|
|
566
|
+
savepoints: true,
|
|
567
|
+
rowLocks: false,
|
|
568
|
+
postgresRls: false,
|
|
569
|
+
cancellableCheckout: false,
|
|
570
|
+
observableCheckout: false,
|
|
571
|
+
isolatedDiscard: false
|
|
572
|
+
}),
|
|
573
|
+
execute(statement) {
|
|
574
|
+
return enqueue(() => execute(statement));
|
|
575
|
+
},
|
|
576
|
+
checkout(options = {}) {
|
|
577
|
+
if (options.signal !== undefined || options.metrics !== undefined)
|
|
578
|
+
return Promise.resolve({
|
|
579
|
+
ok: false,
|
|
580
|
+
error: new Error("SQLite logical checkout does not support acquisition signal or metrics."),
|
|
581
|
+
disposition: "not-acquired"
|
|
582
|
+
});
|
|
583
|
+
return new Promise((publish) => {
|
|
584
|
+
const reservation = enqueue(async () => {
|
|
585
|
+
let unlock;
|
|
586
|
+
const slot = new Promise((done) => {
|
|
587
|
+
unlock = done;
|
|
588
|
+
});
|
|
589
|
+
let disposal;
|
|
590
|
+
const protocol = nativeTransaction({
|
|
591
|
+
execute,
|
|
592
|
+
beginSql: "BEGIN IMMEDIATE",
|
|
593
|
+
state: () => db.inTransaction ? "transaction-active" : "idle",
|
|
594
|
+
control: async (sql) => {
|
|
595
|
+
check();
|
|
596
|
+
db.exec(sql);
|
|
597
|
+
return {};
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
const pin = Object.freeze({
|
|
601
|
+
begin: (work) => {
|
|
602
|
+
if (disposal)
|
|
603
|
+
return Promise.reject(new Error("SQLite pin is no longer active."));
|
|
604
|
+
return protocol.begin(work);
|
|
605
|
+
},
|
|
606
|
+
async release() {
|
|
607
|
+
if (disposal)
|
|
608
|
+
return disposal;
|
|
609
|
+
if (db.inTransaction || protocol.state !== "idle") {
|
|
610
|
+
quarantined = true;
|
|
611
|
+
unlock();
|
|
612
|
+
disposal = Object.freeze({
|
|
613
|
+
ok: false,
|
|
614
|
+
error: new Error("SQLite logical slot is not safely idle."),
|
|
615
|
+
ownership: "retained"
|
|
616
|
+
});
|
|
617
|
+
await reservation;
|
|
618
|
+
return disposal;
|
|
619
|
+
}
|
|
620
|
+
disposal = Object.freeze({ ok: true });
|
|
621
|
+
unlock();
|
|
622
|
+
await reservation;
|
|
623
|
+
return disposal;
|
|
624
|
+
},
|
|
625
|
+
async discard() {
|
|
626
|
+
if (disposal)
|
|
627
|
+
return disposal;
|
|
628
|
+
quarantined = true;
|
|
629
|
+
disposal = Object.freeze({
|
|
630
|
+
ok: false,
|
|
631
|
+
error: new Error("SQLite shared handle quarantined; isolated discard is unavailable."),
|
|
632
|
+
ownership: "retained"
|
|
633
|
+
});
|
|
634
|
+
unlock();
|
|
635
|
+
await reservation;
|
|
636
|
+
return disposal;
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
publish({ ok: true, value: pin });
|
|
640
|
+
await slot;
|
|
641
|
+
});
|
|
642
|
+
reservation.catch((error) => publish({ ok: false, error, disposition: "not-acquired" }));
|
|
643
|
+
});
|
|
644
|
+
},
|
|
645
|
+
close() {
|
|
646
|
+
if (pending > 0)
|
|
647
|
+
throw new Error("Drain SQLite operations before closing the connection.");
|
|
648
|
+
db.close();
|
|
649
|
+
closed = true;
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
return { ok: true, value: pool };
|
|
653
|
+
}
|
|
654
|
+
function createRawSqlitePool(options) {
|
|
655
|
+
const outcome = openRawSqlitePool(options);
|
|
656
|
+
if (outcome.ok)
|
|
657
|
+
return outcome.value;
|
|
658
|
+
if (outcome.cleanupErrors?.length)
|
|
659
|
+
throw new AggregateError([outcome.error, ...outcome.cleanupErrors], "SQLite initialization and cleanup failed.");
|
|
660
|
+
throw outcome.error;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// ../../src/core/database/sqliteConnection.ts
|
|
664
|
+
function createSqliteConnection(filename) {
|
|
665
|
+
const absoluteFilename = sqliteFilename(filename);
|
|
666
|
+
const pool = createRawSqlitePool({ driver: "sqlite", filename });
|
|
667
|
+
const transactions = new WeakSet;
|
|
668
|
+
let observations = 0;
|
|
669
|
+
let closed = false;
|
|
209
670
|
const connection = {
|
|
210
671
|
async unsafe(query, params = []) {
|
|
211
672
|
const active = getActiveDatabaseConnection(connection);
|
|
212
673
|
if (transactions.has(active))
|
|
213
674
|
return active.unsafe(query, params);
|
|
214
|
-
return
|
|
675
|
+
return [...await pool.execute({ text: query, params })];
|
|
215
676
|
},
|
|
216
677
|
async begin(callback) {
|
|
217
|
-
if (transactions.has(getActiveDatabaseConnection(connection)))
|
|
218
|
-
throw new Error("Use
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const transaction = {
|
|
224
|
-
async unsafe(query, params = []) {
|
|
225
|
-
if (!open)
|
|
226
|
-
throw new Error("SQLite transaction is no longer active.");
|
|
227
|
-
return execute(query, params);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
transactions.add(transaction);
|
|
231
|
-
try {
|
|
232
|
-
const result = await runWithDatabaseConnection(transaction, () => callback(transaction));
|
|
233
|
-
db.exec("COMMIT");
|
|
234
|
-
return result;
|
|
235
|
-
} catch (error) {
|
|
236
|
-
db.exec("ROLLBACK");
|
|
237
|
-
throw error;
|
|
238
|
-
} finally {
|
|
239
|
-
open = false;
|
|
240
|
-
}
|
|
241
|
-
});
|
|
678
|
+
if (transactions.has(getActiveDatabaseConnection(connection)))
|
|
679
|
+
throw new Error("Use the active transaction handle for nested SQLite work.");
|
|
680
|
+
const outcome = await pool.checkout();
|
|
681
|
+
if (!outcome.ok)
|
|
682
|
+
throw outcome.error;
|
|
683
|
+
return legacyNativeTransaction(outcome.value, callback, transactions);
|
|
242
684
|
},
|
|
243
685
|
async observeReadOnly(operation, options) {
|
|
244
686
|
if (closed)
|
|
245
687
|
throw new Error("SQLite connection is closed.");
|
|
246
688
|
if (absoluteFilename === ":memory:")
|
|
247
689
|
throw new Error("SQL observations require file-backed SQLite.");
|
|
248
|
-
|
|
690
|
+
observations++;
|
|
249
691
|
try {
|
|
250
692
|
return await observeIsolatedReadOnly({ driver: "sqlite", filename: absoluteFilename }, async (transaction) => {
|
|
251
693
|
transactions.add(transaction);
|
|
252
694
|
return await runWithDatabaseConnection(transaction, () => operation(transaction));
|
|
253
695
|
}, options);
|
|
254
696
|
} finally {
|
|
255
|
-
|
|
697
|
+
observations--;
|
|
256
698
|
}
|
|
257
699
|
},
|
|
258
700
|
close() {
|
|
259
|
-
if (
|
|
701
|
+
if (observations)
|
|
260
702
|
throw new Error("Drain SQLite operations before closing the connection.");
|
|
703
|
+
pool.close();
|
|
261
704
|
closed = true;
|
|
262
|
-
db.close();
|
|
263
705
|
}
|
|
264
706
|
};
|
|
265
707
|
return connection;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/database/tokens.ts
|
|
3
|
+
import { createServiceToken } from "@getstrata/core/contracts/token";
|
|
4
|
+
var DATABASE_SESSION_TOKEN = createServiceToken("core.database.session");
|
|
5
|
+
var DATABASE_MANAGER_TOKEN = createServiceToken("core.database.manager");
|
|
6
|
+
export {
|
|
7
|
+
DATABASE_MANAGER_TOKEN,
|
|
8
|
+
DATABASE_SESSION_TOKEN
|
|
9
|
+
};
|