@encore.dev/sdk 0.0.0 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +362 -0
- package/README.md +122 -2
- package/app_meta.test.ts +134 -0
- package/app_meta.ts +186 -0
- package/config/config.test.ts +381 -0
- package/config/config.ts +178 -0
- package/config/mod.ts +14 -0
- package/config/secrets.ts +147 -0
- package/config/value.ts +243 -0
- package/cron/mod.ts +23 -0
- package/dist/app_meta.d.ts +93 -0
- package/dist/app_meta.js +65 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +105 -0
- package/dist/config/config.js +87 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +4 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +92 -0
- package/dist/config/secrets.js +80 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +51 -0
- package/dist/config/value.js +178 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +22 -0
- package/dist/cron/mod.js +9 -0
- package/dist/cron/mod.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +50 -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 +10 -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 +22 -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 +112 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.js +9 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +73 -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 +14 -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 +9 -0
- package/dist/internal/otel/provider.js +19 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/request_context.d.ts +11 -0
- package/dist/internal/otel/request_context.js +20 -0
- package/dist/internal/otel/request_context.js.map +1 -0
- package/dist/internal/otel/span.d.ts +61 -0
- package/dist/internal/otel/span.js +206 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +15 -0
- package/dist/internal/otel/tracer.js +65 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- package/dist/internal/reqtrack/mod.d.ts +6 -0
- package/dist/internal/reqtrack/mod.js +28 -0
- package/dist/internal/reqtrack/mod.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +11 -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 +12 -0
- package/dist/internal/runtime/mod.js +28 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +122 -0
- package/dist/internal/runtime/napi/napi.d.cts +601 -0
- package/dist/internal/runtime/shutdown.d.ts +13 -0
- package/dist/internal/runtime/shutdown.js +56 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +9 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +39 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +123 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +19 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +9 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +315 -0
- package/dist/metrics/mod.js +488 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +9 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +3 -0
- package/dist/pubsub/encoding.js +10 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +6 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +12 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +128 -0
- package/dist/pubsub/subscription.js +39 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +148 -0
- package/dist/pubsub/topic.js +164 -0
- package/dist/pubsub/topic.js.map +1 -0
- package/dist/req_meta.d.ts +47 -0
- package/dist/req_meta.js +26 -0
- package/dist/req_meta.js.map +1 -0
- package/dist/services/index.d.ts +7 -0
- package/dist/services/index.js +17 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +23 -0
- package/dist/shutdown/mod.js +24 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +216 -0
- package/dist/storage/objects/bucket.js +123 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +14 -0
- package/dist/storage/objects/error.js +81 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +4 -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 +31 -0
- package/dist/storage/objects/refs.js +20 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +295 -0
- package/dist/storage/postgres/database.js +378 -0
- package/dist/storage/postgres/database.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +2 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +573 -0
- package/dist/storage/redis/commands.js +637 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +302 -0
- package/dist/storage/redis/database.js +336 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +88 -0
- package/dist/storage/redis/mod.js +84 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.js +11 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +89 -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 +53 -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 +256 -0
- package/dist/storage/redis/reply.js +572 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +11 -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 +91 -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 +47 -0
- package/dist/storage/redis/target.js +82 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +105 -0
- package/dist/storage/redis/wire.js +333 -0
- package/dist/storage/redis/wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +42 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.map +1 -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/index.ts +20 -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 +23 -0
- package/internal/otel/request_context.ts +32 -0
- package/internal/otel/span.ts +252 -0
- package/internal/otel/tracer.test.ts +352 -0
- package/internal/otel/tracer.ts +104 -0
- package/internal/reqtrack/mod.ts +39 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +41 -0
- package/internal/runtime/napi/napi.cjs +122 -0
- package/internal/runtime/napi/napi.d.cts +601 -0
- package/internal/runtime/shutdown.test.ts +248 -0
- package/internal/runtime/shutdown.ts +64 -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 +337 -0
- package/metrics/mod.test.ts +180 -0
- package/metrics/mod.ts +619 -0
- package/mod.ts +22 -0
- package/otel.test.ts +84 -0
- package/otel.ts +17 -0
- package/package.json +121 -5
- package/pubsub/encoding.ts +8 -0
- package/pubsub/mod.ts +14 -0
- package/pubsub/pubsub.test.ts +186 -0
- package/pubsub/refs.ts +22 -0
- package/pubsub/subscription.ts +180 -0
- package/pubsub/topic.test.ts +121 -0
- package/pubsub/topic.ts +212 -0
- package/req_meta.ts +75 -0
- package/services/index.test.ts +30 -0
- package/services/index.ts +16 -0
- package/shutdown/mod.ts +24 -0
- package/storage/objects/bucket.ts +338 -0
- package/storage/objects/error.ts +94 -0
- package/storage/objects/mod.ts +35 -0
- package/storage/objects/refs.ts +69 -0
- package/storage/postgres/database.integration.test.ts +175 -0
- package/storage/postgres/database.test.ts +241 -0
- package/storage/postgres/database.ts +497 -0
- package/storage/postgres/mod.ts +15 -0
- package/storage/redis/commands.ts +1221 -0
- package/storage/redis/database.ts +466 -0
- package/storage/redis/mod.ts +135 -0
- package/storage/redis/native.ts +11 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +706 -0
- package/storage/redis/reply.ts +705 -0
- package/storage/redis/runtime.test.ts +233 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +131 -0
- package/storage/redis/wire.ts +369 -0
- package/types/mod.ts +92 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import {activeCtxBytes} from '../../internal/otel/index';
|
|
2
|
+
import * as runtime from '../../internal/runtime/mod';
|
|
3
|
+
import {StringLiteral} from '../../internal/utils/constraints';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configures how database migrations are managed for a `Database`.
|
|
7
|
+
*/
|
|
8
|
+
export interface MigrationsConfig {
|
|
9
|
+
path: string;
|
|
10
|
+
source?: 'prisma' | 'drizzle' | 'drizzle/v1';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for a `Database`.
|
|
14
|
+
*/
|
|
15
|
+
export interface DatabaseConfig {
|
|
16
|
+
migrations?: string | MigrationsConfig;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Represents a single row from a query result
|
|
21
|
+
*/
|
|
22
|
+
export type Row = Record<string, any>;
|
|
23
|
+
|
|
24
|
+
/** Represents a type that can be used in query template literals */
|
|
25
|
+
export type Primitive =
|
|
26
|
+
| string
|
|
27
|
+
| string[]
|
|
28
|
+
| number
|
|
29
|
+
| number[]
|
|
30
|
+
| boolean
|
|
31
|
+
| boolean[]
|
|
32
|
+
| Buffer
|
|
33
|
+
| Date
|
|
34
|
+
| Date[]
|
|
35
|
+
| Record<string, any>
|
|
36
|
+
| Record<string, any>[]
|
|
37
|
+
// Preserve the existing public input type during the style migration.
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
|
|
39
|
+
| BigInt
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
|
|
41
|
+
| BigInt[]
|
|
42
|
+
| null
|
|
43
|
+
| undefined;
|
|
44
|
+
|
|
45
|
+
type SQLQueryExecutor =
|
|
46
|
+
runtime.SQLConn | runtime.SQLDatabase | runtime.Transaction;
|
|
47
|
+
|
|
48
|
+
/** The number of affected rows, represented exactly even beyond Number.MAX_SAFE_INTEGER. */
|
|
49
|
+
export interface ExecResult {
|
|
50
|
+
rowsAffected: bigint;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Base class containing shared query functionality */
|
|
54
|
+
class BaseQueryExecutor {
|
|
55
|
+
constructor(protected readonly impl: SQLQueryExecutor) {}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* query queries the database using a template string, replacing your placeholders in the template
|
|
59
|
+
* with parametrised values without risking SQL injections.
|
|
60
|
+
*
|
|
61
|
+
* It returns an async generator, that allows iterating over the results
|
|
62
|
+
* in a streaming fashion using `for await`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
*
|
|
66
|
+
* const email = "foo@example.com";
|
|
67
|
+
* const result = database.query`SELECT id FROM users WHERE email=${email}`
|
|
68
|
+
*
|
|
69
|
+
* This produces the query: "SELECT id FROM users WHERE email=$1".
|
|
70
|
+
*/
|
|
71
|
+
async *query<T extends Row = Record<string, any>>(
|
|
72
|
+
strings: TemplateStringsArray,
|
|
73
|
+
...params: Primitive[]
|
|
74
|
+
): AsyncGenerator<T> {
|
|
75
|
+
yield* this.rawQuery<T>(buildQuery(strings, params), ...params);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* rawQuery queries the database using a raw parametrised SQL query and parameters.
|
|
80
|
+
*
|
|
81
|
+
* It returns an async generator, that allows iterating over the results
|
|
82
|
+
* in a streaming fashion using `for await`.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const query = "SELECT id FROM users WHERE email=$1";
|
|
86
|
+
* const email = "foo@example.com";
|
|
87
|
+
* for await (const row of database.rawQuery(query, email)) {
|
|
88
|
+
* console.log(row);
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* @param query - The raw SQL query string.
|
|
92
|
+
* @param params - The parameters to be used in the query.
|
|
93
|
+
* @returns An async generator that yields rows from the query result.
|
|
94
|
+
*/
|
|
95
|
+
async *rawQuery<T extends Row = Record<string, any>>(
|
|
96
|
+
query: string,
|
|
97
|
+
...params: Primitive[]
|
|
98
|
+
): AsyncGenerator<T> {
|
|
99
|
+
const cursor = await this.impl.query(
|
|
100
|
+
query,
|
|
101
|
+
buildQueryArgs(params),
|
|
102
|
+
activeCtxBytes(),
|
|
103
|
+
);
|
|
104
|
+
try {
|
|
105
|
+
while (true) {
|
|
106
|
+
const row = await cursor.next();
|
|
107
|
+
if (row === null) break;
|
|
108
|
+
yield row.values() as T;
|
|
109
|
+
}
|
|
110
|
+
} finally {
|
|
111
|
+
await cursor.close();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* queryAll queries the database using a template string, replacing your placeholders in the template
|
|
117
|
+
* with parametrised values without risking SQL injections.
|
|
118
|
+
*
|
|
119
|
+
* It returns an array of all results.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
*
|
|
123
|
+
* const email = "foo@example.com";
|
|
124
|
+
* const result = database.queryAll`SELECT id FROM users WHERE email=${email}`
|
|
125
|
+
*
|
|
126
|
+
* This produces the query: "SELECT id FROM users WHERE email=$1".
|
|
127
|
+
*/
|
|
128
|
+
async queryAll<T extends Row = Record<string, any>>(
|
|
129
|
+
strings: TemplateStringsArray,
|
|
130
|
+
...params: Primitive[]
|
|
131
|
+
): Promise<T[]> {
|
|
132
|
+
return this.rawQueryAll<T>(buildQuery(strings, params), ...params);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* rawQueryAll queries the database using a raw parametrised SQL query and parameters.
|
|
137
|
+
*
|
|
138
|
+
* It returns an array of all results.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
*
|
|
142
|
+
* const query = "SELECT id FROM users WHERE email=$1";
|
|
143
|
+
* const email = "foo@example.com";
|
|
144
|
+
* const rows = await database.rawQueryAll(query, email);
|
|
145
|
+
*/
|
|
146
|
+
async rawQueryAll<T extends Row = Record<string, any>>(
|
|
147
|
+
query: string,
|
|
148
|
+
...params: Primitive[]
|
|
149
|
+
): Promise<T[]> {
|
|
150
|
+
const rows: T[] = [];
|
|
151
|
+
for await (const row of this.rawQuery<T>(query, ...params)) rows.push(row);
|
|
152
|
+
return rows;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* queryRow is like query but returns only a single row.
|
|
157
|
+
* If the query selects no rows it returns null.
|
|
158
|
+
* It throws if more than one row matches.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* const email = "foo@example.com";
|
|
162
|
+
* const result = database.queryRow`SELECT id FROM users WHERE email=${email}`
|
|
163
|
+
*/
|
|
164
|
+
async queryRow<T extends Row = Record<string, any>>(
|
|
165
|
+
strings: TemplateStringsArray,
|
|
166
|
+
...params: Primitive[]
|
|
167
|
+
): Promise<T | null> {
|
|
168
|
+
return this.rawQueryRow<T>(buildQuery(strings, params), ...params);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* rawQueryRow is like rawQuery but returns only a single row.
|
|
173
|
+
* If the query selects no rows, it returns null.
|
|
174
|
+
* It throws if more than one row matches.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* const query = "SELECT id FROM users WHERE email=$1";
|
|
178
|
+
* const email = "foo@example.com";
|
|
179
|
+
* const result = await database.rawQueryRow(query, email);
|
|
180
|
+
* console.log(result);
|
|
181
|
+
*
|
|
182
|
+
* @param query - The raw SQL query string.
|
|
183
|
+
* @param params - The parameters to be used in the query.
|
|
184
|
+
* @returns A promise that resolves to a single row or null.
|
|
185
|
+
*/
|
|
186
|
+
async rawQueryRow<T extends Row = Record<string, any>>(
|
|
187
|
+
query: string,
|
|
188
|
+
...params: Primitive[]
|
|
189
|
+
): Promise<T | null> {
|
|
190
|
+
let first: T | null = null;
|
|
191
|
+
for await (const row of this.rawQuery<T>(query, ...params)) {
|
|
192
|
+
if (first !== null) throw new Error('query returned more than one row');
|
|
193
|
+
first = row;
|
|
194
|
+
}
|
|
195
|
+
return first;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* exec executes a query and returns its affected-row count.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* const email = "foo@example.com";
|
|
203
|
+
* const result = database.exec`DELETE FROM users WHERE email=${email}`
|
|
204
|
+
*/
|
|
205
|
+
async exec(
|
|
206
|
+
strings: TemplateStringsArray,
|
|
207
|
+
...params: Primitive[]
|
|
208
|
+
): Promise<ExecResult> {
|
|
209
|
+
return this.rawExec(buildQuery(strings, params), ...params);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* rawExec executes a query and returns its affected-row count.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* const query = "DELETE FROM users WHERE email=$1";
|
|
217
|
+
* const email = "foo@example.com";
|
|
218
|
+
* await database.rawExec(query, email);
|
|
219
|
+
*
|
|
220
|
+
* @param query - The raw SQL query string.
|
|
221
|
+
* @param params - The parameters to be used in the query.
|
|
222
|
+
* @returns The affected-row count after the statement completes.
|
|
223
|
+
*/
|
|
224
|
+
async rawExec(query: string, ...params: Primitive[]): Promise<ExecResult> {
|
|
225
|
+
const cursor = await this.impl.query(
|
|
226
|
+
query,
|
|
227
|
+
buildQueryArgs(params),
|
|
228
|
+
activeCtxBytes(),
|
|
229
|
+
);
|
|
230
|
+
try {
|
|
231
|
+
while ((await cursor.next()) !== null) {
|
|
232
|
+
/* Discard RETURNING rows. */
|
|
233
|
+
}
|
|
234
|
+
return {rowsAffected: await cursor.rowsAffected()};
|
|
235
|
+
} finally {
|
|
236
|
+
await cursor.close();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A query handle that can be passed to application/library code.
|
|
243
|
+
* Obtain one with read(), write(), or admin(), or use a connection/transaction.
|
|
244
|
+
* A Database declaration deliberately does not implement this interface.
|
|
245
|
+
*/
|
|
246
|
+
export abstract class Queryable extends BaseQueryExecutor {
|
|
247
|
+
private readonly queryableBrand = true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Options for selecting read access. */
|
|
251
|
+
export interface ReadOptions {
|
|
252
|
+
/**
|
|
253
|
+
* Read from the primary rather than a read replica. Without it, a database
|
|
254
|
+
* whose runtime config has a read-only pool on a replica reads from that.
|
|
255
|
+
*/
|
|
256
|
+
primary?: boolean;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* A database handle with access recorded by Encore's parser at its creation site.
|
|
261
|
+
*
|
|
262
|
+
* Handles share the declaration's pool. A read handle runs on read-only
|
|
263
|
+
* connections: a statement that writes fails with `read_only_sql_transaction`
|
|
264
|
+
* (SQLSTATE 25006), and the transactions it begins, and those of the
|
|
265
|
+
* connections it acquires, are read-only. A write or admin handle runs
|
|
266
|
+
* read-write.
|
|
267
|
+
*
|
|
268
|
+
* On the database's own pool, which a read handle shares when there is no
|
|
269
|
+
* read-only pool for it, that refuses mistaken writes, not deliberate ones:
|
|
270
|
+
* SQL that changes the session's `default_transaction_read_only` on the same
|
|
271
|
+
* connection (`SET default_transaction_read_only = off`) lifts it. Against
|
|
272
|
+
* untrusted code, the read-only pool's database role is what refuses writes.
|
|
273
|
+
*/
|
|
274
|
+
export class DatabaseRef extends Queryable {
|
|
275
|
+
declare protected readonly impl: runtime.SQLDatabase;
|
|
276
|
+
|
|
277
|
+
constructor(impl: runtime.SQLDatabase) {
|
|
278
|
+
super(impl);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The connection string to connect to the database with another client.
|
|
283
|
+
* A write or admin handle's connects read-write. A read handle's connects
|
|
284
|
+
* as its statements run: through the database's read-only pool, as that
|
|
285
|
+
* pool's role, if they would run on it (from a read replica, unless
|
|
286
|
+
* `primary` is set), and otherwise to the primary. Either way its session
|
|
287
|
+
* is read-only (`default_transaction_read_only` is on), so a write fails
|
|
288
|
+
* with SQLSTATE 25006 until SQL on it turns that off; through the
|
|
289
|
+
* read-only pool, its role refuses writes still.
|
|
290
|
+
*/
|
|
291
|
+
get connectionString(): string {
|
|
292
|
+
return this.impl.connString();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async acquire(): Promise<Connection> {
|
|
296
|
+
return new Connection(await this.impl.acquire());
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async begin(): Promise<Transaction> {
|
|
300
|
+
return new Transaction(await this.impl.begin(activeCtxBytes()));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Constructing a new database object will result in Encore provisioning a database with
|
|
306
|
+
* that name and returning this object to represent it.
|
|
307
|
+
*
|
|
308
|
+
* If you want to reference an existing database, use `Database.named(name)` as it is a
|
|
309
|
+
* compile error to create duplicate databases.
|
|
310
|
+
*/
|
|
311
|
+
export class Database extends BaseQueryExecutor {
|
|
312
|
+
declare protected readonly impl: runtime.SQLDatabase;
|
|
313
|
+
/** The read handles, made on first use. */
|
|
314
|
+
private reader?: DatabaseRef;
|
|
315
|
+
private primaryReader?: DatabaseRef;
|
|
316
|
+
|
|
317
|
+
constructor(name: string, _cfg?: DatabaseConfig) {
|
|
318
|
+
super(runtime.RT.sqlDatabase(name));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Reference an existing database by name, if the database doesn't
|
|
323
|
+
* exist yet, use `new Database(name)` instead.
|
|
324
|
+
*/
|
|
325
|
+
static named<name extends string>(name: StringLiteral<name>): Database {
|
|
326
|
+
return new Database(name);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Select reader access. The handle runs on read-only connections, from a
|
|
331
|
+
* read replica if the database has a read-only pool on one and `primary`
|
|
332
|
+
* is not set, and its writes fail.
|
|
333
|
+
*/
|
|
334
|
+
read(options: ReadOptions = {}): DatabaseRef {
|
|
335
|
+
if (options.primary) {
|
|
336
|
+
return (this.primaryReader ??= this.ref(runtime.SQLAccess.ReadPrimary));
|
|
337
|
+
}
|
|
338
|
+
return (this.reader ??= this.ref(runtime.SQLAccess.Read));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Select reader and writer access. */
|
|
342
|
+
write(): DatabaseRef {
|
|
343
|
+
return new DatabaseRef(this.impl);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Select reader, writer, and admin access. */
|
|
347
|
+
admin(): DatabaseRef {
|
|
348
|
+
return new DatabaseRef(this.impl);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private ref(access: runtime.SQLAccess): DatabaseRef {
|
|
352
|
+
return new DatabaseRef(this.impl.withAccess(access));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Returns the connection string for the database, which connects
|
|
357
|
+
* read-write. A read handle's connects read-only: see
|
|
358
|
+
* {@link DatabaseRef.connectionString}.
|
|
359
|
+
*/
|
|
360
|
+
get connectionString(): string {
|
|
361
|
+
return this.impl.connString();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Acquires a database connection from the database pool.
|
|
366
|
+
*
|
|
367
|
+
* When the connection is closed or is garbage-collected, it is returned to the pool.
|
|
368
|
+
* @returns a new connection to the database
|
|
369
|
+
*/
|
|
370
|
+
async acquire(): Promise<Connection> {
|
|
371
|
+
const impl = await this.impl.acquire();
|
|
372
|
+
return new Connection(impl);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Begins a database transaction.
|
|
377
|
+
*
|
|
378
|
+
* Prefer the `await using` pattern, which automatically rolls back
|
|
379
|
+
* the transaction if neither `commit` nor `rollback` is called before
|
|
380
|
+
* the variable goes out of scope:
|
|
381
|
+
*
|
|
382
|
+
* ```ts
|
|
383
|
+
* await using tx = await db.begin();
|
|
384
|
+
* await tx.exec`INSERT INTO ...`;
|
|
385
|
+
* await tx.commit();
|
|
386
|
+
* ```
|
|
387
|
+
*
|
|
388
|
+
* If you can't use `await using`, make sure to always call `commit`
|
|
389
|
+
* or `rollback` yourself to prevent hanging transactions.
|
|
390
|
+
*
|
|
391
|
+
* @returns a transaction object that implements `AsyncDisposable`
|
|
392
|
+
*/
|
|
393
|
+
async begin(): Promise<Transaction> {
|
|
394
|
+
const source = activeCtxBytes();
|
|
395
|
+
const impl = await this.impl.begin(source);
|
|
396
|
+
return new Transaction(impl);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Represents a database transaction.
|
|
402
|
+
*
|
|
403
|
+
* `Transaction` implements `AsyncDisposable`, so the recommended usage is
|
|
404
|
+
* the `await using` pattern — it automatically rolls back the transaction
|
|
405
|
+
* if neither `commit` nor `rollback` is called before the variable goes
|
|
406
|
+
* out of scope:
|
|
407
|
+
*
|
|
408
|
+
* ```ts
|
|
409
|
+
* await using tx = await db.begin();
|
|
410
|
+
* await tx.exec`INSERT INTO ...`;
|
|
411
|
+
* await tx.commit();
|
|
412
|
+
* ```
|
|
413
|
+
*
|
|
414
|
+
* If you can't use `await using`, make sure to always call `commit` or
|
|
415
|
+
* `rollback` yourself to prevent hanging transactions.
|
|
416
|
+
*/
|
|
417
|
+
export class Transaction extends Queryable implements AsyncDisposable {
|
|
418
|
+
declare protected readonly impl: runtime.Transaction;
|
|
419
|
+
private done: boolean = false;
|
|
420
|
+
|
|
421
|
+
constructor(impl: runtime.Transaction) {
|
|
422
|
+
super(impl);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Commit the transaction.
|
|
427
|
+
*/
|
|
428
|
+
async commit() {
|
|
429
|
+
try {
|
|
430
|
+
await this.impl.commit();
|
|
431
|
+
} finally {
|
|
432
|
+
this.done = this.impl.isClosed();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Rollback the transaction.
|
|
438
|
+
*/
|
|
439
|
+
async rollback() {
|
|
440
|
+
try {
|
|
441
|
+
await this.impl.rollback();
|
|
442
|
+
} finally {
|
|
443
|
+
this.done = this.impl.isClosed();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
async [Symbol.asyncDispose]() {
|
|
448
|
+
if (!this.done) {
|
|
449
|
+
await this.rollback();
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Represents a dedicated connection to a database.
|
|
456
|
+
*/
|
|
457
|
+
export class Connection extends Queryable implements AsyncDisposable {
|
|
458
|
+
declare protected readonly impl: runtime.SQLConn;
|
|
459
|
+
|
|
460
|
+
constructor(impl: runtime.SQLConn) {
|
|
461
|
+
super(impl);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** Begin a transaction that exclusively borrows this connection until completion. */
|
|
465
|
+
async begin(): Promise<Transaction> {
|
|
466
|
+
return new Transaction(await this.impl.begin(activeCtxBytes()));
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
async [Symbol.asyncDispose]() {
|
|
470
|
+
await this.close();
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Returns the connection to the database pool.
|
|
475
|
+
*/
|
|
476
|
+
async close() {
|
|
477
|
+
await this.impl.close();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function buildQuery(strings: TemplateStringsArray, expr: Primitive[]): string {
|
|
482
|
+
let query = '';
|
|
483
|
+
for (let i = 0; i < strings.length; i++) {
|
|
484
|
+
query += strings[i];
|
|
485
|
+
|
|
486
|
+
if (i < expr.length) {
|
|
487
|
+
query += '$' + (i + 1);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return query;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function buildQueryArgs(params: Primitive[]): runtime.QueryArgs {
|
|
495
|
+
// Convert undefined to null.
|
|
496
|
+
return new runtime.QueryArgs(params.map(p => p ?? null));
|
|
497
|
+
}
|