@encore.dev/sdk 2.0.0-beta.15 → 2.0.0-beta.17

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.
Files changed (235) hide show
  1. package/README.md +111 -101
  2. package/app_meta.ts +56 -54
  3. package/config/config.ts +60 -57
  4. package/config/secrets.ts +45 -52
  5. package/config/value.ts +23 -26
  6. package/cron/mod.ts +28 -29
  7. package/dist/app_meta.d.ts +52 -50
  8. package/dist/app_meta.d.ts.map +1 -1
  9. package/dist/app_meta.js +2 -2
  10. package/dist/app_meta.js.map +1 -1
  11. package/dist/config/config.d.ts +58 -55
  12. package/dist/config/config.d.ts.map +1 -1
  13. package/dist/config/config.js +8 -9
  14. package/dist/config/config.js.map +1 -1
  15. package/dist/config/secrets.d.ts +45 -52
  16. package/dist/config/secrets.d.ts.map +1 -1
  17. package/dist/config/secrets.js +8 -9
  18. package/dist/config/secrets.js.map +1 -1
  19. package/dist/config/value.d.ts +20 -23
  20. package/dist/config/value.d.ts.map +1 -1
  21. package/dist/config/value.js +20 -23
  22. package/dist/config/value.js.map +1 -1
  23. package/dist/cron/mod.d.ts +26 -27
  24. package/dist/cron/mod.d.ts.map +1 -1
  25. package/dist/cron/mod.js +13 -14
  26. package/dist/cron/mod.js.map +1 -1
  27. package/dist/frontends/mod.d.ts +20 -19
  28. package/dist/frontends/mod.d.ts.map +1 -1
  29. package/dist/frontends/mod.js +20 -19
  30. package/dist/frontends/mod.js.map +1 -1
  31. package/dist/gateway/errors.d.ts +28 -30
  32. package/dist/gateway/errors.d.ts.map +1 -1
  33. package/dist/gateway/errors.js +29 -31
  34. package/dist/gateway/errors.js.map +1 -1
  35. package/dist/gateway/match.d.ts +54 -54
  36. package/dist/gateway/match.d.ts.map +1 -1
  37. package/dist/gateway/match.js +56 -56
  38. package/dist/gateway/match.js.map +1 -1
  39. package/dist/gateway/middleware.d.ts +62 -64
  40. package/dist/gateway/middleware.d.ts.map +1 -1
  41. package/dist/gateway/middleware.js +1 -1
  42. package/dist/gateway/middleware.js.map +1 -1
  43. package/dist/gateway/mod.d.ts +64 -67
  44. package/dist/gateway/mod.d.ts.map +1 -1
  45. package/dist/gateway/mod.js +72 -76
  46. package/dist/gateway/mod.js.map +1 -1
  47. package/dist/gateway/request.d.ts +138 -134
  48. package/dist/gateway/request.d.ts.map +1 -1
  49. package/dist/gateway/request.js +117 -108
  50. package/dist/gateway/request.js.map +1 -1
  51. package/dist/gateway/serve.d.ts +29 -34
  52. package/dist/gateway/serve.d.ts.map +1 -1
  53. package/dist/gateway/serve.js +51 -56
  54. package/dist/gateway/serve.js.map +1 -1
  55. package/dist/gateways/mod.d.ts +23 -21
  56. package/dist/gateways/mod.d.ts.map +1 -1
  57. package/dist/gateways/mod.js +25 -23
  58. package/dist/gateways/mod.js.map +1 -1
  59. package/dist/internal/runtime/napi/napi.cjs +1 -1
  60. package/dist/internal/runtime/napi/napi.d.cts +10 -3
  61. package/dist/internal/runtime/shutdown.d.ts +75 -57
  62. package/dist/internal/runtime/shutdown.d.ts.map +1 -1
  63. package/dist/internal/runtime/shutdown.js +88 -84
  64. package/dist/internal/runtime/shutdown.js.map +1 -1
  65. package/dist/log/logger.d.ts +43 -37
  66. package/dist/log/logger.d.ts.map +1 -1
  67. package/dist/log/logger.js +26 -25
  68. package/dist/log/logger.js.map +1 -1
  69. package/dist/metrics/mod.d.ts +133 -114
  70. package/dist/metrics/mod.d.ts.map +1 -1
  71. package/dist/metrics/mod.js +109 -101
  72. package/dist/metrics/mod.js.map +1 -1
  73. package/dist/otel.d.ts +6 -3
  74. package/dist/otel.d.ts.map +1 -1
  75. package/dist/otel.js +6 -3
  76. package/dist/otel.js.map +1 -1
  77. package/dist/pubsub/subscription.d.ts +88 -116
  78. package/dist/pubsub/subscription.d.ts.map +1 -1
  79. package/dist/pubsub/subscription.js +14 -17
  80. package/dist/pubsub/subscription.js.map +1 -1
  81. package/dist/pubsub/topic.d.ts +86 -113
  82. package/dist/pubsub/topic.d.ts.map +1 -1
  83. package/dist/pubsub/topic.js +68 -82
  84. package/dist/pubsub/topic.js.map +1 -1
  85. package/dist/reqctx/mod.d.ts +83 -86
  86. package/dist/reqctx/mod.d.ts.map +1 -1
  87. package/dist/reqctx/mod.js +39 -41
  88. package/dist/reqctx/mod.js.map +1 -1
  89. package/dist/services/index.d.ts +76 -75
  90. package/dist/services/index.d.ts.map +1 -1
  91. package/dist/services/index.js +73 -71
  92. package/dist/services/index.js.map +1 -1
  93. package/dist/shutdown/mod.d.ts +26 -25
  94. package/dist/shutdown/mod.d.ts.map +1 -1
  95. package/dist/shutdown/mod.js +26 -25
  96. package/dist/shutdown/mod.js.map +1 -1
  97. package/dist/storage/clickhouse/database.d.ts +113 -115
  98. package/dist/storage/clickhouse/database.d.ts.map +1 -1
  99. package/dist/storage/clickhouse/database.js +65 -79
  100. package/dist/storage/clickhouse/database.js.map +1 -1
  101. package/dist/storage/clickhouse/errors.d.ts +25 -26
  102. package/dist/storage/clickhouse/errors.d.ts.map +1 -1
  103. package/dist/storage/clickhouse/errors.js +15 -17
  104. package/dist/storage/clickhouse/errors.js.map +1 -1
  105. package/dist/storage/clickhouse/mod.d.ts +10 -10
  106. package/dist/storage/clickhouse/mod.js +10 -10
  107. package/dist/storage/objects/bucket.d.ts +196 -225
  108. package/dist/storage/objects/bucket.d.ts.map +1 -1
  109. package/dist/storage/objects/bucket.js +71 -84
  110. package/dist/storage/objects/bucket.js.map +1 -1
  111. package/dist/storage/objects/error.d.ts +4 -4
  112. package/dist/storage/objects/error.js +4 -4
  113. package/dist/storage/postgres/database.d.ts +124 -149
  114. package/dist/storage/postgres/database.d.ts.map +1 -1
  115. package/dist/storage/postgres/database.js +98 -135
  116. package/dist/storage/postgres/database.js.map +1 -1
  117. package/dist/storage/postgres/errors.d.ts +42 -48
  118. package/dist/storage/postgres/errors.d.ts.map +1 -1
  119. package/dist/storage/postgres/errors.js +44 -51
  120. package/dist/storage/postgres/errors.js.map +1 -1
  121. package/dist/storage/postgres/interval.d.ts +19 -19
  122. package/dist/storage/postgres/interval.d.ts.map +1 -1
  123. package/dist/storage/postgres/interval.js +21 -21
  124. package/dist/storage/postgres/interval.js.map +1 -1
  125. package/dist/storage/redis/bitfield.d.ts +48 -23
  126. package/dist/storage/redis/bitfield.d.ts.map +1 -1
  127. package/dist/storage/redis/bitfield.js +38 -19
  128. package/dist/storage/redis/bitfield.js.map +1 -1
  129. package/dist/storage/redis/command.d.ts +16 -12
  130. package/dist/storage/redis/command.d.ts.map +1 -1
  131. package/dist/storage/redis/command.js +16 -12
  132. package/dist/storage/redis/command.js.map +1 -1
  133. package/dist/storage/redis/commands.d.ts +99 -100
  134. package/dist/storage/redis/commands.d.ts.map +1 -1
  135. package/dist/storage/redis/commands.js +84 -85
  136. package/dist/storage/redis/commands.js.map +1 -1
  137. package/dist/storage/redis/database.d.ts +138 -122
  138. package/dist/storage/redis/database.d.ts.map +1 -1
  139. package/dist/storage/redis/database.js +120 -101
  140. package/dist/storage/redis/database.js.map +1 -1
  141. package/dist/storage/redis/mod.d.ts +52 -48
  142. package/dist/storage/redis/mod.d.ts.map +1 -1
  143. package/dist/storage/redis/mod.js +52 -48
  144. package/dist/storage/redis/mod.js.map +1 -1
  145. package/dist/storage/redis/options.d.ts +26 -24
  146. package/dist/storage/redis/options.d.ts.map +1 -1
  147. package/dist/storage/redis/options.js.map +1 -1
  148. package/dist/storage/redis/pubsub.d.ts +16 -16
  149. package/dist/storage/redis/pubsub.d.ts.map +1 -1
  150. package/dist/storage/redis/pubsub.js +14 -14
  151. package/dist/storage/redis/pubsub.js.map +1 -1
  152. package/dist/storage/redis/reply.d.ts +80 -72
  153. package/dist/storage/redis/reply.d.ts.map +1 -1
  154. package/dist/storage/redis/reply.js +53 -46
  155. package/dist/storage/redis/reply.js.map +1 -1
  156. package/dist/storage/redis/scan.d.ts +4 -4
  157. package/dist/storage/redis/scan.d.ts.map +1 -1
  158. package/dist/storage/redis/scan.js +6 -6
  159. package/dist/storage/redis/scan.js.map +1 -1
  160. package/dist/storage/redis/script.d.ts +28 -21
  161. package/dist/storage/redis/script.d.ts.map +1 -1
  162. package/dist/storage/redis/script.js +29 -22
  163. package/dist/storage/redis/script.js.map +1 -1
  164. package/dist/storage/redis/target.d.ts +17 -1
  165. package/dist/storage/redis/target.d.ts.map +1 -1
  166. package/dist/storage/redis/target.js +30 -1
  167. package/dist/storage/redis/target.js.map +1 -1
  168. package/dist/storage/redis/wire.d.ts +6 -6
  169. package/dist/storage/redis/wire.d.ts.map +1 -1
  170. package/dist/storage/redis/wire.js +6 -6
  171. package/dist/storage/redis/wire.js.map +1 -1
  172. package/dist/testing/mod.d.ts +83 -88
  173. package/dist/testing/mod.d.ts.map +1 -1
  174. package/dist/testing/mod.js +75 -81
  175. package/dist/testing/mod.js.map +1 -1
  176. package/dist/testing/playwright.d.ts +28 -32
  177. package/dist/testing/playwright.d.ts.map +1 -1
  178. package/dist/testing/playwright.js +22 -27
  179. package/dist/testing/playwright.js.map +1 -1
  180. package/dist/testing/puppeteer.d.ts +11 -12
  181. package/dist/testing/puppeteer.d.ts.map +1 -1
  182. package/dist/testing/puppeteer.js +17 -18
  183. package/dist/testing/puppeteer.js.map +1 -1
  184. package/dist/testing/system.d.ts +72 -90
  185. package/dist/testing/system.d.ts.map +1 -1
  186. package/dist/testing/system.js +90 -107
  187. package/dist/testing/system.js.map +1 -1
  188. package/dist/tsconfig.build.tsbuildinfo +1 -1
  189. package/frontends/mod.test.ts +6 -2
  190. package/frontends/mod.ts +20 -19
  191. package/gateway/errors.ts +30 -32
  192. package/gateway/match.ts +61 -57
  193. package/gateway/middleware.ts +62 -64
  194. package/gateway/mod.ts +73 -76
  195. package/gateway/request.ts +143 -140
  196. package/gateway/serve.ts +58 -63
  197. package/gateways/mod.ts +25 -23
  198. package/internal/runtime/napi/napi.cjs +1 -1
  199. package/internal/runtime/napi/napi.d.cts +10 -3
  200. package/internal/runtime/shutdown.ts +113 -101
  201. package/log/logger.test.ts +8 -0
  202. package/log/logger.ts +48 -45
  203. package/metrics/mod.ts +138 -122
  204. package/otel.ts +6 -3
  205. package/package.json +1 -1
  206. package/pubsub/subscription.ts +93 -123
  207. package/pubsub/topic.ts +96 -130
  208. package/reqctx/mod.ts +83 -86
  209. package/services/index.ts +78 -78
  210. package/shutdown/mod.ts +26 -25
  211. package/storage/clickhouse/database.ts +115 -129
  212. package/storage/clickhouse/errors.ts +27 -29
  213. package/storage/clickhouse/mod.ts +10 -10
  214. package/storage/objects/bucket.ts +209 -239
  215. package/storage/objects/error.ts +4 -4
  216. package/storage/postgres/database.ts +131 -164
  217. package/storage/postgres/errors.ts +45 -52
  218. package/storage/postgres/interval.ts +21 -21
  219. package/storage/redis/bitfield.ts +51 -26
  220. package/storage/redis/command.ts +16 -12
  221. package/storage/redis/commands.ts +99 -100
  222. package/storage/redis/database.ts +145 -123
  223. package/storage/redis/mod.ts +52 -48
  224. package/storage/redis/options.ts +26 -24
  225. package/storage/redis/pubsub.ts +14 -14
  226. package/storage/redis/redis.test.ts +17 -2
  227. package/storage/redis/reply.ts +85 -77
  228. package/storage/redis/scan.ts +6 -6
  229. package/storage/redis/script.ts +29 -22
  230. package/storage/redis/target.ts +34 -1
  231. package/storage/redis/wire.ts +8 -8
  232. package/testing/mod.ts +99 -105
  233. package/testing/playwright.ts +32 -37
  234. package/testing/puppeteer.ts +22 -23
  235. package/testing/system.ts +117 -133
@@ -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
- * A database's configuration. Encore reads it from the declaration, so its
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
- * The data must survive a restart, and is never evicted. Without it a
29
- * database is a cache: what it holds may be lost or evicted.
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, for policies to read: written out as
35
- * string literals, as in `{data: "pci"}`. Keys and values are lowercase
36
- * letters, digits, `_` and `-`, at most 63 characters; a key starts with a
37
- * letter, and not with `encore`, which is reserved. At most 32 tags.
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} that can only read, made by
52
- * {@link Database.read} or {@link Database.readPrimary}. Free to pass to other
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 hold the commands that read, and nothing else.
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
- /** A call to `script`, which must be one made by {@link Script.readOnly}. */
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
- * A command the command groups lack, by its name, as
82
- * {@link DatabaseRef.command} sends one. Only a command Redis says doesn't
83
- * write is sent.
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} that can read and write, made by
92
- * {@link Database.write}. Free to pass to other code.
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
- * A batch of commands sent in one round trip. Not a transaction: other
111
- * clients' commands may run between them, and one failing does not stop
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
- * A transaction (`MULTI`/`EXEC`): the commands queued on it run together,
127
- * with no other client's in between.
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 is never
130
- * left open while the code waits on something else.
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
- * Takes a connection out of the pool, for what must run on one connection:
145
- * watching keys, then a transaction that depends on them. Return it with
146
- * {@link Connection.close}, or declare it with `await using`.
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 on `keys`: `body` reads what it
155
- * needs through the connection and queues its changes on the transaction,
156
- * and the changes are applied only if none of `keys` changed in between. If
157
- * one did, `body` runs again.
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: the watched keys kept changing: gave up after ${attempts} attempts`,
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
- /** A call to `script`, to give its keys and arguments and then invoke. */
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
- * A command the command groups lack, by its name, which must be a string
198
- * literal: give its arguments in order, marking the keys, then invoke it.
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 the command needs. Before it is
205
- * sent, the runtime checks it against Redis's command table, and fails it
206
- * with a `TypeError` if a key it takes isn't passed with `key`, which gives
207
- * it the database's prefix, or if it is one that would reach past the
208
- * database, change a connection other calls share, or has a call of its own
209
- * here (`MULTI`, `SUBSCRIBE`, `EVAL`, `SCAN`, `FLUSHALL`, …). A command the
210
- * table doesn't know, such as a module's, is sent with its keys as marked.
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 to connect to this database with, for a client
218
- * other than this SDK.
220
+ * The connection string for connecting to this database from an external
221
+ * client.
219
222
  *
220
- * It connects past the database's key prefix: what that client stores under
221
- * a name is not what this SDK reads under it.
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, backed by a connection pool built on first use.
233
+ * A Redis database backed by a connection pool created on first use.
230
234
  *
231
- * Declare it at module scope; its commands are in its command groups:
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 readGroups?: ReadCommandGroups;
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 the configuration from the declaration, so both
249
- * must be literals, and the database must be declared at module scope.
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, and be kebab-case.
252
- * Once deployed, never change it: a new database would be created under the
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, so Encore can resolve it statically. A
266
- * named database takes no configuration: it is configured where it is
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
- /** A handle to the database that can read and write. */
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
- * A handle to the database that can only read, which may connect to a read
280
- * replica. To always read from the primary server, use
281
- * {@link Database.readPrimary}.
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 new ReadOnlyDatabaseRef(this.t, this.readOnlyGroups(), false);
291
+ return (this.reader ??= readOnlyRef(this.t.reader(false)));
285
292
  }
286
293
 
287
294
  /**
288
- * A handle to the database that can only read, and connects to the primary
289
- * server even if there are read replicas available.
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 new ReadOnlyDatabaseRef(this.t, this.readOnlyGroups(), true);
300
+ return (this.primaryReader ??= readOnlyRef(this.t.reader(true)));
293
301
  }
302
+ }
294
303
 
295
- private readOnlyGroups(): ReadCommandGroups {
296
- return (this.readGroups ??= readCommandGroups(this.t));
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 one round trip, made by
302
- * {@link DatabaseRef.pipeline}. Not a transaction. Its commands are queued
303
- * through its command groups: `pipe.string.get(...)`.
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
- /** How many commands are queued. */
325
+ /** The number of commands currently queued. */
318
326
  get length(): number {
319
327
  return this.queued.length;
320
328
  }
321
329
 
322
- /** Queues a call to `script`. Finish it with {@link QueuedInvocation.queue}. */
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
- * Queues a command the command groups lack, by its name: see
329
- * {@link DatabaseRef.command}. Finish it with
330
- * {@link QueuedCommandInvocation.queue}. A command that blocks can't be
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 the queued commands and returns their replies, in order. If the
339
- * server rejected any, fails with a {@link BatchError} holding them all.
340
- * The pipeline is empty afterwards.
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, made by {@link DatabaseRef.transaction}: the commands queued
350
- * on it, through its command groups, run together, with no other client's in
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
- /** How many commands are queued. */
375
+ /** The number of commands currently queued. */
366
376
  get length(): number {
367
377
  return this.queued.length;
368
378
  }
369
379
 
370
- /** Queues a call to `script`. Finish it with {@link QueuedInvocation.queue}. */
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
- * Queues a command the command groups lack, by its name: see
377
- * {@link DatabaseRef.command}. Finish it with
378
- * {@link QueuedCommandInvocation.queue}. A command that blocks can't be
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 the queued commands as one transaction and returns their replies, as
387
- * {@link Pipeline.exec} does. A command the server rejects when it runs does
388
- * not stop the others: Redis has no rollback.
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
- * Abandons the transaction. Nothing was sent, so there is nothing to undo:
397
- * this empties the queue.
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}, made by {@link Connection.transaction}:
406
- * it does nothing if a key the connection watches has changed.
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
- /** How many commands are queued. */
434
+ /** The number of commands currently queued. */
421
435
  get length(): number {
422
436
  return this.queued.length;
423
437
  }
424
438
 
425
- /** Queues a call to `script`. Finish it with {@link QueuedInvocation.queue}. */
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
- * Queues a command the command groups lack, by its name: see
432
- * {@link DatabaseRef.command}. Finish it with
433
- * {@link QueuedCommandInvocation.queue}. A command that blocks can't be
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 the queued commands as one transaction and returns their replies, as
442
- * {@link Transaction.exec} does, or `null` if a watched key changed, in
443
- * which case none of them ran. Either way the connection watches nothing
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 taken out of a database's pool, made by
454
- * {@link DatabaseRef.acquire}. Its commands, in its command groups, run one at
455
- * a time. Return it to the pool with {@link Connection.close}.
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`: a {@link Connection.transaction} then does nothing if any
475
- * of them has changed by the time it is run.
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 every key. */
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
- /** A transaction on this connection, which depends on the keys it watches. */
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
- /** A batch of commands sent in one round trip on this connection. */
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
- /** A call to `script` on this connection. */
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
- * A command the command groups lack, on this connection: see
503
- * {@link DatabaseRef.command}. One that blocks is refused here.
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. Keys it still watches are unwatched
511
- * first.
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();