@encore.dev/sdk 0.0.0 → 2.0.0-beta.11
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/LICENSE +362 -0
- package/README.md +245 -2
- package/app_meta.test.ts +154 -0
- package/app_meta.ts +226 -0
- package/config/config.test.ts +384 -0
- package/config/config.ts +236 -0
- package/config/mod.ts +15 -0
- package/config/secrets.ts +173 -0
- package/config/value.ts +239 -0
- package/cron/mod.test.ts +52 -0
- package/cron/mod.ts +62 -0
- package/dist/app_meta.d.ts +126 -0
- package/dist/app_meta.d.ts.map +1 -0
- package/dist/app_meta.js +67 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +135 -0
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +115 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +5 -0
- package/dist/config/mod.d.ts.map +1 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +124 -0
- package/dist/config/secrets.d.ts.map +1 -0
- package/dist/config/secrets.js +73 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +54 -0
- package/dist/config/value.d.ts.map +1 -0
- package/dist/config/value.js +181 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +57 -0
- package/dist/cron/mod.d.ts.map +1 -0
- package/dist/cron/mod.js +39 -0
- package/dist/cron/mod.js.map +1 -0
- 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 +171 -0
- package/dist/internal/clients/call.d.ts.map +1 -0
- package/dist/internal/clients/call.js +284 -0
- package/dist/internal/clients/call.js.map +1 -0
- package/dist/internal/clients/grpc.d.ts +108 -0
- package/dist/internal/clients/grpc.d.ts.map +1 -0
- package/dist/internal/clients/grpc.js +349 -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 +67 -0
- package/dist/internal/fetch.d.ts.map +1 -0
- package/dist/internal/fetch.js +271 -0
- package/dist/internal/fetch.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +52 -0
- package/dist/internal/otel/backend.d.ts.map +1 -0
- package/dist/internal/otel/backend.js +2 -0
- package/dist/internal/otel/backend.js.map +1 -0
- package/dist/internal/otel/context.d.ts +11 -0
- package/dist/internal/otel/context.d.ts.map +1 -0
- package/dist/internal/otel/context.js +34 -0
- package/dist/internal/otel/context.js.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +23 -0
- package/dist/internal/otel/context_manager.d.ts.map +1 -0
- package/dist/internal/otel/context_manager.js +120 -0
- package/dist/internal/otel/context_manager.js.map +1 -0
- package/dist/internal/otel/encode.d.ts +114 -0
- package/dist/internal/otel/encode.d.ts.map +1 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.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 +65 -0
- package/dist/internal/otel/http.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.d.ts.map +1 -0
- package/dist/internal/otel/index.js +8 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +74 -0
- package/dist/internal/otel/meter.d.ts.map +1 -0
- package/dist/internal/otel/meter.js +265 -0
- package/dist/internal/otel/meter.js.map +1 -0
- package/dist/internal/otel/propagator.d.ts +15 -0
- package/dist/internal/otel/propagator.d.ts.map +1 -0
- package/dist/internal/otel/propagator.js +61 -0
- package/dist/internal/otel/propagator.js.map +1 -0
- package/dist/internal/otel/provider.d.ts +13 -0
- package/dist/internal/otel/provider.d.ts.map +1 -0
- package/dist/internal/otel/provider.js +22 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/span.d.ts +75 -0
- package/dist/internal/otel/span.d.ts.map +1 -0
- package/dist/internal/otel/span.js +234 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +16 -0
- package/dist/internal/otel/tracer.d.ts.map +1 -0
- package/dist/internal/otel/tracer.js +71 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- 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 +12 -0
- package/dist/internal/runtime/meter.d.ts.map +1 -0
- package/dist/internal/runtime/meter.js +22 -0
- package/dist/internal/runtime/meter.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +18 -0
- package/dist/internal/runtime/mod.d.ts.map +1 -0
- package/dist/internal/runtime/mod.js +59 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +308 -0
- package/dist/internal/runtime/napi/napi.d.cts +1249 -0
- package/dist/internal/runtime/shutdown.d.ts +91 -0
- package/dist/internal/runtime/shutdown.d.ts.map +1 -0
- package/dist/internal/runtime/shutdown.js +299 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- 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 +112 -0
- package/dist/internal/testing/scope.d.ts.map +1 -0
- package/dist/internal/testing/scope.js +346 -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 +10 -0
- package/dist/internal/utils/constraints.d.ts.map +1 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +40 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +125 -0
- package/dist/log/logger.d.ts.map +1 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +21 -0
- package/dist/log/runtime.d.ts.map +1 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +10 -0
- package/dist/log/stderr.d.ts.map +1 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +242 -0
- package/dist/metrics/mod.d.ts.map +1 -0
- package/dist/metrics/mod.js +353 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +5 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +3 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +10 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +8 -0
- package/dist/pubsub/encoding.d.ts.map +1 -0
- package/dist/pubsub/encoding.js +18 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +7 -0
- package/dist/pubsub/mod.d.ts.map +1 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +20 -0
- package/dist/pubsub/refs.d.ts.map +1 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +223 -0
- package/dist/pubsub/subscription.d.ts.map +1 -0
- package/dist/pubsub/subscription.js +121 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +218 -0
- package/dist/pubsub/topic.d.ts.map +1 -0
- package/dist/pubsub/topic.js +281 -0
- package/dist/pubsub/topic.js.map +1 -0
- 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 +171 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +187 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +60 -0
- package/dist/shutdown/mod.d.ts.map +1 -0
- package/dist/shutdown/mod.js +59 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/clickhouse/database.d.ts +262 -0
- package/dist/storage/clickhouse/database.d.ts.map +1 -0
- package/dist/storage/clickhouse/database.js +362 -0
- package/dist/storage/clickhouse/database.js.map +1 -0
- package/dist/storage/clickhouse/errors.d.ts +67 -0
- package/dist/storage/clickhouse/errors.d.ts.map +1 -0
- package/dist/storage/clickhouse/errors.js +91 -0
- package/dist/storage/clickhouse/errors.js.map +1 -0
- package/dist/storage/clickhouse/mod.d.ts +31 -0
- package/dist/storage/clickhouse/mod.d.ts.map +1 -0
- package/dist/storage/clickhouse/mod.js +29 -0
- package/dist/storage/clickhouse/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +522 -0
- package/dist/storage/objects/bucket.d.ts.map +1 -0
- package/dist/storage/objects/bucket.js +414 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +24 -0
- package/dist/storage/objects/error.d.ts.map +1 -0
- package/dist/storage/objects/error.js +105 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +5 -0
- package/dist/storage/objects/mod.d.ts.map +1 -0
- package/dist/storage/objects/mod.js +3 -0
- package/dist/storage/objects/mod.js.map +1 -0
- package/dist/storage/objects/refs.d.ts +47 -0
- package/dist/storage/objects/refs.d.ts.map +1 -0
- package/dist/storage/objects/refs.js +26 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +365 -0
- package/dist/storage/postgres/database.d.ts.map +1 -0
- package/dist/storage/postgres/database.js +480 -0
- package/dist/storage/postgres/database.js.map +1 -0
- 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 +5 -0
- package/dist/storage/postgres/mod.d.ts.map +1 -0
- package/dist/storage/postgres/mod.js +4 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- 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 +657 -0
- package/dist/storage/redis/commands.d.ts.map +1 -0
- package/dist/storage/redis/commands.js +759 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +336 -0
- package/dist/storage/redis/database.d.ts.map +1 -0
- package/dist/storage/redis/database.js +398 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +115 -0
- package/dist/storage/redis/mod.d.ts.map +1 -0
- package/dist/storage/redis/mod.js +109 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +16 -0
- package/dist/storage/redis/native.d.ts.map +1 -0
- package/dist/storage/redis/native.js +21 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +90 -0
- package/dist/storage/redis/options.d.ts.map +1 -0
- package/dist/storage/redis/options.js +53 -0
- package/dist/storage/redis/options.js.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +55 -0
- package/dist/storage/redis/pubsub.d.ts.map +1 -0
- package/dist/storage/redis/pubsub.js +88 -0
- package/dist/storage/redis/pubsub.js.map +1 -0
- package/dist/storage/redis/reply.d.ts +290 -0
- package/dist/storage/redis/reply.d.ts.map +1 -0
- package/dist/storage/redis/reply.js +611 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +12 -0
- package/dist/storage/redis/scan.d.ts.map +1 -0
- package/dist/storage/redis/scan.js +76 -0
- package/dist/storage/redis/scan.js.map +1 -0
- package/dist/storage/redis/script.d.ts +92 -0
- package/dist/storage/redis/script.d.ts.map +1 -0
- package/dist/storage/redis/script.js +143 -0
- package/dist/storage/redis/script.js.map +1 -0
- package/dist/storage/redis/target.d.ts +49 -0
- package/dist/storage/redis/target.d.ts.map +1 -0
- package/dist/storage/redis/target.js +93 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +102 -0
- package/dist/storage/redis/wire.d.ts.map +1 -0
- package/dist/storage/redis/wire.js +309 -0
- package/dist/storage/redis/wire.js.map +1 -0
- 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 -0
- package/dist/types/mod.d.ts +43 -0
- package/dist/types/mod.d.ts.map +1 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.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 +418 -0
- package/internal/clients/grpc.test.ts +664 -0
- package/internal/clients/grpc.ts +485 -0
- package/internal/clients/mod.test.ts +481 -0
- package/internal/clients/mod.ts +337 -0
- package/internal/fetch.ts +320 -0
- package/internal/otel/backend.ts +60 -0
- package/internal/otel/context.test.ts +70 -0
- package/internal/otel/context.ts +48 -0
- package/internal/otel/context_manager.test.ts +154 -0
- package/internal/otel/context_manager.ts +141 -0
- package/internal/otel/encode.test.ts +266 -0
- package/internal/otel/encode.ts +408 -0
- package/internal/otel/http.ts +92 -0
- package/internal/otel/index.ts +14 -0
- package/internal/otel/meter.test.ts +231 -0
- package/internal/otel/meter.ts +332 -0
- package/internal/otel/propagator.test.ts +105 -0
- package/internal/otel/propagator.ts +68 -0
- package/internal/otel/provider.ts +26 -0
- package/internal/otel/span.ts +281 -0
- package/internal/otel/tracer.test.ts +380 -0
- package/internal/otel/tracer.ts +111 -0
- package/internal/reqfields.ts +423 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +81 -0
- package/internal/runtime/napi/napi.cjs +308 -0
- package/internal/runtime/napi/napi.d.cts +1249 -0
- package/internal/runtime/shutdown.test.ts +464 -0
- package/internal/runtime/shutdown.ts +350 -0
- 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 +384 -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/internal/utils/constraints.ts +12 -0
- package/log/index.ts +107 -0
- package/log/logger.test.ts +194 -0
- package/log/logger.ts +308 -0
- package/log/runtime.test.ts +65 -0
- package/log/runtime.ts +40 -0
- package/log/stderr.test.ts +23 -0
- package/log/stderr.ts +24 -0
- package/metrics/export.test.ts +341 -0
- package/metrics/mod.test.ts +177 -0
- package/metrics/mod.ts +450 -0
- package/mod.ts +15 -0
- package/otel.test.ts +98 -0
- package/otel.ts +17 -0
- package/package.json +208 -5
- package/pubsub/encoding.ts +18 -0
- package/pubsub/mod.ts +26 -0
- package/pubsub/pubsub.test.ts +405 -0
- package/pubsub/refs.ts +38 -0
- package/pubsub/subscription.ts +359 -0
- package/pubsub/topic.test.ts +147 -0
- package/pubsub/topic.ts +431 -0
- 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 +393 -0
- package/services/index.ts +215 -0
- package/shutdown/mod.ts +78 -0
- package/storage/clickhouse/database.integration.test.ts +337 -0
- package/storage/clickhouse/database.test.ts +279 -0
- package/storage/clickhouse/database.ts +586 -0
- package/storage/clickhouse/errors.ts +130 -0
- package/storage/clickhouse/mod.ts +37 -0
- package/storage/objects/bucket.test.ts +573 -0
- package/storage/objects/bucket.ts +968 -0
- package/storage/objects/error.ts +121 -0
- package/storage/objects/mod.ts +50 -0
- package/storage/objects/refs.ts +117 -0
- package/storage/postgres/database.integration.test.ts +450 -0
- package/storage/postgres/database.test.ts +428 -0
- package/storage/postgres/database.ts +658 -0
- 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 +19 -0
- package/storage/redis/bitfield.ts +162 -0
- package/storage/redis/command.ts +84 -0
- package/storage/redis/commands.ts +1471 -0
- package/storage/redis/database.ts +520 -0
- package/storage/redis/mod.ts +171 -0
- package/storage/redis/native.ts +22 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +866 -0
- package/storage/redis/reply.ts +780 -0
- package/storage/redis/runtime.test.ts +248 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +134 -0
- package/storage/redis/wire.ts +345 -0
- 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 +276 -0
- package/tracing/node.ts +176 -0
- package/tracing/tracing.test.ts +953 -0
- package/types/mod.ts +92 -0
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
import {activeCtxBytes} from '../../internal/otel/index';
|
|
2
|
+
import * as runtime from '../../internal/runtime/mod';
|
|
3
|
+
import {currentScope, testMode} from '../../internal/testing/scope';
|
|
4
|
+
import {StringLiteral} from '../../internal/utils/constraints';
|
|
5
|
+
import {Decimal} from '../../types/mod';
|
|
6
|
+
import {native, nativeSync, TooManyRowsError} from './errors';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for a ClickHouse `Database`.
|
|
10
|
+
*/
|
|
11
|
+
export interface DatabaseConfig {
|
|
12
|
+
/**
|
|
13
|
+
* The directory of the database's migrations, relative to the file that
|
|
14
|
+
* declares it: `1_create_events.up.sql` and so on, with optional
|
|
15
|
+
* `.down.sql` files. Each file's statements run one at a time; a failed
|
|
16
|
+
* one leaves the database's version dirty, as ClickHouse can't roll DDL
|
|
17
|
+
* back.
|
|
18
|
+
*/
|
|
19
|
+
migrations?: string;
|
|
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>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A row of a query's result, keyed by column name.
|
|
32
|
+
*
|
|
33
|
+
* - Integers and floats are numbers, but for 64-bit integers beyond what a
|
|
34
|
+
* number holds exactly (±(2^53−1)), and `Int128`/`UInt128`/`Int256`/
|
|
35
|
+
* `UInt256`, which are `bigint`s, so they are never rounded.
|
|
36
|
+
* - `Decimal` is its exact text: `'12.30'`.
|
|
37
|
+
* - `String` is a string (invalid UTF-8 replaced), and a `FixedString`
|
|
38
|
+
* that isn't UTF-8 a `Buffer`.
|
|
39
|
+
* - `UUID`, `Enum`, `IPv4` and `IPv6` are strings.
|
|
40
|
+
* - `Date`/`Date32` is a `'YYYY-MM-DD'` string, `DateTime`/`DateTime64` a
|
|
41
|
+
* `Date` (to the millisecond), and `Time`/`Time64` a number of seconds.
|
|
42
|
+
* - `Array` and `Tuple` are arrays, a `Map` with string keys an object and
|
|
43
|
+
* any other `Map` a `Map`, and `JSON` its parsed value.
|
|
44
|
+
* - `NULL` is `null`.
|
|
45
|
+
*/
|
|
46
|
+
export type Row = Record<string, any>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A value a query parameter or an inserted column takes. Its ClickHouse type,
|
|
50
|
+
* when a tagged template names it for you, is inferred from it:
|
|
51
|
+
*
|
|
52
|
+
* - `boolean`: `Bool`
|
|
53
|
+
* - an integer `number` (to ±(2^53−1)): `Int64`; any other `number`: `Float64`
|
|
54
|
+
* - `bigint`: `Int64`, or `UInt64` above its maximum (wider ones up to 256 bits)
|
|
55
|
+
* - `string`: `String`; `Buffer`/`Uint8Array`: `String` (its bytes)
|
|
56
|
+
* - `Date`: `DateTime64(6, 'UTC')`
|
|
57
|
+
* - `Decimal`: `Decimal(76, s)`, `s` its digits after the point
|
|
58
|
+
* - an array: `Array(T)`, `T` its first non-null element's type, made
|
|
59
|
+
* `Nullable` if it holds a null
|
|
60
|
+
* - a plain object, or a `Map`: `Map(String, T)` (a `Map`'s keys as their
|
|
61
|
+
* own type)
|
|
62
|
+
* - `null`/`undefined`: `Nullable(String)`, `NULL`
|
|
63
|
+
*
|
|
64
|
+
* Wrap a value whose type should be another in {@link typed}.
|
|
65
|
+
*/
|
|
66
|
+
export type Param =
|
|
67
|
+
| string
|
|
68
|
+
| number
|
|
69
|
+
| bigint
|
|
70
|
+
| boolean
|
|
71
|
+
| Date
|
|
72
|
+
| Buffer
|
|
73
|
+
| Uint8Array
|
|
74
|
+
| Decimal
|
|
75
|
+
| Typed
|
|
76
|
+
| null
|
|
77
|
+
| undefined
|
|
78
|
+
| readonly Param[]
|
|
79
|
+
| Map<Param, Param>
|
|
80
|
+
| {[key: string]: Param};
|
|
81
|
+
|
|
82
|
+
/** A setting's value: sent as its text, a boolean as `1` or `0`. */
|
|
83
|
+
export type SettingValue = string | number | boolean;
|
|
84
|
+
|
|
85
|
+
/** Options for one call. */
|
|
86
|
+
export interface QueryOptions {
|
|
87
|
+
/**
|
|
88
|
+
* ClickHouse settings for this call only, such as `{max_execution_time: 5}`
|
|
89
|
+
* or `{async_insert: true}`. There are no sessions: each call is a request
|
|
90
|
+
* of its own, so `SET` doesn't carry over to the next one. An unknown
|
|
91
|
+
* setting is the server's error.
|
|
92
|
+
*/
|
|
93
|
+
settings?: Record<string, SettingValue>;
|
|
94
|
+
/** The query's id on the server, for `system.query_log`: one is made if not given. */
|
|
95
|
+
queryId?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** What a statement or insert did, as the server summarizes it. */
|
|
99
|
+
export interface ExecResult {
|
|
100
|
+
/** How many rows it wrote. */
|
|
101
|
+
rowsWritten: number;
|
|
102
|
+
/** How many bytes it wrote, uncompressed. */
|
|
103
|
+
bytesWritten: number;
|
|
104
|
+
/** How many rows it read. */
|
|
105
|
+
rowsRead: number;
|
|
106
|
+
/** The query's id on the server. */
|
|
107
|
+
queryId: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The marker the native module recognizes a {@link Typed} by: on the
|
|
111
|
+
* prototype, so a parameter that is an object of data is never taken for one. */
|
|
112
|
+
const TYPED_MARKER = '__encore_clickhouse_type';
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A value with the ClickHouse type it is bound as: see {@link typed}.
|
|
116
|
+
*/
|
|
117
|
+
export class Typed<T = unknown> {
|
|
118
|
+
/** @internal */
|
|
119
|
+
constructor(
|
|
120
|
+
/** The value. */
|
|
121
|
+
readonly value: T,
|
|
122
|
+
/** Its ClickHouse type, such as `'UUID'` or `'Array(UInt32)'`. */
|
|
123
|
+
readonly type: string,
|
|
124
|
+
) {}
|
|
125
|
+
}
|
|
126
|
+
Object.defineProperty(Typed.prototype, TYPED_MARKER, {value: true});
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Binds `value` as the ClickHouse type `type` rather than the one inferred
|
|
130
|
+
* from it, in a tagged template: `` ${typed(id, 'UUID')} `` becomes
|
|
131
|
+
* `{p0:UUID}`. A `Date` bound as `DateTime` is its whole seconds, and as
|
|
132
|
+
* `Date` or `Date32` its day in UTC.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* await db.queryAll`SELECT * FROM events WHERE user_id = ${typed(userId, 'UUID')}`;
|
|
136
|
+
*/
|
|
137
|
+
export function typed<T>(value: T, type: string): Typed<T> {
|
|
138
|
+
return new Typed(value, type);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The native database a handle's calls go to. */
|
|
142
|
+
type Handle = runtime.ClickHouseDatabase | (() => runtime.ClickHouseDatabase);
|
|
143
|
+
|
|
144
|
+
/** Base class with the query methods every handle has. */
|
|
145
|
+
class BaseQueryExecutor {
|
|
146
|
+
/** @internal */
|
|
147
|
+
constructor(
|
|
148
|
+
private readonly handle: Handle,
|
|
149
|
+
private readonly access: runtime.ClickHouseAccess,
|
|
150
|
+
) {}
|
|
151
|
+
|
|
152
|
+
/** The native database: in test mode the current test's instance. */
|
|
153
|
+
protected get impl(): runtime.ClickHouseDatabase {
|
|
154
|
+
return typeof this.handle === 'function' ? this.handle() : this.handle;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The connection string to connect to the database with another client,
|
|
159
|
+
* as this handle's user: `http://user:password@host:port/database`.
|
|
160
|
+
* `@clickhouse/client` reads it as its `url`.
|
|
161
|
+
*/
|
|
162
|
+
get connectionString(): string {
|
|
163
|
+
return nativeSync(() => this.impl.connectionString(this.access));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Queries the database with a template string, binding each `${value}` as
|
|
168
|
+
* a parameter of the type inferred from it (see {@link Param}):
|
|
169
|
+
* `` query`SELECT * FROM events WHERE id = ${id}` `` runs `SELECT * FROM
|
|
170
|
+
* events WHERE id = {p0:Int64}`. Wrap a value in {@link typed} to bind it
|
|
171
|
+
* as another type.
|
|
172
|
+
*
|
|
173
|
+
* It streams the rows: iterate it with `for await`.
|
|
174
|
+
*/
|
|
175
|
+
async *query<T extends Row = Record<string, any>>(
|
|
176
|
+
strings: TemplateStringsArray,
|
|
177
|
+
...params: Param[]
|
|
178
|
+
): AsyncGenerator<T> {
|
|
179
|
+
const {sql, args} = buildQuery(strings, params);
|
|
180
|
+
yield* this.rows<T>(sql, args, undefined);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Queries the database with SQL whose parameters it names, with their
|
|
185
|
+
* types, as `{name:Type}`, and `params` their values by name:
|
|
186
|
+
*
|
|
187
|
+
* ```ts
|
|
188
|
+
* db.rawQuery('SELECT * FROM events WHERE id = {id:UInt64}', {id: 5});
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* It streams the rows: iterate it with `for await`.
|
|
192
|
+
*/
|
|
193
|
+
async *rawQuery<T extends Row = Record<string, any>>(
|
|
194
|
+
sql: string,
|
|
195
|
+
params?: Record<string, Param>,
|
|
196
|
+
options?: QueryOptions,
|
|
197
|
+
): AsyncGenerator<T> {
|
|
198
|
+
yield* this.rows<T>(sql, namedParams(params), options);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** {@link query}, returning every row. */
|
|
202
|
+
async queryAll<T extends Row = Record<string, any>>(
|
|
203
|
+
strings: TemplateStringsArray,
|
|
204
|
+
...params: Param[]
|
|
205
|
+
): Promise<T[]> {
|
|
206
|
+
const {sql, args} = buildQuery(strings, params);
|
|
207
|
+
return this.all<T>(sql, args, undefined);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** {@link rawQuery}, returning every row. */
|
|
211
|
+
async rawQueryAll<T extends Row = Record<string, any>>(
|
|
212
|
+
sql: string,
|
|
213
|
+
params?: Record<string, Param>,
|
|
214
|
+
options?: QueryOptions,
|
|
215
|
+
): Promise<T[]> {
|
|
216
|
+
return this.all<T>(sql, namedParams(params), options);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* {@link query}, returning its one row: `null` if there is none, and a
|
|
221
|
+
* {@link TooManyRowsError} if there is more than one.
|
|
222
|
+
*/
|
|
223
|
+
async queryRow<T extends Row = Record<string, any>>(
|
|
224
|
+
strings: TemplateStringsArray,
|
|
225
|
+
...params: Param[]
|
|
226
|
+
): Promise<T | null> {
|
|
227
|
+
const {sql, args} = buildQuery(strings, params);
|
|
228
|
+
return this.one<T>(sql, args, undefined);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* {@link rawQuery}, returning its one row: `null` if there is none, and a
|
|
233
|
+
* {@link TooManyRowsError} if there is more than one.
|
|
234
|
+
*/
|
|
235
|
+
async rawQueryRow<T extends Row = Record<string, any>>(
|
|
236
|
+
sql: string,
|
|
237
|
+
params?: Record<string, Param>,
|
|
238
|
+
options?: QueryOptions,
|
|
239
|
+
): Promise<T | null> {
|
|
240
|
+
return this.one<T>(sql, namedParams(params), options);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Runs a statement with a template string, binding each `${value}` as
|
|
245
|
+
* {@link query} does, and discards any rows it returns.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* await db.exec`ALTER TABLE events DELETE WHERE user_id = ${id}`;
|
|
249
|
+
*/
|
|
250
|
+
async exec(
|
|
251
|
+
strings: TemplateStringsArray,
|
|
252
|
+
...params: Param[]
|
|
253
|
+
): Promise<ExecResult> {
|
|
254
|
+
const {sql, args} = buildQuery(strings, params);
|
|
255
|
+
return this.run(sql, args, undefined);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Runs a statement whose parameters it names, as {@link rawQuery} does,
|
|
260
|
+
* and discards any rows it returns.
|
|
261
|
+
*/
|
|
262
|
+
async rawExec(
|
|
263
|
+
sql: string,
|
|
264
|
+
params?: Record<string, Param>,
|
|
265
|
+
options?: QueryOptions,
|
|
266
|
+
): Promise<ExecResult> {
|
|
267
|
+
return this.run(sql, namedParams(params), options);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Inserts `rows` into `table` (`table`, or `database.table`). Each row is
|
|
272
|
+
* an object, or a `Map`, keyed by column name. The columns are those the
|
|
273
|
+
* rows have keys for; a row without one of them, or with it `undefined`,
|
|
274
|
+
* leaves it out, so it gets the column's default. `null` is `NULL`, which
|
|
275
|
+
* a column that isn't `Nullable` also takes as its default.
|
|
276
|
+
*
|
|
277
|
+
* The rows travel as JSON: binary strings that aren't UTF-8 don't survive
|
|
278
|
+
* it, so insert those with a parameterized `INSERT … VALUES`.
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* await db.insert('events', [
|
|
282
|
+
* {id: 1, name: 'signup', at: new Date()},
|
|
283
|
+
* {id: 2, name: 'login', at: new Date()},
|
|
284
|
+
* ]);
|
|
285
|
+
*/
|
|
286
|
+
async insert<T extends object = Row>(
|
|
287
|
+
table: string,
|
|
288
|
+
rows: readonly T[],
|
|
289
|
+
options?: QueryOptions,
|
|
290
|
+
): Promise<ExecResult> {
|
|
291
|
+
if (rows.length === 0) {
|
|
292
|
+
return {rowsWritten: 0, bytesWritten: 0, rowsRead: 0, queryId: ''};
|
|
293
|
+
}
|
|
294
|
+
const prepared = rows.map(row =>
|
|
295
|
+
row instanceof Map
|
|
296
|
+
? new Map([...row].map(([k, v]) => [k, prepare(v)]))
|
|
297
|
+
: Object.fromEntries(
|
|
298
|
+
Object.entries(row).map(([k, v]) => [k, prepare(v)]),
|
|
299
|
+
),
|
|
300
|
+
);
|
|
301
|
+
const native_ = nativeSync(
|
|
302
|
+
() => new runtime.ClickHouseInsertRows(prepared),
|
|
303
|
+
);
|
|
304
|
+
const summary = await native(
|
|
305
|
+
this.impl.insert(
|
|
306
|
+
this.access,
|
|
307
|
+
table,
|
|
308
|
+
native_,
|
|
309
|
+
settingsOf(options),
|
|
310
|
+
options?.queryId,
|
|
311
|
+
activeCtxBytes(),
|
|
312
|
+
),
|
|
313
|
+
);
|
|
314
|
+
return execResult(summary);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
private async cursor(
|
|
318
|
+
sql: string,
|
|
319
|
+
args: runtime.ClickHouseParams,
|
|
320
|
+
options: QueryOptions | undefined,
|
|
321
|
+
): Promise<runtime.ClickHouseCursor> {
|
|
322
|
+
return native(
|
|
323
|
+
this.impl.query(
|
|
324
|
+
this.access,
|
|
325
|
+
sql,
|
|
326
|
+
args,
|
|
327
|
+
settingsOf(options),
|
|
328
|
+
options?.queryId,
|
|
329
|
+
activeCtxBytes(),
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private async *rows<T>(
|
|
335
|
+
sql: string,
|
|
336
|
+
args: runtime.ClickHouseParams,
|
|
337
|
+
options: QueryOptions | undefined,
|
|
338
|
+
): AsyncGenerator<T> {
|
|
339
|
+
const cursor = await this.cursor(sql, args, options);
|
|
340
|
+
try {
|
|
341
|
+
while (true) {
|
|
342
|
+
const batch = await native(cursor.next());
|
|
343
|
+
if (batch === null) break;
|
|
344
|
+
for (const row of batch) yield row as T;
|
|
345
|
+
}
|
|
346
|
+
} finally {
|
|
347
|
+
await cursor.close();
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private async all<T>(
|
|
352
|
+
sql: string,
|
|
353
|
+
args: runtime.ClickHouseParams,
|
|
354
|
+
options: QueryOptions | undefined,
|
|
355
|
+
): Promise<T[]> {
|
|
356
|
+
const cursor = await this.cursor(sql, args, options);
|
|
357
|
+
try {
|
|
358
|
+
const rows: T[] = [];
|
|
359
|
+
while (true) {
|
|
360
|
+
const batch = await native(cursor.next());
|
|
361
|
+
if (batch === null) return rows;
|
|
362
|
+
for (const row of batch) rows.push(row as T);
|
|
363
|
+
}
|
|
364
|
+
} finally {
|
|
365
|
+
await cursor.close();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
private async one<T>(
|
|
370
|
+
sql: string,
|
|
371
|
+
args: runtime.ClickHouseParams,
|
|
372
|
+
options: QueryOptions | undefined,
|
|
373
|
+
): Promise<T | null> {
|
|
374
|
+
const cursor = await this.cursor(sql, args, options);
|
|
375
|
+
try {
|
|
376
|
+
let first: T | null = null;
|
|
377
|
+
while (true) {
|
|
378
|
+
// Two rows are enough to know there are too many.
|
|
379
|
+
const batch = await native(cursor.next(2));
|
|
380
|
+
if (batch === null) return first;
|
|
381
|
+
for (const row of batch) {
|
|
382
|
+
if (first !== null) throw new TooManyRowsError();
|
|
383
|
+
first = row as T;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
} finally {
|
|
387
|
+
await cursor.close();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private async run(
|
|
392
|
+
sql: string,
|
|
393
|
+
args: runtime.ClickHouseParams,
|
|
394
|
+
options: QueryOptions | undefined,
|
|
395
|
+
): Promise<ExecResult> {
|
|
396
|
+
const summary = await native(
|
|
397
|
+
this.impl.exec(
|
|
398
|
+
this.access,
|
|
399
|
+
sql,
|
|
400
|
+
args,
|
|
401
|
+
settingsOf(options),
|
|
402
|
+
options?.queryId,
|
|
403
|
+
activeCtxBytes(),
|
|
404
|
+
),
|
|
405
|
+
);
|
|
406
|
+
return execResult(summary);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* A query handle that can be passed to application/library code. Obtain one
|
|
412
|
+
* with `read()`, `write()` or `admin()`. A `Database` declaration
|
|
413
|
+
* deliberately does not implement this class.
|
|
414
|
+
*/
|
|
415
|
+
export abstract class Queryable extends BaseQueryExecutor {
|
|
416
|
+
private readonly queryableBrand = true;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* A handle to a database with one access level, which Encore's parser
|
|
421
|
+
* records where it is made:
|
|
422
|
+
*
|
|
423
|
+
* - `read()`: `SELECT` and `SHOW`. Its writes are refused, by its user's
|
|
424
|
+
* grants, or the `readonly` setting when the runtime config gives the read
|
|
425
|
+
* level no user of its own.
|
|
426
|
+
* - `write()`: reads, and `INSERT`, `ALTER … UPDATE/DELETE` and lightweight
|
|
427
|
+
* `DELETE`.
|
|
428
|
+
* - `admin()`: anything on the database: DDL, `TRUNCATE`, `OPTIMIZE`.
|
|
429
|
+
*
|
|
430
|
+
* Each level is its own ClickHouse user, so its connection string connects
|
|
431
|
+
* with that level's grants.
|
|
432
|
+
*/
|
|
433
|
+
export class DatabaseRef extends Queryable {
|
|
434
|
+
/** @internal */
|
|
435
|
+
constructor(handle: Handle, access: runtime.ClickHouseAccess) {
|
|
436
|
+
super(handle, access);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* A ClickHouse database. Constructing one makes Encore provision a database
|
|
442
|
+
* with that name, run its migrations, and return this object to represent
|
|
443
|
+
* it. Its own methods have write access; `read()`, `write()` and `admin()`
|
|
444
|
+
* select a level.
|
|
445
|
+
*
|
|
446
|
+
* If you want to reference an existing database, use `Database.named(name)`,
|
|
447
|
+
* as it is a compile error to create duplicate databases.
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* import { Database } from "@encore.dev/sdk/storage/clickhouse";
|
|
451
|
+
*
|
|
452
|
+
* const events = new Database("events", { migrations: "./migrations" });
|
|
453
|
+
*
|
|
454
|
+
* const rows = await events.queryAll`
|
|
455
|
+
* SELECT name, count() AS n FROM events WHERE at > ${since} GROUP BY name`;
|
|
456
|
+
*/
|
|
457
|
+
export class Database extends BaseQueryExecutor {
|
|
458
|
+
private readonly handles: Handle;
|
|
459
|
+
private readonly refs = new Map<runtime.ClickHouseAccess, DatabaseRef>();
|
|
460
|
+
|
|
461
|
+
constructor(name: string, _cfg?: DatabaseConfig) {
|
|
462
|
+
const handle: Handle = testMode
|
|
463
|
+
? testHandle(name)
|
|
464
|
+
: runtime.RT.clickhouseDatabase(name);
|
|
465
|
+
super(handle, runtime.ClickHouseAccess.Write);
|
|
466
|
+
this.handles = handle;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Reference an existing database by name. If the database doesn't exist
|
|
471
|
+
* yet, use `new Database(name)` instead.
|
|
472
|
+
*/
|
|
473
|
+
static named<name extends string>(name: StringLiteral<name>): Database {
|
|
474
|
+
return new Database(name);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** Select read access: `SELECT` and `SHOW`. */
|
|
478
|
+
read(): DatabaseRef {
|
|
479
|
+
return this.ref(runtime.ClickHouseAccess.Read);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** Select read and write access: also `INSERT` and mutations. */
|
|
483
|
+
write(): DatabaseRef {
|
|
484
|
+
return this.ref(runtime.ClickHouseAccess.Write);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Select read, write and admin access: anything on the database. */
|
|
488
|
+
admin(): DatabaseRef {
|
|
489
|
+
return this.ref(runtime.ClickHouseAccess.Admin);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
private ref(access: runtime.ClickHouseAccess): DatabaseRef {
|
|
493
|
+
let ref = this.refs.get(access);
|
|
494
|
+
if (ref === undefined) {
|
|
495
|
+
ref = new DatabaseRef(this.handles, access);
|
|
496
|
+
this.refs.set(access, ref);
|
|
497
|
+
}
|
|
498
|
+
return ref;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* The handle of the database `name` in test mode, for each call: the current
|
|
504
|
+
* test's instance of it, or the process's own outside any test, made on
|
|
505
|
+
* first use.
|
|
506
|
+
*/
|
|
507
|
+
function testHandle(name: string): () => runtime.ClickHouseDatabase {
|
|
508
|
+
let own: runtime.ClickHouseDatabase | undefined;
|
|
509
|
+
return () =>
|
|
510
|
+
currentScope()?.clickhouseDatabase(name) ??
|
|
511
|
+
(own ??= runtime.RT.clickhouseDatabase(name));
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* A tagged template's SQL, each `${value}` a `{pN:Type}` placeholder, and its
|
|
516
|
+
* parameters. The types are the native module's: the ones it infers from the
|
|
517
|
+
* values it converts, or those {@link typed} gave them.
|
|
518
|
+
*/
|
|
519
|
+
function buildQuery(
|
|
520
|
+
strings: TemplateStringsArray,
|
|
521
|
+
params: Param[],
|
|
522
|
+
): {sql: string; args: runtime.ClickHouseParams} {
|
|
523
|
+
const names = params.map((_, i) => `p${i}`);
|
|
524
|
+
const args = nativeSync(
|
|
525
|
+
() => new runtime.ClickHouseParams(names, params.map(prepare)),
|
|
526
|
+
);
|
|
527
|
+
const types = args.types();
|
|
528
|
+
let sql = '';
|
|
529
|
+
for (let i = 0; i < strings.length; i++) {
|
|
530
|
+
sql += strings[i];
|
|
531
|
+
if (i < params.length) sql += `{${names[i]}:${types[i]}}`;
|
|
532
|
+
}
|
|
533
|
+
return {sql, args};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function namedParams(
|
|
537
|
+
params: Record<string, Param> | undefined,
|
|
538
|
+
): runtime.ClickHouseParams {
|
|
539
|
+
const entries = Object.entries(params ?? {});
|
|
540
|
+
return nativeSync(
|
|
541
|
+
() =>
|
|
542
|
+
new runtime.ClickHouseParams(
|
|
543
|
+
entries.map(([name]) => name),
|
|
544
|
+
entries.map(([, value]) => prepare(value)),
|
|
545
|
+
),
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* `value` as the native module takes it: a `Decimal` as its text, bound as
|
|
551
|
+
* `Decimal(76, s)`, in an array too.
|
|
552
|
+
*/
|
|
553
|
+
function prepare(value: unknown): unknown {
|
|
554
|
+
if (value instanceof Decimal) {
|
|
555
|
+
const text = value.toString();
|
|
556
|
+
const dot = text.indexOf('.');
|
|
557
|
+
const scale = dot < 0 ? 0 : Math.min(text.length - dot - 1, 76);
|
|
558
|
+
return new Typed(text, `Decimal(76, ${scale})`);
|
|
559
|
+
}
|
|
560
|
+
if (Array.isArray(value) && value.some(v => v instanceof Decimal)) {
|
|
561
|
+
return value.map(prepare);
|
|
562
|
+
}
|
|
563
|
+
return value;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function settingsOf(
|
|
567
|
+
options: QueryOptions | undefined,
|
|
568
|
+
): Record<string, string> | undefined {
|
|
569
|
+
const settings = options?.settings;
|
|
570
|
+
if (settings === undefined) return undefined;
|
|
571
|
+
const out: Record<string, string> = {};
|
|
572
|
+
for (const [name, value] of Object.entries(settings)) {
|
|
573
|
+
out[name] =
|
|
574
|
+
typeof value === 'boolean' ? (value ? '1' : '0') : String(value);
|
|
575
|
+
}
|
|
576
|
+
return out;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function execResult(s: runtime.ClickHouseSummary): ExecResult {
|
|
580
|
+
return {
|
|
581
|
+
rowsWritten: s.rowsWritten,
|
|
582
|
+
bytesWritten: s.bytesWritten,
|
|
583
|
+
rowsRead: s.rowsRead,
|
|
584
|
+
queryId: s.queryId,
|
|
585
|
+
};
|
|
586
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What kind of error a ClickHouse call failed with.
|
|
3
|
+
*
|
|
4
|
+
* - `server`: the server refused or failed the query; `code` and `codeName`
|
|
5
|
+
* say why.
|
|
6
|
+
* - `unavailable`: the server can't be reached, or the database isn't
|
|
7
|
+
* configured for this process; the message says which.
|
|
8
|
+
* - `canceled`: the call was canceled.
|
|
9
|
+
* - `decode`: a response the SDK couldn't decode, such as a column of a type
|
|
10
|
+
* it doesn't read (`Dynamic`, `AggregateFunction`: cast it in the query).
|
|
11
|
+
* - `invalid_argument`: an argument the call can't use, such as a parameter
|
|
12
|
+
* value of no ClickHouse type or a bad table name.
|
|
13
|
+
* - `too_many_rows`: `queryRow` or `rawQueryRow` got more than one row
|
|
14
|
+
* ({@link TooManyRowsError}).
|
|
15
|
+
* - `no_rows`: reserved for the other SDKs' one-row forms; `queryRow`
|
|
16
|
+
* returns `null` for no rows instead.
|
|
17
|
+
*/
|
|
18
|
+
export type ErrorKind =
|
|
19
|
+
| 'server'
|
|
20
|
+
| 'unavailable'
|
|
21
|
+
| 'canceled'
|
|
22
|
+
| 'decode'
|
|
23
|
+
| 'invalid_argument'
|
|
24
|
+
| 'no_rows'
|
|
25
|
+
| 'too_many_rows';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An error of a ClickHouse call: the server's, or the SDK's own.
|
|
29
|
+
*
|
|
30
|
+
* A server error has ClickHouse's exception `code` (such as `60`) and its
|
|
31
|
+
* name, `codeName` (such as `'UNKNOWN_TABLE'`); every other kind has `code`
|
|
32
|
+
* `0` and `codeName` `''`. The name is `codeName` rather than `name`, which
|
|
33
|
+
* every `Error` has for its class: `'DatabaseError'`.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* try {
|
|
37
|
+
* await db.exec`INSERT INTO events (id) VALUES (${id})`;
|
|
38
|
+
* } catch (err) {
|
|
39
|
+
* if (err instanceof DatabaseError && err.codeName === 'UNKNOWN_TABLE') {
|
|
40
|
+
* // ...
|
|
41
|
+
* }
|
|
42
|
+
* throw err;
|
|
43
|
+
* }
|
|
44
|
+
*/
|
|
45
|
+
export class DatabaseError extends Error {
|
|
46
|
+
declare readonly name: 'DatabaseError' | 'TooManyRowsError';
|
|
47
|
+
/** What kind of error it is. */
|
|
48
|
+
readonly kind: ErrorKind;
|
|
49
|
+
/** ClickHouse's exception code, such as `60`: `0` for other kinds. */
|
|
50
|
+
readonly code: number;
|
|
51
|
+
/** The exception code's name, such as `'UNKNOWN_TABLE'`: `''` for other kinds. */
|
|
52
|
+
readonly codeName: string;
|
|
53
|
+
|
|
54
|
+
constructor(fields: {
|
|
55
|
+
message: string;
|
|
56
|
+
kind: ErrorKind;
|
|
57
|
+
code?: number;
|
|
58
|
+
codeName?: string;
|
|
59
|
+
}) {
|
|
60
|
+
super(fields.message);
|
|
61
|
+
Object.defineProperty(this, 'name', {
|
|
62
|
+
value: 'DatabaseError',
|
|
63
|
+
enumerable: false,
|
|
64
|
+
configurable: true,
|
|
65
|
+
});
|
|
66
|
+
this.kind = fields.kind;
|
|
67
|
+
this.code = fields.code ?? 0;
|
|
68
|
+
this.codeName = fields.codeName ?? '';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Thrown by `queryRow` and `rawQueryRow` for a query that returns more than
|
|
74
|
+
* one row: a {@link DatabaseError} of kind `too_many_rows`.
|
|
75
|
+
*/
|
|
76
|
+
export class TooManyRowsError extends DatabaseError {
|
|
77
|
+
declare readonly name: 'TooManyRowsError';
|
|
78
|
+
|
|
79
|
+
constructor() {
|
|
80
|
+
super({message: 'query returned more than one row', kind: 'too_many_rows'});
|
|
81
|
+
Object.defineProperty(this, 'name', {
|
|
82
|
+
value: 'TooManyRowsError',
|
|
83
|
+
enumerable: false,
|
|
84
|
+
configurable: true,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Begins the message of an error the native module fails with for a
|
|
91
|
+
* `DatabaseError`, which the rest of the message holds as JSON. The same as
|
|
92
|
+
* `DATABASE_ERROR_PREFIX` in `sdk/ts/src/clickhouse.rs`.
|
|
93
|
+
*/
|
|
94
|
+
const DATABASE_ERROR_PREFIX = 'encore:clickhouse.DatabaseError:';
|
|
95
|
+
|
|
96
|
+
/** Awaits a native call, throwing the error it rejects with as a
|
|
97
|
+
* `DatabaseError` if it is one. */
|
|
98
|
+
export async function native<T>(call: Promise<T>): Promise<T> {
|
|
99
|
+
try {
|
|
100
|
+
return await call;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
throw fromNative(err);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Runs a synchronous native call, as {@link native} does an async one. */
|
|
107
|
+
export function nativeSync<T>(call: () => T): T {
|
|
108
|
+
try {
|
|
109
|
+
return call();
|
|
110
|
+
} catch (err) {
|
|
111
|
+
throw fromNative(err);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `err`, a native call's, as a `DatabaseError` if it is one. */
|
|
116
|
+
export function fromNative(err: unknown): unknown {
|
|
117
|
+
if (
|
|
118
|
+
!(err instanceof Error) ||
|
|
119
|
+
!err.message.startsWith(DATABASE_ERROR_PREFIX)
|
|
120
|
+
) {
|
|
121
|
+
return err;
|
|
122
|
+
}
|
|
123
|
+
const f = JSON.parse(err.message.slice(DATABASE_ERROR_PREFIX.length));
|
|
124
|
+
return new DatabaseError({
|
|
125
|
+
message: f.message,
|
|
126
|
+
kind: f.kind,
|
|
127
|
+
code: f.code,
|
|
128
|
+
codeName: f.codeName,
|
|
129
|
+
});
|
|
130
|
+
}
|