@expressive/sql 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +207 -157
- package/dist/index.d.ts +207 -157
- package/dist/index.js +72 -123
- package/dist/index.mjs +72 -123
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -67,29 +67,28 @@ var {
|
|
|
67
67
|
// src/type/Field.ts
|
|
68
68
|
var REGISTER = /* @__PURE__ */ new Map();
|
|
69
69
|
var Field = class {
|
|
70
|
-
constructor(
|
|
71
|
-
this.options = options;
|
|
70
|
+
constructor() {
|
|
72
71
|
this.type = "";
|
|
73
72
|
this.unique = false;
|
|
74
73
|
this.nullable = false;
|
|
75
74
|
this.optional = false;
|
|
76
75
|
this.increment = false;
|
|
77
76
|
}
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
/** Real datatype of this field in database. */
|
|
78
|
+
get datatype() {
|
|
79
|
+
return this.type;
|
|
80
|
+
}
|
|
81
|
+
static new(options) {
|
|
82
|
+
if (typeof options === "boolean")
|
|
83
|
+
options = { nullable: options };
|
|
84
|
+
return Object.assign(new this(), options);
|
|
85
|
+
}
|
|
86
|
+
create(property, parent) {
|
|
80
87
|
const field = create(this);
|
|
81
88
|
field.parent = parent;
|
|
82
89
|
field.property = property;
|
|
83
|
-
field.column
|
|
84
|
-
|
|
85
|
-
options = options(field) || {};
|
|
86
|
-
for (const key in options) {
|
|
87
|
-
const value = options[key];
|
|
88
|
-
if (value !== void 0)
|
|
89
|
-
field[key] = value;
|
|
90
|
-
}
|
|
91
|
-
if (!field.datatype)
|
|
92
|
-
field.datatype = field.type;
|
|
90
|
+
if (!field.column)
|
|
91
|
+
field.column = underscore(property);
|
|
93
92
|
return field;
|
|
94
93
|
}
|
|
95
94
|
/**
|
|
@@ -109,25 +108,10 @@ var Field = class {
|
|
|
109
108
|
get(value) {
|
|
110
109
|
return value;
|
|
111
110
|
}
|
|
112
|
-
raw(value) {
|
|
113
|
-
return escape(this.set(value));
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* This method is used to compare this field with another value in a query.
|
|
117
|
-
*
|
|
118
|
-
* @param acc Set of comparisons to be added to if not part of a group.
|
|
119
|
-
*/
|
|
120
|
-
where() {
|
|
121
|
-
const use = (op2) => (right, orEqual) => this.query.where(this, op2 + (orEqual ? "=" : ""), right);
|
|
122
|
-
return {
|
|
123
|
-
is: use("="),
|
|
124
|
-
in: use("IN"),
|
|
125
|
-
not: use("<>"),
|
|
126
|
-
over: use(">"),
|
|
127
|
-
under: use("<")
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
111
|
};
|
|
112
|
+
defineProperty(Field, "does", {
|
|
113
|
+
value: (callback) => callback
|
|
114
|
+
});
|
|
131
115
|
function fields(from) {
|
|
132
116
|
let fields2 = REGISTER.get(from);
|
|
133
117
|
if (!fields2) {
|
|
@@ -136,13 +120,15 @@ function fields(from) {
|
|
|
136
120
|
const reference = new from();
|
|
137
121
|
for (const key in reference) {
|
|
138
122
|
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
139
|
-
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
123
|
+
if (value instanceof Field) {
|
|
124
|
+
const instance = value.create(key, from);
|
|
125
|
+
from.fields.set(key, instance);
|
|
126
|
+
freeze(instance);
|
|
127
|
+
} else if (typeof value === "function") {
|
|
128
|
+
value(from, key);
|
|
129
|
+
} else throw new Error(
|
|
130
|
+
`Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
|
|
131
|
+
);
|
|
146
132
|
}
|
|
147
133
|
}
|
|
148
134
|
return fields2;
|
|
@@ -150,76 +136,44 @@ function fields(from) {
|
|
|
150
136
|
|
|
151
137
|
// src/columns/Bool.ts
|
|
152
138
|
var BooleanColumn = class extends Field {
|
|
153
|
-
constructor(opts = {}) {
|
|
154
|
-
let type = "tinyint";
|
|
155
|
-
let datatype = "tinyint";
|
|
156
|
-
let TRUE = 1;
|
|
157
|
-
let FALSE = 0;
|
|
158
|
-
if ("either" in opts && opts.either) {
|
|
159
|
-
[TRUE, FALSE] = opts.either;
|
|
160
|
-
type = "varchar";
|
|
161
|
-
datatype = `varchar(${Math.max(TRUE.length, FALSE.length)})`;
|
|
162
|
-
}
|
|
163
|
-
super(opts);
|
|
164
|
-
this.type = "tinyint";
|
|
165
|
-
this.TRUE = 1;
|
|
166
|
-
this.FALSE = 0;
|
|
167
|
-
this.type = type;
|
|
168
|
-
this.datatype = datatype;
|
|
169
|
-
this.TRUE = TRUE;
|
|
170
|
-
this.FALSE = FALSE;
|
|
171
|
-
}
|
|
172
139
|
get(value) {
|
|
173
|
-
|
|
140
|
+
const [TRUE] = this.either || [1];
|
|
141
|
+
return value === true || value === TRUE;
|
|
174
142
|
}
|
|
175
143
|
set(value) {
|
|
144
|
+
const [TRUE, FALSE] = this.either || [1, 0];
|
|
176
145
|
if (typeof value !== "boolean")
|
|
177
146
|
throw "Value must be a boolean.";
|
|
178
|
-
return value ?
|
|
147
|
+
return value ? TRUE : FALSE;
|
|
179
148
|
}
|
|
180
149
|
};
|
|
181
150
|
function Bool(opts) {
|
|
182
|
-
return new
|
|
151
|
+
return Bool.Type.new(opts);
|
|
183
152
|
}
|
|
184
153
|
Bool.Type = BooleanColumn;
|
|
185
154
|
|
|
186
155
|
// src/columns/Num.ts
|
|
187
156
|
var NumericColumn = class extends Field {
|
|
188
|
-
constructor() {
|
|
189
|
-
super(...arguments);
|
|
190
|
-
this.type = "int";
|
|
191
|
-
}
|
|
192
157
|
set(value) {
|
|
193
158
|
if (typeof value !== "number" || isNaN(value))
|
|
194
159
|
throw `Got '${value}' but value must be a number.`;
|
|
195
|
-
if (this.type === "int" && value !== Math.floor(value))
|
|
196
|
-
throw `Got '${value}' but datatype is integer.`;
|
|
197
160
|
return value;
|
|
198
161
|
}
|
|
199
162
|
};
|
|
200
|
-
function Num(opts) {
|
|
201
|
-
return new
|
|
163
|
+
function Num(opts = {}) {
|
|
164
|
+
return Num.Type.new(opts);
|
|
202
165
|
}
|
|
203
166
|
Num.Type = NumericColumn;
|
|
204
167
|
|
|
205
168
|
// src/columns/One.ts
|
|
206
169
|
var ForeignColumn = class extends Field {
|
|
207
|
-
constructor(
|
|
208
|
-
super(
|
|
209
|
-
column: underscore(property) + "_id"
|
|
210
|
-
}));
|
|
211
|
-
this.foreignKey = "id";
|
|
170
|
+
constructor() {
|
|
171
|
+
super(...arguments);
|
|
212
172
|
this.nullable = false;
|
|
213
|
-
this.entity = type;
|
|
214
|
-
this.type = "int";
|
|
215
|
-
this.foreignKey = "id";
|
|
216
|
-
this.foreignTable = type.table;
|
|
217
|
-
this.foreignTable = type.table;
|
|
218
|
-
this.nullable = nullable || false;
|
|
219
173
|
}
|
|
220
174
|
set(value) {
|
|
221
175
|
if (this.query?.tables.has(value))
|
|
222
|
-
return value[this.
|
|
176
|
+
return value[this.reference.column];
|
|
223
177
|
return value;
|
|
224
178
|
}
|
|
225
179
|
use(query) {
|
|
@@ -228,30 +182,18 @@ var ForeignColumn = class extends Field {
|
|
|
228
182
|
return table;
|
|
229
183
|
}
|
|
230
184
|
};
|
|
231
|
-
function One(
|
|
232
|
-
|
|
185
|
+
function One(entity, arg2) {
|
|
186
|
+
if (typeof arg2 === "boolean")
|
|
187
|
+
arg2 = { nullable: arg2 };
|
|
188
|
+
const opts = Object.assign({}, arg2);
|
|
189
|
+
const column = opts.column || underscore(entity.name) + "_id";
|
|
190
|
+
const reference = entity.fields.get("id");
|
|
191
|
+
return One.Type.new({ entity, reference, column, ...opts });
|
|
233
192
|
}
|
|
234
193
|
One.Type = ForeignColumn;
|
|
235
194
|
|
|
236
195
|
// src/columns/Str.ts
|
|
237
|
-
var VARIABLE_TYPE = /* @__PURE__ */ new Set(["char", "varchar", "text"]);
|
|
238
196
|
var StringColumn = class extends Field {
|
|
239
|
-
constructor(opts) {
|
|
240
|
-
super(({ parent, property }) => {
|
|
241
|
-
let { type = "varchar", length = 255, datatype } = opts || {};
|
|
242
|
-
if (VARIABLE_TYPE.has(type))
|
|
243
|
-
if (length)
|
|
244
|
-
datatype = `${datatype || type}(${length})`;
|
|
245
|
-
else
|
|
246
|
-
throw `Can't determine datatype! Length is not specified for ${parent.name}.${property}.`;
|
|
247
|
-
return {
|
|
248
|
-
datatype,
|
|
249
|
-
...opts
|
|
250
|
-
};
|
|
251
|
-
});
|
|
252
|
-
this.type = "varchar";
|
|
253
|
-
this.length = 255;
|
|
254
|
-
}
|
|
255
197
|
set(value) {
|
|
256
198
|
if (typeof value !== "string")
|
|
257
199
|
throw "Value must be a string.";
|
|
@@ -261,31 +203,30 @@ var StringColumn = class extends Field {
|
|
|
261
203
|
}
|
|
262
204
|
};
|
|
263
205
|
function Str(opts) {
|
|
264
|
-
return new
|
|
206
|
+
return Str.Type.new(opts);
|
|
265
207
|
}
|
|
266
208
|
Str.Type = StringColumn;
|
|
267
209
|
|
|
268
210
|
// src/columns/Time.ts
|
|
269
211
|
var TimeColumn = class extends Field {
|
|
270
|
-
constructor() {
|
|
271
|
-
super(...arguments);
|
|
272
|
-
this.type = "datetime";
|
|
273
|
-
this.fallback = void 0;
|
|
274
|
-
}
|
|
275
212
|
get(value) {
|
|
276
|
-
|
|
213
|
+
if (value instanceof Date)
|
|
214
|
+
return value;
|
|
215
|
+
return new Date(value.replace(/[-]/g, "/"));
|
|
277
216
|
}
|
|
278
217
|
set(value) {
|
|
279
218
|
if (value === "NOW")
|
|
280
219
|
return "CURRENT_TIMESTAMP";
|
|
281
220
|
else if (value instanceof Date)
|
|
282
221
|
return value.toISOString().slice(0, 19).replace("T", " ");
|
|
222
|
+
else if (typeof value === "string")
|
|
223
|
+
return value;
|
|
283
224
|
else
|
|
284
|
-
throw "Value must be a Date or 'NOW'.";
|
|
225
|
+
throw "Value must be a Date, string, or 'NOW'.";
|
|
285
226
|
}
|
|
286
227
|
};
|
|
287
|
-
function Time(
|
|
288
|
-
return new
|
|
228
|
+
function Time(opts) {
|
|
229
|
+
return Time.Type.new(opts);
|
|
289
230
|
}
|
|
290
231
|
Time.Type = TimeColumn;
|
|
291
232
|
|
|
@@ -368,9 +309,19 @@ var Builder = class {
|
|
|
368
309
|
}
|
|
369
310
|
};
|
|
370
311
|
}
|
|
312
|
+
compare(field) {
|
|
313
|
+
const use = (op2) => (right, orEqual) => this.where(field, orEqual ? op2 + "=" : op2, right);
|
|
314
|
+
return {
|
|
315
|
+
is: use("="),
|
|
316
|
+
in: use("IN"),
|
|
317
|
+
not: use("<>"),
|
|
318
|
+
over: use(">"),
|
|
319
|
+
under: use("<")
|
|
320
|
+
};
|
|
321
|
+
}
|
|
371
322
|
field(field) {
|
|
372
323
|
return {
|
|
373
|
-
...
|
|
324
|
+
...this.compare(field),
|
|
374
325
|
asc: () => {
|
|
375
326
|
this.order.set(field, "asc");
|
|
376
327
|
},
|
|
@@ -477,7 +428,7 @@ var Builder = class {
|
|
|
477
428
|
return where;
|
|
478
429
|
}
|
|
479
430
|
if (right instanceof Parameter) {
|
|
480
|
-
right.digest = left.set.bind(
|
|
431
|
+
right.digest = left.set.bind(left);
|
|
481
432
|
}
|
|
482
433
|
return this.filters.add(left, op2, right);
|
|
483
434
|
}
|
|
@@ -773,7 +724,7 @@ var Generator = class {
|
|
|
773
724
|
else
|
|
774
725
|
throw new Error(`Column ${field} is not nullable.`);
|
|
775
726
|
} else
|
|
776
|
-
value = field.
|
|
727
|
+
value = escape(field.set(value));
|
|
777
728
|
sets.push(`${this.escape(field.column)} = ${value}`);
|
|
778
729
|
}
|
|
779
730
|
if (!sets.length)
|
|
@@ -843,6 +794,7 @@ var Generator = class {
|
|
|
843
794
|
return "?";
|
|
844
795
|
}
|
|
845
796
|
filter(left, op2, right) {
|
|
797
|
+
const raw = (value) => escape(left.set(value));
|
|
846
798
|
if (right === null) {
|
|
847
799
|
right === "NULL";
|
|
848
800
|
if (op2 == "=")
|
|
@@ -852,11 +804,11 @@ var Generator = class {
|
|
|
852
804
|
else
|
|
853
805
|
throw new Error("Cannot compare NULL with " + op2);
|
|
854
806
|
} else if (right instanceof Array)
|
|
855
|
-
right = `(${right.map(
|
|
807
|
+
right = `(${right.map(raw)})`;
|
|
856
808
|
else if (right instanceof Field || right instanceof Value)
|
|
857
809
|
right = this.reference(right);
|
|
858
810
|
else
|
|
859
|
-
right =
|
|
811
|
+
right = raw(right);
|
|
860
812
|
return `${this.reference(left)} ${op2} ${right}`;
|
|
861
813
|
}
|
|
862
814
|
where() {
|
|
@@ -1020,7 +972,7 @@ var Type = _Type;
|
|
|
1020
972
|
var Primary = class extends Field {
|
|
1021
973
|
constructor() {
|
|
1022
974
|
super(...arguments);
|
|
1023
|
-
this.type = "
|
|
975
|
+
this.type = "integer";
|
|
1024
976
|
this.increment = true;
|
|
1025
977
|
this.optional = true;
|
|
1026
978
|
this.nullable = false;
|
|
@@ -1115,15 +1067,12 @@ Query.fn = { ...MathOps, bit: BitWise };
|
|
|
1115
1067
|
|
|
1116
1068
|
// src/type/Table.ts
|
|
1117
1069
|
function Table(arg1, arg2) {
|
|
1118
|
-
return
|
|
1070
|
+
return Field.does((parent) => {
|
|
1119
1071
|
if (typeof arg1 == "string") {
|
|
1120
|
-
const [
|
|
1121
|
-
arg1 = { schema, ...arg2,
|
|
1072
|
+
const [table, schema] = arg1.split(".").reverse();
|
|
1073
|
+
arg1 = { schema, ...arg2, table };
|
|
1122
1074
|
}
|
|
1123
|
-
|
|
1124
|
-
if (name)
|
|
1125
|
-
parent.table = name;
|
|
1126
|
-
assign(parent, rest);
|
|
1075
|
+
assign(parent, arg1);
|
|
1127
1076
|
});
|
|
1128
1077
|
}
|
|
1129
1078
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -28,29 +28,28 @@ var {
|
|
|
28
28
|
// src/type/Field.ts
|
|
29
29
|
var REGISTER = /* @__PURE__ */ new Map();
|
|
30
30
|
var Field = class {
|
|
31
|
-
constructor(
|
|
32
|
-
this.options = options;
|
|
31
|
+
constructor() {
|
|
33
32
|
this.type = "";
|
|
34
33
|
this.unique = false;
|
|
35
34
|
this.nullable = false;
|
|
36
35
|
this.optional = false;
|
|
37
36
|
this.increment = false;
|
|
38
37
|
}
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
/** Real datatype of this field in database. */
|
|
39
|
+
get datatype() {
|
|
40
|
+
return this.type;
|
|
41
|
+
}
|
|
42
|
+
static new(options) {
|
|
43
|
+
if (typeof options === "boolean")
|
|
44
|
+
options = { nullable: options };
|
|
45
|
+
return Object.assign(new this(), options);
|
|
46
|
+
}
|
|
47
|
+
create(property, parent) {
|
|
41
48
|
const field = create(this);
|
|
42
49
|
field.parent = parent;
|
|
43
50
|
field.property = property;
|
|
44
|
-
field.column
|
|
45
|
-
|
|
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;
|
|
51
|
+
if (!field.column)
|
|
52
|
+
field.column = underscore(property);
|
|
54
53
|
return field;
|
|
55
54
|
}
|
|
56
55
|
/**
|
|
@@ -70,25 +69,10 @@ var Field = class {
|
|
|
70
69
|
get(value) {
|
|
71
70
|
return value;
|
|
72
71
|
}
|
|
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
72
|
};
|
|
73
|
+
defineProperty(Field, "does", {
|
|
74
|
+
value: (callback) => callback
|
|
75
|
+
});
|
|
92
76
|
function fields(from) {
|
|
93
77
|
let fields2 = REGISTER.get(from);
|
|
94
78
|
if (!fields2) {
|
|
@@ -97,13 +81,15 @@ function fields(from) {
|
|
|
97
81
|
const reference = new from();
|
|
98
82
|
for (const key in reference) {
|
|
99
83
|
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
84
|
+
if (value instanceof Field) {
|
|
85
|
+
const instance = value.create(key, from);
|
|
86
|
+
from.fields.set(key, instance);
|
|
87
|
+
freeze(instance);
|
|
88
|
+
} else if (typeof value === "function") {
|
|
89
|
+
value(from, key);
|
|
90
|
+
} else throw new Error(
|
|
91
|
+
`Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
|
|
92
|
+
);
|
|
107
93
|
}
|
|
108
94
|
}
|
|
109
95
|
return fields2;
|
|
@@ -111,76 +97,44 @@ function fields(from) {
|
|
|
111
97
|
|
|
112
98
|
// src/columns/Bool.ts
|
|
113
99
|
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
100
|
get(value) {
|
|
134
|
-
|
|
101
|
+
const [TRUE] = this.either || [1];
|
|
102
|
+
return value === true || value === TRUE;
|
|
135
103
|
}
|
|
136
104
|
set(value) {
|
|
105
|
+
const [TRUE, FALSE] = this.either || [1, 0];
|
|
137
106
|
if (typeof value !== "boolean")
|
|
138
107
|
throw "Value must be a boolean.";
|
|
139
|
-
return value ?
|
|
108
|
+
return value ? TRUE : FALSE;
|
|
140
109
|
}
|
|
141
110
|
};
|
|
142
111
|
function Bool(opts) {
|
|
143
|
-
return new
|
|
112
|
+
return Bool.Type.new(opts);
|
|
144
113
|
}
|
|
145
114
|
Bool.Type = BooleanColumn;
|
|
146
115
|
|
|
147
116
|
// src/columns/Num.ts
|
|
148
117
|
var NumericColumn = class extends Field {
|
|
149
|
-
constructor() {
|
|
150
|
-
super(...arguments);
|
|
151
|
-
this.type = "int";
|
|
152
|
-
}
|
|
153
118
|
set(value) {
|
|
154
119
|
if (typeof value !== "number" || isNaN(value))
|
|
155
120
|
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
121
|
return value;
|
|
159
122
|
}
|
|
160
123
|
};
|
|
161
|
-
function Num(opts) {
|
|
162
|
-
return new
|
|
124
|
+
function Num(opts = {}) {
|
|
125
|
+
return Num.Type.new(opts);
|
|
163
126
|
}
|
|
164
127
|
Num.Type = NumericColumn;
|
|
165
128
|
|
|
166
129
|
// src/columns/One.ts
|
|
167
130
|
var ForeignColumn = class extends Field {
|
|
168
|
-
constructor(
|
|
169
|
-
super(
|
|
170
|
-
column: underscore(property) + "_id"
|
|
171
|
-
}));
|
|
172
|
-
this.foreignKey = "id";
|
|
131
|
+
constructor() {
|
|
132
|
+
super(...arguments);
|
|
173
133
|
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
134
|
}
|
|
181
135
|
set(value) {
|
|
182
136
|
if (this.query?.tables.has(value))
|
|
183
|
-
return value[this.
|
|
137
|
+
return value[this.reference.column];
|
|
184
138
|
return value;
|
|
185
139
|
}
|
|
186
140
|
use(query) {
|
|
@@ -189,30 +143,18 @@ var ForeignColumn = class extends Field {
|
|
|
189
143
|
return table;
|
|
190
144
|
}
|
|
191
145
|
};
|
|
192
|
-
function One(
|
|
193
|
-
|
|
146
|
+
function One(entity, arg2) {
|
|
147
|
+
if (typeof arg2 === "boolean")
|
|
148
|
+
arg2 = { nullable: arg2 };
|
|
149
|
+
const opts = Object.assign({}, arg2);
|
|
150
|
+
const column = opts.column || underscore(entity.name) + "_id";
|
|
151
|
+
const reference = entity.fields.get("id");
|
|
152
|
+
return One.Type.new({ entity, reference, column, ...opts });
|
|
194
153
|
}
|
|
195
154
|
One.Type = ForeignColumn;
|
|
196
155
|
|
|
197
156
|
// src/columns/Str.ts
|
|
198
|
-
var VARIABLE_TYPE = /* @__PURE__ */ new Set(["char", "varchar", "text"]);
|
|
199
157
|
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
158
|
set(value) {
|
|
217
159
|
if (typeof value !== "string")
|
|
218
160
|
throw "Value must be a string.";
|
|
@@ -222,31 +164,30 @@ var StringColumn = class extends Field {
|
|
|
222
164
|
}
|
|
223
165
|
};
|
|
224
166
|
function Str(opts) {
|
|
225
|
-
return new
|
|
167
|
+
return Str.Type.new(opts);
|
|
226
168
|
}
|
|
227
169
|
Str.Type = StringColumn;
|
|
228
170
|
|
|
229
171
|
// src/columns/Time.ts
|
|
230
172
|
var TimeColumn = class extends Field {
|
|
231
|
-
constructor() {
|
|
232
|
-
super(...arguments);
|
|
233
|
-
this.type = "datetime";
|
|
234
|
-
this.fallback = void 0;
|
|
235
|
-
}
|
|
236
173
|
get(value) {
|
|
237
|
-
|
|
174
|
+
if (value instanceof Date)
|
|
175
|
+
return value;
|
|
176
|
+
return new Date(value.replace(/[-]/g, "/"));
|
|
238
177
|
}
|
|
239
178
|
set(value) {
|
|
240
179
|
if (value === "NOW")
|
|
241
180
|
return "CURRENT_TIMESTAMP";
|
|
242
181
|
else if (value instanceof Date)
|
|
243
182
|
return value.toISOString().slice(0, 19).replace("T", " ");
|
|
183
|
+
else if (typeof value === "string")
|
|
184
|
+
return value;
|
|
244
185
|
else
|
|
245
|
-
throw "Value must be a Date or 'NOW'.";
|
|
186
|
+
throw "Value must be a Date, string, or 'NOW'.";
|
|
246
187
|
}
|
|
247
188
|
};
|
|
248
|
-
function Time(
|
|
249
|
-
return new
|
|
189
|
+
function Time(opts) {
|
|
190
|
+
return Time.Type.new(opts);
|
|
250
191
|
}
|
|
251
192
|
Time.Type = TimeColumn;
|
|
252
193
|
|
|
@@ -329,9 +270,19 @@ var Builder = class {
|
|
|
329
270
|
}
|
|
330
271
|
};
|
|
331
272
|
}
|
|
273
|
+
compare(field) {
|
|
274
|
+
const use = (op2) => (right, orEqual) => this.where(field, orEqual ? op2 + "=" : op2, right);
|
|
275
|
+
return {
|
|
276
|
+
is: use("="),
|
|
277
|
+
in: use("IN"),
|
|
278
|
+
not: use("<>"),
|
|
279
|
+
over: use(">"),
|
|
280
|
+
under: use("<")
|
|
281
|
+
};
|
|
282
|
+
}
|
|
332
283
|
field(field) {
|
|
333
284
|
return {
|
|
334
|
-
...
|
|
285
|
+
...this.compare(field),
|
|
335
286
|
asc: () => {
|
|
336
287
|
this.order.set(field, "asc");
|
|
337
288
|
},
|
|
@@ -438,7 +389,7 @@ var Builder = class {
|
|
|
438
389
|
return where;
|
|
439
390
|
}
|
|
440
391
|
if (right instanceof Parameter) {
|
|
441
|
-
right.digest = left.set.bind(
|
|
392
|
+
right.digest = left.set.bind(left);
|
|
442
393
|
}
|
|
443
394
|
return this.filters.add(left, op2, right);
|
|
444
395
|
}
|
|
@@ -734,7 +685,7 @@ var Generator = class {
|
|
|
734
685
|
else
|
|
735
686
|
throw new Error(`Column ${field} is not nullable.`);
|
|
736
687
|
} else
|
|
737
|
-
value = field.
|
|
688
|
+
value = escape(field.set(value));
|
|
738
689
|
sets.push(`${this.escape(field.column)} = ${value}`);
|
|
739
690
|
}
|
|
740
691
|
if (!sets.length)
|
|
@@ -804,6 +755,7 @@ var Generator = class {
|
|
|
804
755
|
return "?";
|
|
805
756
|
}
|
|
806
757
|
filter(left, op2, right) {
|
|
758
|
+
const raw = (value) => escape(left.set(value));
|
|
807
759
|
if (right === null) {
|
|
808
760
|
right === "NULL";
|
|
809
761
|
if (op2 == "=")
|
|
@@ -813,11 +765,11 @@ var Generator = class {
|
|
|
813
765
|
else
|
|
814
766
|
throw new Error("Cannot compare NULL with " + op2);
|
|
815
767
|
} else if (right instanceof Array)
|
|
816
|
-
right = `(${right.map(
|
|
768
|
+
right = `(${right.map(raw)})`;
|
|
817
769
|
else if (right instanceof Field || right instanceof Value)
|
|
818
770
|
right = this.reference(right);
|
|
819
771
|
else
|
|
820
|
-
right =
|
|
772
|
+
right = raw(right);
|
|
821
773
|
return `${this.reference(left)} ${op2} ${right}`;
|
|
822
774
|
}
|
|
823
775
|
where() {
|
|
@@ -981,7 +933,7 @@ var Type = _Type;
|
|
|
981
933
|
var Primary = class extends Field {
|
|
982
934
|
constructor() {
|
|
983
935
|
super(...arguments);
|
|
984
|
-
this.type = "
|
|
936
|
+
this.type = "integer";
|
|
985
937
|
this.increment = true;
|
|
986
938
|
this.optional = true;
|
|
987
939
|
this.nullable = false;
|
|
@@ -1076,15 +1028,12 @@ Query.fn = { ...MathOps, bit: BitWise };
|
|
|
1076
1028
|
|
|
1077
1029
|
// src/type/Table.ts
|
|
1078
1030
|
function Table(arg1, arg2) {
|
|
1079
|
-
return
|
|
1031
|
+
return Field.does((parent) => {
|
|
1080
1032
|
if (typeof arg1 == "string") {
|
|
1081
|
-
const [
|
|
1082
|
-
arg1 = { schema, ...arg2,
|
|
1033
|
+
const [table, schema] = arg1.split(".").reverse();
|
|
1034
|
+
arg1 = { schema, ...arg2, table };
|
|
1083
1035
|
}
|
|
1084
|
-
|
|
1085
|
-
if (name)
|
|
1086
|
-
parent.table = name;
|
|
1087
|
-
assign(parent, rest);
|
|
1036
|
+
assign(parent, arg1);
|
|
1088
1037
|
});
|
|
1089
1038
|
}
|
|
1090
1039
|
export {
|