@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,42 +6,45 @@ import {native, TooManyRowsError} from './errors';
|
|
|
6
6
|
import type {Interval} from './interval';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Configures how database migrations are managed for a
|
|
9
|
+
* Configures how database migrations are managed for a {@link Database}.
|
|
10
10
|
*/
|
|
11
11
|
export interface MigrationsConfig {
|
|
12
12
|
path: string;
|
|
13
13
|
source?: 'prisma' | 'drizzle' | 'drizzle/v1';
|
|
14
14
|
}
|
|
15
|
+
|
|
15
16
|
/**
|
|
16
|
-
* Configuration for a
|
|
17
|
+
* Configuration options for declaring a PostgreSQL {@link Database}.
|
|
17
18
|
*/
|
|
18
19
|
export interface DatabaseConfig {
|
|
19
20
|
migrations?: string | MigrationsConfig;
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* Static string-literal tags that classify the database for policy checks
|
|
24
|
+
* (e.g. `{data: "pci"}`). Keys and values may contain lowercase letters,
|
|
25
|
+
* digits, `_`, and `-`, up to 63 characters. Keys must start with a letter
|
|
26
|
+
* and cannot start with the reserved prefix `encore`. At most 32 tags.
|
|
26
27
|
*/
|
|
27
28
|
tags?: Record<string, string>;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
|
-
*
|
|
32
|
+
* A single query result row, keyed by column name.
|
|
32
33
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
34
|
+
* Column types map to JavaScript values as follows:
|
|
35
|
+
* - Integers and floats decode to `number`, except `int8` values outside the
|
|
36
|
+
* safe integer range (±(2^53−1)), which decode to `bigint` to avoid
|
|
37
|
+
* precision loss.
|
|
38
|
+
* - `numeric` decodes to `Decimal`, `interval` to an ISO 8601 string (e.g.
|
|
39
|
+
* `'P1Y2M3DT4H5M6.5S'`; see {@link Interval}), `timestamp`/`timestamptz` to
|
|
40
|
+
* `Date`, and `bytea` to `ArrayBuffer`.
|
|
41
|
+
* - Types without a dedicated decoder (`money`, `timetz`, ranges, `bit`,
|
|
42
|
+
* `xml`, `tsvector`, and extension types) decode to PostgreSQL text strings
|
|
43
|
+
* (or string arrays for array columns) and accept string parameters.
|
|
41
44
|
*/
|
|
42
45
|
export type Row = Record<string, any>;
|
|
43
46
|
|
|
44
|
-
/**
|
|
47
|
+
/** Parameter value accepted in query template literals and raw queries. */
|
|
45
48
|
export type Primitive =
|
|
46
49
|
| string
|
|
47
50
|
| string[]
|
|
@@ -67,36 +70,30 @@ export type Primitive =
|
|
|
67
70
|
type SQLQueryExecutor =
|
|
68
71
|
runtime.SQLConn | runtime.SQLDatabase | runtime.Transaction;
|
|
69
72
|
|
|
70
|
-
/**
|
|
73
|
+
/** Result of an `exec` statement, with an exact affected-row count even beyond `Number.MAX_SAFE_INTEGER`. */
|
|
71
74
|
export interface ExecResult {
|
|
72
75
|
rowsAffected: bigint;
|
|
73
76
|
}
|
|
74
77
|
|
|
75
|
-
/** Base class containing shared query
|
|
78
|
+
/** Base class containing shared query and execution methods. */
|
|
76
79
|
class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
77
|
-
/**
|
|
78
|
-
* the current test's instance of it, in test mode.
|
|
79
|
-
* @internal */
|
|
80
|
+
/** Native handle or lazy per-test resolver. @internal */
|
|
80
81
|
constructor(private readonly handle: I | (() => I)) {}
|
|
81
82
|
|
|
82
|
-
/**
|
|
83
|
+
/** Native handle on which statements execute. */
|
|
83
84
|
protected get impl(): I {
|
|
84
85
|
return typeof this.handle === 'function' ? this.handle() : this.handle;
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
/**
|
|
88
|
-
*
|
|
89
|
-
* with
|
|
90
|
-
*
|
|
91
|
-
* It returns an async generator, that allows iterating over the results
|
|
92
|
-
* in a streaming fashion using `for await`.
|
|
89
|
+
* Streams rows from a parameterized tagged-template SQL query, replacing
|
|
90
|
+
* `${value}` placeholders with `$1`, `$2`, ... to prevent SQL injection.
|
|
91
|
+
* Iterate over results with `for await`.
|
|
93
92
|
*
|
|
94
93
|
* @example
|
|
95
|
-
*
|
|
96
94
|
* const email = "foo@example.com";
|
|
97
|
-
* const result = database.query`SELECT id FROM users WHERE email=${email}
|
|
98
|
-
*
|
|
99
|
-
* This produces the query: "SELECT id FROM users WHERE email=$1".
|
|
95
|
+
* const result = database.query`SELECT id FROM users WHERE email=${email}`;
|
|
96
|
+
* // Executes: "SELECT id FROM users WHERE email=$1"
|
|
100
97
|
*/
|
|
101
98
|
async *query<T extends Row = Record<string, any>>(
|
|
102
99
|
strings: TemplateStringsArray,
|
|
@@ -106,10 +103,8 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
106
103
|
}
|
|
107
104
|
|
|
108
105
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* It returns an async generator, that allows iterating over the results
|
|
112
|
-
* in a streaming fashion using `for await`.
|
|
106
|
+
* Streams rows from a raw parameterized SQL query using `$1`, `$2`, ...
|
|
107
|
+
* placeholders. Iterate over results with `for await`.
|
|
113
108
|
*
|
|
114
109
|
* @example
|
|
115
110
|
* const query = "SELECT id FROM users WHERE email=$1";
|
|
@@ -119,8 +114,8 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
119
114
|
* }
|
|
120
115
|
*
|
|
121
116
|
* @param query - The raw SQL query string.
|
|
122
|
-
* @param params -
|
|
123
|
-
* @returns An async generator
|
|
117
|
+
* @param params - Positional parameters for the query.
|
|
118
|
+
* @returns An async generator yielding rows from the query result.
|
|
124
119
|
*/
|
|
125
120
|
async *rawQuery<T extends Row = Record<string, any>>(
|
|
126
121
|
query: string,
|
|
@@ -141,17 +136,12 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
141
136
|
}
|
|
142
137
|
|
|
143
138
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* It returns an array of all results.
|
|
139
|
+
* Executes a parameterized tagged-template SQL query and collects all rows
|
|
140
|
+
* into an array.
|
|
148
141
|
*
|
|
149
142
|
* @example
|
|
150
|
-
*
|
|
151
143
|
* const email = "foo@example.com";
|
|
152
|
-
* const
|
|
153
|
-
*
|
|
154
|
-
* This produces the query: "SELECT id FROM users WHERE email=$1".
|
|
144
|
+
* const rows = await database.queryAll`SELECT id FROM users WHERE email=${email}`;
|
|
155
145
|
*/
|
|
156
146
|
async queryAll<T extends Row = Record<string, any>>(
|
|
157
147
|
strings: TemplateStringsArray,
|
|
@@ -161,12 +151,9 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
161
151
|
}
|
|
162
152
|
|
|
163
153
|
/**
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
* It returns an array of all results.
|
|
154
|
+
* Executes a raw parameterized SQL query and collects all rows into an array.
|
|
167
155
|
*
|
|
168
156
|
* @example
|
|
169
|
-
*
|
|
170
157
|
* const query = "SELECT id FROM users WHERE email=$1";
|
|
171
158
|
* const email = "foo@example.com";
|
|
172
159
|
* const rows = await database.rawQueryAll(query, email);
|
|
@@ -181,13 +168,13 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
181
168
|
}
|
|
182
169
|
|
|
183
170
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
171
|
+
* Executes a parameterized tagged-template SQL query expecting at most one
|
|
172
|
+
* row. Returns `null` if no rows match, or throws {@link TooManyRowsError}
|
|
173
|
+
* if more than one row matches.
|
|
187
174
|
*
|
|
188
175
|
* @example
|
|
189
176
|
* const email = "foo@example.com";
|
|
190
|
-
* const
|
|
177
|
+
* const row = await database.queryRow`SELECT id FROM users WHERE email=${email}`;
|
|
191
178
|
*/
|
|
192
179
|
async queryRow<T extends Row = Record<string, any>>(
|
|
193
180
|
strings: TemplateStringsArray,
|
|
@@ -197,19 +184,18 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
197
184
|
}
|
|
198
185
|
|
|
199
186
|
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
187
|
+
* Executes a raw parameterized SQL query expecting at most one row. Returns
|
|
188
|
+
* `null` if no rows match, or throws {@link TooManyRowsError} if more than
|
|
189
|
+
* one row matches.
|
|
203
190
|
*
|
|
204
191
|
* @example
|
|
205
192
|
* const query = "SELECT id FROM users WHERE email=$1";
|
|
206
193
|
* const email = "foo@example.com";
|
|
207
194
|
* const result = await database.rawQueryRow(query, email);
|
|
208
|
-
* console.log(result);
|
|
209
195
|
*
|
|
210
196
|
* @param query - The raw SQL query string.
|
|
211
|
-
* @param params -
|
|
212
|
-
* @returns A promise
|
|
197
|
+
* @param params - Positional parameters for the query.
|
|
198
|
+
* @returns A promise resolving to the single matching row or `null`.
|
|
213
199
|
*/
|
|
214
200
|
async rawQueryRow<T extends Row = Record<string, any>>(
|
|
215
201
|
query: string,
|
|
@@ -224,11 +210,12 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
224
210
|
}
|
|
225
211
|
|
|
226
212
|
/**
|
|
227
|
-
*
|
|
213
|
+
* Executes a parameterized tagged-template statement and returns the
|
|
214
|
+
* affected row count.
|
|
228
215
|
*
|
|
229
216
|
* @example
|
|
230
217
|
* const email = "foo@example.com";
|
|
231
|
-
* const result = database.exec`DELETE FROM users WHERE email=${email}
|
|
218
|
+
* const result = await database.exec`DELETE FROM users WHERE email=${email}`;
|
|
232
219
|
*/
|
|
233
220
|
async exec(
|
|
234
221
|
strings: TemplateStringsArray,
|
|
@@ -238,7 +225,7 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
238
225
|
}
|
|
239
226
|
|
|
240
227
|
/**
|
|
241
|
-
*
|
|
228
|
+
* Executes a raw parameterized statement and returns the affected row count.
|
|
242
229
|
*
|
|
243
230
|
* @example
|
|
244
231
|
* const query = "DELETE FROM users WHERE email=$1";
|
|
@@ -246,8 +233,8 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
246
233
|
* await database.rawExec(query, email);
|
|
247
234
|
*
|
|
248
235
|
* @param query - The raw SQL query string.
|
|
249
|
-
* @param params -
|
|
250
|
-
* @returns The affected
|
|
236
|
+
* @param params - Positional parameters for the statement.
|
|
237
|
+
* @returns The affected row count after the statement completes.
|
|
251
238
|
*/
|
|
252
239
|
async rawExec(query: string, ...params: Primitive[]): Promise<ExecResult> {
|
|
253
240
|
const cursor = await native(
|
|
@@ -264,26 +251,27 @@ class BaseQueryExecutor<I extends SQLQueryExecutor = SQLQueryExecutor> {
|
|
|
264
251
|
}
|
|
265
252
|
}
|
|
266
253
|
|
|
267
|
-
/**
|
|
254
|
+
/** PostgreSQL transaction isolation level. */
|
|
268
255
|
export type IsolationLevel =
|
|
269
256
|
'read_committed' | 'repeatable_read' | 'serializable';
|
|
270
257
|
|
|
271
258
|
/**
|
|
272
|
-
*
|
|
273
|
-
* `read_committed`, read-write
|
|
259
|
+
* Options for starting a PostgreSQL transaction. Unspecified options use the
|
|
260
|
+
* database defaults (`read_committed`, read-write — or read-only on a read
|
|
261
|
+
* handle — and non-deferrable).
|
|
274
262
|
*/
|
|
275
263
|
export interface TransactionOptions {
|
|
276
264
|
isolation?: IsolationLevel;
|
|
277
265
|
/**
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
* throws a `TypeError` on one.
|
|
266
|
+
* Starts the transaction as `READ ONLY` (`true`) or `READ WRITE` (`false`).
|
|
267
|
+
* Passing `false` on a read handle throws a `TypeError`.
|
|
281
268
|
*/
|
|
282
269
|
readOnly?: boolean;
|
|
283
270
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
271
|
+
* Starts the transaction as `DEFERRABLE` (`true`) or `NOT DEFERRABLE`
|
|
272
|
+
* (`false`). Applies only to serializable read-only transactions, allowing
|
|
273
|
+
* them to wait for a safe snapshot instead of failing with serialization
|
|
274
|
+
* errors.
|
|
287
275
|
*/
|
|
288
276
|
deferrable?: boolean;
|
|
289
277
|
}
|
|
@@ -295,9 +283,8 @@ const isolationLevels: Record<IsolationLevel, runtime.SQLIsolationLevel> = {
|
|
|
295
283
|
};
|
|
296
284
|
|
|
297
285
|
/**
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
* `readOnly: false` on a read handle's pool or connection.
|
|
286
|
+
* Starts a transaction on `impl` with validated `options`. Throws `TypeError`
|
|
287
|
+
* for unknown isolation levels or `readOnly: false` on a read-only handle.
|
|
301
288
|
*/
|
|
302
289
|
async function beginOn(
|
|
303
290
|
impl: runtime.SQLDatabase | runtime.SQLConn,
|
|
@@ -317,7 +304,7 @@ async function beginOn(
|
|
|
317
304
|
}
|
|
318
305
|
if (readOnly === false && impl.isReadOnly()) {
|
|
319
306
|
throw new TypeError(
|
|
320
|
-
|
|
307
|
+
'read-only database handles cannot start read-write transactions (readOnly cannot be false)',
|
|
321
308
|
);
|
|
322
309
|
}
|
|
323
310
|
opts = {
|
|
@@ -331,9 +318,9 @@ async function beginOn(
|
|
|
331
318
|
}
|
|
332
319
|
|
|
333
320
|
/**
|
|
334
|
-
* A query handle
|
|
335
|
-
*
|
|
336
|
-
*
|
|
321
|
+
* A scoped query handle safe to pass into application or library code. Obtain
|
|
322
|
+
* one with `read()`, `write()`, `admin()`, `acquire()`, or `begin()`.
|
|
323
|
+
* {@link Database} deliberately does not extend `Queryable`.
|
|
337
324
|
*/
|
|
338
325
|
export abstract class Queryable<
|
|
339
326
|
I extends SQLQueryExecutor = SQLQueryExecutor,
|
|
@@ -341,29 +328,29 @@ export abstract class Queryable<
|
|
|
341
328
|
private readonly queryableBrand = true;
|
|
342
329
|
}
|
|
343
330
|
|
|
344
|
-
/** Options for
|
|
331
|
+
/** Options for {@link Database.read}. */
|
|
345
332
|
export interface ReadOptions {
|
|
346
333
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
334
|
+
* Route reads to the primary instance instead of a read replica. When
|
|
335
|
+
* omitted, Encore uses a read-replica pool if one is configured.
|
|
349
336
|
*/
|
|
350
337
|
primary?: boolean;
|
|
351
338
|
}
|
|
352
339
|
|
|
353
340
|
/**
|
|
354
|
-
* A database handle with access recorded by Encore
|
|
341
|
+
* A database handle with an access level recorded statically by Encore at its
|
|
342
|
+
* creation site (`read()`, `write()`, or `admin()`).
|
|
355
343
|
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
* or admin handle runs read-write.
|
|
344
|
+
* Read handles execute on read-only connections where writes fail with a
|
|
345
|
+
* `DatabaseError` (`code: "read_only_transaction"`, SQLSTATE 25006), and any
|
|
346
|
+
* transactions or connections they create are also read-only. Write and admin
|
|
347
|
+
* handles execute read-write.
|
|
361
348
|
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
349
|
+
* When no dedicated read-only pool is configured, read handles share the
|
|
350
|
+
* primary pool with `default_transaction_read_only = on` (which guards against
|
|
351
|
+
* accidental writes, though explicit `SET default_transaction_read_only = off`
|
|
352
|
+
* would lift it); with a dedicated read-only pool, the database role itself
|
|
353
|
+
* enforces read-only permissions.
|
|
367
354
|
*/
|
|
368
355
|
export class DatabaseRef extends Queryable<runtime.SQLDatabase> {
|
|
369
356
|
/** @internal */
|
|
@@ -372,30 +359,26 @@ export class DatabaseRef extends Queryable<runtime.SQLDatabase> {
|
|
|
372
359
|
}
|
|
373
360
|
|
|
374
361
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
* `primary` is set), and otherwise to the primary. Either way its session
|
|
380
|
-
* is read-only (`default_transaction_read_only` is on), so a write fails
|
|
381
|
-
* with SQLSTATE 25006 until SQL on it turns that off; through the
|
|
382
|
-
* read-only pool, its role refuses writes still.
|
|
362
|
+
* PostgreSQL connection string for connecting with an external client using
|
|
363
|
+
* this handle's access level. Write/admin handles connect read-write; read
|
|
364
|
+
* handles connect to the read-only replica pool if configured (unless
|
|
365
|
+
* `primary: true` was passed) with `default_transaction_read_only` enabled.
|
|
383
366
|
*/
|
|
384
367
|
get connectionString(): string {
|
|
385
368
|
return this.impl.connString();
|
|
386
369
|
}
|
|
387
370
|
|
|
388
371
|
/**
|
|
389
|
-
* Acquires a
|
|
390
|
-
* See {@link Database.acquire}.
|
|
372
|
+
* Acquires a dedicated connection from the pool with this handle's access
|
|
373
|
+
* level. See {@link Database.acquire}.
|
|
391
374
|
*/
|
|
392
375
|
async acquire(): Promise<Connection> {
|
|
393
376
|
return new Connection(await native(this.impl.acquire(activeCtxBytes())));
|
|
394
377
|
}
|
|
395
378
|
|
|
396
379
|
/**
|
|
397
|
-
* Begins a transaction with this handle's access
|
|
398
|
-
*
|
|
380
|
+
* Begins a transaction with this handle's access level (always read-only for
|
|
381
|
+
* read handles). See {@link Database.begin}.
|
|
399
382
|
*/
|
|
400
383
|
async begin(options?: TransactionOptions): Promise<Transaction> {
|
|
401
384
|
return beginOn(this.impl, options);
|
|
@@ -403,14 +386,14 @@ export class DatabaseRef extends Queryable<runtime.SQLDatabase> {
|
|
|
403
386
|
}
|
|
404
387
|
|
|
405
388
|
/**
|
|
406
|
-
*
|
|
407
|
-
*
|
|
389
|
+
* Declares a PostgreSQL database. Encore provisions the database, runs its
|
|
390
|
+
* migrations, and returns this handle.
|
|
408
391
|
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
392
|
+
* To reference an existing database without declaring a duplicate, use
|
|
393
|
+
* {@link Database.named}.
|
|
411
394
|
*/
|
|
412
395
|
export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
413
|
-
/**
|
|
396
|
+
/** Cached read handles, created on first use. */
|
|
414
397
|
private reader?: DatabaseRef;
|
|
415
398
|
private primaryReader?: DatabaseRef;
|
|
416
399
|
private readonly dbName: string;
|
|
@@ -421,17 +404,17 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
|
421
404
|
}
|
|
422
405
|
|
|
423
406
|
/**
|
|
424
|
-
*
|
|
425
|
-
*
|
|
407
|
+
* References an existing database by name. Use `new Database(name)` to
|
|
408
|
+
* declare a new database.
|
|
426
409
|
*/
|
|
427
410
|
static named<name extends string>(name: StringLiteral<name>): Database {
|
|
428
411
|
return new Database(name);
|
|
429
412
|
}
|
|
430
413
|
|
|
431
414
|
/**
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
415
|
+
* Returns a read-only handle. Queries run on read-only connections (routed
|
|
416
|
+
* to a read replica when available unless `options.primary` is `true`), and
|
|
417
|
+
* write statements fail.
|
|
435
418
|
*/
|
|
436
419
|
read(options: ReadOptions = {}): DatabaseRef {
|
|
437
420
|
if (options.primary) {
|
|
@@ -440,12 +423,12 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
|
440
423
|
return (this.reader ??= this.ref(runtime.SQLAccess.Read));
|
|
441
424
|
}
|
|
442
425
|
|
|
443
|
-
/**
|
|
426
|
+
/** Returns a read-write handle. */
|
|
444
427
|
write(): DatabaseRef {
|
|
445
428
|
return new DatabaseRef(testMode ? () => this.impl : this.impl);
|
|
446
429
|
}
|
|
447
430
|
|
|
448
|
-
/**
|
|
431
|
+
/** Returns an admin handle with read, write, and admin access. */
|
|
449
432
|
admin(): DatabaseRef {
|
|
450
433
|
return this.write();
|
|
451
434
|
}
|
|
@@ -462,25 +445,24 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
|
462
445
|
}
|
|
463
446
|
|
|
464
447
|
/**
|
|
465
|
-
* Returns
|
|
466
|
-
* read-
|
|
467
|
-
* {@link DatabaseRef.connectionString}.
|
|
448
|
+
* Returns a read-write PostgreSQL connection string for external clients.
|
|
449
|
+
* For a read-only connection string, use {@link DatabaseRef.connectionString}.
|
|
468
450
|
*/
|
|
469
451
|
get connectionString(): string {
|
|
470
452
|
return this.impl.connString();
|
|
471
453
|
}
|
|
472
454
|
|
|
473
455
|
/**
|
|
474
|
-
* Acquires a
|
|
456
|
+
* Acquires a dedicated connection from the pool.
|
|
457
|
+
*
|
|
458
|
+
* When closed or garbage-collected, the connection is returned to the pool.
|
|
459
|
+
* Session settings and roles changed directly via `SET`, `set_config`, or
|
|
460
|
+
* `SET ROLE` are reset before reuse (changes made inside stored functions or
|
|
461
|
+
* procedures are not tracked automatically). To scope a setting such as a
|
|
462
|
+
* tenant ID for row-level security to one unit of work, use
|
|
463
|
+
* `set_config(name, value, true)` inside a transaction.
|
|
475
464
|
*
|
|
476
|
-
*
|
|
477
|
-
* Settings and the role its session was given (`SET`, `set_config`,
|
|
478
|
-
* `SET ROLE`) are reset before the pool lends it again, when a statement
|
|
479
|
-
* sent on it did so: one a function or procedure changes is not noticed,
|
|
480
|
-
* and stays for the next borrower. To scope a setting to a unit of work,
|
|
481
|
-
* such as a tenant id for row-level security, set it in a transaction with
|
|
482
|
-
* `set_config(name, value, true)`, which ends with it.
|
|
483
|
-
* @returns a new connection to the database
|
|
465
|
+
* @returns A dedicated {@link Connection} implementing `AsyncDisposable`.
|
|
484
466
|
*/
|
|
485
467
|
async acquire(): Promise<Connection> {
|
|
486
468
|
const impl = await native(this.impl.acquire(activeCtxBytes()));
|
|
@@ -490,9 +472,8 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
|
490
472
|
/**
|
|
491
473
|
* Begins a database transaction.
|
|
492
474
|
*
|
|
493
|
-
* Prefer
|
|
494
|
-
*
|
|
495
|
-
* the variable goes out of scope:
|
|
475
|
+
* Prefer `await using`, which automatically rolls back the transaction if
|
|
476
|
+
* neither `commit()` nor `rollback()` is called before leaving the scope:
|
|
496
477
|
*
|
|
497
478
|
* ```ts
|
|
498
479
|
* await using tx = await db.begin();
|
|
@@ -500,17 +481,15 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
|
500
481
|
* await tx.commit();
|
|
501
482
|
* ```
|
|
502
483
|
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
* `options` sets the transaction's isolation level and whether it is
|
|
507
|
-
* read-only or deferrable:
|
|
484
|
+
* Otherwise, always call `commit()` or `rollback()` explicitly to avoid
|
|
485
|
+
* leaked transactions. Pass `options` to configure isolation level,
|
|
486
|
+
* read-only mode, or deferrability:
|
|
508
487
|
*
|
|
509
488
|
* ```ts
|
|
510
489
|
* await using tx = await db.begin({isolation: 'serializable'});
|
|
511
490
|
* ```
|
|
512
491
|
*
|
|
513
|
-
* @returns
|
|
492
|
+
* @returns A {@link Transaction} implementing `AsyncDisposable`.
|
|
514
493
|
*/
|
|
515
494
|
async begin(options?: TransactionOptions): Promise<Transaction> {
|
|
516
495
|
return beginOn(this.impl, options);
|
|
@@ -518,21 +497,17 @@ export class Database extends BaseQueryExecutor<runtime.SQLDatabase> {
|
|
|
518
497
|
}
|
|
519
498
|
|
|
520
499
|
/**
|
|
521
|
-
* Represents
|
|
500
|
+
* Represents an active database transaction.
|
|
522
501
|
*
|
|
523
|
-
* `
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
* out of scope:
|
|
502
|
+
* Implements `AsyncDisposable` for use with `await using`, automatically
|
|
503
|
+
* rolling back if neither `commit()` nor `rollback()` is called before leaving
|
|
504
|
+
* the scope:
|
|
527
505
|
*
|
|
528
506
|
* ```ts
|
|
529
507
|
* await using tx = await db.begin();
|
|
530
508
|
* await tx.exec`INSERT INTO ...`;
|
|
531
509
|
* await tx.commit();
|
|
532
510
|
* ```
|
|
533
|
-
*
|
|
534
|
-
* If you can't use `await using`, make sure to always call `commit` or
|
|
535
|
-
* `rollback` yourself to prevent hanging transactions.
|
|
536
511
|
*/
|
|
537
512
|
export class Transaction
|
|
538
513
|
extends Queryable<runtime.Transaction>
|
|
@@ -546,7 +521,7 @@ export class Transaction
|
|
|
546
521
|
}
|
|
547
522
|
|
|
548
523
|
/**
|
|
549
|
-
*
|
|
524
|
+
* Commits the transaction.
|
|
550
525
|
*/
|
|
551
526
|
async commit() {
|
|
552
527
|
try {
|
|
@@ -557,7 +532,7 @@ export class Transaction
|
|
|
557
532
|
}
|
|
558
533
|
|
|
559
534
|
/**
|
|
560
|
-
*
|
|
535
|
+
* Rolls back the transaction.
|
|
561
536
|
*/
|
|
562
537
|
async rollback() {
|
|
563
538
|
try {
|
|
@@ -575,7 +550,7 @@ export class Transaction
|
|
|
575
550
|
}
|
|
576
551
|
|
|
577
552
|
/**
|
|
578
|
-
* Represents a dedicated connection
|
|
553
|
+
* Represents a dedicated PostgreSQL connection acquired from the pool.
|
|
579
554
|
*/
|
|
580
555
|
export class Connection
|
|
581
556
|
extends Queryable<runtime.SQLConn>
|
|
@@ -587,9 +562,9 @@ export class Connection
|
|
|
587
562
|
}
|
|
588
563
|
|
|
589
564
|
/**
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
565
|
+
* Begins a transaction that exclusively borrows this connection until it
|
|
566
|
+
* completes (always read-only on connections acquired from a read handle).
|
|
567
|
+
* See {@link Database.begin}.
|
|
593
568
|
*/
|
|
594
569
|
async begin(options?: TransactionOptions): Promise<Transaction> {
|
|
595
570
|
return beginOn(this.impl, options);
|
|
@@ -607,11 +582,7 @@ export class Connection
|
|
|
607
582
|
}
|
|
608
583
|
}
|
|
609
584
|
|
|
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
|
-
*/
|
|
585
|
+
/** Resolves the database handle for `name` in test mode (per-test or process fallback). */
|
|
615
586
|
function testHandle(name: string): () => runtime.SQLDatabase {
|
|
616
587
|
let own: runtime.SQLDatabase | undefined;
|
|
617
588
|
return () =>
|
|
@@ -637,18 +608,14 @@ function buildQueryArgs(params: Primitive[]): runtime.QueryArgs {
|
|
|
637
608
|
);
|
|
638
609
|
}
|
|
639
610
|
|
|
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
|
-
*/
|
|
611
|
+
/** Converts `undefined` to `null` and `Temporal.Duration` to ISO 8601 strings for PostgreSQL. */
|
|
645
612
|
function param(p: Primitive): Primitive {
|
|
646
613
|
if (p === undefined) return null;
|
|
647
614
|
if (isTemporalDuration(p)) return p.toString();
|
|
648
615
|
return p;
|
|
649
616
|
}
|
|
650
617
|
|
|
651
|
-
/**
|
|
618
|
+
/** Detects `Temporal.Duration` instances when the runtime supports Temporal. */
|
|
652
619
|
function isTemporalDuration(p: unknown): p is {toString(): string} {
|
|
653
620
|
return (
|
|
654
621
|
typeof p === 'object' &&
|