@encore.dev/sdk 2.0.0-beta.14 → 2.0.0-beta.16
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 +144 -90
- package/app_meta.ts +56 -54
- package/config/config.test.ts +47 -13
- package/config/config.ts +60 -57
- package/config/secrets.ts +47 -50
- package/config/value.ts +23 -26
- package/cron/mod.test.ts +12 -0
- package/cron/mod.ts +37 -22
- package/dist/app_meta.d.ts +52 -50
- package/dist/app_meta.d.ts.map +1 -1
- package/dist/app_meta.js +2 -2
- package/dist/app_meta.js.map +1 -1
- package/dist/config/config.d.ts +58 -55
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +8 -9
- package/dist/config/config.js.map +1 -1
- package/dist/config/secrets.d.ts +45 -50
- package/dist/config/secrets.d.ts.map +1 -1
- package/dist/config/secrets.js +11 -9
- package/dist/config/secrets.js.map +1 -1
- package/dist/config/value.d.ts +20 -23
- package/dist/config/value.d.ts.map +1 -1
- package/dist/config/value.js +20 -23
- package/dist/config/value.js.map +1 -1
- package/dist/cron/mod.d.ts +34 -20
- package/dist/cron/mod.d.ts.map +1 -1
- package/dist/cron/mod.js +13 -13
- package/dist/cron/mod.js.map +1 -1
- package/dist/frontends/mod.d.ts +20 -19
- package/dist/frontends/mod.d.ts.map +1 -1
- package/dist/frontends/mod.js +20 -19
- package/dist/frontends/mod.js.map +1 -1
- package/dist/gateway/errors.d.ts +28 -30
- package/dist/gateway/errors.d.ts.map +1 -1
- package/dist/gateway/errors.js +29 -31
- package/dist/gateway/errors.js.map +1 -1
- package/dist/gateway/match.d.ts +54 -54
- package/dist/gateway/match.d.ts.map +1 -1
- package/dist/gateway/match.js +56 -56
- package/dist/gateway/match.js.map +1 -1
- package/dist/gateway/middleware.d.ts +62 -64
- package/dist/gateway/middleware.d.ts.map +1 -1
- package/dist/gateway/middleware.js +1 -1
- package/dist/gateway/middleware.js.map +1 -1
- package/dist/gateway/mod.d.ts +64 -67
- package/dist/gateway/mod.d.ts.map +1 -1
- package/dist/gateway/mod.js +72 -76
- package/dist/gateway/mod.js.map +1 -1
- package/dist/gateway/request.d.ts +138 -134
- package/dist/gateway/request.d.ts.map +1 -1
- package/dist/gateway/request.js +117 -108
- package/dist/gateway/request.js.map +1 -1
- package/dist/gateway/serve.d.ts +29 -34
- package/dist/gateway/serve.d.ts.map +1 -1
- package/dist/gateway/serve.js +51 -56
- package/dist/gateway/serve.js.map +1 -1
- package/dist/gateways/mod.d.ts +23 -21
- package/dist/gateways/mod.d.ts.map +1 -1
- package/dist/gateways/mod.js +25 -23
- package/dist/gateways/mod.js.map +1 -1
- package/dist/internal/otel/http.d.ts +4 -2
- package/dist/internal/otel/http.d.ts.map +1 -1
- package/dist/internal/otel/http.js +32 -5
- package/dist/internal/otel/http.js.map +1 -1
- package/dist/internal/runtime/napi/napi.cjs +6 -5
- package/dist/internal/runtime/napi/napi.d.cts +58 -16
- package/dist/internal/runtime/shutdown.d.ts +75 -57
- package/dist/internal/runtime/shutdown.d.ts.map +1 -1
- package/dist/internal/runtime/shutdown.js +88 -84
- package/dist/internal/runtime/shutdown.js.map +1 -1
- package/dist/log/logger.d.ts +43 -37
- package/dist/log/logger.d.ts.map +1 -1
- package/dist/log/logger.js +26 -25
- package/dist/log/logger.js.map +1 -1
- package/dist/metrics/mod.d.ts +133 -114
- package/dist/metrics/mod.d.ts.map +1 -1
- package/dist/metrics/mod.js +109 -101
- package/dist/metrics/mod.js.map +1 -1
- package/dist/otel.d.ts +6 -3
- package/dist/otel.d.ts.map +1 -1
- package/dist/otel.js +6 -3
- package/dist/otel.js.map +1 -1
- package/dist/pubsub/subscription.d.ts +88 -116
- package/dist/pubsub/subscription.d.ts.map +1 -1
- package/dist/pubsub/subscription.js +14 -17
- package/dist/pubsub/subscription.js.map +1 -1
- package/dist/pubsub/topic.d.ts +86 -113
- package/dist/pubsub/topic.d.ts.map +1 -1
- package/dist/pubsub/topic.js +68 -82
- package/dist/pubsub/topic.js.map +1 -1
- package/dist/redaction/mod.d.ts +62 -0
- package/dist/redaction/mod.d.ts.map +1 -0
- package/dist/redaction/mod.js +102 -0
- package/dist/redaction/mod.js.map +1 -0
- package/dist/reqctx/mod.d.ts +83 -86
- package/dist/reqctx/mod.d.ts.map +1 -1
- package/dist/reqctx/mod.js +39 -41
- package/dist/reqctx/mod.js.map +1 -1
- package/dist/services/index.d.ts +76 -75
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +73 -71
- package/dist/services/index.js.map +1 -1
- package/dist/shutdown/mod.d.ts +26 -25
- package/dist/shutdown/mod.d.ts.map +1 -1
- package/dist/shutdown/mod.js +26 -25
- package/dist/shutdown/mod.js.map +1 -1
- package/dist/storage/clickhouse/database.d.ts +113 -115
- package/dist/storage/clickhouse/database.d.ts.map +1 -1
- package/dist/storage/clickhouse/database.js +65 -79
- package/dist/storage/clickhouse/database.js.map +1 -1
- package/dist/storage/clickhouse/errors.d.ts +25 -26
- package/dist/storage/clickhouse/errors.d.ts.map +1 -1
- package/dist/storage/clickhouse/errors.js +15 -17
- package/dist/storage/clickhouse/errors.js.map +1 -1
- package/dist/storage/clickhouse/mod.d.ts +10 -10
- package/dist/storage/clickhouse/mod.js +10 -10
- package/dist/storage/objects/bucket.d.ts +230 -203
- package/dist/storage/objects/bucket.d.ts.map +1 -1
- package/dist/storage/objects/bucket.js +146 -106
- package/dist/storage/objects/bucket.js.map +1 -1
- package/dist/storage/objects/error.d.ts +4 -4
- package/dist/storage/objects/error.js +4 -4
- package/dist/storage/objects/mod.d.ts +1 -1
- package/dist/storage/objects/mod.d.ts.map +1 -1
- package/dist/storage/objects/mod.js.map +1 -1
- package/dist/storage/objects/refs.d.ts +2 -5
- package/dist/storage/objects/refs.d.ts.map +1 -1
- package/dist/storage/objects/refs.js.map +1 -1
- package/dist/storage/postgres/database.d.ts +124 -149
- package/dist/storage/postgres/database.d.ts.map +1 -1
- package/dist/storage/postgres/database.js +98 -135
- package/dist/storage/postgres/database.js.map +1 -1
- package/dist/storage/postgres/errors.d.ts +42 -48
- package/dist/storage/postgres/errors.d.ts.map +1 -1
- package/dist/storage/postgres/errors.js +44 -51
- package/dist/storage/postgres/errors.js.map +1 -1
- package/dist/storage/postgres/interval.d.ts +19 -19
- package/dist/storage/postgres/interval.d.ts.map +1 -1
- package/dist/storage/postgres/interval.js +21 -21
- package/dist/storage/postgres/interval.js.map +1 -1
- package/dist/storage/redis/bitfield.d.ts +48 -23
- package/dist/storage/redis/bitfield.d.ts.map +1 -1
- package/dist/storage/redis/bitfield.js +38 -19
- package/dist/storage/redis/bitfield.js.map +1 -1
- package/dist/storage/redis/command.d.ts +16 -12
- package/dist/storage/redis/command.d.ts.map +1 -1
- package/dist/storage/redis/command.js +16 -12
- package/dist/storage/redis/command.js.map +1 -1
- package/dist/storage/redis/commands.d.ts +99 -100
- package/dist/storage/redis/commands.d.ts.map +1 -1
- package/dist/storage/redis/commands.js +84 -85
- package/dist/storage/redis/commands.js.map +1 -1
- package/dist/storage/redis/database.d.ts +138 -122
- package/dist/storage/redis/database.d.ts.map +1 -1
- package/dist/storage/redis/database.js +120 -101
- package/dist/storage/redis/database.js.map +1 -1
- package/dist/storage/redis/mod.d.ts +52 -48
- package/dist/storage/redis/mod.d.ts.map +1 -1
- package/dist/storage/redis/mod.js +52 -48
- package/dist/storage/redis/mod.js.map +1 -1
- package/dist/storage/redis/options.d.ts +26 -24
- package/dist/storage/redis/options.d.ts.map +1 -1
- package/dist/storage/redis/options.js.map +1 -1
- package/dist/storage/redis/pubsub.d.ts +16 -16
- package/dist/storage/redis/pubsub.d.ts.map +1 -1
- package/dist/storage/redis/pubsub.js +14 -14
- package/dist/storage/redis/pubsub.js.map +1 -1
- package/dist/storage/redis/reply.d.ts +80 -72
- package/dist/storage/redis/reply.d.ts.map +1 -1
- package/dist/storage/redis/reply.js +53 -46
- package/dist/storage/redis/reply.js.map +1 -1
- package/dist/storage/redis/scan.d.ts +4 -4
- package/dist/storage/redis/scan.d.ts.map +1 -1
- package/dist/storage/redis/scan.js +6 -6
- package/dist/storage/redis/scan.js.map +1 -1
- package/dist/storage/redis/script.d.ts +28 -21
- package/dist/storage/redis/script.d.ts.map +1 -1
- package/dist/storage/redis/script.js +29 -22
- package/dist/storage/redis/script.js.map +1 -1
- package/dist/storage/redis/target.d.ts +17 -1
- package/dist/storage/redis/target.d.ts.map +1 -1
- package/dist/storage/redis/target.js +30 -1
- package/dist/storage/redis/target.js.map +1 -1
- package/dist/storage/redis/wire.d.ts +6 -6
- package/dist/storage/redis/wire.d.ts.map +1 -1
- package/dist/storage/redis/wire.js +6 -6
- package/dist/storage/redis/wire.js.map +1 -1
- package/dist/testing/mod.d.ts +83 -88
- package/dist/testing/mod.d.ts.map +1 -1
- package/dist/testing/mod.js +75 -81
- package/dist/testing/mod.js.map +1 -1
- package/dist/testing/playwright.d.ts +28 -32
- package/dist/testing/playwright.d.ts.map +1 -1
- package/dist/testing/playwright.js +22 -27
- package/dist/testing/playwright.js.map +1 -1
- package/dist/testing/puppeteer.d.ts +11 -12
- package/dist/testing/puppeteer.d.ts.map +1 -1
- package/dist/testing/puppeteer.js +17 -18
- package/dist/testing/puppeteer.js.map +1 -1
- package/dist/testing/system.d.ts +72 -90
- package/dist/testing/system.d.ts.map +1 -1
- package/dist/testing/system.js +90 -107
- package/dist/testing/system.js.map +1 -1
- package/dist/tracing/http.d.ts +2 -1
- package/dist/tracing/http.d.ts.map +1 -1
- package/dist/tracing/http.js +2 -1
- package/dist/tracing/http.js.map +1 -1
- package/dist/tracing/node.d.ts.map +1 -1
- package/dist/tracing/node.js +1 -1
- package/dist/tracing/node.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/frontends/mod.test.ts +6 -2
- package/frontends/mod.ts +20 -19
- package/gateway/errors.ts +30 -32
- package/gateway/match.ts +61 -57
- package/gateway/middleware.ts +62 -64
- package/gateway/mod.ts +73 -76
- package/gateway/request.ts +143 -140
- package/gateway/serve.ts +58 -63
- package/gateways/mod.ts +25 -23
- package/internal/otel/http.test.ts +50 -0
- package/internal/otel/http.ts +40 -5
- package/internal/runtime/napi/napi.cjs +6 -5
- package/internal/runtime/napi/napi.d.cts +58 -16
- package/internal/runtime/shutdown.ts +113 -101
- package/log/logger.test.ts +8 -0
- package/log/logger.ts +48 -45
- package/metrics/mod.ts +138 -122
- package/otel.ts +6 -3
- package/package.json +6 -1
- package/pubsub/subscription.ts +93 -123
- package/pubsub/topic.ts +96 -130
- package/redaction/bridge.test.ts +99 -0
- package/redaction/mod.test.ts +188 -0
- package/redaction/mod.ts +226 -0
- package/reqctx/mod.ts +83 -86
- package/services/index.ts +78 -78
- package/shutdown/mod.ts +26 -25
- package/storage/clickhouse/database.ts +115 -129
- package/storage/clickhouse/errors.ts +27 -29
- package/storage/clickhouse/mod.ts +10 -10
- package/storage/objects/bucket.test.ts +168 -16
- package/storage/objects/bucket.ts +336 -247
- package/storage/objects/error.ts +4 -4
- package/storage/objects/mod.ts +1 -0
- package/storage/objects/refs.ts +2 -5
- package/storage/postgres/database.ts +131 -164
- package/storage/postgres/errors.ts +45 -52
- package/storage/postgres/interval.ts +21 -21
- package/storage/redis/bitfield.ts +51 -26
- package/storage/redis/command.ts +16 -12
- package/storage/redis/commands.ts +99 -100
- package/storage/redis/database.ts +145 -123
- package/storage/redis/mod.ts +52 -48
- package/storage/redis/options.ts +26 -24
- package/storage/redis/pubsub.ts +14 -14
- package/storage/redis/redis.test.ts +17 -2
- package/storage/redis/reply.ts +85 -77
- package/storage/redis/scan.ts +6 -6
- package/storage/redis/script.ts +29 -22
- package/storage/redis/target.ts +34 -1
- package/storage/redis/wire.ts +8 -8
- package/testing/mod.ts +99 -105
- package/testing/playwright.ts +32 -37
- package/testing/puppeteer.ts +22 -23
- package/testing/system.ts +117 -133
- package/tracing/http.ts +2 -1
- package/tracing/native.test.ts +158 -8
- package/tracing/node.ts +1 -0
|
@@ -6,62 +6,62 @@ import {Decimal} from '../../types/mod';
|
|
|
6
6
|
import {native, nativeSync, TooManyRowsError} from './errors';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Configuration for a ClickHouse
|
|
9
|
+
* Configuration for a ClickHouse {@link Database}.
|
|
10
10
|
*/
|
|
11
11
|
export interface DatabaseConfig {
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* back.
|
|
13
|
+
* Path to the migrations directory relative to the declaring file (containing
|
|
14
|
+
* files such as `1_create_events.up.sql` and optional `.down.sql` files).
|
|
15
|
+
* Statements in each file run sequentially; because ClickHouse cannot roll
|
|
16
|
+
* back DDL, a failed migration leaves the schema version dirty.
|
|
18
17
|
*/
|
|
19
18
|
migrations?: string;
|
|
20
19
|
|
|
21
20
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
21
|
+
* Static string-literal tags that classify the database for policy checks
|
|
22
|
+
* (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
|
|
23
|
+
* digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
|
|
24
|
+
* and cannot start with the reserved prefix `encore`. At most 32 tags.
|
|
26
25
|
*/
|
|
27
26
|
tags?: Record<string, string>;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
29
|
/**
|
|
31
|
-
* A
|
|
30
|
+
* A single query result row, keyed by column name.
|
|
32
31
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* - `
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* - `
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
32
|
+
* Column types map to JavaScript values as follows:
|
|
33
|
+
* - Integers and floats decode to `number`, except 64-bit integers outside the
|
|
34
|
+
* safe integer range (±(2^53−1)) and `Int128`/`UInt128`/`Int256`/`UInt256`,
|
|
35
|
+
* which decode to `bigint` to avoid precision loss.
|
|
36
|
+
* - `Decimal` decodes to its exact string representation (e.g. `'12.30'`).
|
|
37
|
+
* - `String` decodes to `string` (invalid UTF-8 replaced); non-UTF-8
|
|
38
|
+
* `FixedString` decodes to `Buffer`.
|
|
39
|
+
* - `UUID`, `Enum`, `IPv4`, and `IPv6` decode to `string`.
|
|
40
|
+
* - `Date`/`Date32` decode to `'YYYY-MM-DD'`, `DateTime`/`DateTime64` to
|
|
41
|
+
* `Date` (millisecond precision), and `Time`/`Time64` to seconds (`number`).
|
|
42
|
+
* - `Array` and `Tuple` decode to arrays; `Map` with string keys decodes to a
|
|
43
|
+
* plain object (other key types decode to `Map`); `JSON` decodes to its
|
|
44
|
+
* parsed value.
|
|
45
|
+
* - `NULL` decodes to `null`.
|
|
45
46
|
*/
|
|
46
47
|
export type Row = Record<string, any>;
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
|
-
* A value a query parameter or
|
|
50
|
-
*
|
|
50
|
+
* A value accepted as a query parameter or inserted column. In tagged-template
|
|
51
|
+
* queries, the ClickHouse parameter type is inferred automatically:
|
|
51
52
|
*
|
|
52
|
-
* - `boolean
|
|
53
|
-
* -
|
|
54
|
-
* - `bigint
|
|
55
|
-
* - `string
|
|
56
|
-
* - `Date
|
|
57
|
-
* - `Decimal
|
|
58
|
-
* -
|
|
59
|
-
*
|
|
60
|
-
* -
|
|
61
|
-
*
|
|
62
|
-
* - `null`/`undefined`: `Nullable(String)`, `NULL`
|
|
53
|
+
* - `boolean` → `Bool`
|
|
54
|
+
* - safe integer `number` → `Int64`; other `number` → `Float64`
|
|
55
|
+
* - `bigint` → `Int64`, or `UInt64` (and wider types up to 256 bits) as needed
|
|
56
|
+
* - `string`, `Buffer`, `Uint8Array` → `String`
|
|
57
|
+
* - `Date` → `DateTime64(6, 'UTC')`
|
|
58
|
+
* - `Decimal` → `Decimal(76, s)` where `s` is the decimal scale
|
|
59
|
+
* - array → `Array(T)` inferred from the first non-null element (`Nullable(T)`
|
|
60
|
+
* if the array contains `null`)
|
|
61
|
+
* - plain object or `Map` → `Map(K, V)`
|
|
62
|
+
* - `null`/`undefined` → `Nullable(String)` (`NULL`)
|
|
63
63
|
*
|
|
64
|
-
* Wrap a value
|
|
64
|
+
* Wrap a value with {@link typed} to specify an explicit ClickHouse type.
|
|
65
65
|
*/
|
|
66
66
|
export type Param =
|
|
67
67
|
| string
|
|
@@ -79,57 +79,57 @@ export type Param =
|
|
|
79
79
|
| Map<Param, Param>
|
|
80
80
|
| {[key: string]: Param};
|
|
81
81
|
|
|
82
|
-
/**
|
|
82
|
+
/** ClickHouse query setting value (booleans are sent as `1` or `0`). */
|
|
83
83
|
export type SettingValue = string | number | boolean;
|
|
84
84
|
|
|
85
|
-
/**
|
|
85
|
+
/** Per-call options for ClickHouse queries and statements. */
|
|
86
86
|
export interface QueryOptions {
|
|
87
87
|
/**
|
|
88
|
-
* ClickHouse settings
|
|
89
|
-
* or `{async_insert: true}
|
|
90
|
-
*
|
|
91
|
-
*
|
|
88
|
+
* ClickHouse settings applied only to this call (e.g.
|
|
89
|
+
* `{max_execution_time: 5}` or `{async_insert: true}`). Because calls are
|
|
90
|
+
* stateless, `SET` does not carry over between calls. Unknown settings cause
|
|
91
|
+
* a server error.
|
|
92
92
|
*/
|
|
93
93
|
settings?: Record<string, SettingValue>;
|
|
94
|
-
/**
|
|
94
|
+
/** Custom server query ID for `system.query_log` (auto-generated if omitted). */
|
|
95
95
|
queryId?: string;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
/**
|
|
98
|
+
/** Execution statistics returned by ClickHouse for a statement or insert. */
|
|
99
99
|
export interface ExecResult {
|
|
100
|
-
/**
|
|
100
|
+
/** Number of rows written. */
|
|
101
101
|
rowsWritten: number;
|
|
102
|
-
/**
|
|
102
|
+
/** Number of uncompressed bytes written. */
|
|
103
103
|
bytesWritten: number;
|
|
104
|
-
/**
|
|
104
|
+
/** Number of rows read. */
|
|
105
105
|
rowsRead: number;
|
|
106
|
-
/**
|
|
106
|
+
/** Server-assigned or caller-provided query ID. */
|
|
107
107
|
queryId: string;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
/**
|
|
111
|
-
* prototype, so a parameter that is an object of data is never taken for one. */
|
|
110
|
+
/** Prototype marker used by the native module to identify {@link Typed} wrappers. */
|
|
112
111
|
const TYPED_MARKER = '__encore_clickhouse_type';
|
|
113
112
|
|
|
114
113
|
/**
|
|
115
|
-
*
|
|
114
|
+
* Wraps a query parameter value with an explicit ClickHouse type (see
|
|
115
|
+
* {@link typed}).
|
|
116
116
|
*/
|
|
117
117
|
export class Typed<T = unknown> {
|
|
118
118
|
/** @internal */
|
|
119
119
|
constructor(
|
|
120
|
-
/** The value. */
|
|
120
|
+
/** The parameter value. */
|
|
121
121
|
readonly value: T,
|
|
122
|
-
/**
|
|
122
|
+
/** Explicit ClickHouse type name (e.g. `'UUID'` or `'Array(UInt32)'`). */
|
|
123
123
|
readonly type: string,
|
|
124
124
|
) {}
|
|
125
125
|
}
|
|
126
126
|
Object.defineProperty(Typed.prototype, TYPED_MARKER, {value: true});
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
|
-
* Binds `value` as the ClickHouse type `type`
|
|
130
|
-
*
|
|
131
|
-
* `{p0:UUID}
|
|
132
|
-
* `Date` or `Date32
|
|
129
|
+
* Binds `value` as the explicit ClickHouse type `type` instead of inferring
|
|
130
|
+
* one in tagged-template queries (e.g. `` ${typed(id, 'UUID')} `` emits
|
|
131
|
+
* `{p0:UUID}`). A `Date` bound as `DateTime` truncates to whole seconds; bound
|
|
132
|
+
* as `Date` or `Date32`, it uses the UTC calendar date.
|
|
133
133
|
*
|
|
134
134
|
* @example
|
|
135
135
|
* await db.queryAll`SELECT * FROM events WHERE user_id = ${typed(userId, 'UUID')}`;
|
|
@@ -138,10 +138,10 @@ export function typed<T>(value: T, type: string): Typed<T> {
|
|
|
138
138
|
return new Typed(value, type);
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
/**
|
|
141
|
+
/** Native ClickHouse handle or lazy test-scoped resolver. */
|
|
142
142
|
type Handle = runtime.ClickHouseDatabase | (() => runtime.ClickHouseDatabase);
|
|
143
143
|
|
|
144
|
-
/** Base class
|
|
144
|
+
/** Base class providing shared ClickHouse query and execution methods. */
|
|
145
145
|
class BaseQueryExecutor {
|
|
146
146
|
/** @internal */
|
|
147
147
|
constructor(
|
|
@@ -149,28 +149,26 @@ class BaseQueryExecutor {
|
|
|
149
149
|
private readonly access: runtime.ClickHouseAccess,
|
|
150
150
|
) {}
|
|
151
151
|
|
|
152
|
-
/**
|
|
152
|
+
/** Native database handle (resolves to the current test's instance in test mode). */
|
|
153
153
|
protected get impl(): runtime.ClickHouseDatabase {
|
|
154
154
|
return typeof this.handle === 'function' ? this.handle() : this.handle;
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
/**
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
158
|
+
* HTTP connection string (`http://user:password@host:port/database`) scoped
|
|
159
|
+
* to this handle's access level, compatible with `@clickhouse/client`'s `url`
|
|
160
|
+
* option.
|
|
161
161
|
*/
|
|
162
162
|
get connectionString(): string {
|
|
163
163
|
return nativeSync(() => this.impl.connectionString(this.access));
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
* `` query`SELECT * FROM events WHERE id = ${id}` ``
|
|
170
|
-
* events WHERE id = {p0:Int64}`.
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* It streams the rows: iterate it with `for await`.
|
|
167
|
+
* Streams rows from a tagged-template query, binding each `${value}` as a
|
|
168
|
+
* typed server parameter (see {@link Param}). For example,
|
|
169
|
+
* `` query`SELECT * FROM events WHERE id = ${id}` `` sends
|
|
170
|
+
* `SELECT * FROM events WHERE id = {p0:Int64}`. Use {@link typed} to
|
|
171
|
+
* override the inferred type. Iterate results with `for await`.
|
|
174
172
|
*/
|
|
175
173
|
async *query<T extends Row = Record<string, any>>(
|
|
176
174
|
strings: TemplateStringsArray,
|
|
@@ -181,14 +179,13 @@ class BaseQueryExecutor {
|
|
|
181
179
|
}
|
|
182
180
|
|
|
183
181
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
182
|
+
* Streams rows from a raw SQL query with named typed placeholders (such as
|
|
183
|
+
* `{id:UInt64}`) and matching entries in `params`. Iterate results with
|
|
184
|
+
* `for await`.
|
|
186
185
|
*
|
|
187
186
|
* ```ts
|
|
188
187
|
* db.rawQuery('SELECT * FROM events WHERE id = {id:UInt64}', {id: 5});
|
|
189
188
|
* ```
|
|
190
|
-
*
|
|
191
|
-
* It streams the rows: iterate it with `for await`.
|
|
192
189
|
*/
|
|
193
190
|
async *rawQuery<T extends Row = Record<string, any>>(
|
|
194
191
|
sql: string,
|
|
@@ -198,7 +195,7 @@ class BaseQueryExecutor {
|
|
|
198
195
|
yield* this.rows<T>(sql, namedParams(params), options);
|
|
199
196
|
}
|
|
200
197
|
|
|
201
|
-
/** {@link query}
|
|
198
|
+
/** Runs a tagged-template query (like {@link query}) and collects all rows into an array. */
|
|
202
199
|
async queryAll<T extends Row = Record<string, any>>(
|
|
203
200
|
strings: TemplateStringsArray,
|
|
204
201
|
...params: Param[]
|
|
@@ -207,7 +204,7 @@ class BaseQueryExecutor {
|
|
|
207
204
|
return this.all<T>(sql, args, undefined);
|
|
208
205
|
}
|
|
209
206
|
|
|
210
|
-
/** {@link rawQuery}
|
|
207
|
+
/** Runs a raw query (like {@link rawQuery}) and collects all rows into an array. */
|
|
211
208
|
async rawQueryAll<T extends Row = Record<string, any>>(
|
|
212
209
|
sql: string,
|
|
213
210
|
params?: Record<string, Param>,
|
|
@@ -217,8 +214,9 @@ class BaseQueryExecutor {
|
|
|
217
214
|
}
|
|
218
215
|
|
|
219
216
|
/**
|
|
220
|
-
* {@link query}
|
|
221
|
-
*
|
|
217
|
+
* Runs a tagged-template query (like {@link query}) expecting at most one
|
|
218
|
+
* row. Returns `null` if no rows match, or throws {@link TooManyRowsError}
|
|
219
|
+
* if more than one row is returned.
|
|
222
220
|
*/
|
|
223
221
|
async queryRow<T extends Row = Record<string, any>>(
|
|
224
222
|
strings: TemplateStringsArray,
|
|
@@ -229,8 +227,9 @@ class BaseQueryExecutor {
|
|
|
229
227
|
}
|
|
230
228
|
|
|
231
229
|
/**
|
|
232
|
-
* {@link rawQuery}
|
|
233
|
-
* {@link TooManyRowsError} if
|
|
230
|
+
* Runs a raw query (like {@link rawQuery}) expecting at most one row.
|
|
231
|
+
* Returns `null` if no rows match, or throws {@link TooManyRowsError} if
|
|
232
|
+
* more than one row is returned.
|
|
234
233
|
*/
|
|
235
234
|
async rawQueryRow<T extends Row = Record<string, any>>(
|
|
236
235
|
sql: string,
|
|
@@ -241,8 +240,8 @@ class BaseQueryExecutor {
|
|
|
241
240
|
}
|
|
242
241
|
|
|
243
242
|
/**
|
|
244
|
-
*
|
|
245
|
-
* {@link query}
|
|
243
|
+
* Executes a tagged-template statement (binding `${value}` placeholders like
|
|
244
|
+
* {@link query}) and discards any returned rows.
|
|
246
245
|
*
|
|
247
246
|
* @example
|
|
248
247
|
* await db.exec`ALTER TABLE events DELETE WHERE user_id = ${id}`;
|
|
@@ -256,8 +255,8 @@ class BaseQueryExecutor {
|
|
|
256
255
|
}
|
|
257
256
|
|
|
258
257
|
/**
|
|
259
|
-
*
|
|
260
|
-
* and discards any rows
|
|
258
|
+
* Executes a raw statement with named typed parameters (like
|
|
259
|
+
* {@link rawQuery}) and discards any returned rows.
|
|
261
260
|
*/
|
|
262
261
|
async rawExec(
|
|
263
262
|
sql: string,
|
|
@@ -268,14 +267,13 @@ class BaseQueryExecutor {
|
|
|
268
267
|
}
|
|
269
268
|
|
|
270
269
|
/**
|
|
271
|
-
*
|
|
272
|
-
* an object
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
* a column that isn't `Nullable` also takes as its default.
|
|
270
|
+
* Bulk-inserts `rows` into `table` (`table` or `database.table`). Each row
|
|
271
|
+
* is an object or `Map` keyed by column name. Omitted or `undefined` fields
|
|
272
|
+
* use the column's default; `null` inserts `NULL` (or the column default for
|
|
273
|
+
* non-`Nullable` columns).
|
|
276
274
|
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
275
|
+
* Rows are serialized as JSON, so non-UTF-8 binary strings are not preserved
|
|
276
|
+
* — use a parameterized `INSERT ... VALUES` statement for binary data.
|
|
279
277
|
*
|
|
280
278
|
* @example
|
|
281
279
|
* await db.insert('events', [
|
|
@@ -408,27 +406,26 @@ class BaseQueryExecutor {
|
|
|
408
406
|
}
|
|
409
407
|
|
|
410
408
|
/**
|
|
411
|
-
* A query handle
|
|
412
|
-
* with `read()`, `write()
|
|
413
|
-
*
|
|
409
|
+
* A scoped query handle safe to pass into application or library code. Obtain
|
|
410
|
+
* one with `read()`, `write()`, or `admin()`. {@link Database} deliberately
|
|
411
|
+
* does not extend `Queryable`.
|
|
414
412
|
*/
|
|
415
413
|
export abstract class Queryable extends BaseQueryExecutor {
|
|
416
414
|
private readonly queryableBrand = true;
|
|
417
415
|
}
|
|
418
416
|
|
|
419
417
|
/**
|
|
420
|
-
* A handle to a
|
|
421
|
-
*
|
|
418
|
+
* A ClickHouse database handle scoped to a specific access level recorded by
|
|
419
|
+
* Encore's parser where it is created:
|
|
422
420
|
*
|
|
423
|
-
* - `read()`: `SELECT` and `SHOW
|
|
424
|
-
* grants
|
|
425
|
-
*
|
|
426
|
-
* - `write()`: reads, and `INSERT`, `ALTER … UPDATE/DELETE` and lightweight
|
|
421
|
+
* - `read()`: `SELECT` and `SHOW` only (writes are rejected by the user's
|
|
422
|
+
* grants or the `readonly` setting).
|
|
423
|
+
* - `write()`: reads plus `INSERT`, `ALTER ... UPDATE/DELETE`, and lightweight
|
|
427
424
|
* `DELETE`.
|
|
428
|
-
* - `admin()`:
|
|
425
|
+
* - `admin()`: full access including DDL, `TRUNCATE`, and `OPTIMIZE`.
|
|
429
426
|
*
|
|
430
|
-
* Each level
|
|
431
|
-
* with
|
|
427
|
+
* Each access level uses its own ClickHouse user, so `connectionString`
|
|
428
|
+
* connects with matching grants.
|
|
432
429
|
*/
|
|
433
430
|
export class DatabaseRef extends Queryable {
|
|
434
431
|
/** @internal */
|
|
@@ -438,13 +435,13 @@ export class DatabaseRef extends Queryable {
|
|
|
438
435
|
}
|
|
439
436
|
|
|
440
437
|
/**
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
438
|
+
* Declares a ClickHouse database. Encore provisions the database, runs its
|
|
439
|
+
* migrations, and returns this handle. Methods called directly on `Database`
|
|
440
|
+
* run with write access; call `read()`, `write()`, or `admin()` to obtain a
|
|
441
|
+
* handle with a specific access level.
|
|
445
442
|
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
443
|
+
* To reference an existing database without declaring a duplicate, use
|
|
444
|
+
* {@link Database.named}.
|
|
448
445
|
*
|
|
449
446
|
* @example
|
|
450
447
|
* import { Database } from "@encore.dev/sdk/storage/clickhouse";
|
|
@@ -467,24 +464,24 @@ export class Database extends BaseQueryExecutor {
|
|
|
467
464
|
}
|
|
468
465
|
|
|
469
466
|
/**
|
|
470
|
-
*
|
|
471
|
-
*
|
|
467
|
+
* References an existing ClickHouse database by name. Use
|
|
468
|
+
* `new Database(name)` to declare a new database.
|
|
472
469
|
*/
|
|
473
470
|
static named<name extends string>(name: StringLiteral<name>): Database {
|
|
474
471
|
return new Database(name);
|
|
475
472
|
}
|
|
476
473
|
|
|
477
|
-
/**
|
|
474
|
+
/** Returns a read-only handle (`SELECT` and `SHOW`). */
|
|
478
475
|
read(): DatabaseRef {
|
|
479
476
|
return this.ref(runtime.ClickHouseAccess.Read);
|
|
480
477
|
}
|
|
481
478
|
|
|
482
|
-
/**
|
|
479
|
+
/** Returns a read-write handle (`SELECT`, `INSERT`, and mutations). */
|
|
483
480
|
write(): DatabaseRef {
|
|
484
481
|
return this.ref(runtime.ClickHouseAccess.Write);
|
|
485
482
|
}
|
|
486
483
|
|
|
487
|
-
/**
|
|
484
|
+
/** Returns an admin handle with full access (including DDL). */
|
|
488
485
|
admin(): DatabaseRef {
|
|
489
486
|
return this.ref(runtime.ClickHouseAccess.Admin);
|
|
490
487
|
}
|
|
@@ -499,11 +496,7 @@ export class Database extends BaseQueryExecutor {
|
|
|
499
496
|
}
|
|
500
497
|
}
|
|
501
498
|
|
|
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
|
-
*/
|
|
499
|
+
/** Resolves the ClickHouse handle for `name` in test mode (per-test or process fallback). */
|
|
507
500
|
function testHandle(name: string): () => runtime.ClickHouseDatabase {
|
|
508
501
|
let own: runtime.ClickHouseDatabase | undefined;
|
|
509
502
|
return () =>
|
|
@@ -511,11 +504,7 @@ function testHandle(name: string): () => runtime.ClickHouseDatabase {
|
|
|
511
504
|
(own ??= runtime.RT.clickhouseDatabase(name));
|
|
512
505
|
}
|
|
513
506
|
|
|
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
|
-
*/
|
|
507
|
+
/** Compiles a tagged template into SQL with `{pN:Type}` placeholders and bound native params. */
|
|
519
508
|
function buildQuery(
|
|
520
509
|
strings: TemplateStringsArray,
|
|
521
510
|
params: Param[],
|
|
@@ -546,10 +535,7 @@ function namedParams(
|
|
|
546
535
|
);
|
|
547
536
|
}
|
|
548
537
|
|
|
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
|
-
*/
|
|
538
|
+
/** Converts `Decimal` values (including inside arrays) into explicit `Typed` values for the native layer. */
|
|
553
539
|
function prepare(value: unknown): unknown {
|
|
554
540
|
if (value instanceof Decimal) {
|
|
555
541
|
const text = value.toString();
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Category of failure for a {@link DatabaseError}.
|
|
3
3
|
*
|
|
4
|
-
* - `server`:
|
|
5
|
-
*
|
|
6
|
-
* - `unavailable`: the server
|
|
7
|
-
* configured for this process
|
|
4
|
+
* - `server`: ClickHouse rejected or failed the query (`code` and `codeName`
|
|
5
|
+
* contain the ClickHouse exception details).
|
|
6
|
+
* - `unavailable`: the server could not be reached, or the database is not
|
|
7
|
+
* configured for this process.
|
|
8
8
|
* - `canceled`: the call was canceled.
|
|
9
|
-
* - `decode`:
|
|
10
|
-
*
|
|
11
|
-
* - `invalid_argument`:
|
|
12
|
-
*
|
|
13
|
-
* - `too_many_rows`: `queryRow` or `rawQueryRow`
|
|
9
|
+
* - `decode`: the SDK could not decode a response column (for example,
|
|
10
|
+
* unsupported types like `Dynamic` or `AggregateFunction` — cast them in SQL).
|
|
11
|
+
* - `invalid_argument`: invalid call argument, such as an unsupported parameter
|
|
12
|
+
* type or invalid table name.
|
|
13
|
+
* - `too_many_rows`: `queryRow` or `rawQueryRow` returned more than one row
|
|
14
14
|
* ({@link TooManyRowsError}).
|
|
15
|
-
* - `no_rows`: reserved for
|
|
16
|
-
*
|
|
15
|
+
* - `no_rows`: reserved for parity with other Encore SDKs (`queryRow` returns
|
|
16
|
+
* `null` when no rows match).
|
|
17
17
|
*/
|
|
18
18
|
export type ErrorKind =
|
|
19
19
|
| 'server'
|
|
@@ -25,12 +25,12 @@ export type ErrorKind =
|
|
|
25
25
|
| 'too_many_rows';
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* Error returned when a ClickHouse operation fails (either on the server or in
|
|
29
|
+
* the SDK).
|
|
29
30
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* `0` and `codeName` `''`.
|
|
33
|
-
* every `Error` has for its class: `'DatabaseError'`.
|
|
31
|
+
* For `kind === 'server'`, `code` (e.g. `60`) and `codeName` (e.g.
|
|
32
|
+
* `'UNKNOWN_TABLE'`) hold the ClickHouse exception code and name; for all other
|
|
33
|
+
* kinds, `code` is `0` and `codeName` is `''`.
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
36
|
* try {
|
|
@@ -44,11 +44,11 @@ export type ErrorKind =
|
|
|
44
44
|
*/
|
|
45
45
|
export class DatabaseError extends Error {
|
|
46
46
|
declare readonly name: 'DatabaseError' | 'TooManyRowsError';
|
|
47
|
-
/**
|
|
47
|
+
/** Category of error. */
|
|
48
48
|
readonly kind: ErrorKind;
|
|
49
|
-
/** ClickHouse
|
|
49
|
+
/** ClickHouse exception code (e.g. `60`), or `0` for non-server errors. */
|
|
50
50
|
readonly code: number;
|
|
51
|
-
/**
|
|
51
|
+
/** ClickHouse exception name (e.g. `'UNKNOWN_TABLE'`), or `''` for non-server errors. */
|
|
52
52
|
readonly codeName: string;
|
|
53
53
|
|
|
54
54
|
constructor(fields: {
|
|
@@ -70,8 +70,8 @@ export class DatabaseError extends Error {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
* Thrown by `queryRow` and `rawQueryRow`
|
|
74
|
-
*
|
|
73
|
+
* Thrown by `queryRow` and `rawQueryRow` when a query returns more than one
|
|
74
|
+
* row. Subclass of {@link DatabaseError} with `kind === 'too_many_rows'`.
|
|
75
75
|
*/
|
|
76
76
|
export class TooManyRowsError extends DatabaseError {
|
|
77
77
|
declare readonly name: 'TooManyRowsError';
|
|
@@ -87,14 +87,12 @@ export class TooManyRowsError extends DatabaseError {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* `DATABASE_ERROR_PREFIX` in `sdk/ts/src/clickhouse.rs`.
|
|
90
|
+
* Prefix for JSON-encoded `DatabaseError` messages from the native module.
|
|
91
|
+
* Must match `DATABASE_ERROR_PREFIX` in `sdk/ts/src/clickhouse.rs`.
|
|
93
92
|
*/
|
|
94
93
|
const DATABASE_ERROR_PREFIX = 'encore:clickhouse.DatabaseError:';
|
|
95
94
|
|
|
96
|
-
/** Awaits a native call
|
|
97
|
-
* `DatabaseError` if it is one. */
|
|
95
|
+
/** Awaits a native call and converts encoded native errors into {@link DatabaseError}. */
|
|
98
96
|
export async function native<T>(call: Promise<T>): Promise<T> {
|
|
99
97
|
try {
|
|
100
98
|
return await call;
|
|
@@ -103,7 +101,7 @@ export async function native<T>(call: Promise<T>): Promise<T> {
|
|
|
103
101
|
}
|
|
104
102
|
}
|
|
105
103
|
|
|
106
|
-
/** Runs a synchronous native call
|
|
104
|
+
/** Runs a synchronous native call and converts encoded native errors into {@link DatabaseError}. */
|
|
107
105
|
export function nativeSync<T>(call: () => T): T {
|
|
108
106
|
try {
|
|
109
107
|
return call();
|
|
@@ -112,7 +110,7 @@ export function nativeSync<T>(call: () => T): T {
|
|
|
112
110
|
}
|
|
113
111
|
}
|
|
114
112
|
|
|
115
|
-
/**
|
|
113
|
+
/** Converts an encoded native error into a {@link DatabaseError}, passing other errors through. */
|
|
116
114
|
export function fromNative(err: unknown): unknown {
|
|
117
115
|
if (
|
|
118
116
|
!(err instanceof Error) ||
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ClickHouse databases.
|
|
3
|
-
* directory
|
|
4
|
-
*
|
|
2
|
+
* ClickHouse databases. Declare a database at module scope with its migrations
|
|
3
|
+
* directory; Encore provisions it, runs migrations, and grants each service
|
|
4
|
+
* the access level its code uses.
|
|
5
5
|
*
|
|
6
6
|
* ```ts
|
|
7
7
|
* import { Database } from "@encore.dev/sdk/storage/clickhouse";
|
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
* SELECT name, count() AS n FROM events WHERE at > ${since} GROUP BY name`;
|
|
14
14
|
* ```
|
|
15
15
|
*
|
|
16
|
-
* Parameters are bound on the server
|
|
17
|
-
* template
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* Parameters are always bound on the server rather than interpolated into SQL:
|
|
17
|
+
* tagged-template `${value}` placeholders become `{p0:Type}` using the inferred
|
|
18
|
+
* ClickHouse type (override with {@link typed}), while the `raw*` methods take
|
|
19
|
+
* SQL with named typed placeholders (e.g. `{id:UInt64}`) and a params object.
|
|
20
20
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
21
|
+
* ClickHouse calls are stateless — there are no transactions or sessions, so
|
|
22
|
+
* temporary tables and `SET` do not persist across calls. Pass per-query
|
|
23
|
+
* settings via {@link QueryOptions.settings} instead.
|
|
24
24
|
*
|
|
25
25
|
* @module
|
|
26
26
|
*/
|