vite-plugin-postgres-import 0.2.0 → 0.3.1
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 +5 -5
- package/package.json +1 -1
- package/parse.js +12 -12
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
|
|
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
|
|
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
|
|
74
|
+
ex.: `Query<R = { ... }>(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
|
|
78
|
+
ex.: `Query<R extends QueryResultRow = { ... }>(c): Promise<R[]>`
|
|
79
79
|
|
|
80
80
|
- `:execrows` - returns number of affected rows.
|
|
81
81
|
|
|
82
|
-
ex
|
|
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
package/parse.js
CHANGED
|
@@ -36,14 +36,12 @@ 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>.*)/
|
|
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)
|
|
40
43
|
.map(parseResultSymbol)
|
|
41
44
|
.toArray(),
|
|
42
|
-
returningClause: /returning\s+(?<returning>[\w\s,\*]+);?$/
|
|
43
|
-
.exec(query)
|
|
44
|
-
?.groups?.returning?.split(/,\s*/)
|
|
45
|
-
?.filter(Boolean)
|
|
46
|
-
?.map(parseResultSymbol),
|
|
47
45
|
};
|
|
48
46
|
|
|
49
47
|
function parseResultSymbol(/** @type {string} */ s) {
|
|
@@ -52,11 +50,11 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
|
|
|
52
50
|
// unwrap
|
|
53
51
|
s = s.substring(1, s.length - 1).trim();
|
|
54
52
|
}
|
|
55
|
-
const gotDatAs = /as ([_\w"]+)
|
|
53
|
+
const gotDatAs = /as ([_\w"]+)$/i.exec(s)?.[1];
|
|
56
54
|
if (gotDatAs) return gotDatAs.endsWith('"') && gotDatAs.includes(" ") ? `'${gotDatAs}'` : gotDatAs;
|
|
57
55
|
const uncCall = /^([\w_]+)[\(\[].*[\)\]]$/.exec(s)?.[1];
|
|
58
56
|
if (uncCall) return uncCall;
|
|
59
|
-
const subselect = /^select\s+([\w_]+)[,]
|
|
57
|
+
const subselect = /^select\s+([\w_]+)[,]?.*$/i.exec(s)?.[1];
|
|
60
58
|
if (subselect) return subselect;
|
|
61
59
|
const word = /(^|\.)[a-zA-Z]+$/.exec(s)?.[0];
|
|
62
60
|
if (word) return word[0] === "." ? word.substring(1) : word;
|
|
@@ -72,7 +70,7 @@ function metadata(/** @type {string} */ s, /** @type {ReturnType<typeof metadata
|
|
|
72
70
|
let inString = null;
|
|
73
71
|
for (const token of select.matchAll(/([:\w_]+)|[\(\)'("")]|[,]|\*/g)) {
|
|
74
72
|
if (token[0] === inString) inString = null;
|
|
75
|
-
else if (token[0] === "'" || token[0] === "$$") inString = token[0];
|
|
73
|
+
else if (token[0] === "'" || token[0] === "$$" || token[0] === '"') inString = token[0];
|
|
76
74
|
if (token[0] === ")") nestingLevel--;
|
|
77
75
|
else if (token[0] === "(") nestingLevel++;
|
|
78
76
|
else if (token[0] === "," && nestingLevel === 0 && !inString) {
|
|
@@ -138,9 +136,11 @@ export function ${module.name}<${module.returnSymbols
|
|
|
138
136
|
? `unknown[]`
|
|
139
137
|
: `[ ${r?.map(() => `unknown`)?.join(", ") ?? ""} ]`
|
|
140
138
|
}`
|
|
141
|
-
: `R${i + 1}
|
|
142
|
-
r
|
|
143
|
-
}${r.length > 3 ? "\
|
|
139
|
+
: `R${i + 1}${module.execution[i] === ":one" ? `` : ` extends QueryResultRow`} = {${
|
|
140
|
+
r.length > 3 ? "\n\t" : " "
|
|
141
|
+
}${r?.map((r) => `${r}: unknown`)?.join(r.length > 3 ? ";\n\t" : "; ") ?? ""}${
|
|
142
|
+
r.length > 3 ? "\n" : " "
|
|
143
|
+
}}`
|
|
144
144
|
)
|
|
145
145
|
.join(", ")}>(
|
|
146
146
|
tx: Pool | ClientBase | Promise<ClientBase>,${keys.length ? `\n\tparams: Record<${keys}, unknown>` : ``}
|