@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
|
@@ -20,21 +20,22 @@ import {ConnTarget, PoolTarget, type Target} from './target';
|
|
|
20
20
|
import {encodeList, type Cmd, type Value} from './wire';
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
24
|
-
* fields must be literals.
|
|
23
|
+
* Configuration options for a Redis database. Encore reads this statically
|
|
24
|
+
* from the declaration, so all fields must be literals.
|
|
25
25
|
*/
|
|
26
26
|
export interface DatabaseConfig {
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
29
|
-
* database
|
|
28
|
+
* Whether data must survive restarts and never be evicted. When omitted or
|
|
29
|
+
* `false`, the database acts as a cache and its data may be lost or evicted.
|
|
30
30
|
*/
|
|
31
31
|
persistent?: boolean;
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* Tags that classify the database
|
|
35
|
-
*
|
|
36
|
-
* letters, digits, `_
|
|
37
|
-
* letter
|
|
34
|
+
* Tags that classify the database for policy evaluation, written as string
|
|
35
|
+
* literals (for example, `{data: "pci"}`). Keys and values may contain
|
|
36
|
+
* lowercase letters, digits, `_`, and `-`, up to 63 characters. Keys must
|
|
37
|
+
* start with a letter and cannot start with the reserved prefix `encore`.
|
|
38
|
+
* At most 32 tags are allowed.
|
|
38
39
|
*/
|
|
39
40
|
tags?: Record<string, string>;
|
|
40
41
|
}
|
|
@@ -48,11 +49,10 @@ function batchReplies(values: Value[]): Reply[] {
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
/**
|
|
51
|
-
* A handle to a {@link Database}
|
|
52
|
-
*
|
|
53
|
-
* code.
|
|
52
|
+
* A read-only handle to a {@link Database}, created by {@link Database.read}
|
|
53
|
+
* or {@link Database.readPrimary}. Safe to pass to other code.
|
|
54
54
|
*
|
|
55
|
-
* Its command groups
|
|
55
|
+
* Its command groups expose only read-only commands.
|
|
56
56
|
*/
|
|
57
57
|
// The command groups are assigned in the constructor; this declares them.
|
|
58
58
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unsafe-declaration-merging
|
|
@@ -63,24 +63,22 @@ export class ReadOnlyDatabaseRef {
|
|
|
63
63
|
constructor(
|
|
64
64
|
private readonly t: PoolTarget,
|
|
65
65
|
groups: ReadCommandGroups,
|
|
66
|
-
/**
|
|
67
|
-
* The runtime gives a database one pool for now, on the primary, which
|
|
68
|
-
* every handle shares.
|
|
69
|
-
*/
|
|
70
|
-
readonly primary: boolean,
|
|
71
66
|
) {
|
|
72
67
|
Object.assign(this, groups);
|
|
73
68
|
}
|
|
74
69
|
|
|
75
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* Prepares an invocation of `script`, which must have been created with
|
|
72
|
+
* {@link Script.readOnly}.
|
|
73
|
+
*/
|
|
76
74
|
script(script: Script): Invocation {
|
|
77
75
|
return new Invocation(this.t, script, true);
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
/**
|
|
81
|
-
*
|
|
82
|
-
* {@link DatabaseRef.command}
|
|
83
|
-
*
|
|
79
|
+
* Prepares an arbitrary Redis command that is not covered by the typed
|
|
80
|
+
* command groups, as described in {@link DatabaseRef.command}. Only commands
|
|
81
|
+
* that Redis marks as read-only are allowed.
|
|
84
82
|
*/
|
|
85
83
|
command<N extends string>(name: StringLiteral<N>): CommandInvocation {
|
|
86
84
|
return new CommandInvocation(this.t, rawCommand(name, true));
|
|
@@ -88,8 +86,8 @@ export class ReadOnlyDatabaseRef {
|
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
/**
|
|
91
|
-
* A handle to a {@link Database}
|
|
92
|
-
* {@link Database.write}.
|
|
89
|
+
* A read-write handle to a {@link Database}, created by
|
|
90
|
+
* {@link Database.write}. Safe to pass to other code.
|
|
93
91
|
*/
|
|
94
92
|
// The command groups are assigned in the constructor; this declares them.
|
|
95
93
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unsafe-declaration-merging
|
|
@@ -107,9 +105,9 @@ export class DatabaseRef {
|
|
|
107
105
|
}
|
|
108
106
|
|
|
109
107
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* the rest.
|
|
108
|
+
* Creates a pipeline that sends a batch of commands in a single round trip.
|
|
109
|
+
* A pipeline is not a transaction: commands from other clients may run
|
|
110
|
+
* between queued commands, and one failing command does not stop the rest.
|
|
113
111
|
*
|
|
114
112
|
* ```ts
|
|
115
113
|
* const pipe = SESSIONS.pipeline();
|
|
@@ -123,11 +121,11 @@ export class DatabaseRef {
|
|
|
123
121
|
}
|
|
124
122
|
|
|
125
123
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
124
|
+
* Creates a transaction (`MULTI`/`EXEC`) whose queued commands run together
|
|
125
|
+
* without commands from other clients interleaving between them.
|
|
128
126
|
*
|
|
129
|
-
* Nothing is sent until {@link Transaction.exec}, so a transaction
|
|
130
|
-
*
|
|
127
|
+
* Nothing is sent until {@link Transaction.exec} is called, so a transaction
|
|
128
|
+
* is never held open while your code awaits other work.
|
|
131
129
|
*
|
|
132
130
|
* ```ts
|
|
133
131
|
* const tx = LEDGER.transaction();
|
|
@@ -141,9 +139,9 @@ export class DatabaseRef {
|
|
|
141
139
|
}
|
|
142
140
|
|
|
143
141
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* {@link Connection.close}, or
|
|
142
|
+
* Acquires a dedicated connection from the pool for operations that must run
|
|
143
|
+
* on the same connection, such as watching keys before a transaction. Return
|
|
144
|
+
* it with {@link Connection.close}, or manage it with `await using`.
|
|
147
145
|
*/
|
|
148
146
|
async acquire(): Promise<Connection> {
|
|
149
147
|
const impl = await this.t.database().acquire(activeCtxBytes());
|
|
@@ -151,10 +149,10 @@ export class DatabaseRef {
|
|
|
151
149
|
}
|
|
152
150
|
|
|
153
151
|
/**
|
|
154
|
-
* Runs `body` as an optimistic transaction
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
152
|
+
* Runs `body` as an optimistic transaction watching `keys`. `body` reads
|
|
153
|
+
* values through the connection and queues writes on the transaction; the
|
|
154
|
+
* transaction commits only if none of `keys` changed in the meantime. If a
|
|
155
|
+
* watched key changes, `body` is retried.
|
|
158
156
|
*
|
|
159
157
|
* ```ts
|
|
160
158
|
* const left = await LEDGER.watch(["balance"], async (conn, tx) => {
|
|
@@ -181,44 +179,50 @@ export class DatabaseRef {
|
|
|
181
179
|
if ((await tx.exec()) !== null) return out;
|
|
182
180
|
}
|
|
183
181
|
throw new Error(
|
|
184
|
-
`redis:
|
|
182
|
+
`redis: watched keys kept changing; gave up after ${attempts} attempts`,
|
|
185
183
|
);
|
|
186
184
|
} finally {
|
|
187
185
|
await conn.close();
|
|
188
186
|
}
|
|
189
187
|
}
|
|
190
188
|
|
|
191
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* Prepares an invocation of `script` so you can attach its keys and
|
|
191
|
+
* arguments and then invoke it.
|
|
192
|
+
*/
|
|
192
193
|
script(script: Script): Invocation {
|
|
193
194
|
return new Invocation(this.t, script, false);
|
|
194
195
|
}
|
|
195
196
|
|
|
196
197
|
/**
|
|
197
|
-
*
|
|
198
|
-
*
|
|
198
|
+
* Prepares an arbitrary Redis command that is not covered by the typed
|
|
199
|
+
* command groups. Pass the command name as a string literal, add its
|
|
200
|
+
* arguments in order (marking keys with `key()`), and then call `invoke()`.
|
|
199
201
|
*
|
|
200
202
|
* ```ts
|
|
201
203
|
* const freq = await SESSIONS.command("OBJECT").arg("FREQ").key("hot").invoke().number();
|
|
202
204
|
* ```
|
|
203
205
|
*
|
|
204
|
-
* Encore reads the name to grant the access
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* database,
|
|
209
|
-
*
|
|
210
|
-
*
|
|
206
|
+
* Encore reads the command name statically to grant the required access.
|
|
207
|
+
* Before sending the command, the runtime checks it against Redis's command
|
|
208
|
+
* table and throws a `TypeError` if a required key argument was not passed
|
|
209
|
+
* with `key()` (which applies the database's key prefix), or if the command
|
|
210
|
+
* reaches outside the database, modifies a shared connection, or already has
|
|
211
|
+
* a dedicated API (`MULTI`, `SUBSCRIBE`, `EVAL`, `SCAN`, `FLUSHALL`, …).
|
|
212
|
+
* Unknown commands, such as module commands, are sent with their keys as
|
|
213
|
+
* marked.
|
|
211
214
|
*/
|
|
212
215
|
command<N extends string>(name: StringLiteral<N>): CommandInvocation {
|
|
213
216
|
return new CommandInvocation(this.t, rawCommand(name, false));
|
|
214
217
|
}
|
|
215
218
|
|
|
216
219
|
/**
|
|
217
|
-
* The connection string
|
|
218
|
-
*
|
|
220
|
+
* The connection string for connecting to this database from an external
|
|
221
|
+
* client.
|
|
219
222
|
*
|
|
220
|
-
*
|
|
221
|
-
*
|
|
223
|
+
* Note that external clients connect without the database's key prefix, so
|
|
224
|
+
* keys stored by an external client will not match keys read by this SDK
|
|
225
|
+
* under the same names.
|
|
222
226
|
*/
|
|
223
227
|
get connectionString(): string {
|
|
224
228
|
return this.t.database().connectionUrl();
|
|
@@ -226,9 +230,10 @@ export class DatabaseRef {
|
|
|
226
230
|
}
|
|
227
231
|
|
|
228
232
|
/**
|
|
229
|
-
* A Redis database
|
|
233
|
+
* A Redis database backed by a connection pool created on first use.
|
|
230
234
|
*
|
|
231
|
-
* Declare
|
|
235
|
+
* Declare databases at module scope and run commands through their command
|
|
236
|
+
* groups:
|
|
232
237
|
*
|
|
233
238
|
* ```ts
|
|
234
239
|
* import { Database } from "@encore.dev/sdk/storage/redis";
|
|
@@ -240,16 +245,17 @@ export class DatabaseRef {
|
|
|
240
245
|
* ```
|
|
241
246
|
*/
|
|
242
247
|
export class Database extends DatabaseRef {
|
|
243
|
-
private
|
|
248
|
+
private reader?: ReadOnlyDatabaseRef;
|
|
249
|
+
private primaryReader?: ReadOnlyDatabaseRef;
|
|
244
250
|
|
|
245
251
|
/**
|
|
246
252
|
* Declares a Redis database.
|
|
247
253
|
*
|
|
248
|
-
* Encore reads the name and
|
|
249
|
-
* must be literals
|
|
254
|
+
* Encore reads the name and configuration statically from the declaration, so
|
|
255
|
+
* both must be literals and the database must be declared at module scope.
|
|
250
256
|
*
|
|
251
|
-
* The name must be unique within the Encore application
|
|
252
|
-
* Once deployed, never
|
|
257
|
+
* The name must be unique within the Encore application and use kebab-case.
|
|
258
|
+
* Once deployed, never rename it — doing so creates a new database under the
|
|
253
259
|
* new name.
|
|
254
260
|
*/
|
|
255
261
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -262,45 +268,47 @@ export class Database extends DatabaseRef {
|
|
|
262
268
|
* References an existing database by name. To declare a new database, use
|
|
263
269
|
* `new Database(...)` instead.
|
|
264
270
|
*
|
|
265
|
-
* The name must be a string literal
|
|
266
|
-
*
|
|
271
|
+
* The name must be a string literal so Encore can resolve it statically. A
|
|
272
|
+
* referenced database takes no configuration; it is configured where it is
|
|
267
273
|
* declared.
|
|
268
274
|
*/
|
|
269
275
|
static named<Name extends string>(name: StringLiteral<Name>): Database {
|
|
270
276
|
return new Database(name);
|
|
271
277
|
}
|
|
272
278
|
|
|
273
|
-
/**
|
|
279
|
+
/** Returns a read-write handle to the database. */
|
|
274
280
|
write(): DatabaseRef {
|
|
275
281
|
return new DatabaseRef(this.t, this.groups);
|
|
276
282
|
}
|
|
277
283
|
|
|
278
284
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
285
|
+
* Returns a read-only handle to the database. Reads from a read replica when
|
|
286
|
+
* a read-only connection pool is configured, and from the primary server
|
|
287
|
+
* otherwise. Write commands are rejected before being sent. To always read
|
|
288
|
+
* from the primary server, use {@link Database.readPrimary}.
|
|
282
289
|
*/
|
|
283
290
|
read(): ReadOnlyDatabaseRef {
|
|
284
|
-
return
|
|
291
|
+
return (this.reader ??= readOnlyRef(this.t.reader(false)));
|
|
285
292
|
}
|
|
286
293
|
|
|
287
294
|
/**
|
|
288
|
-
*
|
|
289
|
-
* server even
|
|
295
|
+
* Returns a read-only handle to the database that always reads from the
|
|
296
|
+
* primary server, even when read replicas are available. Write commands are
|
|
297
|
+
* rejected before being sent.
|
|
290
298
|
*/
|
|
291
299
|
readPrimary(): ReadOnlyDatabaseRef {
|
|
292
|
-
return
|
|
300
|
+
return (this.primaryReader ??= readOnlyRef(this.t.reader(true)));
|
|
293
301
|
}
|
|
302
|
+
}
|
|
294
303
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
}
|
|
304
|
+
function readOnlyRef(t: PoolTarget): ReadOnlyDatabaseRef {
|
|
305
|
+
return new ReadOnlyDatabaseRef(t, readCommandGroups(t));
|
|
298
306
|
}
|
|
299
307
|
|
|
300
308
|
/**
|
|
301
|
-
* A batch of commands sent in
|
|
302
|
-
* {@link DatabaseRef.pipeline}. Not a transaction.
|
|
303
|
-
*
|
|
309
|
+
* A batch of commands sent in a single round trip, created by
|
|
310
|
+
* {@link DatabaseRef.pipeline}. Not a transaction. Queue commands through its
|
|
311
|
+
* command groups (for example, `pipe.string.get(...)`).
|
|
304
312
|
*/
|
|
305
313
|
// The command groups are assigned in the constructor; this declares them.
|
|
306
314
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unsafe-declaration-merging
|
|
@@ -314,30 +322,32 @@ export class Pipeline {
|
|
|
314
322
|
Object.assign(this, queueGroups(this.queued));
|
|
315
323
|
}
|
|
316
324
|
|
|
317
|
-
/**
|
|
325
|
+
/** The number of commands currently queued. */
|
|
318
326
|
get length(): number {
|
|
319
327
|
return this.queued.length;
|
|
320
328
|
}
|
|
321
329
|
|
|
322
|
-
/**
|
|
330
|
+
/**
|
|
331
|
+
* Prepares a call to `script` to be queued on this pipeline. Call
|
|
332
|
+
* {@link QueuedInvocation.queue} to append it.
|
|
333
|
+
*/
|
|
323
334
|
script(script: Script): QueuedInvocation {
|
|
324
335
|
return new QueuedInvocation(this.queued, script);
|
|
325
336
|
}
|
|
326
337
|
|
|
327
338
|
/**
|
|
328
|
-
*
|
|
329
|
-
* {@link
|
|
330
|
-
*
|
|
331
|
-
* queued: the batch fails.
|
|
339
|
+
* Prepares an arbitrary Redis command (see {@link DatabaseRef.command}) to
|
|
340
|
+
* be queued on this pipeline. Call {@link QueuedCommandInvocation.queue} to
|
|
341
|
+
* append it. Blocking commands cannot be queued and will fail the batch.
|
|
332
342
|
*/
|
|
333
343
|
command<N extends string>(name: StringLiteral<N>): QueuedCommandInvocation {
|
|
334
344
|
return new QueuedCommandInvocation(this.queued, rawCommand(name, false));
|
|
335
345
|
}
|
|
336
346
|
|
|
337
347
|
/**
|
|
338
|
-
* Sends
|
|
339
|
-
*
|
|
340
|
-
*
|
|
348
|
+
* Sends all queued commands and returns their replies in order. If the server
|
|
349
|
+
* rejects any command, throws a {@link BatchError} containing all replies.
|
|
350
|
+
* Clears the pipeline afterwards.
|
|
341
351
|
*/
|
|
342
352
|
async exec(): Promise<Reply[]> {
|
|
343
353
|
const values = await this.t.runAll(this.queued.splice(0), false);
|
|
@@ -346,9 +356,9 @@ export class Pipeline {
|
|
|
346
356
|
}
|
|
347
357
|
|
|
348
358
|
/**
|
|
349
|
-
* A transaction
|
|
350
|
-
*
|
|
351
|
-
* between.
|
|
359
|
+
* A transaction created by {@link DatabaseRef.transaction}. Commands queued
|
|
360
|
+
* through its command groups execute together without commands from other
|
|
361
|
+
* clients interleaving between them.
|
|
352
362
|
*/
|
|
353
363
|
// The command groups are assigned in the constructor; this declares them.
|
|
354
364
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unsafe-declaration-merging
|
|
@@ -362,30 +372,33 @@ export class Transaction {
|
|
|
362
372
|
Object.assign(this, queueGroups(this.queued));
|
|
363
373
|
}
|
|
364
374
|
|
|
365
|
-
/**
|
|
375
|
+
/** The number of commands currently queued. */
|
|
366
376
|
get length(): number {
|
|
367
377
|
return this.queued.length;
|
|
368
378
|
}
|
|
369
379
|
|
|
370
|
-
/**
|
|
380
|
+
/**
|
|
381
|
+
* Prepares a call to `script` to be queued on this transaction. Call
|
|
382
|
+
* {@link QueuedInvocation.queue} to append it.
|
|
383
|
+
*/
|
|
371
384
|
script(script: Script): QueuedInvocation {
|
|
372
385
|
return new QueuedInvocation(this.queued, script);
|
|
373
386
|
}
|
|
374
387
|
|
|
375
388
|
/**
|
|
376
|
-
*
|
|
377
|
-
* {@link
|
|
378
|
-
*
|
|
379
|
-
* queued: the batch fails.
|
|
389
|
+
* Prepares an arbitrary Redis command (see {@link DatabaseRef.command}) to
|
|
390
|
+
* be queued on this transaction. Call {@link QueuedCommandInvocation.queue}
|
|
391
|
+
* to append it. Blocking commands cannot be queued and will fail the batch.
|
|
380
392
|
*/
|
|
381
393
|
command<N extends string>(name: StringLiteral<N>): QueuedCommandInvocation {
|
|
382
394
|
return new QueuedCommandInvocation(this.queued, rawCommand(name, false));
|
|
383
395
|
}
|
|
384
396
|
|
|
385
397
|
/**
|
|
386
|
-
* Runs
|
|
387
|
-
* {@link Pipeline.exec}
|
|
388
|
-
*
|
|
398
|
+
* Runs all queued commands as a single transaction and returns their replies,
|
|
399
|
+
* matching {@link Pipeline.exec}. If the server rejects a command during
|
|
400
|
+
* execution, the remaining commands still run — Redis does not support
|
|
401
|
+
* rollbacks.
|
|
389
402
|
*/
|
|
390
403
|
async exec(): Promise<Reply[]> {
|
|
391
404
|
const values = await this.t.runAll(this.queued.splice(0), true);
|
|
@@ -393,8 +406,8 @@ export class Transaction {
|
|
|
393
406
|
}
|
|
394
407
|
|
|
395
408
|
/**
|
|
396
|
-
*
|
|
397
|
-
*
|
|
409
|
+
* Discards the transaction by clearing its queue. Because nothing has been
|
|
410
|
+
* sent yet, there is nothing to undo on the server.
|
|
398
411
|
*/
|
|
399
412
|
discard(): void {
|
|
400
413
|
this.queued.splice(0);
|
|
@@ -402,8 +415,9 @@ export class Transaction {
|
|
|
402
415
|
}
|
|
403
416
|
|
|
404
417
|
/**
|
|
405
|
-
* A transaction on a {@link Connection},
|
|
406
|
-
*
|
|
418
|
+
* A transaction on a {@link Connection}, created by
|
|
419
|
+
* {@link Connection.transaction}. It aborts without running if any key watched
|
|
420
|
+
* by the connection has changed.
|
|
407
421
|
*/
|
|
408
422
|
// The command groups are assigned in the constructor; this declares them.
|
|
409
423
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unsafe-declaration-merging
|
|
@@ -417,31 +431,33 @@ export class WatchedTransaction {
|
|
|
417
431
|
Object.assign(this, queueGroups(this.queued));
|
|
418
432
|
}
|
|
419
433
|
|
|
420
|
-
/**
|
|
434
|
+
/** The number of commands currently queued. */
|
|
421
435
|
get length(): number {
|
|
422
436
|
return this.queued.length;
|
|
423
437
|
}
|
|
424
438
|
|
|
425
|
-
/**
|
|
439
|
+
/**
|
|
440
|
+
* Prepares a call to `script` to be queued on this transaction. Call
|
|
441
|
+
* {@link QueuedInvocation.queue} to append it.
|
|
442
|
+
*/
|
|
426
443
|
script(script: Script): QueuedInvocation {
|
|
427
444
|
return new QueuedInvocation(this.queued, script);
|
|
428
445
|
}
|
|
429
446
|
|
|
430
447
|
/**
|
|
431
|
-
*
|
|
432
|
-
* {@link
|
|
433
|
-
*
|
|
434
|
-
* queued: the batch fails.
|
|
448
|
+
* Prepares an arbitrary Redis command (see {@link DatabaseRef.command}) to
|
|
449
|
+
* be queued on this transaction. Call {@link QueuedCommandInvocation.queue}
|
|
450
|
+
* to append it. Blocking commands cannot be queued and will fail the batch.
|
|
435
451
|
*/
|
|
436
452
|
command<N extends string>(name: StringLiteral<N>): QueuedCommandInvocation {
|
|
437
453
|
return new QueuedCommandInvocation(this.queued, rawCommand(name, false));
|
|
438
454
|
}
|
|
439
455
|
|
|
440
456
|
/**
|
|
441
|
-
* Runs
|
|
442
|
-
* {@link Transaction.exec}
|
|
443
|
-
* which case none of
|
|
444
|
-
* afterwards.
|
|
457
|
+
* Runs all queued commands as a single transaction and returns their replies
|
|
458
|
+
* (as in {@link Transaction.exec}), or `null` if a watched key changed (in
|
|
459
|
+
* which case none of the commands ran). Either way, all watches on the
|
|
460
|
+
* connection are cleared afterwards.
|
|
445
461
|
*/
|
|
446
462
|
async exec(): Promise<Reply[] | null> {
|
|
447
463
|
const values = await this.t.runAll(this.queued.splice(0), true);
|
|
@@ -450,9 +466,9 @@ export class WatchedTransaction {
|
|
|
450
466
|
}
|
|
451
467
|
|
|
452
468
|
/**
|
|
453
|
-
* A connection
|
|
454
|
-
* {@link DatabaseRef.acquire}.
|
|
455
|
-
*
|
|
469
|
+
* A dedicated connection acquired from a database's pool via
|
|
470
|
+
* {@link DatabaseRef.acquire}. Commands run sequentially through its command
|
|
471
|
+
* groups. Return it to the pool with {@link Connection.close}.
|
|
456
472
|
*/
|
|
457
473
|
// The command groups are assigned in the constructor; this declares them.
|
|
458
474
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type, @typescript-eslint/no-unsafe-declaration-merging
|
|
@@ -471,44 +487,50 @@ export class Connection implements AsyncDisposable {
|
|
|
471
487
|
}
|
|
472
488
|
|
|
473
489
|
/**
|
|
474
|
-
* Watches `keys
|
|
475
|
-
*
|
|
490
|
+
* Watches `keys` for changes. A subsequent {@link Connection.transaction}
|
|
491
|
+
* will abort without running if any watched key changes before `exec()`.
|
|
476
492
|
*/
|
|
477
493
|
async watch(...keys: string[]): Promise<void> {
|
|
478
494
|
await this.impl.watch(encodeList(keys), activeCtxBytes());
|
|
479
495
|
}
|
|
480
496
|
|
|
481
|
-
/** Stops watching
|
|
497
|
+
/** Stops watching all keys on this connection. */
|
|
482
498
|
async unwatch(): Promise<void> {
|
|
483
499
|
await this.impl.unwatch(activeCtxBytes());
|
|
484
500
|
}
|
|
485
501
|
|
|
486
|
-
/**
|
|
502
|
+
/**
|
|
503
|
+
* Creates a transaction on this connection that depends on its watched keys.
|
|
504
|
+
*/
|
|
487
505
|
transaction(): WatchedTransaction {
|
|
488
506
|
return new WatchedTransaction(this.t);
|
|
489
507
|
}
|
|
490
508
|
|
|
491
|
-
/**
|
|
509
|
+
/**
|
|
510
|
+
* Creates a pipeline that sends a batch of commands in a single round trip
|
|
511
|
+
* on this connection.
|
|
512
|
+
*/
|
|
492
513
|
pipeline(): Pipeline {
|
|
493
514
|
return new Pipeline(this.t);
|
|
494
515
|
}
|
|
495
516
|
|
|
496
|
-
/**
|
|
517
|
+
/** Prepares an invocation of `script` on this connection. */
|
|
497
518
|
script(script: Script): Invocation {
|
|
498
519
|
return new Invocation(this.t, script, false);
|
|
499
520
|
}
|
|
500
521
|
|
|
501
522
|
/**
|
|
502
|
-
*
|
|
503
|
-
* {@link DatabaseRef.command}.
|
|
523
|
+
* Prepares an arbitrary Redis command on this connection (see
|
|
524
|
+
* {@link DatabaseRef.command}). Blocking commands are not allowed on an
|
|
525
|
+
* acquired connection.
|
|
504
526
|
*/
|
|
505
527
|
command<N extends string>(name: StringLiteral<N>): CommandInvocation {
|
|
506
528
|
return new CommandInvocation(this.t, rawCommand(name, false));
|
|
507
529
|
}
|
|
508
530
|
|
|
509
531
|
/**
|
|
510
|
-
* Returns the connection to its pool
|
|
511
|
-
*
|
|
532
|
+
* Returns the connection to its pool, automatically unwatching any keys that
|
|
533
|
+
* are still watched.
|
|
512
534
|
*/
|
|
513
535
|
async close(): Promise<void> {
|
|
514
536
|
await this.impl.release();
|