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