vite-plugin-postgres-import 0.1.0 → 0.3.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 (3) hide show
  1. package/README.md +5 -5
  2. package/package.json +1 -1
  3. package/parse.js +26 -25
package/README.md CHANGED
@@ -63,23 +63,23 @@ const [all, something, deleted] = await MultiStatement(pool, { foo: "bar" });
63
63
 
64
64
  ## Annotations
65
65
 
66
- The only 3 sqlc annotations that are available are the following:
66
+ The only 4 sqlc annotations that are available are the following:
67
67
 
68
68
  - `:execresult` - default if neither of the 3 below are provided, returns `QueryResult`.
69
69
 
70
- ex: `Query<R extends QueryResultRow = { ... }>(c): Promise<QueryResult<R>>`
70
+ ex.: `Query<R extends QueryResultRow = { ... }>(c): Promise<QueryResult<R>>`
71
71
 
72
72
  - `:one` - returns template argument, or the default-parsed ones from the select/returning clause.
73
73
 
74
- ex: `Query<R = { ... }>(c): Promise<R>`
74
+ ex.: `Query<R extends QueryResultRow = { ... }>(c): Promise<R>`
75
75
 
76
76
  - `:many` - returns template argument as an array, or the default-parsed ones from the select/returning clause.
77
77
 
78
- ex: `Query<R = { ... }>(c): Promise<R[]>`
78
+ ex.: `Query<R extends QueryResultRow = { ... }>(c): Promise<R[]>`
79
79
 
80
80
  - `:execrows` - returns number of affected rows.
81
81
 
82
- ex: `Query(c): Promise<number>`
82
+ ex.: `Query(c): Promise<number>`
83
83
 
84
84
  2 additional annotations not found in sqlc are available:
85
85
 
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.3.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) {
@@ -36,32 +36,33 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
36
36
  rowArray: meta?.tags?.includes(":array"),
37
37
  query,
38
38
  params: new Set(query.matchAll(/(?<![\w_\\:]):[\w_]+/g).map((m) => m?.[0]?.substring(1))),
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
- })
39
+ selectFields: selectList(/(with.*\))?\s*select\s+(?<select>.*)/ims.exec(query)?.groups?.select)
40
+ .map(parseResultSymbol)
41
+ .toArray(),
42
+ returningClause: selectList(/returning\s+(?<returning>[\w\s\.\",\*]+);?$/im.exec(query)?.groups?.returning)
43
+ .map(parseResultSymbol)
58
44
  .toArray(),
59
- returningClause: /(with.*)?(update|delete|insert).*returning\s+(?<returning>.*)$/
60
- .exec(query)
61
- ?.groups?.returning?.split(",")
62
- ?.filter(Boolean),
63
45
  };
64
46
 
47
+ function parseResultSymbol(/** @type {string} */ s) {
48
+ s = s.trim();
49
+ while (s.startsWith("(") && s.endsWith(")")) {
50
+ // unwrap
51
+ s = s.substring(1, s.length - 1).trim();
52
+ }
53
+ const gotDatAs = /as ([_\w"]+)$/i.exec(s)?.[1];
54
+ if (gotDatAs) return gotDatAs.endsWith('"') && gotDatAs.includes(" ") ? `'${gotDatAs}'` : gotDatAs;
55
+ const uncCall = /^([\w_]+)[\(\[].*[\)\]]$/.exec(s)?.[1];
56
+ if (uncCall) return uncCall;
57
+ const subselect = /^select\s+([\w_]+)[,]?.*$/i.exec(s)?.[1];
58
+ if (subselect) return subselect;
59
+ const word = /(^|\.)[a-zA-Z]+$/.exec(s)?.[0];
60
+ if (word) return word[0] === "." ? word.substring(1) : word;
61
+ const star = /^\*$/.exec(s)?.[0];
62
+ if (star) return "[column: string]";
63
+ return '"?column?"'; // ???
64
+ }
65
+
65
66
  function* selectList(/** @type {string=} */ select) {
66
67
  if (!select) return;
67
68
  let index = 0;
@@ -69,7 +70,7 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
69
70
  let inString = null;
70
71
  for (const token of select.matchAll(/([:\w_]+)|[\(\)'("")]|[,]|\*/g)) {
71
72
  if (token[0] === inString) inString = null;
72
- else if (token[0] === "'" || token[0] === "$$") inString = token[0];
73
+ else if (token[0] === "'" || token[0] === "$$" || token[0] === '"') inString = token[0];
73
74
  if (token[0] === ")") nestingLevel--;
74
75
  else if (token[0] === "(") nestingLevel++;
75
76
  else if (token[0] === "," && nestingLevel === 0 && !inString) {