@encore.dev/sdk 2.0.0-beta.7 → 2.0.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -0
- package/app_meta.test.ts +25 -5
- package/app_meta.ts +51 -12
- package/config/config.test.ts +18 -15
- package/config/config.ts +89 -31
- package/config/mod.ts +4 -3
- package/config/secrets.ts +79 -53
- package/config/value.ts +13 -17
- package/cron/mod.test.ts +52 -0
- package/cron/mod.ts +41 -2
- package/dist/app_meta.d.ts +44 -12
- package/dist/app_meta.d.ts.map +1 -0
- package/dist/app_meta.js +2 -0
- package/dist/app_meta.js.map +1 -1
- package/dist/config/config.d.ts +58 -28
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +46 -18
- package/dist/config/config.js.map +1 -1
- package/dist/config/mod.d.ts +5 -4
- package/dist/config/mod.d.ts.map +1 -0
- package/dist/config/mod.js +2 -2
- package/dist/config/mod.js.map +1 -1
- package/dist/config/secrets.d.ts +71 -39
- package/dist/config/secrets.d.ts.map +1 -0
- package/dist/config/secrets.js +31 -38
- package/dist/config/secrets.js.map +1 -1
- package/dist/config/value.d.ts +8 -5
- package/dist/config/value.d.ts.map +1 -0
- package/dist/config/value.js +10 -7
- package/dist/config/value.js.map +1 -1
- package/dist/cron/mod.d.ts +36 -1
- package/dist/cron/mod.d.ts.map +1 -0
- package/dist/cron/mod.js +31 -1
- package/dist/cron/mod.js.map +1 -1
- package/dist/frontends/mod.d.ts +39 -0
- package/dist/frontends/mod.d.ts.map +1 -0
- package/dist/frontends/mod.js +47 -0
- package/dist/frontends/mod.js.map +1 -0
- package/dist/gateway/errors.d.ts +101 -0
- package/dist/gateway/errors.d.ts.map +1 -0
- package/dist/gateway/errors.js +146 -0
- package/dist/gateway/errors.js.map +1 -0
- package/dist/gateway/match.d.ts +127 -0
- package/dist/gateway/match.d.ts.map +1 -0
- package/dist/gateway/match.js +206 -0
- package/dist/gateway/match.js.map +1 -0
- package/dist/gateway/middleware.d.ts +108 -0
- package/dist/gateway/middleware.d.ts.map +1 -0
- package/dist/gateway/middleware.js +9 -0
- package/dist/gateway/middleware.js.map +1 -0
- package/dist/gateway/mod.d.ts +130 -0
- package/dist/gateway/mod.d.ts.map +1 -0
- package/dist/gateway/mod.js +188 -0
- package/dist/gateway/mod.js.map +1 -0
- package/dist/gateway/request.d.ts +304 -0
- package/dist/gateway/request.d.ts.map +1 -0
- package/dist/gateway/request.js +403 -0
- package/dist/gateway/request.js.map +1 -0
- package/dist/gateway/serve.d.ts +114 -0
- package/dist/gateway/serve.d.ts.map +1 -0
- package/dist/gateway/serve.js +510 -0
- package/dist/gateway/serve.js.map +1 -0
- package/dist/gateways/mod.d.ts +42 -0
- package/dist/gateways/mod.d.ts.map +1 -0
- package/dist/gateways/mod.js +55 -0
- package/dist/gateways/mod.js.map +1 -0
- package/dist/internal/clients/call.d.ts +147 -0
- package/dist/internal/clients/call.d.ts.map +1 -0
- package/dist/internal/clients/call.js +274 -0
- package/dist/internal/clients/call.js.map +1 -0
- package/dist/internal/clients/grpc.d.ts +90 -0
- package/dist/internal/clients/grpc.d.ts.map +1 -0
- package/dist/internal/clients/grpc.js +355 -0
- package/dist/internal/clients/grpc.js.map +1 -0
- package/dist/internal/clients/mod.d.ts +81 -0
- package/dist/internal/clients/mod.d.ts.map +1 -0
- package/dist/internal/clients/mod.js +237 -0
- package/dist/internal/clients/mod.js.map +1 -0
- package/dist/internal/fetch.d.ts +58 -0
- package/dist/internal/fetch.d.ts.map +1 -0
- package/dist/internal/fetch.js +251 -0
- package/dist/internal/fetch.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +2 -0
- package/dist/internal/otel/backend.d.ts.map +1 -0
- package/dist/internal/otel/context.d.ts +1 -0
- package/dist/internal/otel/context.d.ts.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +1 -0
- package/dist/internal/otel/context_manager.d.ts.map +1 -0
- package/dist/internal/otel/encode.d.ts +2 -0
- package/dist/internal/otel/encode.d.ts.map +1 -0
- package/dist/internal/otel/http.d.ts +11 -0
- package/dist/internal/otel/http.d.ts.map +1 -0
- package/dist/internal/otel/http.js +64 -0
- package/dist/internal/otel/http.js.map +1 -0
- package/dist/internal/otel/index.d.ts +2 -2
- package/dist/internal/otel/index.d.ts.map +1 -0
- package/dist/internal/otel/index.js +1 -2
- package/dist/internal/otel/index.js.map +1 -1
- package/dist/internal/otel/meter.d.ts +1 -0
- package/dist/internal/otel/meter.d.ts.map +1 -0
- package/dist/internal/otel/propagator.d.ts +1 -0
- package/dist/internal/otel/propagator.d.ts.map +1 -0
- package/dist/internal/otel/provider.d.ts +4 -0
- package/dist/internal/otel/provider.d.ts.map +1 -0
- package/dist/internal/otel/provider.js +3 -0
- package/dist/internal/otel/provider.js.map +1 -1
- package/dist/internal/otel/span.d.ts +15 -1
- package/dist/internal/otel/span.d.ts.map +1 -0
- package/dist/internal/otel/span.js +30 -2
- package/dist/internal/otel/span.js.map +1 -1
- package/dist/internal/otel/tracer.d.ts +1 -0
- package/dist/internal/otel/tracer.d.ts.map +1 -0
- package/dist/internal/otel/tracer.js +11 -5
- package/dist/internal/otel/tracer.js.map +1 -1
- package/dist/internal/reqfields.d.ts +170 -0
- package/dist/internal/reqfields.d.ts.map +1 -0
- package/dist/internal/reqfields.js +349 -0
- package/dist/internal/reqfields.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +1 -0
- package/dist/internal/runtime/meter.d.ts.map +1 -0
- package/dist/internal/runtime/mod.d.ts +6 -0
- package/dist/internal/runtime/mod.d.ts.map +1 -0
- package/dist/internal/runtime/mod.js +32 -2
- package/dist/internal/runtime/mod.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +192 -16
- package/dist/internal/runtime/napi/napi.d.cts +573 -33
- package/dist/internal/runtime/shutdown.d.ts +5 -0
- package/dist/internal/runtime/shutdown.d.ts.map +1 -0
- package/dist/internal/runtime/shutdown.js +9 -1
- package/dist/internal/runtime/shutdown.js.map +1 -1
- package/dist/internal/testing/carry.d.ts +49 -0
- package/dist/internal/testing/carry.d.ts.map +1 -0
- package/dist/internal/testing/carry.js +77 -0
- package/dist/internal/testing/carry.js.map +1 -0
- package/dist/internal/testing/runner.d.ts +2 -0
- package/dist/internal/testing/runner.d.ts.map +1 -0
- package/dist/internal/testing/runner.js +10 -0
- package/dist/internal/testing/runner.js.map +1 -0
- package/dist/internal/testing/scope.d.ts +109 -0
- package/dist/internal/testing/scope.d.ts.map +1 -0
- package/dist/internal/testing/scope.js +335 -0
- package/dist/internal/testing/scope.js.map +1 -0
- package/dist/internal/testing/steps.d.ts +55 -0
- package/dist/internal/testing/steps.d.ts.map +1 -0
- package/dist/internal/testing/steps.js +295 -0
- package/dist/internal/testing/steps.js.map +1 -0
- package/dist/internal/testing/telemetry.d.ts +305 -0
- package/dist/internal/testing/telemetry.d.ts.map +1 -0
- package/dist/internal/testing/telemetry.js +188 -0
- package/dist/internal/testing/telemetry.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +1 -0
- package/dist/internal/utils/constraints.d.ts.map +1 -0
- package/dist/log/index.d.ts +1 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/logger.d.ts +2 -0
- package/dist/log/logger.d.ts.map +1 -0
- package/dist/log/runtime.d.ts +2 -0
- package/dist/log/runtime.d.ts.map +1 -0
- package/dist/log/stderr.d.ts +1 -0
- package/dist/log/stderr.d.ts.map +1 -0
- package/dist/metrics/mod.d.ts +45 -118
- package/dist/metrics/mod.d.ts.map +1 -0
- package/dist/metrics/mod.js +44 -179
- package/dist/metrics/mod.js.map +1 -1
- package/dist/mod.d.ts +2 -3
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +0 -1
- package/dist/mod.js.map +1 -1
- package/dist/otel.d.ts +1 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/pubsub/encoding.d.ts +5 -0
- package/dist/pubsub/encoding.d.ts.map +1 -0
- package/dist/pubsub/encoding.js +10 -2
- package/dist/pubsub/encoding.js.map +1 -1
- package/dist/pubsub/mod.d.ts +5 -4
- package/dist/pubsub/mod.d.ts.map +1 -0
- package/dist/pubsub/mod.js +2 -2
- package/dist/pubsub/mod.js.map +1 -1
- package/dist/pubsub/refs.d.ts +8 -0
- package/dist/pubsub/refs.d.ts.map +1 -0
- package/dist/pubsub/refs.js.map +1 -1
- package/dist/pubsub/subscription.d.ts +110 -15
- package/dist/pubsub/subscription.d.ts.map +1 -0
- package/dist/pubsub/subscription.js +98 -16
- package/dist/pubsub/subscription.js.map +1 -1
- package/dist/pubsub/topic.d.ts +101 -31
- package/dist/pubsub/topic.d.ts.map +1 -0
- package/dist/pubsub/topic.js +168 -51
- package/dist/pubsub/topic.js.map +1 -1
- package/dist/reqctx/mod.d.ts +174 -0
- package/dist/reqctx/mod.d.ts.map +1 -0
- package/dist/reqctx/mod.js +144 -0
- package/dist/reqctx/mod.js.map +1 -0
- package/dist/services/index.d.ts +147 -5
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +157 -9
- package/dist/services/index.js.map +1 -1
- package/dist/shutdown/mod.d.ts +1 -0
- package/dist/shutdown/mod.d.ts.map +1 -0
- package/dist/storage/objects/bucket.d.ts +313 -7
- package/dist/storage/objects/bucket.d.ts.map +1 -0
- package/dist/storage/objects/bucket.js +306 -15
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/objects/error.d.ts +10 -0
- package/dist/storage/objects/error.d.ts.map +1 -0
- package/dist/storage/objects/error.js +25 -1
- package/dist/storage/objects/error.js.map +1 -1
- package/dist/storage/objects/mod.d.ts +4 -3
- package/dist/storage/objects/mod.d.ts.map +1 -0
- package/dist/storage/objects/mod.js +1 -1
- package/dist/storage/objects/mod.js.map +1 -1
- package/dist/storage/objects/refs.d.ts +20 -4
- package/dist/storage/objects/refs.d.ts.map +1 -0
- package/dist/storage/objects/refs.js +6 -0
- package/dist/storage/objects/refs.js.map +1 -1
- package/dist/storage/postgres/database.d.ts +85 -25
- package/dist/storage/postgres/database.d.ts.map +1 -0
- package/dist/storage/postgres/database.js +129 -37
- package/dist/storage/postgres/database.js.map +1 -1
- package/dist/storage/postgres/errors.d.ts +120 -0
- package/dist/storage/postgres/errors.d.ts.map +1 -0
- package/dist/storage/postgres/errors.js +162 -0
- package/dist/storage/postgres/errors.js.map +1 -0
- package/dist/storage/postgres/interval.d.ts +44 -0
- package/dist/storage/postgres/interval.d.ts.map +1 -0
- package/dist/storage/postgres/interval.js +153 -0
- package/dist/storage/postgres/interval.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +4 -1
- package/dist/storage/postgres/mod.d.ts.map +1 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -1
- package/dist/storage/redis/bitfield.d.ts +67 -0
- package/dist/storage/redis/bitfield.d.ts.map +1 -0
- package/dist/storage/redis/bitfield.js +114 -0
- package/dist/storage/redis/bitfield.js.map +1 -0
- package/dist/storage/redis/command.d.ts +45 -0
- package/dist/storage/redis/command.d.ts.map +1 -0
- package/dist/storage/redis/command.js +78 -0
- package/dist/storage/redis/command.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +223 -139
- package/dist/storage/redis/commands.d.ts.map +1 -0
- package/dist/storage/redis/commands.js +128 -6
- package/dist/storage/redis/commands.js.map +1 -1
- package/dist/storage/redis/database.d.ts +56 -22
- package/dist/storage/redis/database.d.ts.map +1 -0
- package/dist/storage/redis/database.js +62 -0
- package/dist/storage/redis/database.js.map +1 -1
- package/dist/storage/redis/mod.d.ts +31 -4
- package/dist/storage/redis/mod.d.ts.map +1 -0
- package/dist/storage/redis/mod.js +26 -1
- package/dist/storage/redis/mod.js.map +1 -1
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.d.ts.map +1 -0
- package/dist/storage/redis/native.js +10 -0
- package/dist/storage/redis/native.js.map +1 -1
- package/dist/storage/redis/options.d.ts +1 -0
- package/dist/storage/redis/options.d.ts.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +2 -0
- package/dist/storage/redis/pubsub.d.ts.map +1 -0
- package/dist/storage/redis/reply.d.ts +34 -0
- package/dist/storage/redis/reply.d.ts.map +1 -0
- package/dist/storage/redis/reply.js +40 -1
- package/dist/storage/redis/reply.js.map +1 -1
- package/dist/storage/redis/scan.d.ts +1 -0
- package/dist/storage/redis/scan.d.ts.map +1 -0
- package/dist/storage/redis/script.d.ts +1 -0
- package/dist/storage/redis/script.d.ts.map +1 -0
- package/dist/storage/redis/target.d.ts +5 -3
- package/dist/storage/redis/target.d.ts.map +1 -0
- package/dist/storage/redis/target.js +39 -28
- package/dist/storage/redis/target.js.map +1 -1
- package/dist/storage/redis/wire.d.ts +7 -10
- package/dist/storage/redis/wire.d.ts.map +1 -0
- package/dist/storage/redis/wire.js +8 -32
- package/dist/storage/redis/wire.js.map +1 -1
- package/dist/testing/bun.d.ts +8 -0
- package/dist/testing/bun.d.ts.map +1 -0
- package/dist/testing/bun.js +13 -0
- package/dist/testing/bun.js.map +1 -0
- package/dist/testing/jest.d.ts +8 -0
- package/dist/testing/jest.d.ts.map +1 -0
- package/dist/testing/jest.js +11 -0
- package/dist/testing/jest.js.map +1 -0
- package/dist/testing/mod.d.ts +183 -0
- package/dist/testing/mod.d.ts.map +1 -0
- package/dist/testing/mod.js +271 -0
- package/dist/testing/mod.js.map +1 -0
- package/dist/testing/node.d.ts +9 -0
- package/dist/testing/node.d.ts.map +1 -0
- package/dist/testing/node.js +12 -0
- package/dist/testing/node.js.map +1 -0
- package/dist/testing/playwright.d.ts +93 -0
- package/dist/testing/playwright.d.ts.map +1 -0
- package/dist/testing/playwright.js +164 -0
- package/dist/testing/playwright.js.map +1 -0
- package/dist/testing/puppeteer.d.ts +36 -0
- package/dist/testing/puppeteer.d.ts.map +1 -0
- package/dist/testing/puppeteer.js +70 -0
- package/dist/testing/puppeteer.js.map +1 -0
- package/dist/testing/system.d.ts +181 -0
- package/dist/testing/system.d.ts.map +1 -0
- package/dist/testing/system.js +255 -0
- package/dist/testing/system.js.map +1 -0
- package/dist/testing/vitest.d.ts +8 -0
- package/dist/testing/vitest.d.ts.map +1 -0
- package/dist/testing/vitest.js +13 -0
- package/dist/testing/vitest.js.map +1 -0
- package/dist/tracing/http.d.ts +11 -0
- package/dist/tracing/http.d.ts.map +1 -0
- package/dist/tracing/http.js +34 -0
- package/dist/tracing/http.js.map +1 -0
- package/dist/tracing/mod.d.ts +20 -0
- package/dist/tracing/mod.d.ts.map +1 -0
- package/dist/tracing/mod.js +47 -0
- package/dist/tracing/mod.js.map +1 -0
- package/dist/tracing/node.d.ts +10 -0
- package/dist/tracing/node.d.ts.map +1 -0
- package/dist/tracing/node.js +142 -0
- package/dist/tracing/node.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/mod.d.ts +1 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/frontends/mod.test.ts +51 -0
- package/frontends/mod.ts +50 -0
- package/gateway/errors.ts +172 -0
- package/gateway/gateway.e2e.test.ts +297 -0
- package/gateway/gateway.test.ts +891 -0
- package/gateway/match.ts +239 -0
- package/gateway/middleware.ts +134 -0
- package/gateway/mod.ts +211 -0
- package/gateway/request.ts +578 -0
- package/gateway/serve.ts +738 -0
- package/gateways/mod.ts +60 -0
- package/internal/clients/call.ts +389 -0
- package/internal/clients/grpc.test.ts +368 -0
- package/internal/clients/grpc.ts +485 -0
- package/internal/clients/mod.test.ts +464 -0
- package/internal/clients/mod.ts +337 -0
- package/internal/fetch.ts +287 -0
- package/internal/otel/http.ts +91 -0
- package/internal/otel/index.ts +1 -7
- package/internal/otel/provider.ts +3 -0
- package/internal/otel/span.ts +31 -2
- package/internal/otel/tracer.test.ts +43 -15
- package/internal/otel/tracer.ts +12 -5
- package/internal/reqfields.ts +423 -0
- package/internal/runtime/mod.ts +41 -2
- package/internal/runtime/napi/napi.cjs +192 -16
- package/internal/runtime/napi/napi.d.cts +573 -33
- package/internal/runtime/shutdown.ts +11 -1
- package/internal/testing/carry.test.ts +242 -0
- package/internal/testing/carry.ts +101 -0
- package/internal/testing/runner.ts +10 -0
- package/internal/testing/scope.ts +372 -0
- package/internal/testing/steps.test.ts +152 -0
- package/internal/testing/steps.ts +342 -0
- package/internal/testing/telemetry.test.ts +210 -0
- package/internal/testing/telemetry.ts +466 -0
- package/metrics/mod.test.ts +13 -16
- package/metrics/mod.ts +57 -226
- package/mod.ts +1 -8
- package/otel.test.ts +13 -6
- package/package.json +92 -15
- package/pubsub/encoding.ts +11 -1
- package/pubsub/mod.ts +16 -4
- package/pubsub/pubsub.test.ts +233 -14
- package/pubsub/refs.ts +16 -0
- package/pubsub/subscription.ts +200 -21
- package/pubsub/topic.test.ts +26 -0
- package/pubsub/topic.ts +255 -36
- package/reqctx/mod.ts +267 -0
- package/reqctx/reqctx.test.ts +216 -0
- package/scripts/postprocess-docs.test.ts +58 -0
- package/services/index.test.ts +341 -1
- package/services/index.ts +185 -9
- package/storage/objects/bucket.test.ts +573 -0
- package/storage/objects/bucket.ts +650 -20
- package/storage/objects/error.ts +28 -1
- package/storage/objects/mod.ts +15 -0
- package/storage/objects/refs.ts +51 -3
- package/storage/postgres/database.integration.test.ts +275 -0
- package/storage/postgres/database.test.ts +193 -6
- package/storage/postgres/database.ts +206 -55
- package/storage/postgres/errors.ts +186 -0
- package/storage/postgres/interval.test.ts +72 -0
- package/storage/postgres/interval.ts +170 -0
- package/storage/postgres/mod.ts +4 -0
- package/storage/redis/bitfield.ts +162 -0
- package/storage/redis/command.ts +84 -0
- package/storage/redis/commands.ts +271 -21
- package/storage/redis/database.ts +77 -23
- package/storage/redis/mod.ts +38 -2
- package/storage/redis/native.ts +11 -0
- package/storage/redis/redis.test.ts +170 -10
- package/storage/redis/reply.ts +76 -1
- package/storage/redis/runtime.test.ts +15 -0
- package/storage/redis/target.ts +47 -44
- package/storage/redis/wire.ts +8 -32
- package/testing/bun.ts +13 -0
- package/testing/jest.ts +11 -0
- package/testing/mod.ts +393 -0
- package/testing/node.ts +12 -0
- package/testing/playwright.ts +247 -0
- package/testing/puppeteer.ts +101 -0
- package/testing/system.test.ts +108 -0
- package/testing/system.ts +373 -0
- package/testing/vitest.ts +13 -0
- package/tracing/http.ts +49 -0
- package/tracing/mod.ts +83 -0
- package/tracing/native.test.ts +267 -0
- package/tracing/node.ts +176 -0
- package/tracing/tracing.test.ts +953 -0
- package/dist/internal/otel/request_context.d.ts +0 -11
- package/dist/internal/otel/request_context.js +0 -20
- package/dist/internal/otel/request_context.js.map +0 -1
- package/dist/internal/reqtrack/mod.d.ts +0 -6
- package/dist/internal/reqtrack/mod.js +0 -28
- package/dist/internal/reqtrack/mod.js.map +0 -1
- package/dist/req_meta.d.ts +0 -47
- package/dist/req_meta.js +0 -26
- package/dist/req_meta.js.map +0 -1
- package/internal/otel/request_context.ts +0 -32
- package/internal/reqtrack/mod.ts +0 -39
- package/req_meta.ts +0 -75
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import {activeCtxBytes} from '../../internal/otel/index';
|
|
2
2
|
import * as runtime from '../../internal/runtime/mod';
|
|
3
|
+
import {currentScope, testMode} from '../../internal/testing/scope';
|
|
3
4
|
import {StringLiteral} from '../../internal/utils/constraints';
|
|
5
|
+
import {native, TooManyRowsError} from './errors';
|
|
6
|
+
import type {Interval} from './interval';
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Configures how database migrations are managed for a `Database`.
|
|
@@ -14,10 +17,27 @@ export interface MigrationsConfig {
|
|
|
14
17
|
*/
|
|
15
18
|
export interface DatabaseConfig {
|
|
16
19
|
migrations?: string | MigrationsConfig;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Tags that classify the database, for policies to read: written out as
|
|
23
|
+
* string literals, as in `{data: "pci"}`. Keys and values are lowercase
|
|
24
|
+
* letters, digits, `_` and `-`, at most 63 characters; a key starts with a
|
|
25
|
+
* letter, and not with `encore`, which is reserved. At most 32 tags.
|
|
26
|
+
*/
|
|
27
|
+
tags?: Record<string, string>;
|
|
17
28
|
}
|
|
18
29
|
|
|
19
30
|
/**
|
|
20
|
-
* Represents a single row from a query result
|
|
31
|
+
* Represents a single row from a query result, keyed by column name.
|
|
32
|
+
*
|
|
33
|
+
* Integers and floats are numbers, except an `int8` beyond what a number
|
|
34
|
+
* holds exactly (±(2^53−1)), which is a `bigint` so it is never rounded.
|
|
35
|
+
* `numeric` is a `Decimal`, `interval` a string in ISO 8601's form
|
|
36
|
+
* (`'P1Y2M3DT4H5M6.5S'`; see {@link Interval}), `timestamp`/`timestamptz` a
|
|
37
|
+
* `Date`, and `bytea` an `ArrayBuffer`. A type the SDK doesn't decode (`money`, `timetz`, ranges,
|
|
38
|
+
* `bit`, `xml`, `tsvector`, an extension's) is a string in the server's text
|
|
39
|
+
* form, and an array of one an array of such strings; a parameter of such a
|
|
40
|
+
* type takes a string, which the server parses.
|
|
21
41
|
*/
|
|
22
42
|
export type Row = Record<string, any>;
|
|
23
43
|
|
|
@@ -32,6 +52,8 @@ export type Primitive =
|
|
|
32
52
|
| Buffer
|
|
33
53
|
| Date
|
|
34
54
|
| Date[]
|
|
55
|
+
| Interval
|
|
56
|
+
| Interval[]
|
|
35
57
|
| Record<string, any>
|
|
36
58
|
| Record<string, any>[]
|
|
37
59
|
// Preserve the existing public input type during the style migration.
|
|
@@ -51,8 +73,16 @@ export interface ExecResult {
|
|
|
51
73
|
}
|
|
52
74
|
|
|
53
75
|
/** Base class containing shared query functionality */
|
|
54
|
-
class BaseQueryExecutor {
|
|
55
|
-
|
|
76
|
+
class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
77
|
+
/** The native handle, or how to find it for each call: a database's is
|
|
78
|
+
* the current test's instance of it, in test mode.
|
|
79
|
+
* @internal */
|
|
80
|
+
constructor(private readonly handle: I | (() => I)) {}
|
|
81
|
+
|
|
82
|
+
/** The native handle statements run on. */
|
|
83
|
+
protected get impl(): I {
|
|
84
|
+
return typeof this.handle === 'function' ? this.handle() : this.handle;
|
|
85
|
+
}
|
|
56
86
|
|
|
57
87
|
/**
|
|
58
88
|
* query queries the database using a template string, replacing your placeholders in the template
|
|
@@ -96,19 +126,17 @@ class BaseQueryExecutor {
|
|
|
96
126
|
query: string,
|
|
97
127
|
...params: Primitive[]
|
|
98
128
|
): AsyncGenerator<T> {
|
|
99
|
-
const cursor = await
|
|
100
|
-
query,
|
|
101
|
-
buildQueryArgs(params),
|
|
102
|
-
activeCtxBytes(),
|
|
129
|
+
const cursor = await native(
|
|
130
|
+
this.impl.query(query, buildQueryArgs(params), activeCtxBytes()),
|
|
103
131
|
);
|
|
104
132
|
try {
|
|
105
133
|
while (true) {
|
|
106
|
-
const row = await cursor.next();
|
|
134
|
+
const row = await native(cursor.next());
|
|
107
135
|
if (row === null) break;
|
|
108
136
|
yield row.values() as T;
|
|
109
137
|
}
|
|
110
138
|
} finally {
|
|
111
|
-
await cursor.close();
|
|
139
|
+
await native(cursor.close());
|
|
112
140
|
}
|
|
113
141
|
}
|
|
114
142
|
|
|
@@ -155,7 +183,7 @@ class BaseQueryExecutor {
|
|
|
155
183
|
/**
|
|
156
184
|
* queryRow is like query but returns only a single row.
|
|
157
185
|
* If the query selects no rows it returns null.
|
|
158
|
-
* It throws if more than one row matches.
|
|
186
|
+
* It throws a {@link TooManyRowsError} if more than one row matches.
|
|
159
187
|
*
|
|
160
188
|
* @example
|
|
161
189
|
* const email = "foo@example.com";
|
|
@@ -171,7 +199,7 @@ class BaseQueryExecutor {
|
|
|
171
199
|
/**
|
|
172
200
|
* rawQueryRow is like rawQuery but returns only a single row.
|
|
173
201
|
* If the query selects no rows, it returns null.
|
|
174
|
-
* It throws if more than one row matches.
|
|
202
|
+
* It throws a {@link TooManyRowsError} if more than one row matches.
|
|
175
203
|
*
|
|
176
204
|
* @example
|
|
177
205
|
* const query = "SELECT id FROM users WHERE email=$1";
|
|
@@ -189,7 +217,7 @@ class BaseQueryExecutor {
|
|
|
189
217
|
): Promise<T | null> {
|
|
190
218
|
let first: T | null = null;
|
|
191
219
|
for await (const row of this.rawQuery<T>(query, ...params)) {
|
|
192
|
-
if (first !== null) throw new
|
|
220
|
+
if (first !== null) throw new TooManyRowsError();
|
|
193
221
|
first = row;
|
|
194
222
|
}
|
|
195
223
|
return first;
|
|
@@ -222,20 +250,84 @@ class BaseQueryExecutor {
|
|
|
222
250
|
* @returns The affected-row count after the statement completes.
|
|
223
251
|
*/
|
|
224
252
|
async rawExec(query: string, ...params: Primitive[]): Promise<ExecResult> {
|
|
225
|
-
const cursor = await
|
|
226
|
-
query,
|
|
227
|
-
buildQueryArgs(params),
|
|
228
|
-
activeCtxBytes(),
|
|
253
|
+
const cursor = await native(
|
|
254
|
+
this.impl.query(query, buildQueryArgs(params), activeCtxBytes()),
|
|
229
255
|
);
|
|
230
256
|
try {
|
|
231
|
-
while ((await cursor.next()) !== null) {
|
|
257
|
+
while ((await native(cursor.next())) !== null) {
|
|
232
258
|
/* Discard RETURNING rows. */
|
|
233
259
|
}
|
|
234
|
-
return {rowsAffected: await cursor.rowsAffected()};
|
|
260
|
+
return {rowsAffected: await native(cursor.rowsAffected())};
|
|
235
261
|
} finally {
|
|
236
|
-
await cursor.close();
|
|
262
|
+
await native(cursor.close());
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** The isolation level of a transaction. */
|
|
268
|
+
export type IsolationLevel =
|
|
269
|
+
'read_committed' | 'repeatable_read' | 'serializable';
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* How a transaction runs. What isn't set is the database's default:
|
|
273
|
+
* `read_committed`, read-write (read-only on a read handle), not deferrable.
|
|
274
|
+
*/
|
|
275
|
+
export interface TransactionOptions {
|
|
276
|
+
isolation?: IsolationLevel;
|
|
277
|
+
/**
|
|
278
|
+
* Begin the transaction `READ ONLY`, or with `false` `READ WRITE`. A read
|
|
279
|
+
* handle's transactions are read-only whatever this says, so `false`
|
|
280
|
+
* throws a `TypeError` on one.
|
|
281
|
+
*/
|
|
282
|
+
readOnly?: boolean;
|
|
283
|
+
/**
|
|
284
|
+
* Begin the transaction `DEFERRABLE`, or with `false` `NOT DEFERRABLE`. It
|
|
285
|
+
* matters only to a serializable, read-only transaction, which then waits
|
|
286
|
+
* until it can run without serialization failures.
|
|
287
|
+
*/
|
|
288
|
+
deferrable?: boolean;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const isolationLevels: Record<IsolationLevel, runtime.SQLIsolationLevel> = {
|
|
292
|
+
read_committed: runtime.SQLIsolationLevel.ReadCommitted,
|
|
293
|
+
repeatable_read: runtime.SQLIsolationLevel.RepeatableRead,
|
|
294
|
+
serializable: runtime.SQLIsolationLevel.Serializable,
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Begins a transaction on `impl`, a pool or connection, with `options`.
|
|
299
|
+
* Throws a `TypeError` for an unknown isolation level, or for
|
|
300
|
+
* `readOnly: false` on a read handle's pool or connection.
|
|
301
|
+
*/
|
|
302
|
+
async function beginOn(
|
|
303
|
+
impl: runtime.SQLDatabase | runtime.SQLConn,
|
|
304
|
+
options: TransactionOptions | undefined,
|
|
305
|
+
): Promise<Transaction> {
|
|
306
|
+
let opts: runtime.SQLTransactionOptions | undefined;
|
|
307
|
+
if (options !== undefined) {
|
|
308
|
+
const {isolation, readOnly, deferrable} = options;
|
|
309
|
+
if (
|
|
310
|
+
isolation !== undefined &&
|
|
311
|
+
!Object.prototype.hasOwnProperty.call(isolationLevels, isolation)
|
|
312
|
+
) {
|
|
313
|
+
throw new TypeError(
|
|
314
|
+
`unknown isolation level ${JSON.stringify(isolation)}: ` +
|
|
315
|
+
`expected one of ${Object.keys(isolationLevels).join(', ')}`,
|
|
316
|
+
);
|
|
237
317
|
}
|
|
318
|
+
if (readOnly === false && impl.isReadOnly()) {
|
|
319
|
+
throw new TypeError(
|
|
320
|
+
"a read handle's transactions are read-only: readOnly cannot be false",
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
opts = {
|
|
324
|
+
isolation:
|
|
325
|
+
isolation === undefined ? undefined : isolationLevels[isolation],
|
|
326
|
+
readOnly,
|
|
327
|
+
deferrable,
|
|
328
|
+
};
|
|
238
329
|
}
|
|
330
|
+
return new Transaction(await native(impl.begin(opts, activeCtxBytes())));
|
|
239
331
|
}
|
|
240
332
|
|
|
241
333
|
/**
|
|
@@ -243,7 +335,9 @@ class BaseQueryExecutor {
|
|
|
243
335
|
* Obtain one with read(), write(), or admin(), or use a connection/transaction.
|
|
244
336
|
* A Database declaration deliberately does not implement this interface.
|
|
245
337
|
*/
|
|
246
|
-
export abstract class Queryable
|
|
338
|
+
export abstract class Queryable<
|
|
339
|
+
I extends SQLQueryExecutor = SQLQueryExecutor,
|
|
340
|
+
> extends BaseQueryExecutor<I> {
|
|
247
341
|
private readonly queryableBrand = true;
|
|
248
342
|
}
|
|
249
343
|
|
|
@@ -260,10 +354,10 @@ export interface ReadOptions {
|
|
|
260
354
|
* A database handle with access recorded by Encore's parser at its creation site.
|
|
261
355
|
*
|
|
262
356
|
* Handles share the declaration's pool. A read handle runs on read-only
|
|
263
|
-
* connections: a statement that writes fails with `
|
|
264
|
-
* (SQLSTATE 25006), and the transactions it
|
|
265
|
-
* connections it acquires, are read-only. A write
|
|
266
|
-
* read-write.
|
|
357
|
+
* connections: a statement that writes fails with a `DatabaseError` whose
|
|
358
|
+
* code is `read_only_transaction` (SQLSTATE 25006), and the transactions it
|
|
359
|
+
* begins, and those of the connections it acquires, are read-only. A write
|
|
360
|
+
* or admin handle runs read-write.
|
|
267
361
|
*
|
|
268
362
|
* On the database's own pool, which a read handle shares when there is no
|
|
269
363
|
* read-only pool for it, that refuses mistaken writes, not deliberate ones:
|
|
@@ -271,10 +365,9 @@ export interface ReadOptions {
|
|
|
271
365
|
* connection (`SET default_transaction_read_only = off`) lifts it. Against
|
|
272
366
|
* untrusted code, the read-only pool's database role is what refuses writes.
|
|
273
367
|
*/
|
|
274
|
-
export class DatabaseRef extends Queryable {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
constructor(impl: runtime.SQLDatabase) {
|
|
368
|
+
export class DatabaseRef extends Queryable<runtime.SQLDatabase> {
|
|
369
|
+
/** @internal */
|
|
370
|
+
constructor(impl: runtime.SQLDatabase | (() => runtime.SQLDatabase)) {
|
|
278
371
|
super(impl);
|
|
279
372
|
}
|
|
280
373
|
|
|
@@ -297,11 +390,15 @@ export class DatabaseRef extends Queryable {
|
|
|
297
390
|
* See {@link Database.acquire}.
|
|
298
391
|
*/
|
|
299
392
|
async acquire(): Promise<Connection> {
|
|
300
|
-
return new Connection(await this.impl.acquire());
|
|
393
|
+
return new Connection(await native(this.impl.acquire(activeCtxBytes())));
|
|
301
394
|
}
|
|
302
395
|
|
|
303
|
-
|
|
304
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Begins a transaction with this handle's access. See {@link Database.begin}.
|
|
398
|
+
* A read handle's transactions are read-only.
|
|
399
|
+
*/
|
|
400
|
+
async begin(options?: TransactionOptions): Promise<Transaction> {
|
|
401
|
+
return beginOn(this.impl, options);
|
|
305
402
|
}
|
|
306
403
|
}
|
|
307
404
|
|
|
@@ -312,14 +409,15 @@ export class DatabaseRef extends Queryable {
|
|
|
312
409
|
* If you want to reference an existing database, use `Database.named(name)` as it is a
|
|
313
410
|
* compile error to create duplicate databases.
|
|
314
411
|
*/
|
|
315
|
-
export class Database extends BaseQueryExecutor {
|
|
316
|
-
declare protected readonly impl: runtime.SQLDatabase;
|
|
412
|
+
export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
317
413
|
/** The read handles, made on first use. */
|
|
318
414
|
private reader?: DatabaseRef;
|
|
319
415
|
private primaryReader?: DatabaseRef;
|
|
416
|
+
private readonly dbName: string;
|
|
320
417
|
|
|
321
418
|
constructor(name: string, _cfg?: DatabaseConfig) {
|
|
322
|
-
super(runtime.RT.sqlDatabase(name));
|
|
419
|
+
super(testMode ? testHandle(name) : runtime.RT.sqlDatabase(name));
|
|
420
|
+
this.dbName = name;
|
|
323
421
|
}
|
|
324
422
|
|
|
325
423
|
/**
|
|
@@ -344,16 +442,23 @@ export class Database extends BaseQueryExecutor {
|
|
|
344
442
|
|
|
345
443
|
/** Select reader and writer access. */
|
|
346
444
|
write(): DatabaseRef {
|
|
347
|
-
return new DatabaseRef(this.impl);
|
|
445
|
+
return new DatabaseRef(testMode ? () => this.impl : this.impl);
|
|
348
446
|
}
|
|
349
447
|
|
|
350
448
|
/** Select reader, writer, and admin access. */
|
|
351
449
|
admin(): DatabaseRef {
|
|
352
|
-
return
|
|
450
|
+
return this.write();
|
|
353
451
|
}
|
|
354
452
|
|
|
355
453
|
private ref(access: runtime.SQLAccess): DatabaseRef {
|
|
356
|
-
return new DatabaseRef(this.impl.withAccess(access));
|
|
454
|
+
if (!testMode) return new DatabaseRef(this.impl.withAccess(access));
|
|
455
|
+
// The current test's instance, for each call.
|
|
456
|
+
let own: runtime.SQLDatabase | undefined;
|
|
457
|
+
return new DatabaseRef(() => {
|
|
458
|
+
const scope = currentScope();
|
|
459
|
+
if (scope !== null) return scope.sqlDatabase(this.dbName, access);
|
|
460
|
+
return (own ??= this.impl.withAccess(access));
|
|
461
|
+
});
|
|
357
462
|
}
|
|
358
463
|
|
|
359
464
|
/**
|
|
@@ -378,7 +483,7 @@ export class Database extends BaseQueryExecutor {
|
|
|
378
483
|
* @returns a new connection to the database
|
|
379
484
|
*/
|
|
380
485
|
async acquire(): Promise<Connection> {
|
|
381
|
-
const impl = await this.impl.acquire();
|
|
486
|
+
const impl = await native(this.impl.acquire(activeCtxBytes()));
|
|
382
487
|
return new Connection(impl);
|
|
383
488
|
}
|
|
384
489
|
|
|
@@ -398,12 +503,17 @@ export class Database extends BaseQueryExecutor {
|
|
|
398
503
|
* If you can't use `await using`, make sure to always call `commit`
|
|
399
504
|
* or `rollback` yourself to prevent hanging transactions.
|
|
400
505
|
*
|
|
506
|
+
* `options` sets the transaction's isolation level and whether it is
|
|
507
|
+
* read-only or deferrable:
|
|
508
|
+
*
|
|
509
|
+
* ```ts
|
|
510
|
+
* await using tx = await db.begin({isolation: 'serializable'});
|
|
511
|
+
* ```
|
|
512
|
+
*
|
|
401
513
|
* @returns a transaction object that implements `AsyncDisposable`
|
|
402
514
|
*/
|
|
403
|
-
async begin(): Promise<Transaction> {
|
|
404
|
-
|
|
405
|
-
const impl = await this.impl.begin(source);
|
|
406
|
-
return new Transaction(impl);
|
|
515
|
+
async begin(options?: TransactionOptions): Promise<Transaction> {
|
|
516
|
+
return beginOn(this.impl, options);
|
|
407
517
|
}
|
|
408
518
|
}
|
|
409
519
|
|
|
@@ -424,10 +534,13 @@ export class Database extends BaseQueryExecutor {
|
|
|
424
534
|
* If you can't use `await using`, make sure to always call `commit` or
|
|
425
535
|
* `rollback` yourself to prevent hanging transactions.
|
|
426
536
|
*/
|
|
427
|
-
export class Transaction
|
|
428
|
-
|
|
537
|
+
export class Transaction
|
|
538
|
+
extends Queryable<runtime.Transaction>
|
|
539
|
+
implements AsyncDisposable
|
|
540
|
+
{
|
|
429
541
|
private done: boolean = false;
|
|
430
542
|
|
|
543
|
+
/** @internal */
|
|
431
544
|
constructor(impl: runtime.Transaction) {
|
|
432
545
|
super(impl);
|
|
433
546
|
}
|
|
@@ -437,7 +550,7 @@ export class Transaction extends Queryable implements AsyncDisposable {
|
|
|
437
550
|
*/
|
|
438
551
|
async commit() {
|
|
439
552
|
try {
|
|
440
|
-
await this.impl.commit();
|
|
553
|
+
await native(this.impl.commit());
|
|
441
554
|
} finally {
|
|
442
555
|
this.done = this.impl.isClosed();
|
|
443
556
|
}
|
|
@@ -448,7 +561,7 @@ export class Transaction extends Queryable implements AsyncDisposable {
|
|
|
448
561
|
*/
|
|
449
562
|
async rollback() {
|
|
450
563
|
try {
|
|
451
|
-
await this.impl.rollback();
|
|
564
|
+
await native(this.impl.rollback());
|
|
452
565
|
} finally {
|
|
453
566
|
this.done = this.impl.isClosed();
|
|
454
567
|
}
|
|
@@ -464,16 +577,22 @@ export class Transaction extends Queryable implements AsyncDisposable {
|
|
|
464
577
|
/**
|
|
465
578
|
* Represents a dedicated connection to a database.
|
|
466
579
|
*/
|
|
467
|
-
export class Connection
|
|
468
|
-
|
|
469
|
-
|
|
580
|
+
export class Connection
|
|
581
|
+
extends Queryable<runtime.SQLConn>
|
|
582
|
+
implements AsyncDisposable
|
|
583
|
+
{
|
|
584
|
+
/** @internal */
|
|
470
585
|
constructor(impl: runtime.SQLConn) {
|
|
471
586
|
super(impl);
|
|
472
587
|
}
|
|
473
588
|
|
|
474
|
-
/**
|
|
475
|
-
|
|
476
|
-
|
|
589
|
+
/**
|
|
590
|
+
* Begin a transaction that exclusively borrows this connection until
|
|
591
|
+
* completion. See {@link Database.begin}. A connection acquired from a
|
|
592
|
+
* read handle begins read-only transactions.
|
|
593
|
+
*/
|
|
594
|
+
async begin(options?: TransactionOptions): Promise<Transaction> {
|
|
595
|
+
return beginOn(this.impl, options);
|
|
477
596
|
}
|
|
478
597
|
|
|
479
598
|
async [Symbol.asyncDispose]() {
|
|
@@ -484,10 +603,21 @@ export class Connection extends Queryable implements AsyncDisposable {
|
|
|
484
603
|
* Returns the connection to the database pool.
|
|
485
604
|
*/
|
|
486
605
|
async close() {
|
|
487
|
-
await this.impl.close();
|
|
606
|
+
await native(this.impl.close());
|
|
488
607
|
}
|
|
489
608
|
}
|
|
490
609
|
|
|
610
|
+
/**
|
|
611
|
+
* The handle of the database `name` in test mode, for each call: the current
|
|
612
|
+
* test's instance of it, or the process's own outside any test, made on
|
|
613
|
+
* first use.
|
|
614
|
+
*/
|
|
615
|
+
function testHandle(name: string): () => runtime.SQLDatabase {
|
|
616
|
+
let own: runtime.SQLDatabase | undefined;
|
|
617
|
+
return () =>
|
|
618
|
+
currentScope()?.sqlDatabase(name) ?? (own ??= runtime.RT.sqlDatabase(name));
|
|
619
|
+
}
|
|
620
|
+
|
|
491
621
|
function buildQuery(strings: TemplateStringsArray, expr: Primitive[]): string {
|
|
492
622
|
let query = '';
|
|
493
623
|
for (let i = 0; i < strings.length; i++) {
|
|
@@ -502,6 +632,27 @@ function buildQuery(strings: TemplateStringsArray, expr: Primitive[]): string {
|
|
|
502
632
|
}
|
|
503
633
|
|
|
504
634
|
function buildQueryArgs(params: Primitive[]): runtime.QueryArgs {
|
|
505
|
-
|
|
506
|
-
|
|
635
|
+
return new runtime.QueryArgs(
|
|
636
|
+
params.map(p => (Array.isArray(p) ? p.map(param) : param(p as Primitive))),
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* `p` as the native module takes it: null for undefined, and a
|
|
642
|
+
* `Temporal.Duration`'s ISO 8601 string, which Postgres parses as an
|
|
643
|
+
* `interval`.
|
|
644
|
+
*/
|
|
645
|
+
function param(p: Primitive): Primitive {
|
|
646
|
+
if (p === undefined) return null;
|
|
647
|
+
if (isTemporalDuration(p)) return p.toString();
|
|
648
|
+
return p;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** Whether `p` is a `Temporal.Duration`, where the runtime has Temporal. */
|
|
652
|
+
function isTemporalDuration(p: unknown): p is {toString(): string} {
|
|
653
|
+
return (
|
|
654
|
+
typeof p === 'object' &&
|
|
655
|
+
p !== null &&
|
|
656
|
+
Object.prototype.toString.call(p) === '[object Temporal.Duration]'
|
|
657
|
+
);
|
|
507
658
|
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What kind of error a database reported, by its SQLSTATE: the cases an app
|
|
3
|
+
* most often handles. `other` is any other; the SQLSTATE says what.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* try {
|
|
7
|
+
* await db.exec`INSERT INTO users (email) VALUES (${email})`;
|
|
8
|
+
* } catch (err) {
|
|
9
|
+
* if (err instanceof DatabaseError && err.code === ErrorCode.UniqueViolation) {
|
|
10
|
+
* return {taken: true};
|
|
11
|
+
* }
|
|
12
|
+
* throw err;
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
export const ErrorCode = {
|
|
16
|
+
/** None of the others; the SQLSTATE says what. */
|
|
17
|
+
Other: 'other',
|
|
18
|
+
/** A `NOT NULL` constraint would be violated (23502). */
|
|
19
|
+
NotNullViolation: 'not_null_violation',
|
|
20
|
+
/** A foreign key constraint would be violated (23503). */
|
|
21
|
+
ForeignKeyViolation: 'foreign_key_violation',
|
|
22
|
+
/** A unique constraint would be violated (23505). */
|
|
23
|
+
UniqueViolation: 'unique_violation',
|
|
24
|
+
/** A check constraint would be violated (23514). */
|
|
25
|
+
CheckViolation: 'check_violation',
|
|
26
|
+
/** An exclusion constraint would be violated (23P01). */
|
|
27
|
+
ExcludeViolation: 'exclude_violation',
|
|
28
|
+
/** A statement ran in a transaction an earlier statement failed (25P02). */
|
|
29
|
+
TransactionFailed: 'transaction_failed',
|
|
30
|
+
/** A deadlock was detected (40P01). */
|
|
31
|
+
DeadlockDetected: 'deadlock_detected',
|
|
32
|
+
/** The server refused a connection: it has as many as it allows (53300). */
|
|
33
|
+
TooManyConnections: 'too_many_connections',
|
|
34
|
+
/**
|
|
35
|
+
* A serializable or repeatable read transaction could not be serialized
|
|
36
|
+
* with a concurrent one (40001); retrying it may succeed.
|
|
37
|
+
*/
|
|
38
|
+
SerializationFailure: 'serialization_failure',
|
|
39
|
+
/** A write in a read-only transaction (25006), such as one through a read handle. */
|
|
40
|
+
ReadOnlyTransaction: 'read_only_transaction',
|
|
41
|
+
/**
|
|
42
|
+
* A lock could not be taken without waiting (55P03), as with
|
|
43
|
+
* `FOR UPDATE NOWAIT` or `lock_timeout`.
|
|
44
|
+
*/
|
|
45
|
+
LockNotAvailable: 'lock_not_available',
|
|
46
|
+
/** The statement was canceled (57014): by a timeout, or on request. */
|
|
47
|
+
QueryCanceled: 'query_canceled',
|
|
48
|
+
/** The database could not be reached, or the connection to it was lost (class 08). */
|
|
49
|
+
ConnectionFailure: 'connection_failure',
|
|
50
|
+
} as const;
|
|
51
|
+
|
|
52
|
+
/** One of the {@link ErrorCode} values: `'unique_violation'`. */
|
|
53
|
+
export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* An error a database reported, or a failure to reach it.
|
|
57
|
+
*
|
|
58
|
+
* A failure to reach the database, which the server can't report, has
|
|
59
|
+
* `sqlState` `"08001"`, `code` `connection_failure`, the client's message
|
|
60
|
+
* and none of the optional fields. The caller's own mistakes, such as a
|
|
61
|
+
* parameter of the wrong type, and using a busy or closed handle, are plain
|
|
62
|
+
* errors instead.
|
|
63
|
+
*
|
|
64
|
+
* The optional fields are sent only for some errors (see
|
|
65
|
+
* https://www.postgresql.org/docs/current/protocol-error-fields.html); one
|
|
66
|
+
* being there says nothing about the others.
|
|
67
|
+
*
|
|
68
|
+
* Its `message` reads `ERROR: duplicate key value violates unique constraint
|
|
69
|
+
* "users_pkey" (Code unique_violation: SQLSTATE 23505)`. A subscription
|
|
70
|
+
* handler that throws one fails with the API error code of its SQLSTATE:
|
|
71
|
+
* `already_exists` for a unique violation.
|
|
72
|
+
*/
|
|
73
|
+
export class DatabaseError extends Error {
|
|
74
|
+
declare readonly name: 'DatabaseError';
|
|
75
|
+
/** What kind of error it is, by its SQLSTATE. */
|
|
76
|
+
readonly code: ErrorCode;
|
|
77
|
+
/** The SQLSTATE: `"23505"`. */
|
|
78
|
+
readonly sqlState: string;
|
|
79
|
+
/** `ERROR`, `FATAL` or `PANIC`. */
|
|
80
|
+
readonly severity: string;
|
|
81
|
+
/**
|
|
82
|
+
* A secondary message with more detail, such as the key a unique
|
|
83
|
+
* violation conflicted on.
|
|
84
|
+
*/
|
|
85
|
+
readonly detail?: string;
|
|
86
|
+
/** A suggestion what to do about it. */
|
|
87
|
+
readonly hint?: string;
|
|
88
|
+
/** The schema of the object the error is about. */
|
|
89
|
+
readonly schemaName?: string;
|
|
90
|
+
/** The table the error is about. */
|
|
91
|
+
readonly tableName?: string;
|
|
92
|
+
/** The column the error is about. */
|
|
93
|
+
readonly columnName?: string;
|
|
94
|
+
/** The data type the error is about. */
|
|
95
|
+
readonly dataTypeName?: string;
|
|
96
|
+
/** The constraint the error is about; an index counts as one. */
|
|
97
|
+
readonly constraintName?: string;
|
|
98
|
+
|
|
99
|
+
constructor(fields: {
|
|
100
|
+
message: string;
|
|
101
|
+
code: ErrorCode;
|
|
102
|
+
sqlState: string;
|
|
103
|
+
severity: string;
|
|
104
|
+
detail?: string;
|
|
105
|
+
hint?: string;
|
|
106
|
+
schemaName?: string;
|
|
107
|
+
tableName?: string;
|
|
108
|
+
columnName?: string;
|
|
109
|
+
dataTypeName?: string;
|
|
110
|
+
constraintName?: string;
|
|
111
|
+
}) {
|
|
112
|
+
super(fields.message);
|
|
113
|
+
Object.defineProperty(this, 'name', {
|
|
114
|
+
value: 'DatabaseError',
|
|
115
|
+
enumerable: false,
|
|
116
|
+
configurable: true,
|
|
117
|
+
});
|
|
118
|
+
this.code = fields.code;
|
|
119
|
+
this.sqlState = fields.sqlState;
|
|
120
|
+
this.severity = fields.severity;
|
|
121
|
+
this.detail = fields.detail;
|
|
122
|
+
this.hint = fields.hint;
|
|
123
|
+
this.schemaName = fields.schemaName;
|
|
124
|
+
this.tableName = fields.tableName;
|
|
125
|
+
this.columnName = fields.columnName;
|
|
126
|
+
this.dataTypeName = fields.dataTypeName;
|
|
127
|
+
this.constraintName = fields.constraintName;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Thrown by `queryRow` and `rawQueryRow` for a query that returns more than one row. */
|
|
132
|
+
export class TooManyRowsError extends Error {
|
|
133
|
+
declare readonly name: 'TooManyRowsError';
|
|
134
|
+
|
|
135
|
+
constructor() {
|
|
136
|
+
super('query returned more than one row');
|
|
137
|
+
Object.defineProperty(this, 'name', {
|
|
138
|
+
value: 'TooManyRowsError',
|
|
139
|
+
enumerable: false,
|
|
140
|
+
configurable: true,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Begins the message of an error the native module rejects with for a
|
|
147
|
+
* `DatabaseError`, which the rest of the message holds as JSON. The same as
|
|
148
|
+
* `DATABASE_ERROR_PREFIX` in `sdk/ts/src/postgres.rs`.
|
|
149
|
+
*/
|
|
150
|
+
const DATABASE_ERROR_PREFIX = 'encore:postgres.DatabaseError:';
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Awaits a native database call, throwing the error it rejects with as a
|
|
154
|
+
* `DatabaseError` if it is one.
|
|
155
|
+
*/
|
|
156
|
+
export async function native<T>(call: Promise<T>): Promise<T> {
|
|
157
|
+
try {
|
|
158
|
+
return await call;
|
|
159
|
+
} catch (err) {
|
|
160
|
+
throw fromNative(err);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** `err`, a native database call's, as a `DatabaseError` if it is one. */
|
|
165
|
+
function fromNative(err: unknown): unknown {
|
|
166
|
+
if (
|
|
167
|
+
!(err instanceof Error) ||
|
|
168
|
+
!err.message.startsWith(DATABASE_ERROR_PREFIX)
|
|
169
|
+
) {
|
|
170
|
+
return err;
|
|
171
|
+
}
|
|
172
|
+
const f = JSON.parse(err.message.slice(DATABASE_ERROR_PREFIX.length));
|
|
173
|
+
return new DatabaseError({
|
|
174
|
+
message: f.message,
|
|
175
|
+
code: f.code,
|
|
176
|
+
sqlState: f.sqlState,
|
|
177
|
+
severity: f.severity,
|
|
178
|
+
detail: f.detail ?? undefined,
|
|
179
|
+
hint: f.hint ?? undefined,
|
|
180
|
+
schemaName: f.schemaName ?? undefined,
|
|
181
|
+
tableName: f.tableName ?? undefined,
|
|
182
|
+
columnName: f.columnName ?? undefined,
|
|
183
|
+
dataTypeName: f.dataTypeName ?? undefined,
|
|
184
|
+
constraintName: f.constraintName ?? undefined,
|
|
185
|
+
});
|
|
186
|
+
}
|