@expressive/sql 0.1.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/.turbo/turbo-build.log +21 -0
- package/dist/index.d.mts +607 -0
- package/dist/index.d.ts +607 -0
- package/dist/index.js +1132 -0
- package/dist/index.mjs +1090 -0
- package/package.json +26 -0
- package/src/columns/Bool.ts +64 -0
- package/src/columns/Num.ts +54 -0
- package/src/columns/One.test.ts +131 -0
- package/src/columns/One.ts +49 -0
- package/src/columns/Str.ts +75 -0
- package/src/columns/Time.ts +29 -0
- package/src/connection/Connection.ts +117 -0
- package/src/connection/Generator.ts +303 -0
- package/src/index.ts +14 -0
- package/src/query/Builder.ts +466 -0
- package/src/query/Computed.ts +76 -0
- package/src/query/Query.test.ts +8 -0
- package/src/query/Query.ts +241 -0
- package/src/tests/delete.test.ts +48 -0
- package/src/tests/insert.test.ts +47 -0
- package/src/tests/join.test.ts +242 -0
- package/src/tests/math.test.ts +69 -0
- package/src/tests/select.test.ts +115 -0
- package/src/tests/template.test.ts +16 -0
- package/src/tests/update.test.ts +85 -0
- package/src/tests/where.test.ts +347 -0
- package/src/type/Field.ts +186 -0
- package/src/type/Table.test.ts +24 -0
- package/src/type/Table.ts +31 -0
- package/src/type/Type.test.ts +67 -0
- package/src/type/Type.ts +144 -0
- package/src/utils.ts +59 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1090 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
function capitalize(string) {
|
|
3
|
+
return string[0].toUpperCase() + string.slice(1);
|
|
4
|
+
}
|
|
5
|
+
function isIterable(obj) {
|
|
6
|
+
return obj != null && typeof obj[Symbol.iterator] === "function";
|
|
7
|
+
}
|
|
8
|
+
function underscore(str) {
|
|
9
|
+
return str.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
10
|
+
}
|
|
11
|
+
function escape(value) {
|
|
12
|
+
if (typeof value != "string")
|
|
13
|
+
return value;
|
|
14
|
+
value = value.replace(/[\0\n\r\b\t\\'"\x1a]/g, (s) => s == "\0" ? "\\0" : s == "\n" ? "\\n" : s == "\r" ? "\\r" : s == "\b" ? "\\b" : s == " " ? "\\t" : s == "" ? "\\Z" : "\\" + s).replace(/[\x00-\x1f\x7f-\x9f]/g, (ch) => "\\x" + ch.charCodeAt(0).toString(16).padStart(2, "0"));
|
|
15
|
+
return `'${value}'`;
|
|
16
|
+
}
|
|
17
|
+
var {
|
|
18
|
+
assign,
|
|
19
|
+
create,
|
|
20
|
+
defineProperty,
|
|
21
|
+
entries,
|
|
22
|
+
freeze,
|
|
23
|
+
getOwnPropertyDescriptor,
|
|
24
|
+
getOwnPropertyNames,
|
|
25
|
+
values
|
|
26
|
+
} = Object;
|
|
27
|
+
|
|
28
|
+
// src/type/Field.ts
|
|
29
|
+
var REGISTER = /* @__PURE__ */ new Map();
|
|
30
|
+
var Field = class {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.options = options;
|
|
33
|
+
this.type = "";
|
|
34
|
+
this.unique = false;
|
|
35
|
+
this.nullable = false;
|
|
36
|
+
this.optional = false;
|
|
37
|
+
this.increment = false;
|
|
38
|
+
}
|
|
39
|
+
init(property, parent) {
|
|
40
|
+
let options = this.options;
|
|
41
|
+
const field = create(this);
|
|
42
|
+
field.parent = parent;
|
|
43
|
+
field.property = property;
|
|
44
|
+
field.column = underscore(property);
|
|
45
|
+
if (typeof options === "function")
|
|
46
|
+
options = options(field) || {};
|
|
47
|
+
for (const key in options) {
|
|
48
|
+
const value = options[key];
|
|
49
|
+
if (value !== void 0)
|
|
50
|
+
field[key] = value;
|
|
51
|
+
}
|
|
52
|
+
if (!field.datatype)
|
|
53
|
+
field.datatype = field.type;
|
|
54
|
+
return field;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* This method dictates behavior of this field when converted from a javascript context to SQL.
|
|
58
|
+
*
|
|
59
|
+
* Use this method to validate, sanitize or convert data before it is inserted into the database.
|
|
60
|
+
*/
|
|
61
|
+
set(value) {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* This method dictates behavior of this field when converted from a SQL context to javascript.
|
|
66
|
+
*
|
|
67
|
+
* Use this method to parse data incoming from the database itself. For example, you might convert
|
|
68
|
+
* a TINYINT(1) field to a boolean, or a DATETIME field to a Date object.
|
|
69
|
+
*/
|
|
70
|
+
get(value) {
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
raw(value) {
|
|
74
|
+
return escape(this.set(value));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* This method is used to compare this field with another value in a query.
|
|
78
|
+
*
|
|
79
|
+
* @param acc Set of comparisons to be added to if not part of a group.
|
|
80
|
+
*/
|
|
81
|
+
where() {
|
|
82
|
+
const use = (op2) => (right, orEqual) => this.query.where(this, op2 + (orEqual ? "=" : ""), right);
|
|
83
|
+
return {
|
|
84
|
+
is: use("="),
|
|
85
|
+
in: use("IN"),
|
|
86
|
+
not: use("<>"),
|
|
87
|
+
over: use(">"),
|
|
88
|
+
under: use("<")
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
function fields(from) {
|
|
93
|
+
let fields2 = REGISTER.get(from);
|
|
94
|
+
if (!fields2) {
|
|
95
|
+
fields2 = /* @__PURE__ */ new Map();
|
|
96
|
+
REGISTER.set(from, fields2);
|
|
97
|
+
const reference = new from();
|
|
98
|
+
for (const key in reference) {
|
|
99
|
+
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
100
|
+
if (!(value instanceof Field))
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
|
|
103
|
+
);
|
|
104
|
+
const instance = value.init(key, from);
|
|
105
|
+
from.fields.set(key, instance);
|
|
106
|
+
freeze(instance);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return fields2;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/columns/Bool.ts
|
|
113
|
+
var BooleanColumn = class extends Field {
|
|
114
|
+
constructor(opts = {}) {
|
|
115
|
+
let type = "tinyint";
|
|
116
|
+
let datatype = "tinyint";
|
|
117
|
+
let TRUE = 1;
|
|
118
|
+
let FALSE = 0;
|
|
119
|
+
if ("either" in opts && opts.either) {
|
|
120
|
+
[TRUE, FALSE] = opts.either;
|
|
121
|
+
type = "varchar";
|
|
122
|
+
datatype = `varchar(${Math.max(TRUE.length, FALSE.length)})`;
|
|
123
|
+
}
|
|
124
|
+
super(opts);
|
|
125
|
+
this.type = "tinyint";
|
|
126
|
+
this.TRUE = 1;
|
|
127
|
+
this.FALSE = 0;
|
|
128
|
+
this.type = type;
|
|
129
|
+
this.datatype = datatype;
|
|
130
|
+
this.TRUE = TRUE;
|
|
131
|
+
this.FALSE = FALSE;
|
|
132
|
+
}
|
|
133
|
+
get(value) {
|
|
134
|
+
return value === this.TRUE;
|
|
135
|
+
}
|
|
136
|
+
set(value) {
|
|
137
|
+
if (typeof value !== "boolean")
|
|
138
|
+
throw "Value must be a boolean.";
|
|
139
|
+
return value ? this.TRUE : this.FALSE;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
function Bool(opts) {
|
|
143
|
+
return new BooleanColumn(opts);
|
|
144
|
+
}
|
|
145
|
+
Bool.Type = BooleanColumn;
|
|
146
|
+
|
|
147
|
+
// src/columns/Num.ts
|
|
148
|
+
var NumericColumn = class extends Field {
|
|
149
|
+
constructor() {
|
|
150
|
+
super(...arguments);
|
|
151
|
+
this.type = "int";
|
|
152
|
+
}
|
|
153
|
+
set(value) {
|
|
154
|
+
if (typeof value !== "number" || isNaN(value))
|
|
155
|
+
throw `Got '${value}' but value must be a number.`;
|
|
156
|
+
if (this.type === "int" && value !== Math.floor(value))
|
|
157
|
+
throw `Got '${value}' but datatype is integer.`;
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
function Num(opts) {
|
|
162
|
+
return new NumericColumn(opts);
|
|
163
|
+
}
|
|
164
|
+
Num.Type = NumericColumn;
|
|
165
|
+
|
|
166
|
+
// src/columns/One.ts
|
|
167
|
+
var ForeignColumn = class extends Field {
|
|
168
|
+
constructor(type, nullable) {
|
|
169
|
+
super(({ property }) => ({
|
|
170
|
+
column: underscore(property) + "_id"
|
|
171
|
+
}));
|
|
172
|
+
this.foreignKey = "id";
|
|
173
|
+
this.nullable = false;
|
|
174
|
+
this.entity = type;
|
|
175
|
+
this.type = "int";
|
|
176
|
+
this.foreignKey = "id";
|
|
177
|
+
this.foreignTable = type.table;
|
|
178
|
+
this.foreignTable = type.table;
|
|
179
|
+
this.nullable = nullable || false;
|
|
180
|
+
}
|
|
181
|
+
set(value) {
|
|
182
|
+
if (this.query?.tables.has(value))
|
|
183
|
+
return value[this.foreignKey];
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
use(query) {
|
|
187
|
+
const table = query.use(this.entity);
|
|
188
|
+
query.where(table.id, "=", this);
|
|
189
|
+
return table;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function One(type, nullable) {
|
|
193
|
+
return new ForeignColumn(type, nullable);
|
|
194
|
+
}
|
|
195
|
+
One.Type = ForeignColumn;
|
|
196
|
+
|
|
197
|
+
// src/columns/Str.ts
|
|
198
|
+
var VARIABLE_TYPE = /* @__PURE__ */ new Set(["char", "varchar", "text"]);
|
|
199
|
+
var StringColumn = class extends Field {
|
|
200
|
+
constructor(opts) {
|
|
201
|
+
super(({ parent, property }) => {
|
|
202
|
+
let { type = "varchar", length = 255, datatype } = opts || {};
|
|
203
|
+
if (VARIABLE_TYPE.has(type))
|
|
204
|
+
if (length)
|
|
205
|
+
datatype = `${datatype || type}(${length})`;
|
|
206
|
+
else
|
|
207
|
+
throw `Can't determine datatype! Length is not specified for ${parent.name}.${property}.`;
|
|
208
|
+
return {
|
|
209
|
+
datatype,
|
|
210
|
+
...opts
|
|
211
|
+
};
|
|
212
|
+
});
|
|
213
|
+
this.type = "varchar";
|
|
214
|
+
this.length = 255;
|
|
215
|
+
}
|
|
216
|
+
set(value) {
|
|
217
|
+
if (typeof value !== "string")
|
|
218
|
+
throw "Value must be a string.";
|
|
219
|
+
if (this.length && value.length > this.length)
|
|
220
|
+
throw `Value length ${value.length} exceeds maximum of ${this.length}.`;
|
|
221
|
+
return value;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
function Str(opts) {
|
|
225
|
+
return new StringColumn(opts);
|
|
226
|
+
}
|
|
227
|
+
Str.Type = StringColumn;
|
|
228
|
+
|
|
229
|
+
// src/columns/Time.ts
|
|
230
|
+
var TimeColumn = class extends Field {
|
|
231
|
+
constructor() {
|
|
232
|
+
super(...arguments);
|
|
233
|
+
this.type = "datetime";
|
|
234
|
+
this.fallback = void 0;
|
|
235
|
+
}
|
|
236
|
+
get(value) {
|
|
237
|
+
return new global.Date(value.replace(/[-]/g, "/") + "Z");
|
|
238
|
+
}
|
|
239
|
+
set(value) {
|
|
240
|
+
if (value === "NOW")
|
|
241
|
+
return "CURRENT_TIMESTAMP";
|
|
242
|
+
else if (value instanceof Date)
|
|
243
|
+
return value.toISOString().slice(0, 19).replace("T", " ");
|
|
244
|
+
else
|
|
245
|
+
throw "Value must be a Date or 'NOW'.";
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
function Time(options) {
|
|
249
|
+
return new TimeColumn(options);
|
|
250
|
+
}
|
|
251
|
+
Time.Type = TimeColumn;
|
|
252
|
+
|
|
253
|
+
// src/connection/Connection.ts
|
|
254
|
+
import { format } from "sql-formatter";
|
|
255
|
+
|
|
256
|
+
// src/query/Builder.ts
|
|
257
|
+
var Builder = class {
|
|
258
|
+
constructor() {
|
|
259
|
+
/**
|
|
260
|
+
* Parameters which will be sent with query.
|
|
261
|
+
* May either be placeholders for a template, or values
|
|
262
|
+
* which the query deems unsafe to interpolate directly.
|
|
263
|
+
*/
|
|
264
|
+
this.params = [];
|
|
265
|
+
this.filters = new Group();
|
|
266
|
+
this.pending = /* @__PURE__ */ new Set();
|
|
267
|
+
this.order = /* @__PURE__ */ new Map();
|
|
268
|
+
this.tables = /* @__PURE__ */ new Map();
|
|
269
|
+
this.deletes = /* @__PURE__ */ new Set();
|
|
270
|
+
this.updates = /* @__PURE__ */ new Map();
|
|
271
|
+
this.inserts = /* @__PURE__ */ new Map();
|
|
272
|
+
}
|
|
273
|
+
commit(returns) {
|
|
274
|
+
if (this.returns)
|
|
275
|
+
throw new Error("This query has already been committed.");
|
|
276
|
+
this.pending.forEach((fn) => fn());
|
|
277
|
+
this.pending.clear();
|
|
278
|
+
if (typeof returns === "function")
|
|
279
|
+
returns = returns();
|
|
280
|
+
if (returns instanceof Field || returns instanceof Value) {
|
|
281
|
+
this.returns = returns;
|
|
282
|
+
} else if (returns) {
|
|
283
|
+
let scan2 = function(obj, path) {
|
|
284
|
+
getOwnPropertyNames(obj).forEach((key) => {
|
|
285
|
+
const use = path ? `${path}.${key}` : key;
|
|
286
|
+
let selects = obj[key];
|
|
287
|
+
if (typeof selects == "function")
|
|
288
|
+
selects = selects();
|
|
289
|
+
if (selects instanceof Field || selects instanceof Value || typeof selects != "object")
|
|
290
|
+
columns.set(use, selects);
|
|
291
|
+
else
|
|
292
|
+
scan2(selects, use);
|
|
293
|
+
});
|
|
294
|
+
};
|
|
295
|
+
var scan = scan2;
|
|
296
|
+
const columns = /* @__PURE__ */ new Map();
|
|
297
|
+
scan2(returns);
|
|
298
|
+
this.returns = columns;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
inject(args) {
|
|
302
|
+
return Array.from({ length: args }, (_, i) => {
|
|
303
|
+
const p = new Parameter(i);
|
|
304
|
+
return () => {
|
|
305
|
+
if (this.params.includes(p))
|
|
306
|
+
throw new Error(`Parameter ${i} is already defined.`);
|
|
307
|
+
else
|
|
308
|
+
this.params.push(p);
|
|
309
|
+
return p;
|
|
310
|
+
};
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
table(table) {
|
|
314
|
+
const target = this.tables.get(table);
|
|
315
|
+
return {
|
|
316
|
+
delete: () => {
|
|
317
|
+
this.deletes.add(target);
|
|
318
|
+
},
|
|
319
|
+
update: (data) => {
|
|
320
|
+
const inserts = /* @__PURE__ */ new Map();
|
|
321
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
322
|
+
const field = target.fields.get(key);
|
|
323
|
+
if (value instanceof DataField)
|
|
324
|
+
value.field = field;
|
|
325
|
+
inserts.set(field, value);
|
|
326
|
+
});
|
|
327
|
+
this.updates.set(target, inserts);
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
field(field) {
|
|
332
|
+
return {
|
|
333
|
+
...field.where(),
|
|
334
|
+
asc: () => {
|
|
335
|
+
this.order.set(field, "asc");
|
|
336
|
+
},
|
|
337
|
+
desc: () => {
|
|
338
|
+
this.order.set(field, "desc");
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
andOr(...args) {
|
|
343
|
+
const local = new Group();
|
|
344
|
+
this.filters.add(local);
|
|
345
|
+
for (const eq of args) {
|
|
346
|
+
if (eq instanceof Cond && eq.restricted)
|
|
347
|
+
throw new Error(
|
|
348
|
+
`Cannot use ${eq.left} ${eq.op} ${eq.right} in a group.`
|
|
349
|
+
);
|
|
350
|
+
this.filters.delete(eq);
|
|
351
|
+
local.add(eq);
|
|
352
|
+
}
|
|
353
|
+
return local;
|
|
354
|
+
}
|
|
355
|
+
use(type, arg2, ...rest) {
|
|
356
|
+
const { fields: fields2, schema } = type;
|
|
357
|
+
const { tables } = this;
|
|
358
|
+
const proxy = {};
|
|
359
|
+
const table = {
|
|
360
|
+
name: type.table,
|
|
361
|
+
fields: /* @__PURE__ */ new Map(),
|
|
362
|
+
joins: [],
|
|
363
|
+
proxy,
|
|
364
|
+
optional: arg2 === true,
|
|
365
|
+
toString() {
|
|
366
|
+
return this.alias ? this.name + " " + this.alias : this.name;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
if (rest.length)
|
|
370
|
+
arg2 = this.with([arg2, ...rest]);
|
|
371
|
+
if (schema) {
|
|
372
|
+
table.alias = "T" + tables.size;
|
|
373
|
+
table.name = schema + "." + table.name;
|
|
374
|
+
}
|
|
375
|
+
tables.set(proxy, table);
|
|
376
|
+
fields2.forEach((field, key) => {
|
|
377
|
+
field = create(field);
|
|
378
|
+
field.query = this;
|
|
379
|
+
field.table = table;
|
|
380
|
+
field.toString = () => `${table.alias || table.name}.${field.column}`;
|
|
381
|
+
table.fields.set(key, field);
|
|
382
|
+
let value;
|
|
383
|
+
defineProperty(proxy, key, {
|
|
384
|
+
get: () => value || (value = field.use ? field.use(this) : field)
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
if (typeof arg2 == "object")
|
|
388
|
+
this.insert(table, arg2);
|
|
389
|
+
freeze(proxy);
|
|
390
|
+
return table.proxy;
|
|
391
|
+
}
|
|
392
|
+
insert(table, input) {
|
|
393
|
+
const inserts = /* @__PURE__ */ new Map();
|
|
394
|
+
for (const [key, field] of table.fields) {
|
|
395
|
+
let value = input[key];
|
|
396
|
+
if (value instanceof DataField)
|
|
397
|
+
value.field = field;
|
|
398
|
+
else
|
|
399
|
+
value = this.digest(value, field);
|
|
400
|
+
if (value !== void 0)
|
|
401
|
+
inserts.set(field, value);
|
|
402
|
+
}
|
|
403
|
+
this.inserts.set(table, inserts);
|
|
404
|
+
}
|
|
405
|
+
digest(value, field) {
|
|
406
|
+
if (value != null)
|
|
407
|
+
try {
|
|
408
|
+
return field.set(value);
|
|
409
|
+
} catch (err) {
|
|
410
|
+
let message = `Provided value for ${field.parent}.${field.column} but not acceptable for type ${field.datatype}.`;
|
|
411
|
+
if (err instanceof Error) {
|
|
412
|
+
err.message = message + "\n" + err.message;
|
|
413
|
+
throw err;
|
|
414
|
+
}
|
|
415
|
+
if (typeof err == "string")
|
|
416
|
+
message += "\n" + err;
|
|
417
|
+
throw new Error(message);
|
|
418
|
+
}
|
|
419
|
+
else if (!field.nullable && !field.optional && !field.increment)
|
|
420
|
+
throw new Error(
|
|
421
|
+
`Can't assign to \`${field.parent}.${field.property}\`. A value is required but got ${value}.`
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
where(left, op2, right) {
|
|
425
|
+
if (typeof right == "function")
|
|
426
|
+
right = right();
|
|
427
|
+
if (right === null && !left.nullable)
|
|
428
|
+
throw new Error(`Column ${left} is not nullable.`);
|
|
429
|
+
if (right instanceof Field || right instanceof DataField) {
|
|
430
|
+
const where = new Cond(left, op2, right, true);
|
|
431
|
+
if (right instanceof DataField) {
|
|
432
|
+
right.field = left;
|
|
433
|
+
right.table.joins.push(where);
|
|
434
|
+
} else
|
|
435
|
+
left.table.joins.push(where);
|
|
436
|
+
return where;
|
|
437
|
+
}
|
|
438
|
+
if (right instanceof Parameter) {
|
|
439
|
+
right.digest = left.set.bind(this);
|
|
440
|
+
}
|
|
441
|
+
return this.filters.add(left, op2, right);
|
|
442
|
+
}
|
|
443
|
+
with(data) {
|
|
444
|
+
return new DataTable(this, data).proxy;
|
|
445
|
+
}
|
|
446
|
+
accept(args) {
|
|
447
|
+
return Array.from(this.params || [], (p) => p.toParam(args));
|
|
448
|
+
}
|
|
449
|
+
parse(raw) {
|
|
450
|
+
const { returns: selects } = this;
|
|
451
|
+
if (selects instanceof Map) {
|
|
452
|
+
const values2 = {};
|
|
453
|
+
selects.forEach((field, column) => {
|
|
454
|
+
const path = column.split(".");
|
|
455
|
+
const property = path.pop();
|
|
456
|
+
let target = values2;
|
|
457
|
+
for (const step of path)
|
|
458
|
+
target = target[step] || (target[step] = {});
|
|
459
|
+
const value2 = raw[column];
|
|
460
|
+
target[property] = field instanceof Field ? field.get(value2) : selects instanceof Parameter ? selects.digest(value2) : value2;
|
|
461
|
+
});
|
|
462
|
+
return values2;
|
|
463
|
+
}
|
|
464
|
+
const value = values(raw)[0];
|
|
465
|
+
if (selects instanceof Parameter)
|
|
466
|
+
return selects.digest(value);
|
|
467
|
+
if (selects instanceof Field)
|
|
468
|
+
return selects.get(value);
|
|
469
|
+
return raw;
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
var Value = class {
|
|
473
|
+
};
|
|
474
|
+
var Parameter = class extends Value {
|
|
475
|
+
constructor(arg) {
|
|
476
|
+
super();
|
|
477
|
+
this.index = -1;
|
|
478
|
+
if (typeof arg == "number")
|
|
479
|
+
this.index = arg;
|
|
480
|
+
else if (typeof arg == "function")
|
|
481
|
+
this.toParam = arg;
|
|
482
|
+
}
|
|
483
|
+
digest(value) {
|
|
484
|
+
return value;
|
|
485
|
+
}
|
|
486
|
+
toParam(from) {
|
|
487
|
+
return this.digest(from[this.index]);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
var DataField = class extends Value {
|
|
491
|
+
constructor(column, table) {
|
|
492
|
+
super();
|
|
493
|
+
this.column = column;
|
|
494
|
+
this.table = table;
|
|
495
|
+
}
|
|
496
|
+
get datatype() {
|
|
497
|
+
return this.field?.datatype || "";
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
var DataTable = class extends Parameter {
|
|
501
|
+
constructor(parent, input) {
|
|
502
|
+
super();
|
|
503
|
+
this.parent = parent;
|
|
504
|
+
this.output = [];
|
|
505
|
+
this.used = /* @__PURE__ */ new Set();
|
|
506
|
+
this.joins = [];
|
|
507
|
+
this.optional = false;
|
|
508
|
+
this.fields = /* @__PURE__ */ new Map();
|
|
509
|
+
this.name = "input";
|
|
510
|
+
const proxy = {};
|
|
511
|
+
const keys = /* @__PURE__ */ new Set();
|
|
512
|
+
for (const item of input)
|
|
513
|
+
Object.keys(item).forEach(keys.add, keys);
|
|
514
|
+
for (const key of keys) {
|
|
515
|
+
const value = new DataField(key, this);
|
|
516
|
+
defineProperty(proxy, key, {
|
|
517
|
+
enumerable: true,
|
|
518
|
+
get: () => {
|
|
519
|
+
this.used.add(value);
|
|
520
|
+
return value;
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
this.proxy = proxy;
|
|
525
|
+
this.index = parent.params.length;
|
|
526
|
+
parent.params.push(this);
|
|
527
|
+
parent.tables.set(proxy, this);
|
|
528
|
+
parent.pending.add(() => {
|
|
529
|
+
Array.from(input, (entry, index) => {
|
|
530
|
+
const emit = {};
|
|
531
|
+
for (const { column, field } of this.used) {
|
|
532
|
+
if (!field)
|
|
533
|
+
throw new Error(`Field for input ${column} is not defined.`);
|
|
534
|
+
try {
|
|
535
|
+
emit[column] = this.parent.digest(entry[column], field);
|
|
536
|
+
} catch (err) {
|
|
537
|
+
let message = `A provided value at \`${column}\` at index [${index}] is not acceptable.`;
|
|
538
|
+
if (err instanceof Error) {
|
|
539
|
+
err.message = message + "\n" + err.message;
|
|
540
|
+
throw err;
|
|
541
|
+
}
|
|
542
|
+
if (typeof err == "string")
|
|
543
|
+
message += "\n" + err;
|
|
544
|
+
throw new Error(message);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
this.output.push(emit);
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
toParam() {
|
|
552
|
+
return Array.from(this.output, (row) => Array.from(this.used, (field) => row[field.column]));
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
var Cond = class {
|
|
556
|
+
constructor(left, op2, right, restricted = false) {
|
|
557
|
+
this.left = left;
|
|
558
|
+
this.op = op2;
|
|
559
|
+
this.right = right;
|
|
560
|
+
this.restricted = restricted;
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
var Group = class {
|
|
564
|
+
constructor() {
|
|
565
|
+
this.children = /* @__PURE__ */ new Set();
|
|
566
|
+
}
|
|
567
|
+
add(left, op2, right) {
|
|
568
|
+
if (left instanceof Field)
|
|
569
|
+
left = new Cond(left, op2, right);
|
|
570
|
+
this.children.add(left);
|
|
571
|
+
return left;
|
|
572
|
+
}
|
|
573
|
+
delete(child) {
|
|
574
|
+
this.children.delete(child);
|
|
575
|
+
}
|
|
576
|
+
get size() {
|
|
577
|
+
return this.children.size;
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
// src/query/Computed.ts
|
|
582
|
+
function op(operator, rank, unary) {
|
|
583
|
+
return (left, right) => {
|
|
584
|
+
return new Computed(operator, left, unary ? void 0 : right, rank);
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
var math = {
|
|
588
|
+
add: op("+", 4),
|
|
589
|
+
sub: op("-", 4),
|
|
590
|
+
mul: op("*", 5),
|
|
591
|
+
div: op("/", 5),
|
|
592
|
+
mod: op("%", 5),
|
|
593
|
+
neg: op("-", 7, true),
|
|
594
|
+
pos: op("+", 7, true)
|
|
595
|
+
};
|
|
596
|
+
var bit = {
|
|
597
|
+
not: op("~", 6, true),
|
|
598
|
+
left: op("<<", 3),
|
|
599
|
+
right: op(">>", 3),
|
|
600
|
+
and: op("&", 2),
|
|
601
|
+
or: op("|", 0),
|
|
602
|
+
xor: op("^", 1)
|
|
603
|
+
};
|
|
604
|
+
var Computed = class extends Value {
|
|
605
|
+
constructor(operator, value, right, rank = 0) {
|
|
606
|
+
super();
|
|
607
|
+
this.operator = operator;
|
|
608
|
+
this.rank = rank;
|
|
609
|
+
if (right === void 0)
|
|
610
|
+
this.right = value;
|
|
611
|
+
else {
|
|
612
|
+
this.left = value;
|
|
613
|
+
this.right = right;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
// src/connection/Generator.ts
|
|
619
|
+
var Generator = class {
|
|
620
|
+
constructor(query) {
|
|
621
|
+
this.query = query;
|
|
622
|
+
this.cte = /* @__PURE__ */ new Map();
|
|
623
|
+
}
|
|
624
|
+
toString() {
|
|
625
|
+
const parts = [
|
|
626
|
+
this.with(),
|
|
627
|
+
this.insert() || this.update() || this.delete() || this.select(),
|
|
628
|
+
this.where(),
|
|
629
|
+
this.order(),
|
|
630
|
+
this.limit()
|
|
631
|
+
];
|
|
632
|
+
return parts.flat(Infinity).filter(Boolean).join(" ");
|
|
633
|
+
}
|
|
634
|
+
escape(name) {
|
|
635
|
+
return String(name).split(".").map((x) => `\`${x}\``).join(".");
|
|
636
|
+
}
|
|
637
|
+
insert() {
|
|
638
|
+
const { inserts, tables } = this.query;
|
|
639
|
+
if (!inserts.size)
|
|
640
|
+
return false;
|
|
641
|
+
const [[table, data]] = inserts;
|
|
642
|
+
const keys = [];
|
|
643
|
+
const values2 = [];
|
|
644
|
+
for (const [field, value] of data) {
|
|
645
|
+
keys.push(this.escape(field.column));
|
|
646
|
+
values2.push(this.value(value));
|
|
647
|
+
}
|
|
648
|
+
return [
|
|
649
|
+
"INSERT INTO",
|
|
650
|
+
this.escape(table.name),
|
|
651
|
+
`(${keys})`,
|
|
652
|
+
`SELECT ${values2}`,
|
|
653
|
+
tables.size > 1 && this.from()
|
|
654
|
+
];
|
|
655
|
+
}
|
|
656
|
+
with() {
|
|
657
|
+
const cte = [];
|
|
658
|
+
this.query.tables.forEach((table) => {
|
|
659
|
+
if (table instanceof DataTable)
|
|
660
|
+
cte.push(`${this.escape(table.name)} AS (${this.jsonTable(table)})`);
|
|
661
|
+
});
|
|
662
|
+
if (cte.length)
|
|
663
|
+
return "WITH " + cte.join();
|
|
664
|
+
}
|
|
665
|
+
value(input) {
|
|
666
|
+
if (input instanceof Value || input instanceof Field)
|
|
667
|
+
return this.reference(input);
|
|
668
|
+
if (typeof input === "string")
|
|
669
|
+
return `'${input.replace(/'/g, "\\'")}'`;
|
|
670
|
+
return String(input);
|
|
671
|
+
}
|
|
672
|
+
jsonTable(table) {
|
|
673
|
+
throw new Error("Not implemented.");
|
|
674
|
+
}
|
|
675
|
+
computed(from) {
|
|
676
|
+
let { left, operator, right } = from;
|
|
677
|
+
if (right instanceof Computed) {
|
|
678
|
+
const value = this.computed(right);
|
|
679
|
+
right = right.rank <= from.rank ? `(${value})` : value;
|
|
680
|
+
}
|
|
681
|
+
if (!left)
|
|
682
|
+
return `${operator}${right}`;
|
|
683
|
+
if (left instanceof Computed) {
|
|
684
|
+
const value = this.computed(left);
|
|
685
|
+
left = left.rank < from.rank ? `(${value})` : value;
|
|
686
|
+
}
|
|
687
|
+
return `${left} ${operator} ${right}`;
|
|
688
|
+
}
|
|
689
|
+
joins(main) {
|
|
690
|
+
const output = [];
|
|
691
|
+
const applied = /* @__PURE__ */ new Set();
|
|
692
|
+
for (const table of this.query.tables.values()) {
|
|
693
|
+
if (table === main)
|
|
694
|
+
continue;
|
|
695
|
+
const { alias, name, optional } = table;
|
|
696
|
+
if (table.joins.length === 0)
|
|
697
|
+
throw new Error(`Table ${name} has no joins.`);
|
|
698
|
+
const joins = table.joins.filter((x) => {
|
|
699
|
+
if (!applied.has(x)) {
|
|
700
|
+
applied.add(x);
|
|
701
|
+
return true;
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
const mode = optional ? "LEFT" : "INNER";
|
|
705
|
+
const as = name + (alias ? ` ${alias}` : "");
|
|
706
|
+
const conds = joins.map((x) => this.filter(x.left, x.op, x.right));
|
|
707
|
+
output.push(`${mode} JOIN ${as} ON ${conds.join(" AND ")}`);
|
|
708
|
+
}
|
|
709
|
+
if (output.length)
|
|
710
|
+
return output.join(" ");
|
|
711
|
+
}
|
|
712
|
+
set(updates) {
|
|
713
|
+
const sets = [];
|
|
714
|
+
for (const [_table, data] of updates)
|
|
715
|
+
for (let [field, value] of data) {
|
|
716
|
+
if (value === void 0)
|
|
717
|
+
continue;
|
|
718
|
+
if (value instanceof Field || value instanceof Value) {
|
|
719
|
+
value = this.reference(value);
|
|
720
|
+
} else if (value === null) {
|
|
721
|
+
if (field.nullable)
|
|
722
|
+
value = "NULL";
|
|
723
|
+
else
|
|
724
|
+
throw new Error(`Column ${field} is not nullable.`);
|
|
725
|
+
} else
|
|
726
|
+
value = field.raw(value);
|
|
727
|
+
sets.push(`${this.escape(field.column)} = ${value}`);
|
|
728
|
+
}
|
|
729
|
+
if (!sets.length)
|
|
730
|
+
throw new Error("Update contains no values.");
|
|
731
|
+
return `SET ${sets}`;
|
|
732
|
+
}
|
|
733
|
+
update(multiTableAllowed = false) {
|
|
734
|
+
const { updates } = this.query;
|
|
735
|
+
if (!updates.size)
|
|
736
|
+
return;
|
|
737
|
+
if (updates.size > 1 && !multiTableAllowed)
|
|
738
|
+
throw new Error("Engine does not support multi-table updates.");
|
|
739
|
+
const [main] = updates.keys();
|
|
740
|
+
return [
|
|
741
|
+
"UPDATE",
|
|
742
|
+
this.escape(main),
|
|
743
|
+
this.joins(main),
|
|
744
|
+
this.set(updates)
|
|
745
|
+
];
|
|
746
|
+
}
|
|
747
|
+
delete() {
|
|
748
|
+
const { query } = this;
|
|
749
|
+
const [main] = this.query.deletes;
|
|
750
|
+
if (!main)
|
|
751
|
+
return;
|
|
752
|
+
const { alias, name } = main;
|
|
753
|
+
const target = query.tables.size > 1 && (alias || name);
|
|
754
|
+
return ["DELETE", target, "FROM", this.escape(name), this.joins(main)];
|
|
755
|
+
}
|
|
756
|
+
reference(from) {
|
|
757
|
+
if (from instanceof DataField)
|
|
758
|
+
return this.escape(from.table.name + "." + from.column);
|
|
759
|
+
if (from instanceof Computed)
|
|
760
|
+
return this.computed(from);
|
|
761
|
+
if (from instanceof Parameter)
|
|
762
|
+
return this.param(from);
|
|
763
|
+
if (from instanceof Field) {
|
|
764
|
+
const column = this.escape(from.column);
|
|
765
|
+
const { table } = from;
|
|
766
|
+
if (table)
|
|
767
|
+
return this.escape(table.alias || table.name) + "." + column;
|
|
768
|
+
}
|
|
769
|
+
return from;
|
|
770
|
+
}
|
|
771
|
+
select() {
|
|
772
|
+
const { returns, tables } = this.query;
|
|
773
|
+
const [main] = tables.values();
|
|
774
|
+
const selection = returns instanceof Map ? (
|
|
775
|
+
// TODO: simplify this
|
|
776
|
+
Array.from(returns).map(([alias, field]) => this.value(field) + ` AS "${alias}"`).join(", ")
|
|
777
|
+
) : returns ? this.reference(returns) : "COUNT(*)";
|
|
778
|
+
return [
|
|
779
|
+
"SELECT",
|
|
780
|
+
selection,
|
|
781
|
+
this.from(),
|
|
782
|
+
this.joins(main)
|
|
783
|
+
];
|
|
784
|
+
}
|
|
785
|
+
from() {
|
|
786
|
+
const [main] = this.query.tables.values();
|
|
787
|
+
if (main)
|
|
788
|
+
return ["FROM", this.escape(main.name), main.alias];
|
|
789
|
+
}
|
|
790
|
+
param(from) {
|
|
791
|
+
return "?";
|
|
792
|
+
}
|
|
793
|
+
filter(left, op2, right) {
|
|
794
|
+
if (right === null) {
|
|
795
|
+
right === "NULL";
|
|
796
|
+
if (op2 == "=")
|
|
797
|
+
op2 = "IS";
|
|
798
|
+
else if (op2 == "!=")
|
|
799
|
+
op2 = "IS NOT";
|
|
800
|
+
else
|
|
801
|
+
throw new Error("Cannot compare NULL with " + op2);
|
|
802
|
+
} else if (right instanceof Array)
|
|
803
|
+
right = `(${right.map(left.raw, left)})`;
|
|
804
|
+
else if (right instanceof Field || right instanceof Value)
|
|
805
|
+
right = this.reference(right);
|
|
806
|
+
else
|
|
807
|
+
right = left.raw(right);
|
|
808
|
+
return `${this.reference(left)} ${op2} ${right}`;
|
|
809
|
+
}
|
|
810
|
+
where() {
|
|
811
|
+
const { filters } = this.query;
|
|
812
|
+
if (!filters.size) return;
|
|
813
|
+
const combine = (group, or) => {
|
|
814
|
+
return Array.from(group.children, (cond, i) => {
|
|
815
|
+
if (cond instanceof Group) {
|
|
816
|
+
const inner = combine(cond, !or);
|
|
817
|
+
const first = or !== false && i === 0;
|
|
818
|
+
return cond.size === 1 || first ? inner : `(${inner})`;
|
|
819
|
+
}
|
|
820
|
+
return this.filter(cond.left, cond.op, cond.right);
|
|
821
|
+
}).join(or ? " OR " : " AND ");
|
|
822
|
+
};
|
|
823
|
+
return ["WHERE", combine(filters)];
|
|
824
|
+
}
|
|
825
|
+
order() {
|
|
826
|
+
const { order } = this.query;
|
|
827
|
+
if (order.size)
|
|
828
|
+
return "ORDER BY " + Array.from(order, ([x, y]) => `${this.reference(x)} ${y}`);
|
|
829
|
+
}
|
|
830
|
+
limit() {
|
|
831
|
+
const { limit } = this.query;
|
|
832
|
+
if (limit)
|
|
833
|
+
return ["LIMIT", limit];
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
// src/connection/Connection.ts
|
|
838
|
+
var Connection = class {
|
|
839
|
+
constructor(using) {
|
|
840
|
+
this.ready = false;
|
|
841
|
+
this.using = new Set(values(using).map((type) => {
|
|
842
|
+
if (type.hasOwnProperty("connection"))
|
|
843
|
+
throw new Error(`Type ${type.name} already has assigned connection.`);
|
|
844
|
+
type.connection = this;
|
|
845
|
+
return type;
|
|
846
|
+
}));
|
|
847
|
+
}
|
|
848
|
+
generate(builder) {
|
|
849
|
+
const { generator } = this.constructor;
|
|
850
|
+
return new generator(builder).toString();
|
|
851
|
+
}
|
|
852
|
+
async then(onfulfilled, onrejected) {
|
|
853
|
+
return (this.ready ? Promise.resolve() : this.sync(true)).then(() => {
|
|
854
|
+
Object.defineProperty(this, "then", { value: void 0 });
|
|
855
|
+
if (onfulfilled)
|
|
856
|
+
onfulfilled(this);
|
|
857
|
+
}).catch(onrejected);
|
|
858
|
+
}
|
|
859
|
+
async close() {
|
|
860
|
+
for (const type of this.using)
|
|
861
|
+
delete type.connection;
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
Connection.generator = Generator;
|
|
865
|
+
var NoConnection = class extends Connection {
|
|
866
|
+
constructor() {
|
|
867
|
+
super([]);
|
|
868
|
+
}
|
|
869
|
+
get schema() {
|
|
870
|
+
throw new Error("No connection to generate schema.");
|
|
871
|
+
}
|
|
872
|
+
prepare(template) {
|
|
873
|
+
async function run() {
|
|
874
|
+
throw new Error("No connection to run query.");
|
|
875
|
+
}
|
|
876
|
+
return {
|
|
877
|
+
all: run,
|
|
878
|
+
get: run,
|
|
879
|
+
run,
|
|
880
|
+
toString() {
|
|
881
|
+
return pretty(template);
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
async sync() {
|
|
886
|
+
return;
|
|
887
|
+
}
|
|
888
|
+
async valid() {
|
|
889
|
+
return true;
|
|
890
|
+
}
|
|
891
|
+
async close() {
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
function pretty(sql) {
|
|
896
|
+
return format(sql, { keywordCase: "upper" }).replace(/ - > > /g, " ->> ").replace(/`([a-zA-Z][a-zA-Z0-9_]*)`/g, "$1");
|
|
897
|
+
}
|
|
898
|
+
Connection.None = new NoConnection();
|
|
899
|
+
|
|
900
|
+
// src/type/Type.ts
|
|
901
|
+
var _Type = class _Type {
|
|
902
|
+
constructor() {
|
|
903
|
+
/**
|
|
904
|
+
* Primary key of this entity.
|
|
905
|
+
* May be any name in the actual database, however requred to be `id` as property of this type.
|
|
906
|
+
*/
|
|
907
|
+
this.id = new Primary();
|
|
908
|
+
}
|
|
909
|
+
static get fields() {
|
|
910
|
+
return fields(this);
|
|
911
|
+
}
|
|
912
|
+
static get table() {
|
|
913
|
+
return this.table = underscore(this.name.replace(/\d+$/, ""));
|
|
914
|
+
}
|
|
915
|
+
static set table(value) {
|
|
916
|
+
defineProperty(this, "table", { value });
|
|
917
|
+
}
|
|
918
|
+
static toString() {
|
|
919
|
+
return this.name;
|
|
920
|
+
}
|
|
921
|
+
static is(obj) {
|
|
922
|
+
return typeof obj === "function" && obj.prototype instanceof _Type;
|
|
923
|
+
}
|
|
924
|
+
static insert(arg1, arg2) {
|
|
925
|
+
let data;
|
|
926
|
+
if (typeof arg2 == "function") {
|
|
927
|
+
if (typeof arg1 == "number")
|
|
928
|
+
data = Array.from({ length: arg1 }, (_, i) => arg2(i));
|
|
929
|
+
else if (isIterable(arg1))
|
|
930
|
+
data = Array.from(arg1, arg2);
|
|
931
|
+
else
|
|
932
|
+
throw new Error("Map function needs an input iterable or a number to generate data.");
|
|
933
|
+
} else if (isIterable(arg1))
|
|
934
|
+
data = Array.from(arg1);
|
|
935
|
+
else if (typeof arg1 == "object")
|
|
936
|
+
data = [arg1];
|
|
937
|
+
else
|
|
938
|
+
throw new Error("Invalid input for insert method.");
|
|
939
|
+
return Query((where) => {
|
|
940
|
+
where(this, ...data);
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
static get(arg1, arg2) {
|
|
944
|
+
const limit = typeof arg1 == "number" ? arg1 : void 0;
|
|
945
|
+
const query = typeof arg1 == "function" ? arg1 : arg2;
|
|
946
|
+
return Query((where) => {
|
|
947
|
+
const self = where(this);
|
|
948
|
+
if (limit)
|
|
949
|
+
where(limit);
|
|
950
|
+
return typeof query == "function" && query(self, where) || self;
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
static one(query) {
|
|
954
|
+
return Query((where) => {
|
|
955
|
+
const self = where(this);
|
|
956
|
+
if (typeof query == "function")
|
|
957
|
+
return query(self, where) || self;
|
|
958
|
+
if (typeof query == "number")
|
|
959
|
+
where(self.id).is(query);
|
|
960
|
+
else
|
|
961
|
+
where(self.id).desc();
|
|
962
|
+
return self;
|
|
963
|
+
}).one(true);
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
_Type.schema = "";
|
|
967
|
+
var Type = _Type;
|
|
968
|
+
var Primary = class extends Field {
|
|
969
|
+
constructor() {
|
|
970
|
+
super(...arguments);
|
|
971
|
+
this.type = "int";
|
|
972
|
+
this.increment = true;
|
|
973
|
+
this.optional = true;
|
|
974
|
+
this.nullable = false;
|
|
975
|
+
this.primary = true;
|
|
976
|
+
this.unique = true;
|
|
977
|
+
}
|
|
978
|
+
};
|
|
979
|
+
|
|
980
|
+
// src/query/Query.ts
|
|
981
|
+
function Query(factory) {
|
|
982
|
+
let connection = void 0;
|
|
983
|
+
function where(arg1) {
|
|
984
|
+
if (Type.is(arg1)) {
|
|
985
|
+
if (!connection)
|
|
986
|
+
connection = arg1.connection;
|
|
987
|
+
else if (connection !== arg1.connection)
|
|
988
|
+
throw new Error(
|
|
989
|
+
`Joined entity ${arg1} does not share a connection with other tables in Query.`
|
|
990
|
+
);
|
|
991
|
+
return builder.use(arg1, ...Array.from(arguments).slice(1));
|
|
992
|
+
}
|
|
993
|
+
if (arg1 instanceof Field)
|
|
994
|
+
return builder.field(arg1);
|
|
995
|
+
if (builder.tables.has(arg1))
|
|
996
|
+
return builder.table(arg1);
|
|
997
|
+
if (arg1 instanceof Cond || arg1 instanceof Group)
|
|
998
|
+
return builder.andOr(...arguments);
|
|
999
|
+
if (typeof arg1 == "number") {
|
|
1000
|
+
builder.limit = arg1;
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
if (Symbol.iterator in arg1)
|
|
1004
|
+
return builder.with(arg1);
|
|
1005
|
+
throw new Error(`Argument ${arg1} is not a query argument.`);
|
|
1006
|
+
}
|
|
1007
|
+
defineProperty(where, "connection", {
|
|
1008
|
+
get() {
|
|
1009
|
+
return connection;
|
|
1010
|
+
},
|
|
1011
|
+
set(conn) {
|
|
1012
|
+
connection = conn;
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
const builder = new Builder();
|
|
1016
|
+
let result = factory.call(builder, where);
|
|
1017
|
+
let args;
|
|
1018
|
+
if (typeof result === "function") {
|
|
1019
|
+
const params = builder.inject(args = result.length);
|
|
1020
|
+
result = result(...params);
|
|
1021
|
+
}
|
|
1022
|
+
builder.commit(result);
|
|
1023
|
+
if (!connection)
|
|
1024
|
+
connection = Connection.None;
|
|
1025
|
+
const template = connection.generate(builder);
|
|
1026
|
+
const statement = connection.prepare(template);
|
|
1027
|
+
const runner = (...params) => {
|
|
1028
|
+
const get = () => statement.all(params).then((a) => a.map((x) => builder.parse(x)));
|
|
1029
|
+
const query = create(Query.prototype);
|
|
1030
|
+
params = builder.accept(params);
|
|
1031
|
+
assign(query, {
|
|
1032
|
+
params,
|
|
1033
|
+
toString: statement.toString,
|
|
1034
|
+
then(resolve, reject) {
|
|
1035
|
+
const run = builder.returns ? get() : statement.run(params);
|
|
1036
|
+
return run.then(resolve).catch(reject);
|
|
1037
|
+
}
|
|
1038
|
+
});
|
|
1039
|
+
if (builder.returns)
|
|
1040
|
+
assign(query, {
|
|
1041
|
+
get,
|
|
1042
|
+
one: async (orFail) => {
|
|
1043
|
+
const res = await statement.get();
|
|
1044
|
+
if (res)
|
|
1045
|
+
return builder.parse(res);
|
|
1046
|
+
if (orFail)
|
|
1047
|
+
throw new Error("Query returned no results.");
|
|
1048
|
+
}
|
|
1049
|
+
});
|
|
1050
|
+
return query;
|
|
1051
|
+
};
|
|
1052
|
+
if (typeof args == "number") {
|
|
1053
|
+
runner.toString = statement.toString;
|
|
1054
|
+
return runner;
|
|
1055
|
+
}
|
|
1056
|
+
return runner();
|
|
1057
|
+
}
|
|
1058
|
+
Query.Builder = Builder;
|
|
1059
|
+
|
|
1060
|
+
// src/type/Table.ts
|
|
1061
|
+
function Table(arg1, arg2) {
|
|
1062
|
+
return new Field(({ parent }) => {
|
|
1063
|
+
if (typeof arg1 == "string") {
|
|
1064
|
+
const [name2, schema] = arg1.split(".").reverse();
|
|
1065
|
+
arg1 = { schema, ...arg2, name: name2 };
|
|
1066
|
+
}
|
|
1067
|
+
const { name, ...rest } = arg1;
|
|
1068
|
+
if (name)
|
|
1069
|
+
parent.table = name;
|
|
1070
|
+
assign(parent, rest);
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
export {
|
|
1074
|
+
Bool,
|
|
1075
|
+
Builder,
|
|
1076
|
+
Connection,
|
|
1077
|
+
DataTable,
|
|
1078
|
+
Field,
|
|
1079
|
+
Generator,
|
|
1080
|
+
Num,
|
|
1081
|
+
One,
|
|
1082
|
+
Parameter,
|
|
1083
|
+
Query,
|
|
1084
|
+
Str,
|
|
1085
|
+
Table,
|
|
1086
|
+
Time,
|
|
1087
|
+
Type,
|
|
1088
|
+
bit,
|
|
1089
|
+
math
|
|
1090
|
+
};
|