@expressive/sql 0.5.0 → 0.7.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 +4 -3
- package/dist/columns/Bool.d.ts +1 -0
- package/dist/columns/Bool.js +1 -0
- package/dist/columns/Bool.js.map +1 -1
- package/dist/columns/Num.d.ts +5 -0
- package/dist/columns/Num.js +5 -0
- package/dist/columns/Num.js.map +1 -1
- package/dist/columns/One.d.ts +1 -0
- package/dist/columns/One.js +1 -0
- package/dist/columns/One.js.map +1 -1
- package/dist/columns/Str.d.ts +1 -0
- package/dist/columns/Str.js +1 -0
- package/dist/columns/Str.js.map +1 -1
- package/dist/columns/Time.d.ts +5 -0
- package/dist/columns/Time.js +5 -0
- package/dist/columns/Time.js.map +1 -1
- package/dist/connection/Connection.d.ts +32 -14
- package/dist/connection/Connection.js +10 -48
- package/dist/connection/Connection.js.map +1 -1
- package/dist/connection/Generator.d.ts +40 -4
- package/dist/connection/Generator.js +55 -22
- package/dist/connection/Generator.js.map +1 -1
- package/dist/datatypes.d.ts +38 -5
- package/dist/datatypes.js +5 -0
- package/dist/datatypes.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/sqlite/SQLiteConnection.d.ts +42 -3
- package/dist/sqlite/SQLiteConnection.js +89 -22
- package/dist/sqlite/SQLiteConnection.js.map +1 -1
- package/dist/sqlite/SQLiteGenerator.d.ts +2 -1
- package/dist/sqlite/SQLiteGenerator.js.map +1 -1
- package/dist/sqlite/engine.d.ts +80 -8
- package/dist/sqlite/engine.js +58 -6
- package/dist/sqlite/engine.js.map +1 -1
- package/package.json +8 -5
- package/skills/expressive-sql/SKILL.md +168 -0
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import { escape } from "../utils.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Field, now } from "@expressive/orm";
|
|
3
|
+
import { Computed, DataField, DataTable, Parameter, QueryTemplate } from "@expressive/orm/driver";
|
|
3
4
|
|
|
4
5
|
//#region src/connection/Generator.ts
|
|
5
6
|
function isValue(input) {
|
|
6
7
|
return input instanceof Field || input instanceof Parameter || input instanceof DataField || input instanceof QueryTemplate || input instanceof Computed;
|
|
7
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Renders a `Query` builder as one SQL statement - `String(generator)`.
|
|
11
|
+
*
|
|
12
|
+
* A dialect subclasses it and overrides the protected hooks. Literals render inline, escaped; parameters bind.
|
|
13
|
+
*/
|
|
8
14
|
var Generator = class {
|
|
9
15
|
constructor(query) {
|
|
10
16
|
this.query = query;
|
|
11
17
|
}
|
|
12
18
|
toString() {
|
|
13
19
|
return [
|
|
14
|
-
this.with(),
|
|
15
|
-
this.insert() || this.update() || this.delete() || this.select(),
|
|
20
|
+
this.insert() || [this.with(), this.update() || this.delete() || this.select()],
|
|
16
21
|
this.where(),
|
|
17
22
|
this.order(),
|
|
18
23
|
this.limit(),
|
|
@@ -20,9 +25,11 @@ var Generator = class {
|
|
|
20
25
|
this.returning()
|
|
21
26
|
].flat(Infinity).filter(Boolean).join(" ");
|
|
22
27
|
}
|
|
28
|
+
/** Quote an identifier, each dotted part on its own. */
|
|
23
29
|
escape(name) {
|
|
24
30
|
return String(name).split(".").map((x) => `\`${x}\``).join(".");
|
|
25
31
|
}
|
|
32
|
+
/** `INSERT INTO`, or `false` when the query inserts nothing. */
|
|
26
33
|
insert() {
|
|
27
34
|
const { inserts, tables } = this.query;
|
|
28
35
|
if (!inserts.size) return false;
|
|
@@ -34,18 +41,23 @@ var Generator = class {
|
|
|
34
41
|
values.push(this.value(value, field));
|
|
35
42
|
}
|
|
36
43
|
if (!keys.length && tables.size > 1) throw new Error(`Insert into ${table.name} for each joined row needs at least one value.`);
|
|
44
|
+
const into = ["INSERT INTO", this.escape(table.name)];
|
|
37
45
|
if (!keys.length) return [
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
this.with(),
|
|
47
|
+
into,
|
|
40
48
|
"DEFAULT VALUES"
|
|
41
49
|
];
|
|
50
|
+
return this.insertWith(this.with(), [into, `(${keys})`], [`SELECT ${values}`, tables.size > 1 && this.from()]);
|
|
51
|
+
}
|
|
52
|
+
/** Order an insert's data CTE, target and select - for a dialect placing `WITH` elsewhere. */
|
|
53
|
+
insertWith(cte, into, select) {
|
|
42
54
|
return [
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
tables.size > 1 && this.from()
|
|
55
|
+
cte,
|
|
56
|
+
into,
|
|
57
|
+
select
|
|
47
58
|
];
|
|
48
59
|
}
|
|
60
|
+
/** `ON CONFLICT` clause of an upsert. */
|
|
49
61
|
upsert() {
|
|
50
62
|
const { conflict } = this.query;
|
|
51
63
|
if (!conflict) return;
|
|
@@ -60,11 +72,13 @@ var Generator = class {
|
|
|
60
72
|
set.join(", ")
|
|
61
73
|
];
|
|
62
74
|
}
|
|
75
|
+
/** `RETURNING` clause of an insert that selects. */
|
|
63
76
|
returning() {
|
|
64
77
|
const { inserts, returns } = this.query;
|
|
65
78
|
if (!inserts.size || !returns) return;
|
|
66
79
|
return "RETURNING " + (returns instanceof Map ? Array.from(returns, ([alias, field]) => `${this.value(field)} AS "${alias}"`).join(", ") : this.reference(returns));
|
|
67
80
|
}
|
|
81
|
+
/** `WITH` clause over the query's data tables. */
|
|
68
82
|
with() {
|
|
69
83
|
const cte = [];
|
|
70
84
|
this.query.tables.forEach((table) => {
|
|
@@ -72,17 +86,29 @@ var Generator = class {
|
|
|
72
86
|
});
|
|
73
87
|
if (cte.length) return "WITH " + cte.join();
|
|
74
88
|
}
|
|
89
|
+
/** A value - `now`, a reference, or a literal. */
|
|
75
90
|
value(input, field) {
|
|
76
91
|
if (input === now) return this.now(field);
|
|
77
92
|
if (isValue(input)) return this.reference(input);
|
|
78
|
-
return
|
|
93
|
+
return this.literal(input);
|
|
94
|
+
}
|
|
95
|
+
/** A literal - every literal in values, `SET` and `WHERE` passes through it. */
|
|
96
|
+
literal(value) {
|
|
97
|
+
return String(escape(value));
|
|
79
98
|
}
|
|
99
|
+
/** A table with its alias - for `FROM`, joins, `UPDATE` and `DELETE`. */
|
|
100
|
+
table({ name, alias }) {
|
|
101
|
+
return this.escape(name) + (alias ? ` ${this.escape(alias)}` : "");
|
|
102
|
+
}
|
|
103
|
+
/** The current time - `CURRENT_TIMESTAMP`; a dialect renders it per `field`'s storage. */
|
|
80
104
|
now(_field) {
|
|
81
105
|
return "CURRENT_TIMESTAMP";
|
|
82
106
|
}
|
|
107
|
+
/** Select a data table's rows out of its bound parameter. The base throws - a dialect implements it. */
|
|
83
108
|
jsonTable(_table) {
|
|
84
109
|
throw new Error("Not implemented.");
|
|
85
110
|
}
|
|
111
|
+
/** An arithmetic expression, parenthesized by operator rank. */
|
|
86
112
|
computed(from) {
|
|
87
113
|
const { left, operator, right, rank } = from;
|
|
88
114
|
const side = (input, tight) => {
|
|
@@ -93,25 +119,27 @@ var Generator = class {
|
|
|
93
119
|
if (left === void 0) return operator + side(right, true);
|
|
94
120
|
return `${side(left, false)} ${operator} ${side(right, true)}`;
|
|
95
121
|
}
|
|
122
|
+
/** `JOIN` clauses for every table but `main`. Throws for a table without a join condition. */
|
|
96
123
|
joins(main) {
|
|
97
124
|
const output = [];
|
|
98
125
|
for (const table of this.query.tables.values()) {
|
|
99
126
|
if (table === main) continue;
|
|
100
|
-
const {
|
|
127
|
+
const { name, optional } = table;
|
|
101
128
|
if (table.joins.length === 0) throw new Error(`Table ${name} has no joins.`);
|
|
102
129
|
const mode = optional ? "LEFT" : "INNER";
|
|
103
|
-
const as = this.escape(name) + (alias ? ` ${this.escape(alias)}` : "");
|
|
104
130
|
const conds = table.joins.map((x) => this.filter(x.left, x.op, x.right));
|
|
105
|
-
output.push(`${mode} JOIN ${
|
|
131
|
+
output.push(`${mode} JOIN ${this.table(table)} ON ${conds.join(" AND ")}`);
|
|
106
132
|
}
|
|
107
133
|
if (output.length) return output.join(" ");
|
|
108
134
|
}
|
|
135
|
+
/** `SET` clause of an update. Throws without values. */
|
|
109
136
|
set(updates) {
|
|
110
137
|
const sets = [];
|
|
111
|
-
for (const [_table, data] of updates) for (const [field, value] of data) sets.push(`${this.escape(field.column)} = ${value === now ? this.now(field) : isValue(value) ? this.reference(value) : value === null ? "NULL" :
|
|
138
|
+
for (const [_table, data] of updates) for (const [field, value] of data) sets.push(`${this.escape(field.column)} = ${value === now ? this.now(field) : isValue(value) ? this.reference(value) : value === null ? "NULL" : this.literal(value)}`);
|
|
112
139
|
if (!sets.length) throw new Error("Update contains no values.");
|
|
113
140
|
return `SET ${sets}`;
|
|
114
141
|
}
|
|
142
|
+
/** `UPDATE` head. Throws when the query updates more than one table. */
|
|
115
143
|
update() {
|
|
116
144
|
const { updates } = this.query;
|
|
117
145
|
if (!updates.size) return;
|
|
@@ -119,11 +147,12 @@ var Generator = class {
|
|
|
119
147
|
const [main] = updates.keys();
|
|
120
148
|
return [
|
|
121
149
|
"UPDATE",
|
|
122
|
-
this.
|
|
150
|
+
this.table(main),
|
|
123
151
|
this.joins(main),
|
|
124
152
|
this.set(updates)
|
|
125
153
|
];
|
|
126
154
|
}
|
|
155
|
+
/** `DELETE FROM` head. */
|
|
127
156
|
delete() {
|
|
128
157
|
const { query } = this;
|
|
129
158
|
const [main] = query.deletes;
|
|
@@ -133,10 +162,11 @@ var Generator = class {
|
|
|
133
162
|
"DELETE",
|
|
134
163
|
query.tables.size > 1 && (alias || name),
|
|
135
164
|
"FROM",
|
|
136
|
-
this.
|
|
165
|
+
this.table(main),
|
|
137
166
|
this.joins(main)
|
|
138
167
|
];
|
|
139
168
|
}
|
|
169
|
+
/** A column, parameter, data column, raw template or expression. */
|
|
140
170
|
reference(from) {
|
|
141
171
|
if (from instanceof DataField) return this.escape(from.table.name + "." + from.column);
|
|
142
172
|
if (from instanceof QueryTemplate) return from.from;
|
|
@@ -145,6 +175,7 @@ var Generator = class {
|
|
|
145
175
|
const { table, column } = from;
|
|
146
176
|
return this.escape(table.alias || table.name) + "." + this.escape(column);
|
|
147
177
|
}
|
|
178
|
+
/** `SELECT` head with joins - `COUNT(*)` when the query selects nothing. */
|
|
148
179
|
select() {
|
|
149
180
|
const { returns, tables } = this.query;
|
|
150
181
|
const [main] = tables.values();
|
|
@@ -155,19 +186,18 @@ var Generator = class {
|
|
|
155
186
|
main && this.joins(main)
|
|
156
187
|
];
|
|
157
188
|
}
|
|
189
|
+
/** `FROM` clause of the main table. */
|
|
158
190
|
from() {
|
|
159
191
|
const [main] = this.query.tables.values();
|
|
160
|
-
if (main) return [
|
|
161
|
-
"FROM",
|
|
162
|
-
this.escape(main.name),
|
|
163
|
-
main.alias
|
|
164
|
-
];
|
|
192
|
+
if (main) return ["FROM", this.table(main)];
|
|
165
193
|
}
|
|
194
|
+
/** A parameter placeholder. */
|
|
166
195
|
param(_from) {
|
|
167
196
|
return "?";
|
|
168
197
|
}
|
|
198
|
+
/** One condition. `null` renders `IS` / `IS NOT`; an empty list a constant. */
|
|
169
199
|
filter(left, op, right) {
|
|
170
|
-
const raw = (value) => value === now ? this.now(left) : isValue(value) ? this.reference(value) :
|
|
200
|
+
const raw = (value) => value === now ? this.now(left) : isValue(value) ? this.reference(value) : this.literal(op.includes("LIKE") ? value : left.set(value));
|
|
171
201
|
if (right === null) {
|
|
172
202
|
if (op == "=") op = "IS";
|
|
173
203
|
else if (op == "<>" || op == "!=") op = "IS NOT";
|
|
@@ -179,6 +209,7 @@ var Generator = class {
|
|
|
179
209
|
} else right = raw(right);
|
|
180
210
|
return `${this.reference(left)} ${op} ${right}`;
|
|
181
211
|
}
|
|
212
|
+
/** `WHERE` clause - top level joined with AND, alternating with OR by depth. */
|
|
182
213
|
where() {
|
|
183
214
|
const { filters } = this.query;
|
|
184
215
|
if (!filters.size) return;
|
|
@@ -194,10 +225,12 @@ var Generator = class {
|
|
|
194
225
|
};
|
|
195
226
|
return ["WHERE", combine(filters)];
|
|
196
227
|
}
|
|
228
|
+
/** `ORDER BY` clause. */
|
|
197
229
|
order() {
|
|
198
230
|
const { order } = this.query;
|
|
199
231
|
if (order.size) return "ORDER BY " + Array.from(order, ([x, y]) => `${this.reference(x)} ${y}`);
|
|
200
232
|
}
|
|
233
|
+
/** `LIMIT` and `OFFSET` clauses. */
|
|
201
234
|
limit() {
|
|
202
235
|
const { limit, offset } = this.query;
|
|
203
236
|
return [limit && `LIMIT ${limit}`, offset && `OFFSET ${offset}`];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Generator.js","names":[],"sources":["../../src/connection/Generator.ts"],"sourcesContent":["import { Builder, Computed, DataField, DataTable, Field, now, Parameter, QueryTemplate } from '@expressive/orm';\n\nimport { escape } from '../utils';\n\ntype Expression = Builder[\"filters\"];\n\nfunction isValue(input: unknown){\n return (\n input instanceof Field ||\n input instanceof Parameter ||\n input instanceof DataField ||\n input instanceof QueryTemplate ||\n input instanceof Computed\n );\n}\n\nexport class Generator {\n constructor(public query: Builder){}\n\n toString() {\n const parts = [\n this.with(),\n this.insert() ||\n this.update() ||\n this.delete() ||\n this.select(),\n this.where(),\n this.order(),\n this.limit(),\n this.upsert(),\n this.returning()\n ];\n\n return parts.flat(Infinity).filter(Boolean).join(' ');\n }\n\n escape(name: unknown){\n return String(name).split('.').map(x => `\\`${x}\\``).join('.');\n }\n\n protected insert() {\n const { inserts, tables } = this.query;\n\n if (!inserts.size)\n return false;\n\n const [[table, data]] = inserts;\n\n const keys: string[] = [];\n const values: string[] = [];\n\n for (const [field, value] of data) {\n keys.push(this.escape(field.column));\n values.push(this.value(value, field));\n }\n\n if (!keys.length && tables.size > 1)\n throw new Error(`Insert into ${table.name} for each joined row needs at least one value.`);\n\n if (!keys.length)\n return ['INSERT INTO', this.escape(table.name), 'DEFAULT VALUES'];\n\n return [\n 'INSERT INTO',\n this.escape(table.name),\n `(${keys}) SELECT ${values}`,\n tables.size > 1 && this.from()\n ];\n }\n\n protected upsert(){\n const { conflict } = this.query;\n\n if (!conflict)\n return;\n\n const on = `ON CONFLICT (${conflict.on.map(field => this.escape(field.column))})`;\n\n if (!conflict.update)\n return [on, 'DO NOTHING'];\n\n const set = conflict.update.map(({ column }) => `${this.escape(column)} = excluded.${this.escape(column)}`);\n\n for (const [field, value] of conflict.set)\n set.push(`${this.escape(field.column)} = ${this.value(value)}`);\n\n if (!set.length)\n set.push(...conflict.on.map(({ column }) => `${this.escape(column)} = excluded.${this.escape(column)}`));\n\n return [on, 'DO UPDATE SET', set.join(', ')];\n }\n\n protected returning(){\n const { inserts, returns } = this.query;\n\n if (!inserts.size || !returns)\n return;\n\n return 'RETURNING ' + (\n returns instanceof Map\n ? Array.from(returns, ([alias, field]) => `${this.value(field)} AS \"${alias}\"`).join(', ')\n : this.reference(returns)\n );\n }\n\n protected with(){\n const cte = [] as string[];\n\n this.query.tables.forEach((table) => {\n if (table instanceof DataTable)\n cte.push(`${this.escape(table.name)} AS (${this.jsonTable(table)})`);\n });\n\n if(cte.length)\n return 'WITH ' + cte.join();\n }\n\n protected value(input: unknown, field?: Field){\n if(input === now)\n return this.now(field);\n\n if(isValue(input))\n return this.reference(input);\n\n return String(escape(input));\n }\n\n protected now(_field?: Field){\n return \"CURRENT_TIMESTAMP\";\n }\n\n protected jsonTable(_table: DataTable): string {\n throw new Error(\"Not implemented.\");\n }\n\n protected computed(from: Computed<unknown>): string {\n const { left, operator, right, rank } = from;\n const side = (input: unknown, tight: boolean) => {\n if(!(input instanceof Computed))\n return this.value(input);\n\n const value = this.computed(input);\n\n return (tight ? input.rank <= rank : input.rank < rank) ? `(${value})` : value;\n }\n\n if(left === undefined)\n return operator + side(right, true);\n\n return `${side(left, false)} ${operator} ${side(right, true)}`;\n }\n\n protected joins(main: Builder.ITable){\n const output: string[] = [];\n\n for(const table of this.query.tables.values()){\n if(table === main)\n continue;\n\n const { alias, name, optional } = table;\n\n if(table.joins.length === 0)\n throw new Error(`Table ${name} has no joins.`);\n\n const mode = optional ? \"LEFT\" : \"INNER\";\n const as = this.escape(name) + (alias ? ` ${this.escape(alias)}` : \"\");\n const conds = table.joins.map(x => this.filter(x.left, x.op, x.right));\n\n output.push(`${mode} JOIN ${as} ON ${conds.join(' AND ')}`);\n }\n\n if(output.length)\n return output.join(' ');\n }\n\n protected set(updates: Map<Builder.ITable, Builder.Insert>){\n const sets: string[] = [];\n\n for(const [_table, data] of updates)\n for(const [field, value] of data)\n sets.push(`${this.escape(field.column)} = ${\n value === now ? this.now(field) :\n isValue(value) ? this.reference(value) :\n value === null ? 'NULL' :\n escape(value)\n }`);\n\n if(!sets.length)\n throw new Error('Update contains no values.');\n\n return `SET ${sets}`;\n }\n\n protected update(){\n const { updates } = this.query;\n\n if(!updates.size)\n return;\n\n if(updates.size > 1)\n throw new Error('Engine does not support multi-table updates.');\n\n const [ main ] = updates.keys();\n\n return [\n 'UPDATE',\n this.escape(main),\n this.joins(main),\n this.set(updates)\n ]\n }\n\n protected delete(){\n const { query } = this;\n const [ main ] = query.deletes;\n\n if(!main)\n return;\n\n const { alias, name } = main;\n const target = query.tables.size > 1 && (alias || name);\n\n return ['DELETE', target, 'FROM', this.escape(name), this.joins(main)]\n }\n\n protected reference(from: unknown): string {\n if(from instanceof DataField)\n return this.escape(from.table.name + '.' + from.column);\n\n if(from instanceof QueryTemplate)\n return from.from;\n\n if(from instanceof Computed)\n return this.computed(from);\n\n if(from instanceof Parameter)\n return this.param(from);\n\n const { table, column } = from as Field;\n\n return this.escape(table!.alias || table!.name) + \".\" + this.escape(column);\n }\n\n protected select(){\n const { returns, tables } = this.query;\n const [ main ] = tables.values();\n\n const selection =\n returns instanceof Map ?\n Array.from(returns, ([alias, field]) => this.value(field) + ` AS \"${alias}\"`).join(', ') :\n returns ?\n this.reference(returns) :\n 'COUNT(*)';\n\n return [\n \"SELECT\",\n selection,\n this.from(),\n main && this.joins(main)\n ]\n }\n\n protected from(){\n const [ main ] = this.query.tables.values();\n\n if(main)\n return [\"FROM\", this.escape(main.name), main.alias]\n }\n\n protected param(_from: Parameter){\n return \"?\";\n }\n\n protected filter(left: Field, op: string, right: unknown){\n const raw = (value: unknown) => (\n value === now ? this.now(left) :\n isValue(value) ? this.reference(value) :\n escape(op.includes(\"LIKE\") ? value : left.set(value))\n );\n\n if (right === null){\n if(op == '=')\n op = 'IS';\n else if(op == '<>' || op == '!=')\n op = 'IS NOT';\n else\n throw new Error('Cannot compare NULL with ' + op);\n\n right = 'NULL';\n }\n else if(right instanceof Array){\n if(!right.length)\n return op == 'NOT IN' ? '1 = 1' : '1 = 0';\n\n right = `(${right.map(raw)})`;\n }\n else\n right = raw(right);\n\n return `${this.reference(left)} ${op} ${right}`;\n }\n\n protected where() {\n const { filters } = this.query;\n\n if (!filters.size) return;\n\n const combine = (group: Expression, or?: boolean): string => {\n return Array\n .from(group.children, (cond, i) => {\n if (\"children\" in cond) {\n const inner = combine(cond, !or);\n const first = or !== false && i === 0;\n return cond.size === 1 || first ? inner : `(${inner})`;\n }\n\n return this.filter(cond.left, cond.op, cond.right);\n })\n .join(or ? ' OR ' : ' AND ');\n };\n\n return ['WHERE', combine(filters)];\n }\n\n protected order(){\n const { order } = this.query;\n\n if(order.size)\n return 'ORDER BY ' +\n Array.from(order, ([x, y]) => `${this.reference(x)} ${y}`);\n }\n\n protected limit(){\n const { limit, offset } = this.query;\n\n return [\n limit && `LIMIT ${limit}`,\n offset && `OFFSET ${offset}`\n ];\n }\n}\n"],"mappings":";;;;AAMA,SAAS,QAAQ,OAAe;AAC9B,QACE,iBAAiB,SACjB,iBAAiB,aACjB,iBAAiB,aACjB,iBAAiB,iBACjB,iBAAiB;;AAIrB,IAAa,YAAb,MAAuB;CACrB,YAAY,AAAO,OAAe;EAAf;;CAEnB,WAAW;AAcT,SAbc;GACZ,KAAK,MAAM;GACX,KAAK,QAAQ,IACb,KAAK,QAAQ,IACb,KAAK,QAAQ,IACb,KAAK,QAAQ;GACb,KAAK,OAAO;GACZ,KAAK,OAAO;GACZ,KAAK,OAAO;GACZ,KAAK,QAAQ;GACb,KAAK,WAAW;GACjB,CAEY,KAAK,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,IAAI;;CAGvD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;AAE/B,SAAO,CACL,SAAS,SAAS,SAClB,UAAU,UAAU,SACrB"}
|
|
1
|
+
{"version":3,"file":"Generator.js","names":[],"sources":["../../src/connection/Generator.ts"],"sourcesContent":["import { Field, now } from '@expressive/orm';\nimport { Builder, Computed, DataField, DataTable, Parameter, QueryTemplate } from '@expressive/orm/driver';\n\nimport { escape } from '../utils';\n\ntype Expression = Builder[\"filters\"];\n\nfunction isValue(input: unknown){\n return (\n input instanceof Field ||\n input instanceof Parameter ||\n input instanceof DataField ||\n input instanceof QueryTemplate ||\n input instanceof Computed\n );\n}\n\n/**\n * Renders a `Query` builder as one SQL statement - `String(generator)`.\n *\n * A dialect subclasses it and overrides the protected hooks. Literals render inline, escaped; parameters bind.\n */\nexport class Generator {\n constructor(public query: Builder){}\n\n toString() {\n const parts = [\n this.insert() || [\n this.with(),\n this.update() ||\n this.delete() ||\n this.select()\n ],\n this.where(),\n this.order(),\n this.limit(),\n this.upsert(),\n this.returning()\n ];\n\n return parts.flat(Infinity).filter(Boolean).join(' ');\n }\n\n /** Quote an identifier, each dotted part on its own. */\n escape(name: unknown){\n return String(name).split('.').map(x => `\\`${x}\\``).join('.');\n }\n\n /** `INSERT INTO`, or `false` when the query inserts nothing. */\n protected insert() {\n const { inserts, tables } = this.query;\n\n if (!inserts.size)\n return false;\n\n const [[table, data]] = inserts;\n\n const keys: string[] = [];\n const values: string[] = [];\n\n for (const [field, value] of data) {\n keys.push(this.escape(field.column));\n values.push(this.value(value, field));\n }\n\n if (!keys.length && tables.size > 1)\n throw new Error(`Insert into ${table.name} for each joined row needs at least one value.`);\n\n const into = ['INSERT INTO', this.escape(table.name)];\n\n if (!keys.length)\n return [this.with(), into, 'DEFAULT VALUES'];\n\n return this.insertWith(this.with(), [into, `(${keys})`], [\n `SELECT ${values}`,\n tables.size > 1 && this.from()\n ]);\n }\n\n /** Order an insert's data CTE, target and select - for a dialect placing `WITH` elsewhere. */\n protected insertWith(cte: string | undefined, into: unknown[], select: unknown[]): unknown[] {\n return [cte, into, select];\n }\n\n /** `ON CONFLICT` clause of an upsert. */\n protected upsert(){\n const { conflict } = this.query;\n\n if (!conflict)\n return;\n\n const on = `ON CONFLICT (${conflict.on.map(field => this.escape(field.column))})`;\n\n if (!conflict.update)\n return [on, 'DO NOTHING'];\n\n const set = conflict.update.map(({ column }) => `${this.escape(column)} = excluded.${this.escape(column)}`);\n\n for (const [field, value] of conflict.set)\n set.push(`${this.escape(field.column)} = ${this.value(value)}`);\n\n if (!set.length)\n set.push(...conflict.on.map(({ column }) => `${this.escape(column)} = excluded.${this.escape(column)}`));\n\n return [on, 'DO UPDATE SET', set.join(', ')];\n }\n\n /** `RETURNING` clause of an insert that selects. */\n protected returning(){\n const { inserts, returns } = this.query;\n\n if (!inserts.size || !returns)\n return;\n\n return 'RETURNING ' + (\n returns instanceof Map\n ? Array.from(returns, ([alias, field]) => `${this.value(field)} AS \"${alias}\"`).join(', ')\n : this.reference(returns)\n );\n }\n\n /** `WITH` clause over the query's data tables. */\n protected with(){\n const cte = [] as string[];\n\n this.query.tables.forEach((table) => {\n if (table instanceof DataTable)\n cte.push(`${this.escape(table.name)} AS (${this.jsonTable(table)})`);\n });\n\n if(cte.length)\n return 'WITH ' + cte.join();\n }\n\n /** A value - `now`, a reference, or a literal. */\n protected value(input: unknown, field?: Field){\n if(input === now)\n return this.now(field);\n\n if(isValue(input))\n return this.reference(input);\n\n return this.literal(input);\n }\n\n /** A literal - every literal in values, `SET` and `WHERE` passes through it. */\n protected literal(value: unknown){\n return String(escape(value));\n }\n\n /** A table with its alias - for `FROM`, joins, `UPDATE` and `DELETE`. */\n protected table({ name, alias }: Builder.ITable){\n return this.escape(name) + (alias ? ` ${this.escape(alias)}` : \"\");\n }\n\n /** The current time - `CURRENT_TIMESTAMP`; a dialect renders it per `field`'s storage. */\n protected now(_field?: Field){\n return \"CURRENT_TIMESTAMP\";\n }\n\n /** Select a data table's rows out of its bound parameter. The base throws - a dialect implements it. */\n protected jsonTable(_table: DataTable): string {\n throw new Error(\"Not implemented.\");\n }\n\n /** An arithmetic expression, parenthesized by operator rank. */\n protected computed(from: Computed<unknown>): string {\n const { left, operator, right, rank } = from;\n const side = (input: unknown, tight: boolean) => {\n if(!(input instanceof Computed))\n return this.value(input);\n\n const value = this.computed(input);\n\n return (tight ? input.rank <= rank : input.rank < rank) ? `(${value})` : value;\n }\n\n if(left === undefined)\n return operator + side(right, true);\n\n return `${side(left, false)} ${operator} ${side(right, true)}`;\n }\n\n /** `JOIN` clauses for every table but `main`. Throws for a table without a join condition. */\n protected joins(main: Builder.ITable){\n const output: string[] = [];\n\n for(const table of this.query.tables.values()){\n if(table === main)\n continue;\n\n const { name, optional } = table;\n\n if(table.joins.length === 0)\n throw new Error(`Table ${name} has no joins.`);\n\n const mode = optional ? \"LEFT\" : \"INNER\";\n const conds = table.joins.map(x => this.filter(x.left, x.op, x.right));\n\n output.push(`${mode} JOIN ${this.table(table)} ON ${conds.join(' AND ')}`);\n }\n\n if(output.length)\n return output.join(' ');\n }\n\n /** `SET` clause of an update. Throws without values. */\n protected set(updates: Map<Builder.ITable, Builder.Insert>){\n const sets: string[] = [];\n\n for(const [_table, data] of updates)\n for(const [field, value] of data)\n sets.push(`${this.escape(field.column)} = ${\n value === now ? this.now(field) :\n isValue(value) ? this.reference(value) :\n value === null ? 'NULL' :\n this.literal(value)\n }`);\n\n if(!sets.length)\n throw new Error('Update contains no values.');\n\n return `SET ${sets}`;\n }\n\n /** `UPDATE` head. Throws when the query updates more than one table. */\n protected update(){\n const { updates } = this.query;\n\n if(!updates.size)\n return;\n\n if(updates.size > 1)\n throw new Error('Engine does not support multi-table updates.');\n\n const [ main ] = updates.keys();\n\n return [\n 'UPDATE',\n this.table(main),\n this.joins(main),\n this.set(updates)\n ]\n }\n\n /** `DELETE FROM` head. */\n protected delete(){\n const { query } = this;\n const [ main ] = query.deletes;\n\n if(!main)\n return;\n\n const { alias, name } = main;\n const target = query.tables.size > 1 && (alias || name);\n\n return ['DELETE', target, 'FROM', this.table(main), this.joins(main)]\n }\n\n /** A column, parameter, data column, raw template or expression. */\n protected reference(from: unknown): string {\n if(from instanceof DataField)\n return this.escape(from.table.name + '.' + from.column);\n\n if(from instanceof QueryTemplate)\n return from.from;\n\n if(from instanceof Computed)\n return this.computed(from);\n\n if(from instanceof Parameter)\n return this.param(from);\n\n const { table, column } = from as Field;\n\n return this.escape(table!.alias || table!.name) + \".\" + this.escape(column);\n }\n\n /** `SELECT` head with joins - `COUNT(*)` when the query selects nothing. */\n protected select(){\n const { returns, tables } = this.query;\n const [ main ] = tables.values();\n\n const selection =\n returns instanceof Map ?\n Array.from(returns, ([alias, field]) => this.value(field) + ` AS \"${alias}\"`).join(', ') :\n returns ?\n this.reference(returns) :\n 'COUNT(*)';\n\n return [\n \"SELECT\",\n selection,\n this.from(),\n main && this.joins(main)\n ]\n }\n\n /** `FROM` clause of the main table. */\n protected from(){\n const [ main ] = this.query.tables.values();\n\n if(main)\n return [\"FROM\", this.table(main)]\n }\n\n /** A parameter placeholder. */\n protected param(_from: Parameter){\n return \"?\";\n }\n\n /** One condition. `null` renders `IS` / `IS NOT`; an empty list a constant. */\n protected filter(left: Field, op: string, right: unknown){\n const raw = (value: unknown) => (\n value === now ? this.now(left) :\n isValue(value) ? this.reference(value) :\n this.literal(op.includes(\"LIKE\") ? value : left.set(value))\n );\n\n if (right === null){\n if(op == '=')\n op = 'IS';\n else if(op == '<>' || op == '!=')\n op = 'IS NOT';\n else\n throw new Error('Cannot compare NULL with ' + op);\n\n right = 'NULL';\n }\n else if(right instanceof Array){\n if(!right.length)\n return op == 'NOT IN' ? '1 = 1' : '1 = 0';\n\n right = `(${right.map(raw)})`;\n }\n else\n right = raw(right);\n\n return `${this.reference(left)} ${op} ${right}`;\n }\n\n /** `WHERE` clause - top level joined with AND, alternating with OR by depth. */\n protected where() {\n const { filters } = this.query;\n\n if (!filters.size) return;\n\n const combine = (group: Expression, or?: boolean): string => {\n return Array\n .from(group.children, (cond, i) => {\n if (\"children\" in cond) {\n const inner = combine(cond, !or);\n const first = or !== false && i === 0;\n return cond.size === 1 || first ? inner : `(${inner})`;\n }\n\n return this.filter(cond.left, cond.op, cond.right);\n })\n .join(or ? ' OR ' : ' AND ');\n };\n\n return ['WHERE', combine(filters)];\n }\n\n /** `ORDER BY` clause. */\n protected order(){\n const { order } = this.query;\n\n if(order.size)\n return 'ORDER BY ' +\n Array.from(order, ([x, y]) => `${this.reference(x)} ${y}`);\n }\n\n /** `LIMIT` and `OFFSET` clauses. */\n protected limit(){\n const { limit, offset } = this.query;\n\n return [\n limit && `LIMIT ${limit}`,\n offset && `OFFSET ${offset}`\n ];\n }\n}\n"],"mappings":";;;;;AAOA,SAAS,QAAQ,OAAe;AAC9B,QACE,iBAAiB,SACjB,iBAAiB,aACjB,iBAAiB,aACjB,iBAAiB,iBACjB,iBAAiB;;;;;;;AASrB,IAAa,YAAb,MAAuB;CACrB,YAAY,AAAO,OAAe;EAAf;;CAEnB,WAAW;AAeT,SAdc;GACZ,KAAK,QAAQ,IAAI,CACf,KAAK,MAAM,EACX,KAAK,QAAQ,IACb,KAAK,QAAQ,IACb,KAAK,QAAQ,CACd;GACD,KAAK,OAAO;GACZ,KAAK,OAAO;GACZ,KAAK,OAAO;GACZ,KAAK,QAAQ;GACb,KAAK,WAAW;GACjB,CAEY,KAAK,SAAS,CAAC,OAAO,QAAQ,CAAC,KAAK,IAAI;;;CAIvD,OAAO,MAAc;AACnB,SAAO,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,KAAI,MAAK,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI;;;CAI/D,AAAU,SAAS;EACjB,MAAM,EAAE,SAAS,WAAW,KAAK;AAEjC,MAAI,CAAC,QAAQ,KACX,QAAO;EAET,MAAM,CAAC,CAAC,OAAO,SAAS;EAExB,MAAM,OAAiB,EAAE;EACzB,MAAM,SAAmB,EAAE;AAE3B,OAAK,MAAM,CAAC,OAAO,UAAU,MAAM;AACjC,QAAK,KAAK,KAAK,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.d.ts
CHANGED
|
@@ -1,69 +1,102 @@
|
|
|
1
|
+
import { now } from "@expressive/orm";
|
|
2
|
+
|
|
1
3
|
//#region src/datatypes.d.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
interface Datatypes {
|
|
5
|
+
/** The current time - rendered by the database in the column's storage; statics take the app's clock. */
|
|
6
|
+
readonly now: typeof now;
|
|
7
|
+
/** `str` (default) - unbounded text. `date` - ISO 8601 text. `bool` - text, with `either`. */
|
|
6
8
|
readonly text: {
|
|
7
9
|
readonly type: "text";
|
|
8
10
|
};
|
|
11
|
+
/** `str` - a UUID. As `key(sql.uuid)`, a UUIDv7 is generated on insert when absent. */
|
|
9
12
|
readonly uuid: {
|
|
10
13
|
readonly type: "uuid";
|
|
11
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* 32-bit integer - for `num`, `one` (default), `bool` and `date`.
|
|
17
|
+
*
|
|
18
|
+
* On `num` a fraction or a value out of range throws on write. `bool` stores 1 or 0; `date`, epoch seconds.
|
|
19
|
+
*/
|
|
12
20
|
readonly integer: {
|
|
13
21
|
readonly type: "integer";
|
|
14
22
|
};
|
|
23
|
+
/** `num` - 16-bit integer; a fraction or a value out of range throws on write. */
|
|
15
24
|
readonly smallint: {
|
|
16
25
|
readonly type: "smallint";
|
|
17
26
|
};
|
|
27
|
+
/** `num`, `one` - 64-bit integer. On `num` a fraction throws on write. */
|
|
18
28
|
readonly bigint: {
|
|
19
29
|
readonly type: "bigint";
|
|
20
30
|
};
|
|
31
|
+
/** `num` - floating point. */
|
|
21
32
|
readonly real: {
|
|
22
33
|
readonly type: "real";
|
|
23
34
|
};
|
|
35
|
+
/** `num` - floating point. */
|
|
24
36
|
readonly float: {
|
|
25
37
|
readonly type: "float";
|
|
26
38
|
};
|
|
39
|
+
/** `num` - floating point, `double precision` in schema. */
|
|
27
40
|
readonly double: {
|
|
28
41
|
readonly type: "double";
|
|
29
42
|
};
|
|
43
|
+
/** `bool` (default). */
|
|
30
44
|
readonly boolean: {
|
|
31
45
|
readonly type: "boolean";
|
|
32
46
|
};
|
|
47
|
+
/** `date` (default) - date and time. */
|
|
33
48
|
readonly timestamp: {
|
|
34
49
|
readonly type: "timestamp";
|
|
35
50
|
};
|
|
51
|
+
/** `date` - a calendar day, `YYYY-MM-DD`. Written from the UTC day, read as local midnight. */
|
|
36
52
|
readonly date: {
|
|
37
53
|
readonly type: "date";
|
|
38
54
|
};
|
|
55
|
+
/** `date` - time of day, `HH:MM:SS` UTC, read on 1970-01-01. */
|
|
39
56
|
readonly time: {
|
|
40
57
|
readonly type: "time";
|
|
41
58
|
};
|
|
59
|
+
/** `date` - epoch milliseconds in a 64-bit integer. */
|
|
42
60
|
readonly ms: {
|
|
43
61
|
readonly type: "ms";
|
|
44
62
|
};
|
|
63
|
+
/** `date` - epoch seconds in an integer. */
|
|
45
64
|
readonly seconds: {
|
|
46
65
|
readonly type: "seconds";
|
|
47
66
|
};
|
|
67
|
+
/** `str` - text up to `length` characters; longer throws on write. `bool` - with `either`. */
|
|
48
68
|
readonly varchar: <N extends number>(length: N) => {
|
|
49
69
|
readonly type: "varchar";
|
|
50
70
|
readonly length: N;
|
|
51
71
|
};
|
|
72
|
+
/** `str` - fixed-length text; longer than `length` throws on write. */
|
|
52
73
|
readonly char: <N extends number>(length: N) => {
|
|
53
74
|
readonly type: "char";
|
|
54
75
|
readonly length: N;
|
|
55
76
|
};
|
|
77
|
+
/**
|
|
78
|
+
* `num` - exact decimal of `precision` digits, `scale` (default 0) after the point.
|
|
79
|
+
*
|
|
80
|
+
* A value past either throws on write.
|
|
81
|
+
*/
|
|
56
82
|
readonly numeric: (precision: number, scale?: number) => {
|
|
57
83
|
readonly type: "numeric";
|
|
58
84
|
readonly precision: number;
|
|
59
85
|
readonly scale: number;
|
|
60
86
|
};
|
|
87
|
+
/** `num` - exact decimal, as `numeric`. */
|
|
61
88
|
readonly decimal: (precision: number, scale?: number) => {
|
|
62
89
|
readonly type: "decimal";
|
|
63
90
|
readonly precision: number;
|
|
64
91
|
readonly scale: number;
|
|
65
92
|
};
|
|
66
|
-
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Column datatypes, passed to a field helper - `str(sql.varchar(255))`, `date(sql.ms)`.
|
|
96
|
+
*
|
|
97
|
+
* Each fits only the helpers named on it; elsewhere it is a type error.
|
|
98
|
+
*/
|
|
99
|
+
declare const sql: Datatypes;
|
|
67
100
|
//#endregion
|
|
68
101
|
export { sql };
|
|
69
102
|
//# sourceMappingURL=datatypes.d.ts.map
|
package/dist/datatypes.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { now } from "@expressive/orm";
|
|
2
2
|
|
|
3
3
|
//#region src/datatypes.ts
|
|
4
|
+
/**
|
|
5
|
+
* Column datatypes, passed to a field helper - `str(sql.varchar(255))`, `date(sql.ms)`.
|
|
6
|
+
*
|
|
7
|
+
* Each fits only the helpers named on it; elsewhere it is a type error.
|
|
8
|
+
*/
|
|
4
9
|
const sql = {
|
|
5
10
|
now,
|
|
6
11
|
text: { type: "text" },
|
package/dist/datatypes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datatypes.js","names":[],"sources":["../src/datatypes.ts"],"sourcesContent":["import { now } from '@expressive/orm';\n\nconst sql = {\n now,\n text: { type: \"text\" },\n uuid: { type: \"uuid\" },\n integer: { type: \"integer\" },\n smallint: { type: \"smallint\" },\n bigint: { type: \"bigint\" },\n real: { type: \"real\" },\n float: { type: \"float\" },\n double: { type: \"double\" },\n boolean: { type: \"boolean\" },\n timestamp: { type: \"timestamp\" },\n date: { type: \"date\" },\n time: { type: \"time\" },\n ms: { type: \"ms\" },\n seconds: { type: \"seconds\" },\n varchar: <N extends number>(length: N) => ({ type: \"varchar\", length }) as const,\n char: <N extends number>(length: N) => ({ type: \"char\", length }) as const,\n numeric: (precision: number, scale = 0) => ({ type: \"numeric\", precision, scale }) as const,\n decimal: (precision: number, scale = 0) => ({ type: \"decimal\", precision, scale }) as const\n}
|
|
1
|
+
{"version":3,"file":"datatypes.js","names":[],"sources":["../src/datatypes.ts"],"sourcesContent":["import { now } from '@expressive/orm';\n\ninterface Datatypes {\n /** The current time - rendered by the database in the column's storage; statics take the app's clock. */\n readonly now: typeof now;\n /** `str` (default) - unbounded text. `date` - ISO 8601 text. `bool` - text, with `either`. */\n readonly text: { readonly type: \"text\" };\n /** `str` - a UUID. As `key(sql.uuid)`, a UUIDv7 is generated on insert when absent. */\n readonly uuid: { readonly type: \"uuid\" };\n /**\n * 32-bit integer - for `num`, `one` (default), `bool` and `date`.\n *\n * On `num` a fraction or a value out of range throws on write. `bool` stores 1 or 0; `date`, epoch seconds.\n */\n readonly integer: { readonly type: \"integer\" };\n /** `num` - 16-bit integer; a fraction or a value out of range throws on write. */\n readonly smallint: { readonly type: \"smallint\" };\n /** `num`, `one` - 64-bit integer. On `num` a fraction throws on write. */\n readonly bigint: { readonly type: \"bigint\" };\n /** `num` - floating point. */\n readonly real: { readonly type: \"real\" };\n /** `num` - floating point. */\n readonly float: { readonly type: \"float\" };\n /** `num` - floating point, `double precision` in schema. */\n readonly double: { readonly type: \"double\" };\n /** `bool` (default). */\n readonly boolean: { readonly type: \"boolean\" };\n /** `date` (default) - date and time. */\n readonly timestamp: { readonly type: \"timestamp\" };\n /** `date` - a calendar day, `YYYY-MM-DD`. Written from the UTC day, read as local midnight. */\n readonly date: { readonly type: \"date\" };\n /** `date` - time of day, `HH:MM:SS` UTC, read on 1970-01-01. */\n readonly time: { readonly type: \"time\" };\n /** `date` - epoch milliseconds in a 64-bit integer. */\n readonly ms: { readonly type: \"ms\" };\n /** `date` - epoch seconds in an integer. */\n readonly seconds: { readonly type: \"seconds\" };\n /** `str` - text up to `length` characters; longer throws on write. `bool` - with `either`. */\n readonly varchar: <N extends number>(length: N) => { readonly type: \"varchar\"; readonly length: N };\n /** `str` - fixed-length text; longer than `length` throws on write. */\n readonly char: <N extends number>(length: N) => { readonly type: \"char\"; readonly length: N };\n /**\n * `num` - exact decimal of `precision` digits, `scale` (default 0) after the point.\n *\n * A value past either throws on write.\n */\n readonly numeric: (precision: number, scale?: number) => { readonly type: \"numeric\"; readonly precision: number; readonly scale: number };\n /** `num` - exact decimal, as `numeric`. */\n readonly decimal: (precision: number, scale?: number) => { readonly type: \"decimal\"; readonly precision: number; readonly scale: number };\n}\n\n/**\n * Column datatypes, passed to a field helper - `str(sql.varchar(255))`, `date(sql.ms)`.\n *\n * Each fits only the helpers named on it; elsewhere it is a type error.\n */\nconst sql: Datatypes = {\n now,\n text: { type: \"text\" },\n uuid: { type: \"uuid\" },\n integer: { type: \"integer\" },\n smallint: { type: \"smallint\" },\n bigint: { type: \"bigint\" },\n real: { type: \"real\" },\n float: { type: \"float\" },\n double: { type: \"double\" },\n boolean: { type: \"boolean\" },\n timestamp: { type: \"timestamp\" },\n date: { type: \"date\" },\n time: { type: \"time\" },\n ms: { type: \"ms\" },\n seconds: { type: \"seconds\" },\n varchar: <N extends number>(length: N) => ({ type: \"varchar\", length }) as const,\n char: <N extends number>(length: N) => ({ type: \"char\", length }) as const,\n numeric: (precision: number, scale = 0) => ({ type: \"numeric\", precision, scale }) as const,\n decimal: (precision: number, scale = 0) => ({ type: \"decimal\", precision, scale }) as const\n};\n\nexport { sql };\n"],"mappings":";;;;;;;;AAwDA,MAAM,MAAiB;CACrB;CACA,MAAM,EAAE,MAAM,QAAQ;CACtB,MAAM,EAAE,MAAM,QAAQ;CACtB,SAAS,EAAE,MAAM,WAAW;CAC5B,UAAU,EAAE,MAAM,YAAY;CAC9B,QAAQ,EAAE,MAAM,UAAU;CAC1B,MAAM,EAAE,MAAM,QAAQ;CACtB,OAAO,EAAE,MAAM,SAAS;CACxB,QAAQ,EAAE,MAAM,UAAU;CAC1B,SAAS,EAAE,MAAM,WAAW;CAC5B,WAAW,EAAE,MAAM,aAAa;CAChC,MAAM,EAAE,MAAM,QAAQ;CACtB,MAAM,EAAE,MAAM,QAAQ;CACtB,IAAI,EAAE,MAAM,MAAM;CAClB,SAAS,EAAE,MAAM,WAAW;CAC5B,UAA4B,YAAe;EAAE,MAAM;EAAW;EAAQ;CACtE,OAAyB,YAAe;EAAE,MAAM;EAAQ;EAAQ;CAChE,UAAU,WAAmB,QAAQ,OAAO;EAAE,MAAM;EAAW;EAAW;EAAO;CACjF,UAAU,WAAmB,QAAQ,OAAO;EAAE,MAAM;EAAW;EAAW;EAAO;CAClF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { TimeColumn } from "./columns/Time.js";
|
|
|
6
6
|
import { Connection } from "./connection/Connection.js";
|
|
7
7
|
import { Generator } from "./connection/Generator.js";
|
|
8
8
|
import { sql } from "./datatypes.js";
|
|
9
|
+
import { Engine } from "./sqlite/engine.js";
|
|
9
10
|
import { SQLiteConnection } from "./sqlite/SQLiteConnection.js";
|
|
10
11
|
export * from "@expressive/orm";
|
|
11
|
-
export { BooleanColumn, Connection, ForeignColumn, Generator, NumericColumn, SQLiteConnection, StringColumn, TimeColumn, sql };
|
|
12
|
+
export { BooleanColumn, Connection, type Engine, ForeignColumn, Generator, NumericColumn, SQLiteConnection, StringColumn, TimeColumn, sql };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Connection as Connection$1 } from "../connection/Connection.js";
|
|
2
|
-
import { Engine } from "./engine.js";
|
|
3
|
-
import {
|
|
2
|
+
import { D1Database, Engine, LibSQLClient } from "./engine.js";
|
|
3
|
+
import { Field, Query } from "@expressive/orm";
|
|
4
|
+
import { Builder } from "@expressive/orm/driver";
|
|
4
5
|
|
|
5
6
|
//#region src/sqlite/SQLiteConnection.d.ts
|
|
6
7
|
type TableInfo = {
|
|
@@ -9,20 +10,58 @@ type TableInfo = {
|
|
|
9
10
|
notnull: number;
|
|
10
11
|
dflt_value: any;
|
|
11
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* A connection to SQLite - a file, in memory, a libSQL address, a D1 binding, a libSQL client or a given `Engine`.
|
|
15
|
+
*
|
|
16
|
+
* The built-in loader takes the first of `bun:sqlite`, `node:sqlite`, `better-sqlite3`, `sqlite3`, `@libsql/client`, with foreign keys on.
|
|
17
|
+
* Awaiting it runs `sync`. No live rows.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const db = await SQLiteConnection.new([User, Post], "app.db");
|
|
21
|
+
*/
|
|
12
22
|
declare class SQLiteConnection extends Connection$1 {
|
|
23
|
+
#private;
|
|
13
24
|
protected filename: string;
|
|
14
25
|
protected engine?: Promise<Engine>;
|
|
15
|
-
|
|
26
|
+
protected owned: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Bind `using` to a SQLite database. Construct with `SQLiteConnection.new`.
|
|
29
|
+
*
|
|
30
|
+
* Without `source`, the database is in memory. A filename opens through the built-in loader.
|
|
31
|
+
* A `libsql:`, `https:`, `http:`, `ws:` or `wss:` address opens a `@libsql/client` 0.18 or later - pass a token as `?authToken=`.
|
|
32
|
+
* A D1 binding, a libSQL client or an `Engine` is used as given - foreign keys are not switched on, and `close()` leaves it open.
|
|
33
|
+
* D1 refuses `transaction`. libSQL runs each `transaction` on a libSQL write transaction, one at a time.
|
|
34
|
+
*/
|
|
35
|
+
constructor(using: Connection$1.Types, source?: string | Engine | D1Database | LibSQLClient);
|
|
16
36
|
protected db(): Promise<Engine>;
|
|
37
|
+
/** `CREATE TABLE` and `CREATE INDEX` statements for the bound classes, formatted. */
|
|
17
38
|
get schema(): string;
|
|
39
|
+
/**
|
|
40
|
+
* Compile a builder to SQLite SQL.
|
|
41
|
+
*
|
|
42
|
+
* A failure when run rethrows as `Error running SQL: <message>` with the statement.
|
|
43
|
+
*/
|
|
18
44
|
prepare(builder: Builder): {
|
|
19
45
|
all: (p?: any[]) => Promise<any[]>;
|
|
20
46
|
run: (p?: any[]) => Promise<number>;
|
|
21
47
|
toString: () => string;
|
|
22
48
|
};
|
|
23
49
|
protected transact<T>(fn: () => Promise<T>): Promise<T>;
|
|
50
|
+
/** Close the database when this connection opened it, then release the connection. */
|
|
24
51
|
close(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Bring the database in line with the bound classes, then mark the connection ready.
|
|
54
|
+
*
|
|
55
|
+
* Creates missing tables and adds missing columns and indexes, in one transaction. Throws for any problem `plan` reports, changing nothing.
|
|
56
|
+
* `fix` false only verifies - anything missing throws too. `force` rebuilds a table whose column type or nullability drifted.
|
|
57
|
+
* Throws when the connection is already ready.
|
|
58
|
+
*/
|
|
25
59
|
sync(fix?: boolean, force?: boolean): Promise<this>;
|
|
60
|
+
/**
|
|
61
|
+
* List what `sync` would do, without doing it.
|
|
62
|
+
*
|
|
63
|
+
* `problems` are what it cannot fix - drift without `force`, a column SQLite cannot add, a missing referenced table, `static schema`.
|
|
64
|
+
*/
|
|
26
65
|
plan(force?: boolean): Promise<Connection$1.Plan>;
|
|
27
66
|
protected apply(steps: Connection$1.Step[]): Promise<void>;
|
|
28
67
|
valid(type: Query.Table): Promise<false | Field<unknown>[]>;
|