vite-plugin-postgres-import 0.1.0 → 0.2.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/parse.js +25 -22
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.1.0",
4
+ "version": "0.2.0",
5
5
  "main": "plugin.js",
6
6
  "type": "module",
7
7
  "author": "SirKostya009",
package/parse.js CHANGED
@@ -10,7 +10,7 @@ export function* parseModule(/** @type {string} */ sql) {
10
10
  const rema = matches[i];
11
11
  const [first, ...rest] = sql
12
12
  .substring(rema.index, i + 1 < matches.length ? matches[i + 1].index : sql.length)
13
- .split(";")
13
+ .split(/;\s*$/m)
14
14
  .map((s) => s.trim())
15
15
  .filter(Boolean);
16
16
  if (rest.length > 0) {
@@ -37,31 +37,34 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
37
37
  query,
38
38
  params: new Set(query.matchAll(/(?<![\w_\\:]):[\w_]+/g).map((m) => m?.[0]?.substring(1))),
39
39
  selectFields: selectList(/(with.*\))?\s*select\s+(?<select>.*)/ms.exec(query)?.groups?.select)
40
- .map((s) => s.trim())
41
- .map((s) => {
42
- while (s.startsWith("(") && s.endsWith(")")) {
43
- // unwrap
44
- s = s.substring(1, s.length - 1).trim();
45
- }
46
- const gotDatAs = /as ([_\w"]+)$/.exec(s)?.[1];
47
- if (gotDatAs) return gotDatAs.endsWith('"') && gotDatAs.includes(" ") ? `'${gotDatAs}'` : gotDatAs;
48
- const uncCall = /^([\w_]+)[\(\[].*[\)\]]$/.exec(s)?.[1];
49
- if (uncCall) return uncCall;
50
- const subselect = /^select\s+([\w_]+)[,]?.*$/.exec(s)?.[1];
51
- if (subselect) return subselect;
52
- const word = /(^|\.)[a-zA-Z]+$/.exec(s)?.[0];
53
- if (word) return word[0] === "." ? word.substring(1) : word;
54
- const star = /^\*$/.exec(s)?.[0];
55
- if (star) return "[column: string]";
56
- return '"?column?"'; // ???
57
- })
40
+ .map(parseResultSymbol)
58
41
  .toArray(),
59
- returningClause: /(with.*)?(update|delete|insert).*returning\s+(?<returning>.*)$/
42
+ returningClause: /returning\s+(?<returning>[\w\s,\*]+);?$/
60
43
  .exec(query)
61
- ?.groups?.returning?.split(",")
62
- ?.filter(Boolean),
44
+ ?.groups?.returning?.split(/,\s*/)
45
+ ?.filter(Boolean)
46
+ ?.map(parseResultSymbol),
63
47
  };
64
48
 
49
+ function parseResultSymbol(/** @type {string} */ s) {
50
+ s = s.trim();
51
+ while (s.startsWith("(") && s.endsWith(")")) {
52
+ // unwrap
53
+ s = s.substring(1, s.length - 1).trim();
54
+ }
55
+ const gotDatAs = /as ([_\w"]+)$/.exec(s)?.[1];
56
+ if (gotDatAs) return gotDatAs.endsWith('"') && gotDatAs.includes(" ") ? `'${gotDatAs}'` : gotDatAs;
57
+ const uncCall = /^([\w_]+)[\(\[].*[\)\]]$/.exec(s)?.[1];
58
+ if (uncCall) return uncCall;
59
+ const subselect = /^select\s+([\w_]+)[,]?.*$/.exec(s)?.[1];
60
+ if (subselect) return subselect;
61
+ const word = /(^|\.)[a-zA-Z]+$/.exec(s)?.[0];
62
+ if (word) return word[0] === "." ? word.substring(1) : word;
63
+ const star = /^\*$/.exec(s)?.[0];
64
+ if (star) return "[column: string]";
65
+ return '"?column?"'; // ???
66
+ }
67
+
65
68
  function* selectList(/** @type {string=} */ select) {
66
69
  if (!select) return;
67
70
  let index = 0;