vite-plugin-postgres-import 0.8.1 → 0.9.0

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 CHANGED
@@ -184,6 +184,112 @@ Where the default runtime comes from:
184
184
  - `type` - either `'object'` — the export is a `Queryable` (or a promise of one) — or `'function'` — the export
185
185
  is called on each query invocation and returns a `Queryable` (or a promise of one). _default:_ `'object'`
186
186
 
187
+ ### `describe`
188
+
189
+ Types params and result columns from a live database instead of guessing them from the query text.
190
+ Every query is parsed and described the way psql's `\gdesc` does it - nothing gets executed.
191
+
192
+ - `true` - connect using the standard `PG*` environment variables
193
+ - a connection string or a `pg` client config
194
+
195
+ ```js
196
+ postgres({ describe: process.env.DATABASE_URL ?? false });
197
+ ```
198
+
199
+ ```sql
200
+ -- name: GetUser :one
201
+ select id, name, bio, mood
202
+ from users
203
+ where id = :id!;
204
+ ```
205
+
206
+ ```ts
207
+ function GetUser<R1 extends QueryResultRow = {
208
+ id: string;
209
+ name: string;
210
+ bio: string | null;
211
+ mood: "sad" | "ok" | "happy";
212
+ }>(params: { id: number | bigint | string }, config?): Promise<R1>;
213
+ ```
214
+
215
+ - column types follow `pg`'s default type parsers (`int8` and `numeric` are strings, `timestamptz` is a `Date`, etc.),
216
+ enums become unions of their labels, `json` and `jsonb` are `unknown`
217
+ - columns are non-null only when they come straight from a `not null` table column and the query has no outer joins
218
+ nor `rollup`, `cube` or `grouping sets`, unless [hinted](#column-nullability)
219
+ - params of multi-statement queries are always required `string`s, as they're embedded into sql string
220
+ - queries that fail to describe are reported as warnings and fall back to the naive types
221
+ - connecting times out after 5 seconds unless `connectionTimeoutMillis` is set, after which describing is skipped
222
+ for 10 seconds
223
+
224
+ Only the generated `.d.ts` files depend on it, the generated JS stays the same. Hence the database is only ever
225
+ connected to by the dev server, `vite build` and vitest don't connect, nor do they touch the `.d.ts` files
226
+ describing has generated.
227
+
228
+ _default:_ `false`
229
+
230
+ ### `typesOverrides`
231
+
232
+ TypeScript types to generate for postgres types when `describe` is enabled, e.g. when you've changed `pg`'s parsers
233
+ via `types.setTypeParser`. Keyed by type name, optionally schema-qualified. A string applies to both params and columns,
234
+ an object sets them separately:
235
+
236
+ ```js
237
+ postgres({
238
+ describe: true,
239
+ typesOverrides: {
240
+ int8: "bigint",
241
+ "int8[]": "bigint[]",
242
+ jsonb: "import('#types').Json",
243
+ "public.email": { input: "string", output: "import('#types').Email" },
244
+ },
245
+ });
246
+ ```
247
+
248
+ - builtins, enums, composites and domains can be overridden
249
+ - `pg` parses arrays separately from their elements, so array columns need their own `name[]` entry,
250
+ array params fall back to the element's override
251
+ - domain columns pick up their override only when selected straight from a table, otherwise the base type's applies
252
+ - types are inserted verbatim into the generated `.d.ts`, so don't use relative paths in `import()`
253
+
254
+ _default:_ `{}`
255
+
256
+ ## Params
257
+
258
+ Params are written as `:name`. Since nullability of a param can't be inferred, a described param of type `T`
259
+ is generated as an optional `name?: T | null`. Append a `!` to have it generated as a required `name: T`.
260
+ Params that aren't described are always required, as `unknown`, or `{}` with a `!`:
261
+
262
+ ```sql
263
+ -- name: UpdateBio :execrows
264
+ update users
265
+ set bio = :bio
266
+ where id = :id!;
267
+ ```
268
+
269
+ ```ts
270
+ function UpdateBio(params: { bio?: string | null; id: number | bigint | string }, config?): Promise<number>;
271
+ ```
272
+
273
+ ## Column nullability
274
+
275
+ When `describe` can't tell whether a column is nullable, e.g. for `count(*)` or columns behind an outer join you know
276
+ always matches, suffix its alias with `!` to make it non-null, or with `?` to make it nullable.
277
+ The suffix is stripped from the alias and all of its references in the query that gets sent, so rows have plain
278
+ `total` and `bio` keys:
279
+
280
+ ```sql
281
+ -- name: Stats :one
282
+ select count(*) as "total!", u.bio as "bio?"
283
+ from users u
284
+ group by u.bio;
285
+ ```
286
+
287
+ ```ts
288
+ function Stats<R1 extends QueryResultRow = { total: string; bio: string | null }>(params?, config?): Promise<R1>;
289
+ ```
290
+
291
+ Only quoted `as "alias!"` aliases are recognized.
292
+
187
293
  ## Import aliases
188
294
 
189
295
  If your `package.json` declares [subpath imports](https://nodejs.org/api/packages.html#subpath-imports),
@@ -295,7 +401,7 @@ and debugger stepping map back to the `.sql` source.
295
401
 
296
402
  ## What this plugin does NOT do
297
403
 
298
- This plugin does not connect to the database or scan a schema folder, instead naively
404
+ Unless `describe` is enabled, this plugin does not connect to the database or scan a schema folder, instead naively
299
405
  parsing select or returning clauses to figure out potential response types.
300
406
 
301
407
  ## License
package/describe.js ADDED
@@ -0,0 +1,352 @@
1
+ import { bindParams, stripHints } from "./parse.js";
2
+
3
+ /**
4
+ * @import { Client, ClientConfig, FieldDef } from "pg"
5
+ * @import { parseModule } from "./parse.js"
6
+ */
7
+
8
+ /** @typedef {ReturnType<typeof parseModule> extends Generator<infer T> ? T : never} Module */
9
+ /** @typedef {{ params: number[], fields: FieldDef[] }} Description */
10
+
11
+ // mirrors pg-types' default text parsers, oids absent here come back as strings
12
+ /** @type {Record<number, string>} */
13
+ const outputTypes = {
14
+ 16: "boolean",
15
+ 17: "Buffer",
16
+ 21: "number",
17
+ 23: "number",
18
+ 26: "number",
19
+ 114: "unknown",
20
+ 600: "{ x: number; y: number }",
21
+ 700: "number",
22
+ 701: "number",
23
+ 718: "{ x: number; y: number; radius: number }",
24
+ 1082: "Date",
25
+ 1114: "Date",
26
+ 1184: "Date",
27
+ 1186: "Interval",
28
+ 3802: "unknown",
29
+ };
30
+
31
+ /** @type {Record<number, string>} */
32
+ const inputTypes = {
33
+ 16: "boolean",
34
+ 17: "Buffer",
35
+ 20: "number | bigint | string",
36
+ 21: "number",
37
+ 23: "number",
38
+ 26: "number",
39
+ 114: "unknown",
40
+ 700: "number",
41
+ 701: "number",
42
+ 1082: "Date | string",
43
+ 1114: "Date | string",
44
+ 1184: "Date | string",
45
+ 1186: "Interval | string",
46
+ 1700: "number | string",
47
+ 3802: "unknown",
48
+ };
49
+
50
+ const stringTypes = new Set([18, 19, 20, 25, 142, 650, 790, 829, 869, 1042, 1043, 1083, 1266, 1560, 1562, 1700, 2950]);
51
+
52
+ // array oid -> element oid, only for arrays pg-types parses, the rest come back as raw strings
53
+ /** @type {Record<number, number>} */
54
+ const arrays = {
55
+ 199: 114,
56
+ 651: 650,
57
+ 791: 790,
58
+ 1000: 16,
59
+ 1001: 17,
60
+ 1005: 21,
61
+ 1007: 23,
62
+ 1008: 24,
63
+ 1009: 25,
64
+ 1014: 1042,
65
+ 1015: 1043,
66
+ 1016: 20,
67
+ 1017: 600,
68
+ 1021: 700,
69
+ 1022: 701,
70
+ 1028: 26,
71
+ 1040: 829,
72
+ 1041: 869,
73
+ 1115: 1114,
74
+ 1182: 1082,
75
+ 1183: 1083,
76
+ 1185: 1184,
77
+ 1187: 1186,
78
+ 1231: 1700,
79
+ 1270: 1266,
80
+ 2951: 2950,
81
+ 3807: 3802,
82
+ 3907: 3906,
83
+ };
84
+
85
+ const catalogQuery = `select
86
+ (select coalesce(json_agg(json_build_array(
87
+ t.oid::int8,
88
+ case when t.typcategory = 'A' then t.typelem::int8 else 0 end,
89
+ case when t.typtype = 'd' then t.typbasetype::int8 else 0 end,
90
+ n.nspname,
91
+ t.typname,
92
+ (select json_agg(e.enumlabel order by e.enumsortorder) from pg_enum e where e.enumtypid = t.oid)
93
+ )), '[]')
94
+ from pg_type t
95
+ join pg_namespace n on n.oid = t.typnamespace
96
+ where t.oid = any($1::oid[])
97
+ or t.oid in (select case when typcategory = 'A' then typelem else typbasetype end from pg_type where oid = any($1::oid[]))) as types,
98
+ (select coalesce(json_agg(json_build_array(
99
+ a.attrelid::int8,
100
+ a.attnum,
101
+ a.attnotnull,
102
+ case when t.typtype = 'd' then json_build_array(n.nspname, t.typname) end
103
+ )), '[]')
104
+ from unnest($2::oid[], $3::int2[]) c(rel, num)
105
+ join pg_attribute a on a.attrelid = c.rel and a.attnum = c.num
106
+ join pg_type t on t.oid = a.atttypid
107
+ join pg_namespace n on n.oid = t.typnamespace) as columns`;
108
+
109
+ // both null out columns of otherwise not null table columns
110
+ const nullifying = /\b(left|right|full)\s+(outer\s+)?join\b|\b(rollup|cube|grouping\s+sets)\s*\(/i;
111
+
112
+ /** Parses and describes statements without executing them, the way psql's \gdesc does. */
113
+ class Describe {
114
+ /** @param {string[]} texts */
115
+ constructor(texts) {
116
+ this.texts = texts;
117
+ /** @type {Description[]} */
118
+ this.results = [];
119
+ /** @type {Promise<{ results: Description[], error?: Error }>} */
120
+ this.promise = new Promise((resolve) => (this.resolve = resolve));
121
+ /** @type {import("events").EventEmitter | undefined} */
122
+ this.connection = undefined;
123
+ this.onParams = (/** @type {{ dataTypeIDs: number[] }} */ msg) =>
124
+ // pg-protocol reads these as signed int32, unlike row description oids
125
+ this.results.push({ params: msg.dataTypeIDs.map((oid) => oid >>> 0), fields: [] });
126
+ }
127
+
128
+ /** @param {any} connection */
129
+ submit(connection) {
130
+ this.connection = connection;
131
+ connection.on("parameterDescription", this.onParams);
132
+ for (const text of this.texts) {
133
+ connection.parse({ text });
134
+ connection.describe({ type: "S" });
135
+ }
136
+ connection.sync();
137
+ }
138
+
139
+ /** @param {{ fields: FieldDef[] }} msg */
140
+ handleRowDescription(msg) {
141
+ this.results[this.results.length - 1].fields = msg.fields;
142
+ }
143
+
144
+ handleReadyForQuery() {
145
+ this.done();
146
+ }
147
+
148
+ /** @param {Error} error */
149
+ handleError(error) {
150
+ this.done(error);
151
+ }
152
+
153
+ /** @param {Error} [error] */
154
+ done(error) {
155
+ this.connection?.off("parameterDescription", this.onParams);
156
+ this.resolve?.({ results: this.results, error });
157
+ }
158
+ }
159
+
160
+ /**
161
+ * @param {true | string | ClientConfig} config
162
+ * @param {Record<string, string | { input?: string, output?: string }>} [typesOverrides]
163
+ */
164
+ export function describer(config, typesOverrides = {}) {
165
+ const overrides = new Map(
166
+ Object.entries(typesOverrides).map(([k, v]) => [k, typeof v === "string" ? { input: v, output: v } : v]),
167
+ );
168
+
169
+ /** @type {Promise<{ client: Client, DatabaseError: Function }> | undefined} */
170
+ let connection;
171
+ /** @type {Promise<unknown>} */
172
+ let tail = Promise.resolve();
173
+
174
+ /**
175
+ * pg no longer allows queueing queries on a client, so concurrent transforms take turns.
176
+ * @template T
177
+ * @param {() => Promise<T>} fn
178
+ */
179
+ function exclusive(fn) {
180
+ const run = tail.then(fn);
181
+ tail = run.catch(() => {});
182
+ return run;
183
+ }
184
+
185
+ /** @type {WeakSet<object>} */
186
+ const reported = new WeakSet();
187
+
188
+ function connect() {
189
+ if (connection) return connection;
190
+ const pending = (connection = import("pg").then(async ({ default: pg }) => {
191
+ const client = new pg.Client({
192
+ connectionTimeoutMillis: 5000,
193
+ ...(config === true ? {} : typeof config === "string" ? { connectionString: config } : config),
194
+ });
195
+ client.on("error", (err) => {
196
+ console.warn(`[postgres] connection error: ${err.message}`);
197
+ if (connection === pending) connection = undefined;
198
+ });
199
+ await client.connect();
200
+ return { client, DatabaseError: pg.DatabaseError };
201
+ }));
202
+ // fail fast for a while, instead of every file waiting out the timeout again
203
+ pending.catch(() =>
204
+ setTimeout(() => {
205
+ if (connection === pending) connection = undefined;
206
+ }, 10_000).unref(),
207
+ );
208
+ return pending;
209
+ }
210
+
211
+ /**
212
+ * Pipelines every statement in a single round trip, re-sending the ones following a failed statement.
213
+ * @param {string[]} texts
214
+ * @returns {Promise<(Description | { error: Error })[]>}
215
+ */
216
+ async function describe(texts) {
217
+ const { client, DatabaseError } = await connect();
218
+ /** @type {(Description | { error: Error })[]} */
219
+ const out = [];
220
+ while (out.length < texts.length) {
221
+ const query = new Describe(texts.slice(out.length));
222
+ client.query(/** @type {any} */ (query));
223
+ const { results, error } = await query.promise;
224
+ out.push(...results);
225
+ if (!error) break;
226
+ if (!(error instanceof DatabaseError)) throw error;
227
+ out.push({ error });
228
+ }
229
+ return out;
230
+ }
231
+
232
+ return {
233
+ /**
234
+ * Sets `columns` and `paramTypes` on each statement from what the database reports.
235
+ * @param {(Module | Module[])[]} modules
236
+ * @param {string} filename
237
+ */
238
+ async annotate(modules, filename) {
239
+ const statements = modules.flat();
240
+ if (!statements.length) return;
241
+ const texts = statements.map((s) => bindParams(stripHints(s.query), s.params));
242
+
243
+ /** @type {Map<number, { elem: number, base: number, name: [schema: string, name: string], labels: string[] | null }>} */
244
+ const catalog = new Map();
245
+ /** @type {Map<string, { notnull: boolean, domain: [schema: string, name: string] | null }>} */
246
+ const columns = new Map();
247
+ /** @type {Awaited<ReturnType<typeof describe>>} */
248
+ let described = [];
249
+ try {
250
+ await exclusive(async () => {
251
+ described = await describe(texts);
252
+
253
+ /** @type {Set<number>} */
254
+ const lookup = new Set();
255
+ const rels = [];
256
+ const nums = [];
257
+ for (const d of described) {
258
+ if (!("params" in d)) continue;
259
+ for (const oid of [...d.params, ...d.fields.map((f) => f.dataTypeID)]) {
260
+ if (overrides.size || !(oid in outputTypes || oid in inputTypes || oid in arrays || stringTypes.has(oid))) {
261
+ lookup.add(oid);
262
+ }
263
+ }
264
+ for (const f of d.fields) {
265
+ if (f.tableID) {
266
+ rels.push(f.tableID);
267
+ nums.push(f.columnID);
268
+ }
269
+ }
270
+ }
271
+ if (lookup.size || rels.length) {
272
+ const { client } = await connect();
273
+ const { rows } = await client.query(catalogQuery, [[...lookup], rels, nums]);
274
+ for (const [oid, elem, base, schema, name, labels] of rows[0].types) {
275
+ catalog.set(oid, { elem, base, name: [schema, name], labels });
276
+ }
277
+ for (const [rel, num, notnull, domain] of rows[0].columns) columns.set(`${rel}.${num}`, { notnull, domain });
278
+ }
279
+ });
280
+ } catch (err) {
281
+ if (!reported.has(/** @type {Error} */ (err))) {
282
+ reported.add(/** @type {Error} */ (err));
283
+ console.warn(`[postgres] could not describe ${filename}: ${/** @type {Error} */ (err).message}`);
284
+ }
285
+ return;
286
+ }
287
+
288
+ const labels = (/** @type {number} */ oid) => catalog.get(oid)?.labels?.map((l) => JSON.stringify(l)).join(" | ");
289
+
290
+ /** @returns {[schema: string, name: string] | undefined} */
291
+ const typeName = (/** @type {number} */ oid) => {
292
+ const t = catalog.get(oid);
293
+ if (!t?.elem) return t?.name;
294
+ const elem = catalog.get(t.elem)?.name;
295
+ return elem && [elem[0], `${elem[1]}[]`];
296
+ };
297
+
298
+ const override = (/** @type {[string, string] | null | undefined} */ type, /** @type {"input" | "output"} */ side) =>
299
+ type && (overrides.get(`${type[0]}.${type[1]}`) ?? overrides.get(type[1]))?.[side];
300
+
301
+ // pg parses arrays separately from their elements, so element overrides don't apply to them
302
+ /** @returns {string} */
303
+ const baseOutput = (/** @type {number} */ oid) =>
304
+ outputTypes[oid] ??
305
+ (oid in arrays ? `${wrap(oid === 1231 ? "number" : baseOutput(arrays[oid]))}[]` : labels(oid) ?? "string");
306
+
307
+ const output = (/** @type {number} */ oid) => override(typeName(oid), "output") ?? baseOutput(oid);
308
+
309
+ /** @returns {string} */
310
+ const input = (/** @type {number} */ oid) => {
311
+ const type = override(typeName(oid), "input") ?? inputTypes[oid];
312
+ if (type) return type;
313
+ const t = catalog.get(oid);
314
+ const elem = arrays[oid] ?? t?.elem;
315
+ if (elem) return `${wrap(input(elem))}[]`;
316
+ if (t?.base) return input(t.base);
317
+ return labels(oid) ?? (oid ? "string" : "unknown");
318
+ };
319
+
320
+ for (let i = 0; i < statements.length; ++i) {
321
+ const s = statements[i];
322
+ const d = described[i];
323
+ if ("error" in d) {
324
+ console.warn(`[postgres] ${filename}: ${s.name}: ${d.error.message}`);
325
+ continue;
326
+ }
327
+ const nullified = nullifying.test(s.query);
328
+ s.columns = d.fields.map((f) => {
329
+ const column = columns.get(`${f.tableID}.${f.columnID}`);
330
+ const type = override(column?.domain, "output") ?? output(f.dataTypeID);
331
+ const hint = s.hints.get(f.name);
332
+ const nullable = hint ? hint === "?" : nullified || !column?.notnull;
333
+ return [
334
+ /^[A-Za-z_$][\w$]*$/.test(f.name) ? f.name : JSON.stringify(f.name),
335
+ type === "unknown" || !nullable ? type : `${type} | null`,
336
+ ];
337
+ });
338
+ s.paramTypes = new Map(s.params.values().map((p, j) => [p, input(d.params[j])]));
339
+ }
340
+ },
341
+
342
+ end() {
343
+ const pending = connection;
344
+ connection = undefined;
345
+ pending?.then(({ client }) => client.end()).catch(() => {});
346
+ },
347
+ };
348
+ }
349
+
350
+ function wrap(/** @type {string} */ type) {
351
+ return type.includes("|") ? `(${type})` : type;
352
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-postgres-import",
3
3
  "description": "Vite plugin for importing SQL files as JS modules with sqlc-style annotations",
4
- "version": "0.8.1",
4
+ "version": "0.9.0",
5
5
  "main": "plugin.js",
6
6
  "types": "plugin.d.ts",
7
7
  "type": "module",
@@ -12,6 +12,7 @@
12
12
  "license": "MIT",
13
13
  "repository": "github:sirkostya009/vite-plugin-postgres-import",
14
14
  "files": [
15
+ "describe.js",
15
16
  "parse.js",
16
17
  "plugin.d.ts",
17
18
  "plugin.d.ts.map"
package/parse.js CHANGED
@@ -1,4 +1,24 @@
1
1
  const annotationRegex = /--\s*name:\s*(?<name>[a-zA-Z][\w_]*)\s*(?<tags>(:\w+\s*)*)?\r?\n/g;
2
+ const paramRegex = /(?<![\w\\:]):(\w+)(!(?![=~]))?/g;
3
+ const hintRegex = /\bas\s+"([^"]+?)([!?])"/gi;
4
+
5
+ /** Strips `!` and `?` nullability hints off of `as "alias!"` column aliases and all references to them. */
6
+ export function stripHints(/** @type {string} */ query) {
7
+ const hinted = new Set(query.matchAll(hintRegex).map((m) => `"${m[1]}${m[2]}"`));
8
+ return hinted.size ? query.replaceAll(/"[^"]+"/g, (q) => (hinted.has(q) ? `${q.slice(0, -2)}"` : q)) : query;
9
+ }
10
+
11
+ /**
12
+ * Replaces `:param` (and `:param!`) placeholders, `$1`, `$2`, ... by default.
13
+ * @param {string} query
14
+ * @param {Iterable<string>} params
15
+ */
16
+ export function bindParams(query, params, replace = (/** @type {string} */ _, /** @type {number} */ i) => `$${i + 1}`) {
17
+ const index = new Map([...params].map((p, i) => [p, i]));
18
+ return query.replaceAll(paramRegex, (m, /** @type {string} */ param) =>
19
+ index.has(param) ? replace(param, /** @type {number} */ (index.get(param))) : m,
20
+ );
21
+ }
2
22
 
3
23
  export function* parseModule(/** @type {string} */ sql) {
4
24
  annotationRegex.lastIndex = 0;
@@ -102,6 +122,8 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
102
122
  const match = firstMeta ? /--\s*(?<tags>(:\w+\s*)*)\r?\n/.exec(s) : annotationRegex.exec(s);
103
123
  const meta = match?.groups;
104
124
  const query = s.substring(match?.[0]?.length ?? 0).trim();
125
+ const stripped = stripHints(query);
126
+ const params = query.matchAll(paramRegex).toArray();
105
127
 
106
128
  return {
107
129
  name: firstMeta?.name || meta?.name,
@@ -110,11 +132,17 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
110
132
  mode: /:cursor|:iterable/.exec(meta?.tags)?.[0],
111
133
  rowArray: meta?.tags?.includes(":array"),
112
134
  query,
113
- params: new Set(query.matchAll(/(?<![\w_\\:]):[\w_]+/g).map((m) => m?.[0]?.substring(1))),
114
- selectFields: selectList(/(with.*\))?\s*select\s+(?<select>.*)/ims.exec(query)?.groups?.select)
135
+ params: new Set(params.map((m) => m[1])),
136
+ required: new Set(params.filter((m) => m[2]).map((m) => m[1])),
137
+ /** @type {[key: string, type: string][] | undefined} */
138
+ columns: undefined,
139
+ /** @type {Map<string, string> | undefined} */
140
+ paramTypes: undefined,
141
+ hints: new Map(query.matchAll(hintRegex).map((m) => [m[1], m[2]])),
142
+ selectFields: selectList(/(with.*\))?\s*select\s+(?<select>.*)/ims.exec(stripped)?.groups?.select)
115
143
  .map(parseResultSymbol)
116
144
  .toArray(),
117
- returningClause: selectList(/returning\s+(?<returning>[\w\s\.\",\*]+);?$/im.exec(query)?.groups?.returning)
145
+ returningClause: selectList(/returning\s+(?<returning>[\w\s\.\",\*]+);?$/im.exec(stripped)?.groups?.returning)
118
146
  .map(parseResultSymbol)
119
147
  .toArray(),
120
148
  };
@@ -172,8 +200,10 @@ export function codegen(
172
200
  ) {
173
201
  let dts = [
174
202
  // `declare module '${modulePrefix}${filename}.sql' {`
175
- `import type { QueryResultRow, QueryResult, QueryArrayResult } from 'pg';\nimport { Queryable } from 'vite-plugin-postgres-import';`,
203
+ `import type { QueryResultRow, QueryResult, QueryArrayResult } from 'pg';\nimport type { Queryable, Interval } from 'vite-plugin-postgres-import';`,
176
204
  ];
205
+ const returnSymbols = (/** @type {ReturnType<typeof metadata>} */ m) =>
206
+ m.columns ?? (m.returningClause?.length ? m.returningClause : m.selectFields).map((s) => [s, "unknown"]);
177
207
  let js = [
178
208
  `import { escapeLiteral } from 'pg';\nimport { ${runtime.export} as __runtime } from '${runtime.module}';`,
179
209
  ];
@@ -186,19 +216,24 @@ export function codegen(
186
216
  ...module[0],
187
217
  query: module.flatMap((m) => m.query).join(";\n\n"),
188
218
  params: new Set(module.flatMap((m) => [...m.params])),
219
+ required: new Set(module.flatMap((m) => [...m.required])),
220
+ // values are inlined with escapeLiteral, which only accepts strings
221
+ paramTypes: module.some((m) => m.paramTypes)
222
+ ? new Map(module.flatMap((m) => [...m.params]).map((p) => [p, "string"]))
223
+ : undefined,
189
224
  execution: module.map((m) => m.execution),
190
- returnSymbols: module.map((m) => (m.returningClause?.length ? m.returningClause : m.selectFields)),
225
+ returnSymbols: module.map(returnSymbols),
191
226
  multiStatement: true,
192
227
  }
193
228
  : {
194
229
  ...module,
195
230
  execution: [module.execution],
196
- returnSymbols: [module.returningClause?.length ? module.returningClause : module.selectFields],
231
+ returnSymbols: [returnSymbols(module)],
197
232
  multiStatement: "",
198
233
  };
199
234
 
200
235
  if (!module.rowArray) {
201
- module.returnSymbols = module.returnSymbols.map((rs) => [...new Set(rs)]);
236
+ module.returnSymbols = module.returnSymbols.map((rs) => [...new Map(rs)]);
202
237
  }
203
238
 
204
239
  mappings.push({ name: module.name, srcLine: module.srcLine ?? 0, srcCol: module.srcCol ?? 0 });
@@ -208,11 +243,17 @@ export function codegen(
208
243
  js[0] += `\nimport Cursor from 'pg-cursor';`;
209
244
  }
210
245
 
246
+ let optionalParams = true;
211
247
  const keys = module.params
212
248
  .values()
213
- .map((k) => `'${k}'`)
249
+ .map((p) => {
250
+ const type = module.paramTypes?.get(p) ?? "unknown";
251
+ if (type !== "unknown" && !module.required.has(p) && !module.multiStatement) return `${p}?: ${type} | null`;
252
+ optionalParams = false;
253
+ return `${p}: ${type === "unknown" && module.required.has(p) ? "{}" : type}`;
254
+ })
214
255
  .toArray()
215
- .join(" | ");
256
+ .join("; ");
216
257
 
217
258
  dts.push(`/**
218
259
  * ${"```"}sql
@@ -223,16 +264,16 @@ export function ${module.name}<${module.returnSymbols
223
264
  .map((r, i) =>
224
265
  module.rowArray
225
266
  ? `R${i + 1} extends any[] = ${
226
- r.find((k) => k.startsWith("[column: string]"))
267
+ r.find(([k]) => k.startsWith("[column: string]"))
227
268
  ? `unknown[]`
228
- : `[ ${r?.map(() => `unknown`)?.join(", ") ?? ""} ]`
269
+ : `[ ${r.map(([, t]) => t).join(", ")} ]`
229
270
  }`
230
- : `R${i + 1} extends QueryResultRow = {${r.length > 3 ? "\n\t" : " "}${
231
- r?.map((r) => `${r}: unknown`)?.join(r.length > 3 ? ";\n\t" : "; ") ?? ""
232
- }${r.length > 3 ? "\n" : " "}}`
271
+ : `R${i + 1} extends QueryResultRow = {${r.length > 3 ? "\n\t" : " "}${r
272
+ .map(([k, t]) => `${k}: ${t}`)
273
+ .join(r.length > 3 ? ";\n\t" : "; ")}${r.length > 3 ? "\n" : " "}}`
233
274
  )
234
275
  .join(", ")}>(
235
- ${keys ? `params: Record<${keys}, unknown>` : `params?: Record<string, never>`},
276
+ ${keys ? `params${optionalParams ? "?" : ""}: { ${keys} }` : `params?: Record<string, never>`},
236
277
  config?: { db?: Queryable${module.mode === ":iterable" ? "; read?: number" : ""} },
237
278
  ): ${
238
279
  module.mode === ":cursor"
@@ -259,14 +300,9 @@ export function ${module.name}<${module.returnSymbols
259
300
  .reduce((curr, p) => [...curr, p], [])
260
301
  .join();
261
302
 
262
- module.query = module.query.replaceAll("`", "\\`");
263
-
264
- module.params.values().forEach(
265
- (param, i) =>
266
- (module.query = module.query.replaceAll(
267
- new RegExp(`(?<![\w_]):${param}(?![\w_])`, "gi"),
268
- module.multiStatement ? `\${escapeLiteral(${param})}` : `$${i + 1}` // the escapists
269
- ))
303
+ const escaped = module.query.replaceAll("\\", "\\\\").replaceAll("`", "\\`").replaceAll("${", "\\${");
304
+ module.query = bindParams(stripHints(escaped), module.params, (param, i) =>
305
+ module.multiStatement ? `\${escapeLiteral(${param})}` : `$${i + 1}` // the escapists
270
306
  );
271
307
 
272
308
  if (module.prepared && module.execution.size > 1) {
package/plugin.d.ts CHANGED
@@ -1,5 +1,17 @@
1
- import type { QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg";
1
+ import type { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg";
2
2
  import type { Plugin } from "vite";
3
+ export type Interval = {
4
+ years?: number;
5
+ months?: number;
6
+ days?: number;
7
+ hours?: number;
8
+ minutes?: number;
9
+ seconds?: number;
10
+ milliseconds?: number;
11
+ toPostgres(): string;
12
+ toISO(): string;
13
+ toISOString(): string;
14
+ };
3
15
  export type QueryableObject = {
4
16
  query<T extends Submittable>(queryStream: T): T;
5
17
  query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
@@ -20,6 +32,17 @@ export type PostgresOptions = {
20
32
  * Where the default runtime comes from.
21
33
  */
22
34
  runtime?: RuntimeOptions;
35
+ /**
36
+ * Type params and result columns by describing every query against a live database, only ever done by the dev server. `true` connects using the `PG*` environment variables, a connection string or `pg` client config may be given instead. Defaults to `false`.
37
+ */
38
+ describe?: boolean | string | ClientConfig;
39
+ /**
40
+ * TypeScript types to use for postgres types when `describe` is enabled, keyed by type name (`int8`, `public.mood`, `int8[]`). A string applies to both params (`input`) and columns (`output`).
41
+ */
42
+ typesOverrides?: Record<string, string | {
43
+ input?: string;
44
+ output?: string;
45
+ }>;
23
46
  };
24
47
  export type RuntimeOptions = {
25
48
  /**
@@ -36,9 +59,24 @@ export type RuntimeOptions = {
36
59
  type?: "object" | "function";
37
60
  };
38
61
  /**
39
- * @import { QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
62
+ * @import { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
40
63
  * @import { Plugin, TransformResult } from "vite"
41
64
  */
65
+ /**
66
+ * Value pg parses `interval` columns into.
67
+ * @typedef {{
68
+ * years?: number;
69
+ * months?: number;
70
+ * days?: number;
71
+ * hours?: number;
72
+ * minutes?: number;
73
+ * seconds?: number;
74
+ * milliseconds?: number;
75
+ * toPostgres(): string;
76
+ * toISO(): string;
77
+ * toISOString(): string;
78
+ * }} Interval
79
+ */
42
80
  /**
43
81
  * @typedef {{
44
82
  * query<T extends Submittable>(queryStream: T): T;
@@ -53,6 +91,8 @@ export type RuntimeOptions = {
53
91
  * @property {string} [typesFolder] Path to a folder where all declaration are kept relative to `rootFolder`, i.e. a file at path `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`. Make sure you include this one in your `tsconfig.json`. Defaults to `"node_modules/@types/vite-plugin-postgres-import/"`.
54
92
  * @property {string} [rootFolder] Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
55
93
  * @property {RuntimeOptions} [runtime] Where the default runtime comes from.
94
+ * @property {boolean | string | ClientConfig} [describe] Type params and result columns by describing every query against a live database, only ever done by the dev server. `true` connects using the `PG*` environment variables, a connection string or `pg` client config may be given instead. Defaults to `false`.
95
+ * @property {Record<string, string | { input?: string, output?: string }>} [typesOverrides] TypeScript types to use for postgres types when `describe` is enabled, keyed by type name (`int8`, `public.mood`, `int8[]`). A string applies to both params (`input`) and columns (`output`).
56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oB,MAAM,AAA1B,CACA,EADW,MAAM,CACjB;;;;IAAmC,IAAI,AAAvC,CACF,EADa,QAAQ,GAAG,UAAU,CAClC;CAAA,CAAA;AA9CD;;;GAGG;AAEH;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;GAOG;AAEH,sEAAsE;AAEtE;;;;;;;GAOG;AAEH;;;;;GAKG;AAEH;;;GAGG;AACH,wBAA8B,QAAQ,CAAC,IAAI,AAHxC,CACA,EADQ,eAGqC,GAFnC,OAAO,CAAC,MAAM,CAAC,CAiK3B"}
package/plugin.js CHANGED
@@ -1,12 +1,29 @@
1
1
  import fs from "node:fs/promises";
2
2
  import path from "node:path";
3
+ import { describer } from "./describe.js";
3
4
  import { codegen, encodeMappings, exportMappings, parseModule } from "./parse.js";
4
5
 
5
6
  /**
6
- * @import { QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
7
+ * @import { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
7
8
  * @import { Plugin, TransformResult } from "vite"
8
9
  */
9
10
 
11
+ /**
12
+ * Value pg parses `interval` columns into.
13
+ * @typedef {{
14
+ * years?: number;
15
+ * months?: number;
16
+ * days?: number;
17
+ * hours?: number;
18
+ * minutes?: number;
19
+ * seconds?: number;
20
+ * milliseconds?: number;
21
+ * toPostgres(): string;
22
+ * toISO(): string;
23
+ * toISOString(): string;
24
+ * }} Interval
25
+ */
26
+
10
27
  /**
11
28
  * @typedef {{
12
29
  * query<T extends Submittable>(queryStream: T): T;
@@ -23,6 +40,8 @@ import { codegen, encodeMappings, exportMappings, parseModule } from "./parse.js
23
40
  * @property {string} [typesFolder] Path to a folder where all declaration are kept relative to `rootFolder`, i.e. a file at path `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`. Make sure you include this one in your `tsconfig.json`. Defaults to `"node_modules/@types/vite-plugin-postgres-import/"`.
24
41
  * @property {string} [rootFolder] Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
25
42
  * @property {RuntimeOptions} [runtime] Where the default runtime comes from.
43
+ * @property {boolean | string | ClientConfig} [describe] Type params and result columns by describing every query against a live database, only ever done by the dev server. `true` connects using the `PG*` environment variables, a connection string or `pg` client config may be given instead. Defaults to `false`.
44
+ * @property {Record<string, string | { input?: string, output?: string }>} [typesOverrides] TypeScript types to use for postgres types when `describe` is enabled, keyed by type name (`int8`, `public.mood`, `int8[]`). A string applies to both params (`input`) and columns (`output`).
26
45
  */
27
46
 
28
47
  /**
@@ -41,8 +60,12 @@ export default async function postgres(opts = {}) {
41
60
  typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
42
61
  rootFolder = process.cwd(),
43
62
  runtime: { module: runtimeModule = "#db-runtime", export: runtimeExport = "db", type: runtimeType = "object" } = {},
63
+ describe = false,
64
+ typesOverrides,
44
65
  } = opts;
45
66
  const runtime = { module: runtimeModule, export: runtimeExport, type: runtimeType };
67
+ const db = describe ? describer(describe, typesOverrides) : undefined;
68
+ let dev = false;
46
69
  const imports = await fs
47
70
  .readFile(path.join(rootFolder, "package.json"), "utf8")
48
71
  .then((s) => JSON.parse(s).imports)
@@ -77,6 +100,9 @@ export default async function postgres(opts = {}) {
77
100
 
78
101
  return {
79
102
  name: "vite-plugin-sql-postgres",
103
+ configResolved(config) {
104
+ dev = config.command === "serve" && config.mode !== "test";
105
+ },
80
106
  transform: {
81
107
  filter: {
82
108
  id: /\.sql$/,
@@ -102,6 +128,9 @@ export default async function postgres(opts = {}) {
102
128
  handleHotUpdate({ modules }) {
103
129
  return modules;
104
130
  },
131
+ buildEnd() {
132
+ db?.end();
133
+ },
105
134
  };
106
135
 
107
136
  /** @returns {Promise<TransformResult | undefined>} */
@@ -111,57 +140,63 @@ export default async function postgres(opts = {}) {
111
140
  }
112
141
  const filename = path.relative(rootFolder, id);
113
142
 
143
+ const modules = parseModule(sql).toArray();
144
+ if (dev) await db?.annotate(modules, filename);
145
+
114
146
  const { js, dts, moduleDeclaration, mappings } = codegen(
115
- parseModule(sql),
147
+ modules,
116
148
  filename,
117
149
  moduleName(filename),
118
150
  runtime,
119
151
  );
120
152
 
121
- const dtsFolder = path.join(typesFolder, path.relative(rootFolder, path.dirname(id)));
122
- const dtsName = path.basename(filename, ".sql") + ".sql.d.ts";
123
- await fs.mkdir(dtsFolder, { recursive: true });
124
- await fs.writeFile(path.join(dtsFolder, dtsName), dts + `//# sourceMappingURL=${dtsName}.map\n`);
125
- await fs.writeFile(
126
- path.join(dtsFolder, dtsName + ".map"),
127
- JSON.stringify({
128
- version: 3,
129
- file: dtsName,
130
- sources: [path.relative(dtsFolder, id).replaceAll(path.sep, "/")],
131
- names: [],
132
- mappings: encodeMappings([
133
- { genLine: 0, genCol: 0, srcLine: 0, srcCol: 0 },
134
- ...exportMappings(dts, mappings),
135
- ]),
136
- }),
137
- );
138
- if (moduleDeclaration.length) {
139
- moduleDeclarations.set(filename, { text: moduleDeclaration.join("\n"), mappings, id });
140
- let line = 0;
141
- const sources = [];
142
- const combined = [];
143
- const texts = [];
144
- for (const { text, mappings, id } of moduleDeclarations.values()) {
145
- const srcIdx = sources.push(path.relative(typesFolder, id).replaceAll(path.sep, "/")) - 1;
146
- combined.push({ genLine: line, genCol: 0, srcIdx, srcLine: 0, srcCol: 0 });
147
- combined.push(...exportMappings(text, mappings, srcIdx, line));
148
- texts.push(text);
149
- line += text.split("\n").length;
150
- }
153
+ // described types must not be overwritten by naive ones outside of dev
154
+ if (!db || dev) {
155
+ const dtsFolder = path.join(typesFolder, path.relative(rootFolder, path.dirname(id)));
156
+ const dtsName = path.basename(filename, ".sql") + ".sql.d.ts";
157
+ await fs.mkdir(dtsFolder, { recursive: true });
158
+ await fs.writeFile(path.join(dtsFolder, dtsName), dts + `//# sourceMappingURL=${dtsName}.map\n`);
151
159
  await fs.writeFile(
152
- path.join(typesFolder, "modules.sql.d.ts"),
153
- texts.join("\n") + "\n//# sourceMappingURL=modules.sql.d.ts.map\n",
154
- );
155
- await fs.writeFile(
156
- path.join(typesFolder, "modules.sql.d.ts.map"),
160
+ path.join(dtsFolder, dtsName + ".map"),
157
161
  JSON.stringify({
158
162
  version: 3,
159
- file: "modules.sql.d.ts",
160
- sources,
163
+ file: dtsName,
164
+ sources: [path.relative(dtsFolder, id).replaceAll(path.sep, "/")],
161
165
  names: [],
162
- mappings: encodeMappings(combined),
166
+ mappings: encodeMappings([
167
+ { genLine: 0, genCol: 0, srcLine: 0, srcCol: 0 },
168
+ ...exportMappings(dts, mappings),
169
+ ]),
163
170
  }),
164
171
  );
172
+ if (moduleDeclaration.length) {
173
+ moduleDeclarations.set(filename, { text: moduleDeclaration.join("\n"), mappings, id });
174
+ let line = 0;
175
+ const sources = [];
176
+ const combined = [];
177
+ const texts = [];
178
+ for (const { text, mappings, id } of moduleDeclarations.values()) {
179
+ const srcIdx = sources.push(path.relative(typesFolder, id).replaceAll(path.sep, "/")) - 1;
180
+ combined.push({ genLine: line, genCol: 0, srcIdx, srcLine: 0, srcCol: 0 });
181
+ combined.push(...exportMappings(text, mappings, srcIdx, line));
182
+ texts.push(text);
183
+ line += text.split("\n").length;
184
+ }
185
+ await fs.writeFile(
186
+ path.join(typesFolder, "modules.sql.d.ts"),
187
+ texts.join("\n") + "\n//# sourceMappingURL=modules.sql.d.ts.map\n",
188
+ );
189
+ await fs.writeFile(
190
+ path.join(typesFolder, "modules.sql.d.ts.map"),
191
+ JSON.stringify({
192
+ version: 3,
193
+ file: "modules.sql.d.ts",
194
+ sources,
195
+ names: [],
196
+ mappings: encodeMappings(combined),
197
+ }),
198
+ );
199
+ }
165
200
  }
166
201
 
167
202
  return {