rado 0.2.21 → 0.2.23
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/dist/define/Column.d.ts +2 -1
- package/dist/define/Column.js +9 -5
- package/dist/define/Expr.d.ts +64 -79
- package/dist/define/Expr.js +117 -76
- package/dist/define/Functions.js +1 -1
- package/dist/define/Ops.d.ts +7 -7
- package/dist/define/Ops.js +11 -12
- package/dist/define/Param.d.ts +15 -13
- package/dist/define/Param.js +18 -9
- package/dist/define/Query.d.ts +150 -50
- package/dist/define/Query.js +400 -53
- package/dist/define/Schema.d.ts +5 -4
- package/dist/define/Schema.js +40 -28
- package/dist/define/Selection.d.ts +2 -2
- package/dist/define/Sql.d.ts +6 -0
- package/dist/define/Sql.js +22 -0
- package/dist/define/Table.d.ts +7 -9
- package/dist/define/Table.js +55 -44
- package/dist/define/Target.d.ts +21 -21
- package/dist/define/Target.js +31 -14
- package/dist/driver/bun-sqlite.js +3 -4
- package/dist/driver/sqlite3.d.ts +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/Driver.d.ts +23 -25
- package/dist/lib/Driver.js +50 -32
- package/dist/lib/Formatter.d.ts +32 -21
- package/dist/lib/Formatter.js +229 -196
- package/dist/lib/Statement.js +1 -1
- package/dist/sqlite/SqliteFormatter.d.ts +1 -1
- package/dist/sqlite/SqliteFormatter.js +15 -19
- package/dist/sqlite/SqliteFunctions.d.ts +3 -3
- package/dist/sqlite/SqliteSchema.d.ts +3 -3
- package/dist/sqlite/SqliteSchema.js +3 -3
- package/package.json +4 -4
- package/dist/define/Cursor.d.ts +0 -108
- package/dist/define/Cursor.js +0 -338
package/dist/define/Query.js
CHANGED
|
@@ -1,79 +1,426 @@
|
|
|
1
1
|
// src/define/Query.ts
|
|
2
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
3
|
+
import { Functions } from "./Functions.js";
|
|
4
|
+
import { Schema } from "./Schema.js";
|
|
5
|
+
import { Table, createTable } from "./Table.js";
|
|
6
|
+
import { Target } from "./Target.js";
|
|
7
|
+
var { assign } = Object;
|
|
8
|
+
var DATA = Symbol("Query.Data");
|
|
2
9
|
var QueryType = /* @__PURE__ */ ((QueryType2) => {
|
|
3
|
-
QueryType2["Insert"] = "Insert";
|
|
4
|
-
QueryType2["Select"] = "Select";
|
|
5
|
-
QueryType2["Update"] = "Update";
|
|
6
|
-
QueryType2["Delete"] = "Delete";
|
|
7
|
-
QueryType2["CreateTable"] = "CreateTable";
|
|
8
|
-
QueryType2["AlterTable"] = "AlterTable";
|
|
9
|
-
QueryType2["DropTable"] = "DropTable";
|
|
10
|
-
QueryType2["CreateIndex"] = "CreateIndex";
|
|
11
|
-
QueryType2["DropIndex"] = "DropIndex";
|
|
12
|
-
QueryType2["Batch"] = "Batch";
|
|
13
|
-
QueryType2["Transaction"] = "Transaction";
|
|
14
|
-
QueryType2["Raw"] = "Raw";
|
|
10
|
+
QueryType2["Insert"] = "QueryData.Insert";
|
|
11
|
+
QueryType2["Select"] = "QueryData.Select";
|
|
12
|
+
QueryType2["Update"] = "QueryData.Update";
|
|
13
|
+
QueryType2["Delete"] = "QueryData.Delete";
|
|
14
|
+
QueryType2["CreateTable"] = "QueryData.CreateTable";
|
|
15
|
+
QueryType2["AlterTable"] = "QueryData.AlterTable";
|
|
16
|
+
QueryType2["DropTable"] = "QueryData.DropTable";
|
|
17
|
+
QueryType2["CreateIndex"] = "QueryData.CreateIndex";
|
|
18
|
+
QueryType2["DropIndex"] = "QueryData.DropIndex";
|
|
19
|
+
QueryType2["Batch"] = "QueryData.Batch";
|
|
20
|
+
QueryType2["Transaction"] = "QueryData.Transaction";
|
|
21
|
+
QueryType2["Raw"] = "QueryData.Raw";
|
|
15
22
|
return QueryType2;
|
|
16
23
|
})(QueryType || {});
|
|
17
|
-
var
|
|
18
|
-
((
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
var QueryData;
|
|
25
|
+
((QueryData2) => {
|
|
26
|
+
class Data {
|
|
27
|
+
constructor(data) {
|
|
28
|
+
assign(this, data);
|
|
29
|
+
}
|
|
21
30
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return { type: "Select" /* Select */, ...select };
|
|
31
|
+
class Select extends Data {
|
|
32
|
+
type = "QueryData.Select" /* Select */;
|
|
25
33
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
QueryData2.Select = Select;
|
|
35
|
+
class Insert extends Data {
|
|
36
|
+
type = "QueryData.Insert" /* Insert */;
|
|
29
37
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
QueryData2.Insert = Insert;
|
|
39
|
+
class Update extends Data {
|
|
40
|
+
type = "QueryData.Update" /* Update */;
|
|
33
41
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
QueryData2.Update = Update;
|
|
43
|
+
class Delete extends Data {
|
|
44
|
+
type = "QueryData.Delete" /* Delete */;
|
|
37
45
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
46
|
+
QueryData2.Delete = Delete;
|
|
47
|
+
class CreateTable extends Data {
|
|
48
|
+
type = "QueryData.CreateTable" /* CreateTable */;
|
|
41
49
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
QueryData2.CreateTable = CreateTable;
|
|
51
|
+
class AlterTable extends Data {
|
|
52
|
+
type = "QueryData.AlterTable" /* AlterTable */;
|
|
45
53
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
QueryData2.AlterTable = AlterTable;
|
|
55
|
+
class DropTable extends Data {
|
|
56
|
+
type = "QueryData.DropTable" /* DropTable */;
|
|
49
57
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
QueryData2.DropTable = DropTable;
|
|
59
|
+
class CreateIndex extends Data {
|
|
60
|
+
type = "QueryData.CreateIndex" /* CreateIndex */;
|
|
53
61
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
QueryData2.CreateIndex = CreateIndex;
|
|
63
|
+
class DropIndex extends Data {
|
|
64
|
+
type = "QueryData.DropIndex" /* DropIndex */;
|
|
57
65
|
}
|
|
58
|
-
|
|
66
|
+
QueryData2.DropIndex = DropIndex;
|
|
67
|
+
class Batch extends Data {
|
|
68
|
+
type = "QueryData.Batch" /* Batch */;
|
|
69
|
+
}
|
|
70
|
+
QueryData2.Batch = Batch;
|
|
59
71
|
let TransactionOperation;
|
|
60
72
|
((TransactionOperation2) => {
|
|
61
73
|
TransactionOperation2["Begin"] = "Begin";
|
|
62
74
|
TransactionOperation2["Commit"] = "Commit";
|
|
63
75
|
TransactionOperation2["Rollback"] = "Rollback";
|
|
64
|
-
})(TransactionOperation =
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
})(TransactionOperation = QueryData2.TransactionOperation || (QueryData2.TransactionOperation = {}));
|
|
77
|
+
class Transaction extends Data {
|
|
78
|
+
type = "QueryData.Transaction" /* Transaction */;
|
|
79
|
+
}
|
|
80
|
+
QueryData2.Transaction = Transaction;
|
|
81
|
+
class Raw extends Data {
|
|
82
|
+
type = "QueryData.Raw" /* Raw */;
|
|
83
|
+
constructor(data) {
|
|
84
|
+
if (data.strings.some((chunk) => chunk.includes("?")))
|
|
85
|
+
throw new TypeError("SQL injection hazard");
|
|
86
|
+
super(data);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
QueryData2.Raw = Raw;
|
|
90
|
+
})(QueryData || (QueryData = {}));
|
|
91
|
+
var Query = class {
|
|
92
|
+
[DATA];
|
|
93
|
+
constructor(query) {
|
|
94
|
+
this[DATA] = query;
|
|
95
|
+
}
|
|
96
|
+
static all(strings, ...params) {
|
|
97
|
+
return new Query(
|
|
98
|
+
new QueryData.Raw({ expectedReturn: "rows", strings, params })
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
next(cursor) {
|
|
102
|
+
return new Query(
|
|
103
|
+
new QueryData.Batch({
|
|
104
|
+
queries: [this[DATA], cursor[DATA]]
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
on(driver) {
|
|
109
|
+
return driver.executeQuery(this[DATA]);
|
|
110
|
+
}
|
|
111
|
+
toSql(driver, options = { forceInline: true }) {
|
|
112
|
+
return driver.formatter.compile(this[DATA], options).sql;
|
|
113
|
+
}
|
|
114
|
+
toJSON() {
|
|
115
|
+
return this[DATA];
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
function addWhere(query, where) {
|
|
119
|
+
const conditions = where.slice();
|
|
120
|
+
if (query.where)
|
|
121
|
+
conditions.push(new Expr(query.where));
|
|
122
|
+
return {
|
|
123
|
+
...query,
|
|
124
|
+
where: Expr.and(...conditions)[Expr.Data]
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
((Query2) => {
|
|
128
|
+
class Delete extends Query2 {
|
|
129
|
+
where(...where) {
|
|
130
|
+
return new Delete(addWhere(this[DATA], where));
|
|
131
|
+
}
|
|
132
|
+
take(limit) {
|
|
133
|
+
return new Delete({ ...this[DATA], limit });
|
|
134
|
+
}
|
|
135
|
+
skip(offset) {
|
|
136
|
+
return new Delete({ ...this[DATA], offset });
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
Query2.Delete = Delete;
|
|
140
|
+
class Update extends Query2 {
|
|
141
|
+
set(set) {
|
|
142
|
+
return new Update({ ...this[DATA], set });
|
|
143
|
+
}
|
|
144
|
+
where(...where) {
|
|
145
|
+
return new Update(addWhere(this[DATA], where));
|
|
146
|
+
}
|
|
147
|
+
take(limit) {
|
|
148
|
+
return new Update({ ...this[DATA], limit });
|
|
149
|
+
}
|
|
150
|
+
skip(offset) {
|
|
151
|
+
return new Update({ ...this[DATA], offset });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
Query2.Update = Update;
|
|
155
|
+
class InsertValuesReturning extends Query2 {
|
|
156
|
+
}
|
|
157
|
+
Query2.InsertValuesReturning = InsertValuesReturning;
|
|
158
|
+
class Inserted extends Query2 {
|
|
159
|
+
constructor(query) {
|
|
160
|
+
super(query);
|
|
161
|
+
}
|
|
162
|
+
returning(selection) {
|
|
163
|
+
return new InsertValuesReturning(
|
|
164
|
+
new QueryData.Insert({
|
|
165
|
+
...this[DATA],
|
|
166
|
+
selection: ExprData.create(selection)
|
|
167
|
+
})
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
Query2.Inserted = Inserted;
|
|
172
|
+
class Insert {
|
|
173
|
+
constructor(into) {
|
|
174
|
+
this.into = into;
|
|
175
|
+
}
|
|
176
|
+
selection(query) {
|
|
177
|
+
return new Inserted(
|
|
178
|
+
new QueryData.Insert({ into: this.into, select: query[DATA] })
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
values(...data) {
|
|
182
|
+
return new Inserted(new QueryData.Insert({ into: this.into, data }));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
Query2.Insert = Insert;
|
|
186
|
+
class CreateTable extends Query2 {
|
|
187
|
+
constructor(table) {
|
|
188
|
+
super(Schema.create(table));
|
|
189
|
+
this.table = table;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
Query2.CreateTable = CreateTable;
|
|
193
|
+
class Batch extends Query2 {
|
|
194
|
+
constructor(queries) {
|
|
195
|
+
super(new QueryData.Batch({ queries }));
|
|
196
|
+
this.queries = queries;
|
|
197
|
+
}
|
|
198
|
+
next(cursor) {
|
|
199
|
+
return new Query2(
|
|
200
|
+
new QueryData.Batch({
|
|
201
|
+
queries: [...this[DATA].queries, cursor[DATA]]
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
Query2.Batch = Batch;
|
|
207
|
+
function joinTarget(joinType, query, to, on) {
|
|
208
|
+
const toQuery = Query2.isQuery(to) ? to[DATA] : void 0;
|
|
209
|
+
const target = toQuery ? toQuery.from : new Target.Table(to[Table.Data]);
|
|
210
|
+
if (!query.from || !target)
|
|
211
|
+
throw new Error("No from clause");
|
|
212
|
+
const conditions = [...on];
|
|
213
|
+
if (toQuery) {
|
|
214
|
+
const where = toQuery.where;
|
|
215
|
+
if (where)
|
|
216
|
+
conditions.push(new Expr(where));
|
|
217
|
+
}
|
|
218
|
+
return new Target.Join(
|
|
219
|
+
query.from,
|
|
220
|
+
target,
|
|
221
|
+
joinType,
|
|
222
|
+
Expr.and(...conditions)[Expr.Data]
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
class SelectMultiple extends Query2 {
|
|
226
|
+
constructor(query) {
|
|
227
|
+
super(query);
|
|
228
|
+
}
|
|
229
|
+
leftJoin(that, ...on) {
|
|
230
|
+
const query = this[DATA];
|
|
231
|
+
return new SelectMultiple({
|
|
232
|
+
...query,
|
|
233
|
+
from: joinTarget("left", query, that, on)
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
innerJoin(that, ...on) {
|
|
237
|
+
const query = this[DATA];
|
|
238
|
+
return new SelectMultiple({
|
|
239
|
+
...query,
|
|
240
|
+
from: joinTarget("inner", query, that, on)
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
select(selection) {
|
|
244
|
+
return new SelectMultiple({
|
|
245
|
+
...this[DATA],
|
|
246
|
+
selection: ExprData.create(selection)
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
count() {
|
|
250
|
+
return new SelectSingle({
|
|
251
|
+
...this[DATA],
|
|
252
|
+
selection: Functions.count()[Expr.Data],
|
|
253
|
+
singleResult: true
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
orderBy(...orderBy) {
|
|
257
|
+
return new SelectMultiple({
|
|
258
|
+
...this[DATA],
|
|
259
|
+
orderBy: orderBy.map((e) => {
|
|
260
|
+
return Expr.isExpr(e) ? e.asc() : e;
|
|
261
|
+
})
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
groupBy(...groupBy) {
|
|
265
|
+
return new SelectMultiple({
|
|
266
|
+
...this[DATA],
|
|
267
|
+
groupBy: groupBy.map(ExprData.create)
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
maybeFirst() {
|
|
271
|
+
return new SelectSingle({ ...this[DATA], singleResult: true });
|
|
272
|
+
}
|
|
273
|
+
first() {
|
|
274
|
+
return new SelectSingle({
|
|
275
|
+
...this[DATA],
|
|
276
|
+
singleResult: true,
|
|
277
|
+
validate: true
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
where(...where) {
|
|
281
|
+
return new SelectMultiple(addWhere(this[DATA], where));
|
|
282
|
+
}
|
|
283
|
+
take(limit) {
|
|
284
|
+
return new SelectMultiple({ ...this[DATA], limit });
|
|
285
|
+
}
|
|
286
|
+
skip(offset) {
|
|
287
|
+
return new SelectMultiple({ ...this[DATA], offset });
|
|
288
|
+
}
|
|
289
|
+
[Expr.ToExpr]() {
|
|
290
|
+
return new Expr(new ExprData.Query(this[DATA]));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
Query2.SelectMultiple = SelectMultiple;
|
|
294
|
+
class TableSelect extends SelectMultiple {
|
|
295
|
+
constructor(table, conditions = []) {
|
|
296
|
+
const target = new Target.Table(table);
|
|
297
|
+
super(
|
|
298
|
+
new QueryData.Select({
|
|
299
|
+
from: target,
|
|
300
|
+
selection: new ExprData.Row(target),
|
|
301
|
+
where: Expr.and(...conditions)[Expr.Data]
|
|
302
|
+
})
|
|
303
|
+
);
|
|
304
|
+
this.table = table;
|
|
305
|
+
}
|
|
306
|
+
as(alias) {
|
|
307
|
+
return createTable({ ...this.table, alias });
|
|
308
|
+
}
|
|
309
|
+
create() {
|
|
310
|
+
return new Query2.CreateTable(this.table);
|
|
311
|
+
}
|
|
312
|
+
insertSelect(query) {
|
|
313
|
+
return new Query2.Inserted(
|
|
314
|
+
new QueryData.Insert({ into: this.table, select: query[DATA] })
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
insertOne(record) {
|
|
318
|
+
return new Query2(
|
|
319
|
+
new QueryData.Insert({
|
|
320
|
+
into: this.table,
|
|
321
|
+
data: [record],
|
|
322
|
+
selection: new ExprData.Row(new Target.Table(this.table)),
|
|
323
|
+
singleResult: true
|
|
324
|
+
})
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
insertAll(data) {
|
|
328
|
+
return new Query2.Insert(this.table).values(...data);
|
|
329
|
+
}
|
|
330
|
+
set(data) {
|
|
331
|
+
return new Query2.Update(
|
|
332
|
+
new QueryData.Update({
|
|
333
|
+
table: this.table,
|
|
334
|
+
where: this[DATA].where
|
|
335
|
+
})
|
|
336
|
+
).set(data);
|
|
337
|
+
}
|
|
338
|
+
delete() {
|
|
339
|
+
return new Query2.Delete(
|
|
340
|
+
new QueryData.Delete({
|
|
341
|
+
table: this.table,
|
|
342
|
+
where: this[DATA].where
|
|
343
|
+
})
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
get(name) {
|
|
347
|
+
return new Expr(
|
|
348
|
+
new ExprData.Field(new ExprData.Row(new Target.Table(this.table)), name)
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
Query2.TableSelect = TableSelect;
|
|
353
|
+
class SelectSingle extends Query2 {
|
|
354
|
+
constructor(query) {
|
|
355
|
+
super(query);
|
|
356
|
+
}
|
|
357
|
+
leftJoin(that, ...on) {
|
|
358
|
+
const query = this[DATA];
|
|
359
|
+
return new SelectSingle({
|
|
360
|
+
...query,
|
|
361
|
+
from: joinTarget("left", query, that, on)
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
innerJoin(that, ...on) {
|
|
365
|
+
const query = this[DATA];
|
|
366
|
+
return new SelectSingle({
|
|
367
|
+
...query,
|
|
368
|
+
from: joinTarget("inner", query, that, on)
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
select(selection) {
|
|
372
|
+
return new SelectSingle({
|
|
373
|
+
...this[DATA],
|
|
374
|
+
selection: ExprData.create(selection)
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
orderBy(...orderBy) {
|
|
378
|
+
return new SelectSingle({
|
|
379
|
+
...this[DATA],
|
|
380
|
+
orderBy
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
groupBy(...groupBy) {
|
|
384
|
+
return new SelectSingle({
|
|
385
|
+
...this[DATA],
|
|
386
|
+
groupBy: groupBy.map(ExprData.create)
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
where(...where) {
|
|
390
|
+
return new SelectSingle(addWhere(this[DATA], where));
|
|
391
|
+
}
|
|
392
|
+
take(limit) {
|
|
393
|
+
return new SelectSingle({ ...this[DATA], limit });
|
|
394
|
+
}
|
|
395
|
+
skip(offset) {
|
|
396
|
+
return new SelectSingle({ ...this[DATA], offset });
|
|
397
|
+
}
|
|
398
|
+
all() {
|
|
399
|
+
return new SelectMultiple({ ...this[DATA], singleResult: false });
|
|
400
|
+
}
|
|
401
|
+
[Expr.ToExpr]() {
|
|
402
|
+
return new Expr(new ExprData.Query(this[DATA]));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
Query2.SelectSingle = SelectSingle;
|
|
406
|
+
})(Query || (Query = {}));
|
|
407
|
+
((Query2) => {
|
|
408
|
+
Query2.Data = DATA;
|
|
409
|
+
function isQuery(input) {
|
|
410
|
+
return input !== null && Boolean(DATA in input);
|
|
411
|
+
}
|
|
412
|
+
Query2.isQuery = isQuery;
|
|
413
|
+
function isSingle(input) {
|
|
414
|
+
return input instanceof Query2.SelectSingle;
|
|
67
415
|
}
|
|
68
|
-
Query2.
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
throw new TypeError("SQL injection hazard");
|
|
72
|
-
return { type: "Raw" /* Raw */, ...raw };
|
|
416
|
+
Query2.isSingle = isSingle;
|
|
417
|
+
function isMultiple(input) {
|
|
418
|
+
return input instanceof Query2.SelectSingle;
|
|
73
419
|
}
|
|
74
|
-
Query2.
|
|
420
|
+
Query2.isMultiple = isMultiple;
|
|
75
421
|
})(Query || (Query = {}));
|
|
76
422
|
export {
|
|
77
423
|
Query,
|
|
424
|
+
QueryData,
|
|
78
425
|
QueryType
|
|
79
426
|
};
|
package/dist/define/Schema.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { Formatter } from '../lib/Formatter';
|
|
2
|
-
import {
|
|
3
|
-
import { TableData } from './Table';
|
|
2
|
+
import { QueryData } from './Query';
|
|
3
|
+
import { Table, TableData } from './Table';
|
|
4
4
|
export interface SchemaInstructions {
|
|
5
5
|
columns: Record<string, string>;
|
|
6
6
|
indexes: Record<string, string>;
|
|
7
7
|
}
|
|
8
8
|
export declare namespace Schema {
|
|
9
|
-
function create(schema: TableData):
|
|
10
|
-
function upgrade(formatter: Formatter, local: SchemaInstructions, schema: TableData): Array<
|
|
9
|
+
function create(schema: TableData): QueryData.Batch;
|
|
10
|
+
function upgrade(formatter: Formatter, local: SchemaInstructions, schema: TableData): Array<QueryData>;
|
|
11
11
|
}
|
|
12
|
+
export declare function schema(...tables: Array<Table<any>>): void;
|
package/dist/define/Schema.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
// src/define/Schema.ts
|
|
2
2
|
import { Statement } from "../lib/Statement.js";
|
|
3
3
|
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
-
import {
|
|
4
|
+
import { QueryData, QueryType } from "./Query.js";
|
|
5
5
|
import { Target } from "./Target.js";
|
|
6
6
|
var Schema;
|
|
7
7
|
((Schema2) => {
|
|
8
|
-
function create(
|
|
8
|
+
function create(schema2) {
|
|
9
9
|
const queries = [];
|
|
10
|
-
queries.push(
|
|
11
|
-
const meta =
|
|
10
|
+
queries.push(new QueryData.CreateTable({ table: schema2, ifNotExists: true }));
|
|
11
|
+
const meta = schema2.meta();
|
|
12
12
|
if (meta.indexes)
|
|
13
13
|
for (const index of Object.values(meta.indexes))
|
|
14
14
|
queries.push(
|
|
15
|
-
|
|
15
|
+
new QueryData.CreateIndex({ table: schema2, index, ifNotExists: true })
|
|
16
16
|
);
|
|
17
|
-
return
|
|
17
|
+
return new QueryData.Batch({ queries });
|
|
18
18
|
}
|
|
19
19
|
Schema2.create = create;
|
|
20
20
|
function removeLeadingWhitespace(str) {
|
|
@@ -23,48 +23,55 @@ var Schema;
|
|
|
23
23
|
function recreateTable(table, addedColumns) {
|
|
24
24
|
const queries = [];
|
|
25
25
|
const tempTable = { ...table, name: `$$new_${table.name}` };
|
|
26
|
-
queries.push(
|
|
26
|
+
queries.push(new QueryData.CreateTable({ table: tempTable }));
|
|
27
27
|
queries.push(
|
|
28
|
-
|
|
28
|
+
new QueryData.Insert({
|
|
29
29
|
into: tempTable,
|
|
30
|
-
select:
|
|
31
|
-
from: Target.Table(table),
|
|
32
|
-
selection: ExprData.Record(
|
|
30
|
+
select: new QueryData.Select({
|
|
31
|
+
from: new Target.Table(table),
|
|
32
|
+
selection: new ExprData.Record(
|
|
33
33
|
Object.fromEntries(
|
|
34
34
|
Object.entries(table.columns).map(([key, column]) => [
|
|
35
35
|
key,
|
|
36
|
-
addedColumns.has(key) ? (typeof column.defaultValue === "function" ? column.defaultValue() : column.defaultValue) || Expr.NULL[Expr.Data] : ExprData.Field(
|
|
36
|
+
addedColumns.has(key) ? (typeof column.defaultValue === "function" ? column.defaultValue() : column.defaultValue) || Expr.NULL[Expr.Data] : new ExprData.Field(
|
|
37
|
+
new ExprData.Row(new Target.Table(table)),
|
|
38
|
+
key
|
|
39
|
+
)
|
|
37
40
|
])
|
|
38
41
|
)
|
|
39
42
|
)
|
|
40
43
|
})
|
|
41
44
|
})
|
|
42
45
|
);
|
|
43
|
-
queries.push(
|
|
46
|
+
queries.push(new QueryData.DropTable({ table, ifExists: true }));
|
|
44
47
|
queries.push(
|
|
45
|
-
|
|
48
|
+
new QueryData.AlterTable({
|
|
46
49
|
table,
|
|
47
50
|
renameTable: { from: tempTable.name }
|
|
48
51
|
})
|
|
49
52
|
);
|
|
50
53
|
for (const index of Object.values(table.meta().indexes))
|
|
51
|
-
queries.push(
|
|
54
|
+
queries.push(new QueryData.CreateIndex({ table, index }));
|
|
52
55
|
return queries;
|
|
53
56
|
}
|
|
54
|
-
function upgrade(formatter, local,
|
|
57
|
+
function upgrade(formatter, local, schema2) {
|
|
55
58
|
const columnNames = /* @__PURE__ */ new Set([
|
|
56
59
|
...Object.keys(local.columns),
|
|
57
|
-
...Object.keys(
|
|
60
|
+
...Object.keys(schema2.columns)
|
|
58
61
|
]);
|
|
59
62
|
const res = [];
|
|
60
63
|
let recreate = false;
|
|
61
64
|
for (const columnName of columnNames) {
|
|
62
65
|
const localInstruction = local.columns[columnName];
|
|
63
|
-
const schemaCol =
|
|
66
|
+
const schemaCol = schema2.columns[columnName];
|
|
64
67
|
if (!localInstruction) {
|
|
65
|
-
res.push(
|
|
68
|
+
res.push(
|
|
69
|
+
new QueryData.AlterTable({ table: schema2, addColumn: schemaCol })
|
|
70
|
+
);
|
|
66
71
|
} else if (!schemaCol) {
|
|
67
|
-
res.push(
|
|
72
|
+
res.push(
|
|
73
|
+
new QueryData.AlterTable({ table: schema2, dropColumn: columnName })
|
|
74
|
+
);
|
|
68
75
|
} else {
|
|
69
76
|
const { sql: instruction } = formatter.formatColumn(
|
|
70
77
|
{ stmt: new Statement(formatter) },
|
|
@@ -79,9 +86,9 @@ var Schema;
|
|
|
79
86
|
const added = res.filter(
|
|
80
87
|
(query) => query.type === QueryType.AlterTable && Boolean(query.addColumn)
|
|
81
88
|
).map((query) => query.addColumn.name);
|
|
82
|
-
return recreateTable(
|
|
89
|
+
return recreateTable(schema2, new Set(added));
|
|
83
90
|
}
|
|
84
|
-
const meta =
|
|
91
|
+
const meta = schema2.meta();
|
|
85
92
|
const indexNames = /* @__PURE__ */ new Set([
|
|
86
93
|
...Object.keys(local.indexes),
|
|
87
94
|
...Object.keys(meta.indexes)
|
|
@@ -90,16 +97,18 @@ var Schema;
|
|
|
90
97
|
const localInstruction = local.indexes[indexName];
|
|
91
98
|
const schemaIndex = meta.indexes[indexName];
|
|
92
99
|
if (!localInstruction) {
|
|
93
|
-
res.push(
|
|
100
|
+
res.push(new QueryData.CreateIndex({ table: schema2, index: schemaIndex }));
|
|
94
101
|
} else if (!schemaIndex) {
|
|
95
|
-
res.unshift(
|
|
102
|
+
res.unshift(new QueryData.DropIndex({ table: schema2, name: indexName }));
|
|
96
103
|
} else {
|
|
97
104
|
const { sql: instruction } = formatter.compile(
|
|
98
|
-
|
|
105
|
+
new QueryData.CreateIndex({ table: schema2, index: schemaIndex })
|
|
99
106
|
);
|
|
100
107
|
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
101
|
-
res.unshift(
|
|
102
|
-
res.push(
|
|
108
|
+
res.unshift(new QueryData.DropIndex({ table: schema2, name: indexName }));
|
|
109
|
+
res.push(
|
|
110
|
+
new QueryData.CreateIndex({ table: schema2, index: schemaIndex })
|
|
111
|
+
);
|
|
103
112
|
}
|
|
104
113
|
}
|
|
105
114
|
}
|
|
@@ -107,6 +116,9 @@ var Schema;
|
|
|
107
116
|
}
|
|
108
117
|
Schema2.upgrade = upgrade;
|
|
109
118
|
})(Schema || (Schema = {}));
|
|
119
|
+
function schema(...tables) {
|
|
120
|
+
}
|
|
110
121
|
export {
|
|
111
|
-
Schema
|
|
122
|
+
Schema,
|
|
123
|
+
schema
|
|
112
124
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Cursor } from './Cursor';
|
|
2
1
|
import type { Expr } from './Expr';
|
|
3
|
-
type
|
|
2
|
+
import type { Query } from './Query';
|
|
3
|
+
type SelectionBase = unknown | (() => any) | Expr<any> | Query.SelectMultiple<any> | Query.SelectSingle<any>;
|
|
4
4
|
interface SelectionRecord extends Record<string, Selection> {
|
|
5
5
|
}
|
|
6
6
|
export type Selection = SelectionBase | SelectionRecord;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Query } from './Query';
|
|
2
|
+
export declare function sql<T>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
|
|
3
|
+
export declare namespace sql {
|
|
4
|
+
function all<T>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
|
|
5
|
+
function get<T>(strings: TemplateStringsArray, ...params: Array<unknown>): Query<T>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// src/define/Sql.ts
|
|
2
|
+
import { Query, QueryData } from "./Query.js";
|
|
3
|
+
function sql(strings, ...params) {
|
|
4
|
+
return new Query(new QueryData.Raw({ strings, params }));
|
|
5
|
+
}
|
|
6
|
+
((sql2) => {
|
|
7
|
+
function all(strings, ...params) {
|
|
8
|
+
return new Query(
|
|
9
|
+
new QueryData.Raw({ strings, params, expectedReturn: "rows" })
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
sql2.all = all;
|
|
13
|
+
function get(strings, ...params) {
|
|
14
|
+
return new Query(
|
|
15
|
+
new QueryData.Raw({ strings, params, expectedReturn: "row" })
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
sql2.get = get;
|
|
19
|
+
})(sql || (sql = {}));
|
|
20
|
+
export {
|
|
21
|
+
sql
|
|
22
|
+
};
|