shutro 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/audit/index.d.ts +59 -0
- package/dist/audit/index.js +96 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +11 -0
- package/dist/cli/cli.d.ts +10 -0
- package/dist/cli/cli.js +164 -0
- package/dist/cli/config.d.ts +18 -0
- package/dist/cli/config.js +49 -0
- package/dist/cli/errors.d.ts +16 -0
- package/dist/cli/errors.js +23 -0
- package/dist/cli/generate/generate.d.ts +33 -0
- package/dist/cli/generate/generate.js +84 -0
- package/dist/cli/generate/render.d.ts +23 -0
- package/dist/cli/generate/render.js +249 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.js +65 -0
- package/dist/cli/inspect.d.ts +10 -0
- package/dist/cli/inspect.js +82 -0
- package/dist/cli/packages.d.ts +6 -0
- package/dist/cli/packages.js +6 -0
- package/dist/cli/project.d.ts +18 -0
- package/dist/cli/project.js +39 -0
- package/dist/cli/resolve.d.ts +2 -0
- package/dist/cli/resolve.js +28 -0
- package/dist/core/compute/graph.d.ts +52 -0
- package/dist/core/compute/graph.js +162 -0
- package/dist/core/data/data.d.ts +59 -0
- package/dist/core/data/data.js +119 -0
- package/dist/core/data/load.d.ts +37 -0
- package/dist/core/data/load.js +236 -0
- package/dist/core/env/env.d.ts +468 -0
- package/dist/core/env/env.js +916 -0
- package/dist/core/env/identity.d.ts +43 -0
- package/dist/core/env/identity.js +65 -0
- package/dist/core/env/inspector.d.ts +55 -0
- package/dist/core/env/inspector.js +85 -0
- package/dist/core/env/time.d.ts +12 -0
- package/dist/core/env/time.js +38 -0
- package/dist/core/errors.d.ts +256 -0
- package/dist/core/errors.js +360 -0
- package/dist/core/events/events.d.ts +44 -0
- package/dist/core/events/events.js +58 -0
- package/dist/core/events/workers.d.ts +56 -0
- package/dist/core/events/workers.js +155 -0
- package/dist/core/index.d.ts +51 -0
- package/dist/core/index.js +42 -0
- package/dist/core/model/company.d.ts +6 -0
- package/dist/core/model/company.js +31 -0
- package/dist/core/model/faces.d.ts +42 -0
- package/dist/core/model/faces.js +1 -0
- package/dist/core/model/field.d.ts +479 -0
- package/dist/core/model/field.js +461 -0
- package/dist/core/model/generated.d.ts +77 -0
- package/dist/core/model/generated.js +1 -0
- package/dist/core/model/id.d.ts +33 -0
- package/dist/core/model/id.js +4 -0
- package/dist/core/model/model.d.ts +387 -0
- package/dist/core/model/model.js +566 -0
- package/dist/core/model/partition.d.ts +31 -0
- package/dist/core/model/partition.js +71 -0
- package/dist/core/model/related.d.ts +17 -0
- package/dist/core/model/related.js +60 -0
- package/dist/core/model/typed.d.ts +107 -0
- package/dist/core/model/typed.js +46 -0
- package/dist/core/money/currency.d.ts +39 -0
- package/dist/core/money/currency.js +37 -0
- package/dist/core/money/decimal.d.ts +26 -0
- package/dist/core/money/decimal.js +51 -0
- package/dist/core/money/fx.d.ts +68 -0
- package/dist/core/money/fx.js +124 -0
- package/dist/core/money/money.d.ts +84 -0
- package/dist/core/money/money.js +173 -0
- package/dist/core/money/quantity.d.ts +67 -0
- package/dist/core/money/quantity.js +108 -0
- package/dist/core/money/rounding.d.ts +11 -0
- package/dist/core/money/rounding.js +12 -0
- package/dist/core/recordset/aggregate.d.ts +22 -0
- package/dist/core/recordset/aggregate.js +258 -0
- package/dist/core/recordset/audit.d.ts +33 -0
- package/dist/core/recordset/audit.js +262 -0
- package/dist/core/recordset/compute.d.ts +36 -0
- package/dist/core/recordset/compute.js +414 -0
- package/dist/core/recordset/constraints.d.ts +9 -0
- package/dist/core/recordset/constraints.js +61 -0
- package/dist/core/recordset/domain.d.ts +31 -0
- package/dist/core/recordset/domain.js +300 -0
- package/dist/core/recordset/onchange.d.ts +43 -0
- package/dist/core/recordset/onchange.js +240 -0
- package/dist/core/recordset/recordset.d.ts +110 -0
- package/dist/core/recordset/recordset.js +789 -0
- package/dist/core/recordset/relations.d.ts +15 -0
- package/dist/core/recordset/relations.js +13 -0
- package/dist/core/recordset/rls.d.ts +15 -0
- package/dist/core/recordset/rls.js +108 -0
- package/dist/core/recordset/security.d.ts +56 -0
- package/dist/core/recordset/security.js +269 -0
- package/dist/core/recordset/sequences.d.ts +31 -0
- package/dist/core/recordset/sequences.js +154 -0
- package/dist/core/recordset/source.d.ts +9 -0
- package/dist/core/recordset/source.js +19 -0
- package/dist/core/recordset/sqlErrors.d.ts +6 -0
- package/dist/core/recordset/sqlErrors.js +39 -0
- package/dist/core/recordset/transitions.d.ts +10 -0
- package/dist/core/recordset/transitions.js +80 -0
- package/dist/core/recordset/types.d.ts +415 -0
- package/dist/core/recordset/types.js +8 -0
- package/dist/core/recordset/values.d.ts +33 -0
- package/dist/core/recordset/values.js +177 -0
- package/dist/core/recordset/writes.d.ts +42 -0
- package/dist/core/recordset/writes.js +786 -0
- package/dist/core/registry/ddl.d.ts +31 -0
- package/dist/core/registry/ddl.js +174 -0
- package/dist/core/registry/extension.d.ts +94 -0
- package/dist/core/registry/extension.js +90 -0
- package/dist/core/registry/module.d.ts +89 -0
- package/dist/core/registry/module.js +98 -0
- package/dist/core/registry/registry.d.ts +130 -0
- package/dist/core/registry/registry.js +591 -0
- package/dist/core/security/runtime.d.ts +102 -0
- package/dist/core/security/runtime.js +232 -0
- package/dist/driver-pg/driver.d.ts +88 -0
- package/dist/driver-pg/driver.js +177 -0
- package/dist/driver-pg/errors.d.ts +72 -0
- package/dist/driver-pg/errors.js +182 -0
- package/dist/driver-pg/index.d.ts +7 -0
- package/dist/driver-pg/index.js +7 -0
- package/dist/i18n/index.d.ts +25 -0
- package/dist/i18n/index.js +51 -0
- package/dist/migrate/apply.d.ts +78 -0
- package/dist/migrate/apply.js +167 -0
- package/dist/migrate/catalog.d.ts +10 -0
- package/dist/migrate/catalog.js +122 -0
- package/dist/migrate/diff.d.ts +98 -0
- package/dist/migrate/diff.js +160 -0
- package/dist/migrate/errors.d.ts +21 -0
- package/dist/migrate/errors.js +31 -0
- package/dist/migrate/index.d.ts +18 -0
- package/dist/migrate/index.js +18 -0
- package/dist/migrate/render.d.ts +5 -0
- package/dist/migrate/render.js +101 -0
- package/dist/migrate/schema.d.ts +61 -0
- package/dist/migrate/schema.js +233 -0
- package/dist/security/index.d.ts +147 -0
- package/dist/security/index.js +278 -0
- package/dist/sequence/index.d.ts +63 -0
- package/dist/sequence/index.js +106 -0
- package/dist/sql/ast.d.ts +49 -0
- package/dist/sql/ast.js +47 -0
- package/dist/sql/builder/builder.d.ts +215 -0
- package/dist/sql/builder/builder.js +308 -0
- package/dist/sql/builder/expression.d.ts +80 -0
- package/dist/sql/builder/expression.js +126 -0
- package/dist/sql/compile.d.ts +18 -0
- package/dist/sql/compile.js +41 -0
- package/dist/sql/errors.d.ts +13 -0
- package/dist/sql/errors.js +16 -0
- package/dist/sql/index.d.ts +19 -0
- package/dist/sql/index.js +19 -0
- package/dist/sql/tag.d.ts +47 -0
- package/dist/sql/tag.js +123 -0
- package/package.json +82 -4
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { compile } from '../compile.js';
|
|
3
|
+
import { SqlError } from '../errors.js';
|
|
4
|
+
import { sql } from '../tag.js';
|
|
5
|
+
import { columnRef, compare, expressionBuilder, isExpression, } from './expression.js';
|
|
6
|
+
function parseTable(expression) {
|
|
7
|
+
if (typeof expression !== 'string') {
|
|
8
|
+
throw new SqlError('SQL_INVALID_IDENTIFIER', 'A table reference must be a string.', "Use 'table' or 'table as alias'.");
|
|
9
|
+
}
|
|
10
|
+
const at = expression.indexOf(' as ');
|
|
11
|
+
if (at < 0) {
|
|
12
|
+
sql.id(expression);
|
|
13
|
+
return { table: expression, alias: undefined };
|
|
14
|
+
}
|
|
15
|
+
const table = expression.slice(0, at);
|
|
16
|
+
const alias = expression.slice(at + 4);
|
|
17
|
+
sql.id(table);
|
|
18
|
+
sql.id(alias);
|
|
19
|
+
return { table, alias };
|
|
20
|
+
}
|
|
21
|
+
function renderSource(source) {
|
|
22
|
+
return source.alias === undefined
|
|
23
|
+
? sql.id(source.table)
|
|
24
|
+
: sql `${sql.id(source.table)} AS ${sql.id(source.alias)}`;
|
|
25
|
+
}
|
|
26
|
+
function renderCtes(ctes) {
|
|
27
|
+
if (ctes.length === 0)
|
|
28
|
+
return sql.empty;
|
|
29
|
+
return sql `WITH ${sql.join(ctes.map((c) => sql `${sql.id(c.name)} AS (${c.query.fragment()})`))} `;
|
|
30
|
+
}
|
|
31
|
+
function renderWhere(keyword, conditions) {
|
|
32
|
+
if (conditions.length === 0)
|
|
33
|
+
return sql.empty;
|
|
34
|
+
return sql ` ${keyword} ${sql.join(conditions.map((c) => c.fragment), sql ` AND `)}`;
|
|
35
|
+
}
|
|
36
|
+
function renderReturning(columns) {
|
|
37
|
+
if (columns.length === 0)
|
|
38
|
+
return sql.empty;
|
|
39
|
+
return sql ` RETURNING ${sql.join(columns.map(columnRef))}`;
|
|
40
|
+
}
|
|
41
|
+
function selectItem(item) {
|
|
42
|
+
const at = item.indexOf(' as ');
|
|
43
|
+
if (at < 0)
|
|
44
|
+
return columnRef(item);
|
|
45
|
+
return sql `${columnRef(item.slice(0, at))} AS ${sql.id(item.slice(at + 4))}`;
|
|
46
|
+
}
|
|
47
|
+
function value(v) {
|
|
48
|
+
return isExpression(v) ? v.fragment : sql `${v}`;
|
|
49
|
+
}
|
|
50
|
+
function condition(args) {
|
|
51
|
+
const [first, op, rhs] = args;
|
|
52
|
+
if (typeof first === 'function')
|
|
53
|
+
return first(expressionBuilder);
|
|
54
|
+
return compare(first, op, rhs);
|
|
55
|
+
}
|
|
56
|
+
function count(n, what) {
|
|
57
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
58
|
+
throw new SqlError('SQL_INVALID_PARAM', `${what} must be a non-negative integer, got ${String(n)}.`, `Pass e.g. ${what}(80).`);
|
|
59
|
+
}
|
|
60
|
+
return n;
|
|
61
|
+
}
|
|
62
|
+
/** A `SELECT` query. Immutable: every method returns a new query. */
|
|
63
|
+
export class SelectQuery {
|
|
64
|
+
#state;
|
|
65
|
+
/** @internal */
|
|
66
|
+
constructor(state) {
|
|
67
|
+
this.#state = Object.freeze(state);
|
|
68
|
+
}
|
|
69
|
+
#with(changes) {
|
|
70
|
+
return new _a({ ...this.#state, ...changes });
|
|
71
|
+
}
|
|
72
|
+
select(arg) {
|
|
73
|
+
const items = typeof arg === 'function'
|
|
74
|
+
? arg(expressionBuilder).map((a) => sql `${a.expression.fragment} AS ${sql.id(a.alias)}`)
|
|
75
|
+
: arg.map(selectItem);
|
|
76
|
+
return this.#with({ columns: [...this.#state.columns, ...items] });
|
|
77
|
+
}
|
|
78
|
+
/** `SELECT *`. */
|
|
79
|
+
selectAll() {
|
|
80
|
+
return this.#with({ selectAll: true });
|
|
81
|
+
}
|
|
82
|
+
innerJoin(table, ...on) {
|
|
83
|
+
return this.#join('INNER', table, on);
|
|
84
|
+
}
|
|
85
|
+
leftJoin(table, ...on) {
|
|
86
|
+
return this.#join('LEFT', table, on);
|
|
87
|
+
}
|
|
88
|
+
#join(kind, table, on) {
|
|
89
|
+
const [a, b] = on;
|
|
90
|
+
const expression = typeof a === 'function'
|
|
91
|
+
? a(expressionBuilder)
|
|
92
|
+
: compare(a, '=', expressionBuilder.col(b));
|
|
93
|
+
return new _a({
|
|
94
|
+
...this.#state,
|
|
95
|
+
joins: [...this.#state.joins, { kind, source: parseTable(table), on: expression }],
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
where(...args) {
|
|
99
|
+
return this.#with({ where: [...this.#state.where, condition(args)] });
|
|
100
|
+
}
|
|
101
|
+
groupBy(columns) {
|
|
102
|
+
for (const c of columns)
|
|
103
|
+
columnRef(c);
|
|
104
|
+
return this.#with({ groupBy: [...this.#state.groupBy, ...columns], groupingSets: undefined });
|
|
105
|
+
}
|
|
106
|
+
/** `GROUP BY GROUPING SETS ((a, b), (a), ())`. Replaces `groupBy`. */
|
|
107
|
+
groupingSets(sets) {
|
|
108
|
+
for (const set of sets)
|
|
109
|
+
for (const c of set)
|
|
110
|
+
columnRef(c);
|
|
111
|
+
return this.#with({ groupingSets: sets.map((s) => [...s]), groupBy: [] });
|
|
112
|
+
}
|
|
113
|
+
having(...args) {
|
|
114
|
+
return this.#with({ having: [...this.#state.having, condition(args)] });
|
|
115
|
+
}
|
|
116
|
+
orderBy(column, direction = 'asc', options) {
|
|
117
|
+
columnRef(column);
|
|
118
|
+
return this.#with({
|
|
119
|
+
orderBy: [...this.#state.orderBy, { column, direction, nulls: options?.nulls }],
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
limit(n) {
|
|
123
|
+
return this.#with({ limit: count(n, 'limit') });
|
|
124
|
+
}
|
|
125
|
+
offset(n) {
|
|
126
|
+
return this.#with({ offset: count(n, 'offset') });
|
|
127
|
+
}
|
|
128
|
+
fragment() {
|
|
129
|
+
const s = this.#state;
|
|
130
|
+
if (!s.selectAll && s.columns.length === 0) {
|
|
131
|
+
throw new SqlError('SQL_EMPTY_SELECT', 'The select query has no columns.', 'Call .select([...]) or .selectAll() before compiling.');
|
|
132
|
+
}
|
|
133
|
+
const parts = [
|
|
134
|
+
sql `${renderCtes(s.ctes)}SELECT ${s.selectAll ? sql `*` : sql.join(s.columns)} FROM ${renderSource(s.from)}`,
|
|
135
|
+
];
|
|
136
|
+
for (const j of s.joins)
|
|
137
|
+
parts.push(sql ` ${j.kind === 'INNER' ? sql `INNER JOIN` : sql `LEFT JOIN`} ${renderSource(j.source)} ON ${j.on.fragment}`);
|
|
138
|
+
parts.push(renderWhere(sql `WHERE`, s.where));
|
|
139
|
+
if (s.groupingSets !== undefined) {
|
|
140
|
+
parts.push(sql ` GROUP BY GROUPING SETS (${sql.join(s.groupingSets.map((set) => sql `(${sql.join(set.map(columnRef))})`))})`);
|
|
141
|
+
}
|
|
142
|
+
else if (s.groupBy.length > 0) {
|
|
143
|
+
parts.push(sql ` GROUP BY ${sql.join(s.groupBy.map(columnRef))}`);
|
|
144
|
+
}
|
|
145
|
+
parts.push(renderWhere(sql `HAVING`, s.having));
|
|
146
|
+
if (s.orderBy.length > 0) {
|
|
147
|
+
parts.push(sql ` ORDER BY ${sql.join(s.orderBy.map((o) => sql `${columnRef(o.column)} ${o.direction === 'desc' ? sql `DESC` : sql `ASC`}${o.nulls === undefined
|
|
148
|
+
? sql.empty
|
|
149
|
+
: o.nulls === 'first'
|
|
150
|
+
? sql ` NULLS FIRST`
|
|
151
|
+
: sql ` NULLS LAST`}`))}`);
|
|
152
|
+
}
|
|
153
|
+
if (s.limit !== undefined)
|
|
154
|
+
parts.push(sql ` LIMIT ${s.limit}`);
|
|
155
|
+
if (s.offset !== undefined)
|
|
156
|
+
parts.push(sql ` OFFSET ${s.offset}`);
|
|
157
|
+
return sql.join(parts, sql.empty);
|
|
158
|
+
}
|
|
159
|
+
compile() {
|
|
160
|
+
return compile(this.fragment());
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
_a = SelectQuery;
|
|
164
|
+
/** An `INSERT` query. */
|
|
165
|
+
export class InsertQuery {
|
|
166
|
+
#state;
|
|
167
|
+
/** @internal */
|
|
168
|
+
constructor(state) {
|
|
169
|
+
this.#state = Object.freeze(state);
|
|
170
|
+
}
|
|
171
|
+
/** Rows to insert. Keys missing from a row (or `undefined`) become `DEFAULT`. */
|
|
172
|
+
values(rows) {
|
|
173
|
+
return new InsertQuery({ ...this.#state, rows: [...this.#state.rows, ...rows] });
|
|
174
|
+
}
|
|
175
|
+
returning(columns) {
|
|
176
|
+
return new InsertQuery({
|
|
177
|
+
...this.#state,
|
|
178
|
+
returning: [...this.#state.returning, ...columns],
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
fragment() {
|
|
182
|
+
const s = this.#state;
|
|
183
|
+
if (s.rows.length === 0) {
|
|
184
|
+
throw new SqlError('SQL_EMPTY_VALUES', 'The insert has no rows.', 'Call .values([...]) with at least one row before compiling.');
|
|
185
|
+
}
|
|
186
|
+
const columns = [];
|
|
187
|
+
for (const row of s.rows)
|
|
188
|
+
for (const key of Object.keys(row))
|
|
189
|
+
if (row[key] !== undefined && !columns.includes(key))
|
|
190
|
+
columns.push(key);
|
|
191
|
+
if (columns.length === 0) {
|
|
192
|
+
throw new SqlError('SQL_EMPTY_VALUES', 'The insert rows have no values.', 'Give at least one column a value, or insert defaults with a raw sql`INSERT ... DEFAULT VALUES`.');
|
|
193
|
+
}
|
|
194
|
+
const rows = s.rows.map((row) => sql `(${sql.join(columns.map((c) => (row[c] === undefined ? sql `DEFAULT` : value(row[c]))))})`);
|
|
195
|
+
return sql `${renderCtes(s.ctes)}INSERT INTO ${sql.id(s.table)} (${sql.join(columns.map((c) => sql.id(c)))}) VALUES ${sql.join(rows)}${renderReturning(s.returning)}`;
|
|
196
|
+
}
|
|
197
|
+
compile() {
|
|
198
|
+
return compile(this.fragment());
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** An `UPDATE` query. */
|
|
202
|
+
export class UpdateQuery {
|
|
203
|
+
#state;
|
|
204
|
+
/** @internal */
|
|
205
|
+
constructor(state) {
|
|
206
|
+
this.#state = Object.freeze(state);
|
|
207
|
+
}
|
|
208
|
+
set(arg) {
|
|
209
|
+
const values = typeof arg === 'function' ? arg(expressionBuilder) : arg;
|
|
210
|
+
const sets = Object.entries(values).filter((entry) => entry[1] !== undefined);
|
|
211
|
+
for (const [column] of sets)
|
|
212
|
+
sql.id(column);
|
|
213
|
+
return new UpdateQuery({ ...this.#state, sets: [...this.#state.sets, ...sets] });
|
|
214
|
+
}
|
|
215
|
+
where(...args) {
|
|
216
|
+
return new UpdateQuery({
|
|
217
|
+
...this.#state,
|
|
218
|
+
where: [...this.#state.where, condition(args)],
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
returning(columns) {
|
|
222
|
+
return new UpdateQuery({
|
|
223
|
+
...this.#state,
|
|
224
|
+
returning: [...this.#state.returning, ...columns],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
fragment() {
|
|
228
|
+
const s = this.#state;
|
|
229
|
+
if (s.sets.length === 0) {
|
|
230
|
+
throw new SqlError('SQL_EMPTY_VALUES', 'The update sets no columns.', 'Call .set({ column: value }) before compiling.');
|
|
231
|
+
}
|
|
232
|
+
const sets = sql.join(s.sets.map(([column, v]) => sql `${sql.id(column)} = ${value(v)}`));
|
|
233
|
+
return sql `${renderCtes(s.ctes)}UPDATE ${renderSource(s.target)} SET ${sets}${renderWhere(sql `WHERE`, s.where)}${renderReturning(s.returning)}`;
|
|
234
|
+
}
|
|
235
|
+
compile() {
|
|
236
|
+
return compile(this.fragment());
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** A `DELETE` query. */
|
|
240
|
+
export class DeleteQuery {
|
|
241
|
+
#state;
|
|
242
|
+
/** @internal */
|
|
243
|
+
constructor(state) {
|
|
244
|
+
this.#state = Object.freeze(state);
|
|
245
|
+
}
|
|
246
|
+
where(...args) {
|
|
247
|
+
return new DeleteQuery({
|
|
248
|
+
...this.#state,
|
|
249
|
+
where: [...this.#state.where, condition(args)],
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
returning(columns) {
|
|
253
|
+
return new DeleteQuery({
|
|
254
|
+
...this.#state,
|
|
255
|
+
returning: [...this.#state.returning, ...columns],
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
fragment() {
|
|
259
|
+
const s = this.#state;
|
|
260
|
+
return sql `${renderCtes(s.ctes)}DELETE FROM ${renderSource(s.target)}${renderWhere(sql `WHERE`, s.where)}${renderReturning(s.returning)}`;
|
|
261
|
+
}
|
|
262
|
+
compile() {
|
|
263
|
+
return compile(this.fragment());
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function builder(ctes) {
|
|
267
|
+
const self = {
|
|
268
|
+
selectFrom: (table) => new SelectQuery({
|
|
269
|
+
ctes,
|
|
270
|
+
from: parseTable(table),
|
|
271
|
+
joins: [],
|
|
272
|
+
columns: [],
|
|
273
|
+
selectAll: false,
|
|
274
|
+
where: [],
|
|
275
|
+
groupBy: [],
|
|
276
|
+
groupingSets: undefined,
|
|
277
|
+
having: [],
|
|
278
|
+
orderBy: [],
|
|
279
|
+
limit: undefined,
|
|
280
|
+
offset: undefined,
|
|
281
|
+
}),
|
|
282
|
+
insertInto: (table) => {
|
|
283
|
+
sql.id(table);
|
|
284
|
+
return new InsertQuery({ ctes, table, rows: [], returning: [] });
|
|
285
|
+
},
|
|
286
|
+
updateTable: (table) => new UpdateQuery({ ctes, target: parseTable(table), sets: [], where: [], returning: [] }),
|
|
287
|
+
deleteFrom: (table) => new DeleteQuery({ ctes, target: parseTable(table), where: [], returning: [] }),
|
|
288
|
+
with: (name, build) => {
|
|
289
|
+
sql.id(name);
|
|
290
|
+
return builder([...ctes, { name, query: build(self) }]);
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
return Object.freeze(self);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Creates a query builder for a schema.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* interface Db { sale_order: { id: number; name: string; state: string; partner_id: number } }
|
|
301
|
+
* const qb = createQueryBuilder<Db>();
|
|
302
|
+
* qb.selectFrom('sale_order as so').select(['so.id', 'so.name']).where('so.state', '=', 'sale').compile();
|
|
303
|
+
* // { text: 'SELECT "so"."id", "so"."name" FROM "sale_order" AS "so" WHERE "so"."state" = $1', params: ['sale'] }
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
export function createQueryBuilder() {
|
|
307
|
+
return builder([]);
|
|
308
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expressions of the typed query builder (RFC 0005 §7.7). Every expression is a `sql` fragment underneath, so values
|
|
3
|
+
* can only ever become parameters.
|
|
4
|
+
*/
|
|
5
|
+
import { type SqlFragment } from '../ast.js';
|
|
6
|
+
/** A typed SQL expression. `T` is the type of the value it produces. */
|
|
7
|
+
export interface Expression<T> {
|
|
8
|
+
readonly '~expr': true;
|
|
9
|
+
/** Phantom. */
|
|
10
|
+
readonly '~type'?: T;
|
|
11
|
+
readonly fragment: SqlFragment;
|
|
12
|
+
/** Names the expression in a select list. */
|
|
13
|
+
as<A extends string>(alias: A): AliasedExpression<T, A>;
|
|
14
|
+
}
|
|
15
|
+
/** An expression with an output name. */
|
|
16
|
+
export interface AliasedExpression<T, A extends string> {
|
|
17
|
+
readonly expression: Expression<T>;
|
|
18
|
+
readonly alias: A;
|
|
19
|
+
}
|
|
20
|
+
/** Anything with a `fragment()`: a sub-query usable in `exists` / `in`. */
|
|
21
|
+
export interface Fragmentable {
|
|
22
|
+
fragment(): SqlFragment;
|
|
23
|
+
}
|
|
24
|
+
/** Wraps a fragment as an expression. */
|
|
25
|
+
export declare function expr<T>(fragment: SqlFragment): Expression<T>;
|
|
26
|
+
/** True for expressions made by this module. */
|
|
27
|
+
export declare function isExpression(value: unknown): value is Expression<unknown>;
|
|
28
|
+
/** `"so"."name"` from `'so.name'`, `"name"` from `'name'`. Validates every part like `sql.id`. */
|
|
29
|
+
export declare function columnRef(ref: string): SqlFragment;
|
|
30
|
+
/** Comparison and pattern operators that take a value or expression on the right. */
|
|
31
|
+
export type ValueOperator = '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=' | 'like' | 'ilike' | 'not like' | 'not ilike' | 'is distinct from' | 'is not distinct from';
|
|
32
|
+
/** Membership operators: right side is an array (one parameter) or a sub-query. */
|
|
33
|
+
export type ListOperator = 'in' | 'not in';
|
|
34
|
+
/** Null / boolean tests. */
|
|
35
|
+
export type NullOperator = 'is' | 'is not';
|
|
36
|
+
/** Every operator. */
|
|
37
|
+
export type Operator = ValueOperator | ListOperator | NullOperator;
|
|
38
|
+
/** Builds `lhs <op> rhs` with the operator's semantics (arrays as one parameter, null tests, sub-queries). */
|
|
39
|
+
export declare function compare(lhs: string | Expression<unknown>, op: Operator, rhs: unknown): Expression<boolean>;
|
|
40
|
+
/** The type of column `R` (`'alias.column'` or `'column'`) in scope `S` of schema `DB`. */
|
|
41
|
+
export type ColumnTypeOf<DB, S, R extends string> = R extends `${infer A}.${infer C}` ? A extends keyof S ? S[A] extends keyof DB ? C extends keyof DB[S[A]] ? DB[S[A]][C] : never : never : never : {
|
|
42
|
+
[A in keyof S]: S[A] extends keyof DB ? R extends keyof DB[S[A]] ? DB[S[A]][R] : never : never;
|
|
43
|
+
}[keyof S];
|
|
44
|
+
/** Aggregate and other function builders. */
|
|
45
|
+
export interface FunctionBuilder<Col extends string, DB, S> {
|
|
46
|
+
/** `count(*)`, `count("col")` or `count(DISTINCT "col")`. */
|
|
47
|
+
count(column?: Col, options?: {
|
|
48
|
+
readonly distinct?: boolean;
|
|
49
|
+
}): Expression<number>;
|
|
50
|
+
sum<R extends Col>(column: R): Expression<ColumnTypeOf<DB, S, R> | null>;
|
|
51
|
+
avg<R extends Col>(column: R): Expression<string | null>;
|
|
52
|
+
min<R extends Col>(column: R): Expression<ColumnTypeOf<DB, S, R> | null>;
|
|
53
|
+
max<R extends Col>(column: R): Expression<ColumnTypeOf<DB, S, R> | null>;
|
|
54
|
+
/** `grouping("a", "b")`: which GROUPING SETS level a row belongs to. */
|
|
55
|
+
grouping(columns: readonly Col[]): Expression<number>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The expression builder passed to `where`, `having`, `select` and join callbacks. Callable for comparisons;
|
|
59
|
+
* `and` / `or` / `not` / `exists` / `col` / `val` / `sql` / `fn` for the rest.
|
|
60
|
+
*/
|
|
61
|
+
export interface ExpressionBuilder<DB, S, Col extends string = string> {
|
|
62
|
+
<R extends Col>(lhs: R, op: ValueOperator, rhs: ColumnTypeOf<DB, S, R> | Expression<ColumnTypeOf<DB, S, R>>): Expression<boolean>;
|
|
63
|
+
<R extends Col>(lhs: R, op: ListOperator, rhs: readonly ColumnTypeOf<DB, S, R>[] | Fragmentable): Expression<boolean>;
|
|
64
|
+
<R extends Col>(lhs: R, op: NullOperator, rhs: null | boolean): Expression<boolean>;
|
|
65
|
+
<T>(lhs: Expression<T>, op: ValueOperator, rhs: T | Expression<T>): Expression<boolean>;
|
|
66
|
+
<T>(lhs: Expression<T>, op: NullOperator, rhs: null | boolean): Expression<boolean>;
|
|
67
|
+
and(expressions: readonly Expression<boolean>[]): Expression<boolean>;
|
|
68
|
+
or(expressions: readonly Expression<boolean>[]): Expression<boolean>;
|
|
69
|
+
not(expression: Expression<boolean>): Expression<boolean>;
|
|
70
|
+
exists(query: Fragmentable): Expression<boolean>;
|
|
71
|
+
/** A column reference as an expression (for column-to-column comparisons). */
|
|
72
|
+
col<R extends Col>(ref: R): Expression<ColumnTypeOf<DB, S, R>>;
|
|
73
|
+
/** A bound value. */
|
|
74
|
+
val<T>(value: T): Expression<T>;
|
|
75
|
+
/** A raw `sql` fragment as an expression. */
|
|
76
|
+
sql<T>(fragment: SqlFragment): Expression<T>;
|
|
77
|
+
readonly fn: FunctionBuilder<Col, DB, S>;
|
|
78
|
+
}
|
|
79
|
+
/** The one, stateless expression builder; scopes only exist in the types. */
|
|
80
|
+
export declare const expressionBuilder: ExpressionBuilder<unknown, unknown, string>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expressions of the typed query builder (RFC 0005 §7.7). Every expression is a `sql` fragment underneath, so values
|
|
3
|
+
* can only ever become parameters.
|
|
4
|
+
*/
|
|
5
|
+
import { isSql } from '../ast.js';
|
|
6
|
+
import { SqlError } from '../errors.js';
|
|
7
|
+
import { sql } from '../tag.js';
|
|
8
|
+
class Expr {
|
|
9
|
+
'~expr' = true;
|
|
10
|
+
fragment;
|
|
11
|
+
constructor(fragment) {
|
|
12
|
+
this.fragment = fragment;
|
|
13
|
+
Object.freeze(this);
|
|
14
|
+
}
|
|
15
|
+
as(alias) {
|
|
16
|
+
sql.id(alias);
|
|
17
|
+
return Object.freeze({ expression: this, alias });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Wraps a fragment as an expression. */
|
|
21
|
+
export function expr(fragment) {
|
|
22
|
+
return new Expr(fragment);
|
|
23
|
+
}
|
|
24
|
+
/** True for expressions made by this module. */
|
|
25
|
+
export function isExpression(value) {
|
|
26
|
+
return value instanceof Expr;
|
|
27
|
+
}
|
|
28
|
+
function isFragmentable(value) {
|
|
29
|
+
return (typeof value === 'object' &&
|
|
30
|
+
value !== null &&
|
|
31
|
+
typeof value.fragment === 'function');
|
|
32
|
+
}
|
|
33
|
+
/** `"so"."name"` from `'so.name'`, `"name"` from `'name'`. Validates every part like `sql.id`. */
|
|
34
|
+
export function columnRef(ref) {
|
|
35
|
+
if (typeof ref !== 'string') {
|
|
36
|
+
throw new SqlError('SQL_INVALID_IDENTIFIER', 'A column reference must be a string.', "Use 'column' or 'alias.column'.");
|
|
37
|
+
}
|
|
38
|
+
return sql.id(...ref.split('.'));
|
|
39
|
+
}
|
|
40
|
+
/** Operator keywords as fragments, so no runtime string ever becomes SQL text. */
|
|
41
|
+
const OPERATORS = {
|
|
42
|
+
'=': sql `=`,
|
|
43
|
+
'<>': sql `<>`,
|
|
44
|
+
'!=': sql `<>`,
|
|
45
|
+
'<': sql `<`,
|
|
46
|
+
'<=': sql `<=`,
|
|
47
|
+
'>': sql `>`,
|
|
48
|
+
'>=': sql `>=`,
|
|
49
|
+
like: sql `LIKE`,
|
|
50
|
+
ilike: sql `ILIKE`,
|
|
51
|
+
'not like': sql `NOT LIKE`,
|
|
52
|
+
'not ilike': sql `NOT ILIKE`,
|
|
53
|
+
'is distinct from': sql `IS DISTINCT FROM`,
|
|
54
|
+
'is not distinct from': sql `IS NOT DISTINCT FROM`,
|
|
55
|
+
};
|
|
56
|
+
const TRUE = sql `TRUE`;
|
|
57
|
+
const FALSE = sql `FALSE`;
|
|
58
|
+
function operand(value) {
|
|
59
|
+
if (isExpression(value))
|
|
60
|
+
return value.fragment;
|
|
61
|
+
if (isSql(value))
|
|
62
|
+
return value;
|
|
63
|
+
return sql `${value}`;
|
|
64
|
+
}
|
|
65
|
+
/** Builds `lhs <op> rhs` with the operator's semantics (arrays as one parameter, null tests, sub-queries). */
|
|
66
|
+
export function compare(lhs, op, rhs) {
|
|
67
|
+
const left = typeof lhs === 'string' ? columnRef(lhs) : lhs.fragment;
|
|
68
|
+
if (op === 'in' || op === 'not in') {
|
|
69
|
+
if (Array.isArray(rhs)) {
|
|
70
|
+
if (rhs.length === 0)
|
|
71
|
+
return expr(op === 'in' ? FALSE : TRUE);
|
|
72
|
+
return expr(op === 'in' ? sql `${left} = ANY(${rhs})` : sql `${left} <> ALL(${rhs})`);
|
|
73
|
+
}
|
|
74
|
+
if (isFragmentable(rhs)) {
|
|
75
|
+
return expr(op === 'in' ? sql `${left} IN (${rhs.fragment()})` : sql `${left} NOT IN (${rhs.fragment()})`);
|
|
76
|
+
}
|
|
77
|
+
throw new SqlError('SQL_INVALID_OPERATOR', `'${op}' needs an array or a sub-query on the right.`, "Pass a list: where('id', 'in', [1, 2]) or a select query.");
|
|
78
|
+
}
|
|
79
|
+
if (op === 'is' || op === 'is not') {
|
|
80
|
+
const not = op === 'is not' ? sql `NOT ` : sql.empty;
|
|
81
|
+
if (rhs === null)
|
|
82
|
+
return expr(sql `${left} IS ${not}NULL`);
|
|
83
|
+
if (typeof rhs === 'boolean')
|
|
84
|
+
return expr(sql `${left} IS ${not}${rhs ? TRUE : FALSE}`);
|
|
85
|
+
throw new SqlError('SQL_INVALID_OPERATOR', `'${op}' only tests null, true or false.`, "Use where('col', 'is', null) or 'is not'; compare values with '='.");
|
|
86
|
+
}
|
|
87
|
+
const word = OPERATORS[op];
|
|
88
|
+
if (word === undefined) {
|
|
89
|
+
throw new SqlError('SQL_INVALID_OPERATOR', `'${String(op)}' is not a supported operator.`, `Use one of: ${Object.keys(OPERATORS).join(', ')}, in, not in, is, is not.`);
|
|
90
|
+
}
|
|
91
|
+
return expr(sql `${left} ${word} ${operand(rhs)}`);
|
|
92
|
+
}
|
|
93
|
+
function combine(exprs, separator, empty) {
|
|
94
|
+
const only = exprs[0];
|
|
95
|
+
if (only === undefined)
|
|
96
|
+
return expr(empty);
|
|
97
|
+
if (exprs.length === 1)
|
|
98
|
+
return only;
|
|
99
|
+
return expr(sql `(${sql.join(exprs.map((e) => e.fragment), separator)})`);
|
|
100
|
+
}
|
|
101
|
+
function aggregate(name, column, distinct = false) {
|
|
102
|
+
if (column === undefined)
|
|
103
|
+
return sql `${name}(*)`;
|
|
104
|
+
return distinct
|
|
105
|
+
? sql `${name}(DISTINCT ${columnRef(column)})`
|
|
106
|
+
: sql `${name}(${columnRef(column)})`;
|
|
107
|
+
}
|
|
108
|
+
const fn = Object.freeze({
|
|
109
|
+
count: (column, options) => expr(aggregate(sql `count`, column, options?.distinct)),
|
|
110
|
+
sum: (column) => expr(aggregate(sql `sum`, column)),
|
|
111
|
+
avg: (column) => expr(aggregate(sql `avg`, column)),
|
|
112
|
+
min: (column) => expr(aggregate(sql `min`, column)),
|
|
113
|
+
max: (column) => expr(aggregate(sql `max`, column)),
|
|
114
|
+
grouping: (columns) => expr(sql `grouping(${sql.join(columns.map(columnRef))})`),
|
|
115
|
+
});
|
|
116
|
+
/** The one, stateless expression builder; scopes only exist in the types. */
|
|
117
|
+
export const expressionBuilder = Object.assign(((lhs, op, rhs) => compare(lhs, op, rhs)), {
|
|
118
|
+
and: (exprs) => combine(exprs, sql ` AND `, TRUE),
|
|
119
|
+
or: (exprs) => combine(exprs, sql ` OR `, FALSE),
|
|
120
|
+
not: (e) => expr(sql `NOT (${e.fragment})`),
|
|
121
|
+
exists: (q) => expr(sql `EXISTS (${q.fragment()})`),
|
|
122
|
+
col: (ref) => expr(columnRef(ref)),
|
|
123
|
+
val: (value) => expr(sql `${value}`),
|
|
124
|
+
sql: (fragment) => expr(fragment),
|
|
125
|
+
fn,
|
|
126
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type SqlFragment } from './ast.js';
|
|
2
|
+
/** PostgreSQL's wire protocol carries the parameter count as a 16-bit integer. */
|
|
3
|
+
export declare const MAX_PARAMS = 65535;
|
|
4
|
+
/** SQL ready for the driver: text with `$n` placeholders, plus the values in order. */
|
|
5
|
+
export interface CompiledSql {
|
|
6
|
+
/** SQL text. Contains no values, so identical query shapes give identical text (plan and prepared-statement reuse). */
|
|
7
|
+
readonly text: string;
|
|
8
|
+
/** Values for `$1..$n`, in order. */
|
|
9
|
+
readonly params: readonly unknown[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Renders a fragment to parameterized SQL. Parameters are numbered `$1..$n` in order of appearance, and
|
|
13
|
+
* identifiers are always double-quoted.
|
|
14
|
+
*
|
|
15
|
+
* @throws {SqlError} `SQL_NOT_A_FRAGMENT` if `sql` was not created by the `sql` tag; `SQL_TOO_MANY_PARAMS` above
|
|
16
|
+
* {@link MAX_PARAMS} parameters.
|
|
17
|
+
*/
|
|
18
|
+
export declare function compile(sql: SqlFragment): CompiledSql;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { isSql } from './ast.js';
|
|
2
|
+
import { SqlError } from './errors.js';
|
|
3
|
+
/** PostgreSQL's wire protocol carries the parameter count as a 16-bit integer. */
|
|
4
|
+
export const MAX_PARAMS = 65_535;
|
|
5
|
+
/**
|
|
6
|
+
* Renders a fragment to parameterized SQL. Parameters are numbered `$1..$n` in order of appearance, and
|
|
7
|
+
* identifiers are always double-quoted.
|
|
8
|
+
*
|
|
9
|
+
* @throws {SqlError} `SQL_NOT_A_FRAGMENT` if `sql` was not created by the `sql` tag; `SQL_TOO_MANY_PARAMS` above
|
|
10
|
+
* {@link MAX_PARAMS} parameters.
|
|
11
|
+
*/
|
|
12
|
+
export function compile(sql) {
|
|
13
|
+
if (!isSql(sql)) {
|
|
14
|
+
throw new SqlError('SQL_NOT_A_FRAGMENT', 'compile() was given something that is not a sql fragment.', 'Build queries with sql`...` and its helpers; fragments cannot be constructed by hand.');
|
|
15
|
+
}
|
|
16
|
+
const text = [];
|
|
17
|
+
const params = [];
|
|
18
|
+
const render = (node) => {
|
|
19
|
+
switch (node.kind) {
|
|
20
|
+
case 'text':
|
|
21
|
+
text.push(node.text);
|
|
22
|
+
return;
|
|
23
|
+
case 'ident':
|
|
24
|
+
text.push(node.parts.map((p) => `"${p}"`).join('.'));
|
|
25
|
+
return;
|
|
26
|
+
case 'param':
|
|
27
|
+
if (params.length === MAX_PARAMS) {
|
|
28
|
+
throw new SqlError('SQL_TOO_MANY_PARAMS', `The query has more than ${MAX_PARAMS} parameters, the PostgreSQL protocol limit.`, 'Pass lists as one array parameter instead, e.g. id = ANY(<sql.array(ids, "bigint")>) or unnest(...).');
|
|
29
|
+
}
|
|
30
|
+
params.push(node.value);
|
|
31
|
+
text.push(node.cast === undefined ? `$${params.length}` : `$${params.length}::${node.cast}`);
|
|
32
|
+
return;
|
|
33
|
+
case 'seq':
|
|
34
|
+
for (const child of node.nodes)
|
|
35
|
+
render(child);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
render(sql.node);
|
|
40
|
+
return Object.freeze({ text: text.join(''), params: Object.freeze(params) });
|
|
41
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Machine-readable reason for a {@link SqlError}. */
|
|
2
|
+
export type SqlErrorCode = 'SQL_NOT_A_TEMPLATE' | 'SQL_NOT_A_FRAGMENT' | 'SQL_UNDEFINED_PARAM' | 'SQL_INVALID_NUMBER' | 'SQL_INVALID_PARAM' | 'SQL_INVALID_IDENTIFIER' | 'SQL_INVALID_TYPE' | 'SQL_TOO_MANY_PARAMS' | 'SQL_EMPTY_SELECT' | 'SQL_EMPTY_VALUES' | 'SQL_INVALID_OPERATOR';
|
|
3
|
+
/**
|
|
4
|
+
* Raised when a SQL fragment can't be built or compiled safely. The message says what went wrong, and
|
|
5
|
+
* {@link SqlError.hint} says how to fix it.
|
|
6
|
+
*/
|
|
7
|
+
export declare class SqlError extends Error {
|
|
8
|
+
/** Machine-readable reason. */
|
|
9
|
+
readonly code: SqlErrorCode;
|
|
10
|
+
/** How to fix the problem. Also included at the end of `message`. */
|
|
11
|
+
readonly hint: string;
|
|
12
|
+
constructor(code: SqlErrorCode, problem: string, hint: string);
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raised when a SQL fragment can't be built or compiled safely. The message says what went wrong, and
|
|
3
|
+
* {@link SqlError.hint} says how to fix it.
|
|
4
|
+
*/
|
|
5
|
+
export class SqlError extends Error {
|
|
6
|
+
/** Machine-readable reason. */
|
|
7
|
+
code;
|
|
8
|
+
/** How to fix the problem. Also included at the end of `message`. */
|
|
9
|
+
hint;
|
|
10
|
+
constructor(code, problem, hint) {
|
|
11
|
+
super(`${problem} ${hint}`);
|
|
12
|
+
this.name = 'SqlError';
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.hint = hint;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Shutro's SQL layer (RFC 0005): an immutable SQL AST, the `sql` tagged template, and a compiler to parameterized
|
|
4
|
+
* SQL. Values only ever travel as parameters; identifiers are validated and quoted.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { compile, sql } from './index.js';
|
|
9
|
+
*
|
|
10
|
+
* const q = sql`SELECT ${sql.id('id')} FROM ${sql.id('sale_order')} WHERE ${sql.id('state')} = ANY(${sql.array(states, 'text')})`;
|
|
11
|
+
* compile(q); // { text: 'SELECT "id" FROM "sale_order" WHERE "state" = ANY($1::text[])', params: [states] }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export { type IdentNode, isSql, type ParamNode, type SeqNode, SqlFragment, type SqlNode, type TextNode, visit, } from './ast.js';
|
|
15
|
+
export { type AddTable, type ColumnRef, createQueryBuilder, DeleteQuery, InsertQuery, type Query, type QueryBuilder, type RowOf, type Schema, type Scope, SelectQuery, type SelectRef, type TableExpr, UpdateQuery, } from './builder/builder.js';
|
|
16
|
+
export { type AliasedExpression, type ColumnTypeOf, type Expression, type ExpressionBuilder, type Fragmentable, type FunctionBuilder, isExpression, type ListOperator, type NullOperator, type Operator, type ValueOperator, } from './builder/expression.js';
|
|
17
|
+
export { type CompiledSql, compile, MAX_PARAMS } from './compile.js';
|
|
18
|
+
export { SqlError, type SqlErrorCode } from './errors.js';
|
|
19
|
+
export { type SqlTag, sql } from './tag.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* Shutro's SQL layer (RFC 0005): an immutable SQL AST, the `sql` tagged template, and a compiler to parameterized
|
|
4
|
+
* SQL. Values only ever travel as parameters; identifiers are validated and quoted.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { compile, sql } from './index.js';
|
|
9
|
+
*
|
|
10
|
+
* const q = sql`SELECT ${sql.id('id')} FROM ${sql.id('sale_order')} WHERE ${sql.id('state')} = ANY(${sql.array(states, 'text')})`;
|
|
11
|
+
* compile(q); // { text: 'SELECT "id" FROM "sale_order" WHERE "state" = ANY($1::text[])', params: [states] }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export { isSql, SqlFragment, visit, } from './ast.js';
|
|
15
|
+
export { createQueryBuilder, DeleteQuery, InsertQuery, SelectQuery, UpdateQuery, } from './builder/builder.js';
|
|
16
|
+
export { isExpression, } from './builder/expression.js';
|
|
17
|
+
export { compile, MAX_PARAMS } from './compile.js';
|
|
18
|
+
export { SqlError } from './errors.js';
|
|
19
|
+
export { sql } from './tag.js';
|