vite-plugin-postgres-import 0.8.0 → 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,14 +1,21 @@
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.0",
4
+ "version": "0.9.0",
5
5
  "main": "plugin.js",
6
+ "types": "plugin.d.ts",
6
7
  "type": "module",
8
+ "scripts": {
9
+ "prepack": "tsc -p ."
10
+ },
7
11
  "author": "SirKostya009",
8
12
  "license": "MIT",
9
13
  "repository": "github:sirkostya009/vite-plugin-postgres-import",
10
14
  "files": [
11
- "parse.js"
15
+ "describe.js",
16
+ "parse.js",
17
+ "plugin.d.ts",
18
+ "plugin.d.ts.map"
12
19
  ],
13
20
  "keywords": [
14
21
  "vite",
@@ -18,20 +25,16 @@
18
25
  "sqlc"
19
26
  ],
20
27
  "devDependencies": {
21
- "@sveltejs/kit": "^2",
22
28
  "@types/pg": "*",
29
+ "typescript": "^7",
23
30
  "vite": "^8"
24
31
  },
25
32
  "peerDependencies": {
26
- "@sveltejs/kit": "^2",
27
33
  "@types/pg": "*",
28
34
  "pg": "*",
29
35
  "pg-cursor": "*"
30
36
  },
31
37
  "peerDependenciesMeta": {
32
- "@sveltejs/kit": {
33
- "optional": true
34
- },
35
38
  "pg-cursor": {
36
39
  "optional": true
37
40
  },
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,17 +200,10 @@ export function codegen(
172
200
  ) {
173
201
  let dts = [
174
202
  // `declare module '${modulePrefix}${filename}.sql' {`
175
- `import type { QueryResultRow, QueryResult, QueryArrayResult, QueryConfig, QueryArrayConfig, QueryConfigValues, Submittable } from 'pg';
176
-
177
- interface QueryableObject {
178
- query<T extends Submittable>(queryStream: T): T;
179
- query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
180
- query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
181
- query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
182
- }
183
-
184
- type Queryable = QueryableObject | Promise<QueryableObject>;`,
203
+ `import type { QueryResultRow, QueryResult, QueryArrayResult } from 'pg';\nimport type { Queryable, Interval } from 'vite-plugin-postgres-import';`,
185
204
  ];
205
+ const returnSymbols = (/** @type {ReturnType<typeof metadata>} */ m) =>
206
+ m.columns ?? (m.returningClause?.length ? m.returningClause : m.selectFields).map((s) => [s, "unknown"]);
186
207
  let js = [
187
208
  `import { escapeLiteral } from 'pg';\nimport { ${runtime.export} as __runtime } from '${runtime.module}';`,
188
209
  ];
@@ -195,19 +216,24 @@ type Queryable = QueryableObject | Promise<QueryableObject>;`,
195
216
  ...module[0],
196
217
  query: module.flatMap((m) => m.query).join(";\n\n"),
197
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,
198
224
  execution: module.map((m) => m.execution),
199
- returnSymbols: module.map((m) => (m.returningClause?.length ? m.returningClause : m.selectFields)),
225
+ returnSymbols: module.map(returnSymbols),
200
226
  multiStatement: true,
201
227
  }
202
228
  : {
203
229
  ...module,
204
230
  execution: [module.execution],
205
- returnSymbols: [module.returningClause?.length ? module.returningClause : module.selectFields],
231
+ returnSymbols: [returnSymbols(module)],
206
232
  multiStatement: "",
207
233
  };
208
234
 
209
235
  if (!module.rowArray) {
210
- module.returnSymbols = module.returnSymbols.map((rs) => [...new Set(rs)]);
236
+ module.returnSymbols = module.returnSymbols.map((rs) => [...new Map(rs)]);
211
237
  }
212
238
 
213
239
  mappings.push({ name: module.name, srcLine: module.srcLine ?? 0, srcCol: module.srcCol ?? 0 });
@@ -217,11 +243,17 @@ type Queryable = QueryableObject | Promise<QueryableObject>;`,
217
243
  js[0] += `\nimport Cursor from 'pg-cursor';`;
218
244
  }
219
245
 
246
+ let optionalParams = true;
220
247
  const keys = module.params
221
248
  .values()
222
- .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
+ })
223
255
  .toArray()
224
- .join(" | ");
256
+ .join("; ");
225
257
 
226
258
  dts.push(`/**
227
259
  * ${"```"}sql
@@ -232,16 +264,16 @@ export function ${module.name}<${module.returnSymbols
232
264
  .map((r, i) =>
233
265
  module.rowArray
234
266
  ? `R${i + 1} extends any[] = ${
235
- r.find((k) => k.startsWith("[column: string]"))
267
+ r.find(([k]) => k.startsWith("[column: string]"))
236
268
  ? `unknown[]`
237
- : `[ ${r?.map(() => `unknown`)?.join(", ") ?? ""} ]`
269
+ : `[ ${r.map(([, t]) => t).join(", ")} ]`
238
270
  }`
239
- : `R${i + 1} extends QueryResultRow = {${r.length > 3 ? "\n\t" : " "}${
240
- r?.map((r) => `${r}: unknown`)?.join(r.length > 3 ? ";\n\t" : "; ") ?? ""
241
- }${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" : " "}}`
242
274
  )
243
275
  .join(", ")}>(
244
- ${keys ? `params: Record<${keys}, unknown>` : `params?: Record<string, never>`},
276
+ ${keys ? `params${optionalParams ? "?" : ""}: { ${keys} }` : `params?: Record<string, never>`},
245
277
  config?: { db?: Queryable${module.mode === ":iterable" ? "; read?: number" : ""} },
246
278
  ): ${
247
279
  module.mode === ":cursor"
@@ -268,14 +300,9 @@ export function ${module.name}<${module.returnSymbols
268
300
  .reduce((curr, p) => [...curr, p], [])
269
301
  .join();
270
302
 
271
- module.query = module.query.replaceAll("`", "\\`");
272
-
273
- module.params.values().forEach(
274
- (param, i) =>
275
- (module.query = module.query.replaceAll(
276
- new RegExp(`(?<![\w_]):${param}(?![\w_])`, "gi"),
277
- module.multiStatement ? `\${escapeLiteral(${param})}` : `$${i + 1}` // the escapists
278
- ))
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
279
306
  );
280
307
 
281
308
  if (module.prepared && module.execution.size > 1) {
package/plugin.d.ts ADDED
@@ -0,0 +1,108 @@
1
+ import type { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg";
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
+ };
15
+ export type QueryableObject = {
16
+ query<T extends Submittable>(queryStream: T): T;
17
+ query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
18
+ query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
19
+ query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
20
+ };
21
+ export type Queryable = QueryableObject | Promise<QueryableObject>;
22
+ export type PostgresOptions = {
23
+ /**
24
+ * 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/"`.
25
+ */
26
+ typesFolder?: string;
27
+ /**
28
+ * Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
29
+ */
30
+ rootFolder?: string;
31
+ /**
32
+ * Where the default runtime comes from.
33
+ */
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
+ }>;
46
+ };
47
+ export type RuntimeOptions = {
48
+ /**
49
+ * Module specifier the default runtime is imported from in every generated module. Defaults to `"#db-runtime"`.
50
+ */
51
+ module?: string;
52
+ /**
53
+ * Named export of `module` providing the default runtime. Defaults to `"db"`.
54
+ */
55
+ export?: string;
56
+ /**
57
+ * Whether the export is a `Queryable` itself or a function returning one (possibly async). Defaults to `"object"`.
58
+ */
59
+ type?: "object" | "function";
60
+ };
61
+ /**
62
+ * @import { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
63
+ * @import { Plugin, TransformResult } from "vite"
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
+ */
80
+ /**
81
+ * @typedef {{
82
+ * query<T extends Submittable>(queryStream: T): T;
83
+ * query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
84
+ * query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
85
+ * query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
86
+ * }} QueryableObject
87
+ */
88
+ /** @typedef {QueryableObject | Promise<QueryableObject>} Queryable */
89
+ /**
90
+ * @typedef {object} PostgresOptions
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/"`.
92
+ * @property {string} [rootFolder] Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
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`).
96
+ */
97
+ /**
98
+ * @typedef {object} RuntimeOptions
99
+ * @property {string} [module] Module specifier the default runtime is imported from in every generated module. Defaults to `"#db-runtime"`.
100
+ * @property {string} [export] Named export of `module` providing the default runtime. Defaults to `"db"`.
101
+ * @property {"object" | "function"} [type] Whether the export is a `Queryable` itself or a function returning one (possibly async). Defaults to `"object"`.
102
+ */
103
+ /**
104
+ * @param {PostgresOptions} [opts]
105
+ * @returns {Promise<Plugin>}
106
+ */
107
+ export default function postgres(opts?: PostgresOptions): Promise<Plugin>;
108
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["plugin.js"],"names":[],"mappings":"AAMG,OAAQ,KAAA,EAAE,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,IAAI,CAChJ;AAAA,OAAQ,KAAA,EAAE,MAAM,EAAmB,MAAM,MAAM,CAAA;AAK/C,YAWG,QAAQ,GAXD;IACR,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,IAAI,MAAM,CAAC;IACrB,KAAK,IAAI,MAAM,CAAC;IAChB,WAAW,IAAI,MAAM,CAAC;CACvB,CAAU;AAIX,YAKG,eAAe,GALR;IACR,KAAK,CAAC,CAAC,SAAS,WAAW,EAAE,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC;IAChD,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,GAAG,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,EAAE,EAAE,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC;IACzI,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IACrG,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,EAAE,iBAAiB,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;CACtJ,CAAiB;AAGjB,YAAsD,SAAS,GAArD,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAAW;AAGhE,YAAkB,eAAe,GACjC;;;;IAAoB,WAAW,AAA/B,CACA,EADW,MAAM,CACjB;;;;IAAoB,UAAU,AAA9B,CACA,EADW,MAAM,CACjB;;;;IAA4B,OAAO,AAAnC,CACA,EADW,cAAc,CACzB;;;;IAA6C,QAAQ,AAArD,CACA,EADW,OAAO,GAAG,MAAM,GAAG,YAAY,CAC1C;;;;IAA0E,cAAc,AAAxF,CACF,EADa,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CACzE;CAAA,CAAA;AAGE,YAAkB,cAAc,GAChC;;;;IAAoB,MAAM,AAA1B,CACA,EADW,MAAM,CACjB;;;;IAAoB,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,30 +1,71 @@
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
- * @param {object} [opts]
7
- * @param {string} [opts.typesFolder="node_modules/@types/vite-plugin-postgres-import/"]
8
- * Path to a folder where all declarations are kept relative to `rootFolder`, i.e. a file at path
9
- * `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`.
10
- * Make sure you include this one in your `tsconfig.json`.
11
- * @param {string} [opts.rootFolder=process.cwd()]
12
- * Root folder relative to which path calculation will be happening.
13
- * @param {object} [opts.runtime] Where the default runtime comes from.
14
- * @param {string} [opts.runtime.module="#db-runtime"]
15
- * Module specifier the default runtime is imported from in every generated module.
16
- * @param {string} [opts.runtime.export="db"]
17
- * Named export of `runtime.module` providing the default runtime.
18
- * @param {"object" | "function"} [opts.runtime.type="object"]
19
- * Whether the runtime export is a `Queryable` itself or a function returning one (possibly async).
20
- * @returns {Promise<import('vite').Plugin>}
7
+ * @import { ClientConfig, QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
8
+ * @import { Plugin, TransformResult } from "vite"
21
9
  */
22
- export default async function postgres({
23
- typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
24
- rootFolder = process.cwd(),
25
- runtime: { module: runtimeModule = "#db-runtime", export: runtimeExport = "db", type: runtimeType = "object" } = {},
26
- } = {}) {
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
+
27
+ /**
28
+ * @typedef {{
29
+ * query<T extends Submittable>(queryStream: T): T;
30
+ * query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
31
+ * query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
32
+ * query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
33
+ * }} QueryableObject
34
+ */
35
+
36
+ /** @typedef {QueryableObject | Promise<QueryableObject>} Queryable */
37
+
38
+ /**
39
+ * @typedef {object} PostgresOptions
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/"`.
41
+ * @property {string} [rootFolder] Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
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`).
45
+ */
46
+
47
+ /**
48
+ * @typedef {object} RuntimeOptions
49
+ * @property {string} [module] Module specifier the default runtime is imported from in every generated module. Defaults to `"#db-runtime"`.
50
+ * @property {string} [export] Named export of `module` providing the default runtime. Defaults to `"db"`.
51
+ * @property {"object" | "function"} [type] Whether the export is a `Queryable` itself or a function returning one (possibly async). Defaults to `"object"`.
52
+ */
53
+
54
+ /**
55
+ * @param {PostgresOptions} [opts]
56
+ * @returns {Promise<Plugin>}
57
+ */
58
+ export default async function postgres(opts = {}) {
59
+ const {
60
+ typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
61
+ rootFolder = process.cwd(),
62
+ runtime: { module: runtimeModule = "#db-runtime", export: runtimeExport = "db", type: runtimeType = "object" } = {},
63
+ describe = false,
64
+ typesOverrides,
65
+ } = opts;
27
66
  const runtime = { module: runtimeModule, export: runtimeExport, type: runtimeType };
67
+ const db = describe ? describer(describe, typesOverrides) : undefined;
68
+ let dev = false;
28
69
  const imports = await fs
29
70
  .readFile(path.join(rootFolder, "package.json"), "utf8")
30
71
  .then((s) => JSON.parse(s).imports)
@@ -59,6 +100,9 @@ export default async function postgres({
59
100
 
60
101
  return {
61
102
  name: "vite-plugin-sql-postgres",
103
+ configResolved(config) {
104
+ dev = config.command === "serve" && config.mode !== "test";
105
+ },
62
106
  transform: {
63
107
  filter: {
64
108
  id: /\.sql$/,
@@ -84,66 +128,75 @@ export default async function postgres({
84
128
  handleHotUpdate({ modules }) {
85
129
  return modules;
86
130
  },
131
+ buildEnd() {
132
+ db?.end();
133
+ },
87
134
  };
88
135
 
89
- /** @returns {Promise<import("vite").TransformResult>} */
136
+ /** @returns {Promise<TransformResult | undefined>} */
90
137
  async function transform(/** @type {string} */ sql, /** @type {string} */ id) {
91
138
  if (!/\.sql$/.test(id)) {
92
139
  return;
93
140
  }
94
141
  const filename = path.relative(rootFolder, id);
95
142
 
143
+ const modules = parseModule(sql).toArray();
144
+ if (dev) await db?.annotate(modules, filename);
145
+
96
146
  const { js, dts, moduleDeclaration, mappings } = codegen(
97
- parseModule(sql),
147
+ modules,
98
148
  filename,
99
149
  moduleName(filename),
100
150
  runtime,
101
151
  );
102
152
 
103
- const dtsFolder = path.join(typesFolder, path.relative(rootFolder, path.dirname(id)));
104
- const dtsName = path.basename(filename, ".sql") + ".sql.d.ts";
105
- await fs.mkdir(dtsFolder, { recursive: true });
106
- await fs.writeFile(path.join(dtsFolder, dtsName), dts + `//# sourceMappingURL=${dtsName}.map\n`);
107
- await fs.writeFile(
108
- path.join(dtsFolder, dtsName + ".map"),
109
- JSON.stringify({
110
- version: 3,
111
- file: dtsName,
112
- sources: [path.relative(dtsFolder, id).replaceAll(path.sep, "/")],
113
- names: [],
114
- mappings: encodeMappings([
115
- { genLine: 0, genCol: 0, srcLine: 0, srcCol: 0 },
116
- ...exportMappings(dts, mappings),
117
- ]),
118
- }),
119
- );
120
- if (moduleDeclaration.length) {
121
- moduleDeclarations.set(filename, { text: moduleDeclaration.join("\n"), mappings, id });
122
- let line = 0;
123
- const sources = [];
124
- const combined = [];
125
- const texts = [];
126
- for (const { text, mappings, id } of moduleDeclarations.values()) {
127
- const srcIdx = sources.push(path.relative(typesFolder, id).replaceAll(path.sep, "/")) - 1;
128
- combined.push({ genLine: line, genCol: 0, srcIdx, srcLine: 0, srcCol: 0 });
129
- combined.push(...exportMappings(text, mappings, srcIdx, line));
130
- texts.push(text);
131
- line += text.split("\n").length;
132
- }
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`);
133
159
  await fs.writeFile(
134
- path.join(typesFolder, "modules.sql.d.ts"),
135
- texts.join("\n") + "\n//# sourceMappingURL=modules.sql.d.ts.map\n",
136
- );
137
- await fs.writeFile(
138
- path.join(typesFolder, "modules.sql.d.ts.map"),
160
+ path.join(dtsFolder, dtsName + ".map"),
139
161
  JSON.stringify({
140
162
  version: 3,
141
- file: "modules.sql.d.ts",
142
- sources,
163
+ file: dtsName,
164
+ sources: [path.relative(dtsFolder, id).replaceAll(path.sep, "/")],
143
165
  names: [],
144
- mappings: encodeMappings(combined),
166
+ mappings: encodeMappings([
167
+ { genLine: 0, genCol: 0, srcLine: 0, srcCol: 0 },
168
+ ...exportMappings(dts, mappings),
169
+ ]),
145
170
  }),
146
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
+ }
147
200
  }
148
201
 
149
202
  return {