@expressive/sql 0.3.1 → 0.4.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 +132 -113
- package/dist/index.d.ts +132 -113
- package/dist/index.js +92 -92
- package/dist/index.mjs +90 -90
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -29,11 +29,11 @@ __export(index_exports, {
|
|
|
29
29
|
Num: () => Num,
|
|
30
30
|
One: () => One,
|
|
31
31
|
Parameter: () => Parameter,
|
|
32
|
+
Primary: () => Primary,
|
|
32
33
|
Query: () => Query,
|
|
33
34
|
Str: () => Str,
|
|
34
35
|
Table: () => Table,
|
|
35
|
-
Time: () => Time
|
|
36
|
-
Type: () => Type
|
|
36
|
+
Time: () => Time
|
|
37
37
|
});
|
|
38
38
|
module.exports = __toCommonJS(index_exports);
|
|
39
39
|
|
|
@@ -50,8 +50,8 @@ function underscore(str) {
|
|
|
50
50
|
function escape(value) {
|
|
51
51
|
if (typeof value != "string")
|
|
52
52
|
return value;
|
|
53
|
-
|
|
54
|
-
return `'${
|
|
53
|
+
const escaped = 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"));
|
|
54
|
+
return `'${escaped}'`;
|
|
55
55
|
}
|
|
56
56
|
var {
|
|
57
57
|
assign,
|
|
@@ -65,31 +65,21 @@ var {
|
|
|
65
65
|
} = Object;
|
|
66
66
|
|
|
67
67
|
// src/type/Field.ts
|
|
68
|
-
var REGISTER = /* @__PURE__ */ new Map();
|
|
69
68
|
var Field = class {
|
|
70
69
|
constructor() {
|
|
71
70
|
this.type = "";
|
|
72
71
|
this.unique = false;
|
|
73
72
|
this.nullable = false;
|
|
74
73
|
this.optional = false;
|
|
74
|
+
this.absent = false;
|
|
75
75
|
this.increment = false;
|
|
76
76
|
}
|
|
77
77
|
/** Real datatype of this field in database. */
|
|
78
78
|
get datatype() {
|
|
79
79
|
return this.type;
|
|
80
80
|
}
|
|
81
|
-
static new(options) {
|
|
82
|
-
|
|
83
|
-
options = { nullable: options };
|
|
84
|
-
return Object.assign(new this(), options);
|
|
85
|
-
}
|
|
86
|
-
create(property, parent) {
|
|
87
|
-
const field = create(this);
|
|
88
|
-
field.parent = parent;
|
|
89
|
-
field.property = property;
|
|
90
|
-
if (!field.column)
|
|
91
|
-
field.column = underscore(property);
|
|
92
|
-
return field;
|
|
81
|
+
static new(...options) {
|
|
82
|
+
return Object.assign(new this(), ...options.map((x) => typeof x === "string" ? { column: x } : x === null ? { nullable: true } : x));
|
|
93
83
|
}
|
|
94
84
|
/**
|
|
95
85
|
* This method dictates behavior of this field when converted from a javascript context to SQL.
|
|
@@ -112,27 +102,6 @@ var Field = class {
|
|
|
112
102
|
defineProperty(Field, "does", {
|
|
113
103
|
value: (callback) => callback
|
|
114
104
|
});
|
|
115
|
-
function fields(from) {
|
|
116
|
-
let fields2 = REGISTER.get(from);
|
|
117
|
-
if (!fields2) {
|
|
118
|
-
fields2 = /* @__PURE__ */ new Map();
|
|
119
|
-
REGISTER.set(from, fields2);
|
|
120
|
-
const reference = new from();
|
|
121
|
-
for (const key in reference) {
|
|
122
|
-
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
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
|
-
);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return fields2;
|
|
135
|
-
}
|
|
136
105
|
|
|
137
106
|
// src/columns/Bool.ts
|
|
138
107
|
var BooleanColumn = class extends Field {
|
|
@@ -147,8 +116,9 @@ var BooleanColumn = class extends Field {
|
|
|
147
116
|
return value ? TRUE : FALSE;
|
|
148
117
|
}
|
|
149
118
|
};
|
|
150
|
-
function Bool(opts) {
|
|
151
|
-
return Bool.Type.new(opts);
|
|
119
|
+
function Bool(...opts) {
|
|
120
|
+
return Bool.Type.new(...opts);
|
|
121
|
+
;
|
|
152
122
|
}
|
|
153
123
|
Bool.Type = BooleanColumn;
|
|
154
124
|
|
|
@@ -160,8 +130,8 @@ var NumericColumn = class extends Field {
|
|
|
160
130
|
return value;
|
|
161
131
|
}
|
|
162
132
|
};
|
|
163
|
-
function Num(opts
|
|
164
|
-
return Num.Type.new(opts);
|
|
133
|
+
function Num(...opts) {
|
|
134
|
+
return Num.Type.new(...opts);
|
|
165
135
|
}
|
|
166
136
|
Num.Type = NumericColumn;
|
|
167
137
|
|
|
@@ -182,13 +152,11 @@ var ForeignColumn = class extends Field {
|
|
|
182
152
|
return table;
|
|
183
153
|
}
|
|
184
154
|
};
|
|
185
|
-
function One(entity,
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
const reference = entity.fields.get("id");
|
|
191
|
-
return One.Type.new({ entity, reference, column, ...opts });
|
|
155
|
+
function One(entity, ...options) {
|
|
156
|
+
const field = "id";
|
|
157
|
+
const column = underscore(entity.name) + "_" + field;
|
|
158
|
+
const reference = entity.fields.get(field);
|
|
159
|
+
return One.Type.new({ column, reference }, ...options, { entity });
|
|
192
160
|
}
|
|
193
161
|
One.Type = ForeignColumn;
|
|
194
162
|
|
|
@@ -202,8 +170,8 @@ var StringColumn = class extends Field {
|
|
|
202
170
|
return value;
|
|
203
171
|
}
|
|
204
172
|
};
|
|
205
|
-
function Str(opts) {
|
|
206
|
-
return Str.Type.new(opts);
|
|
173
|
+
function Str(...opts) {
|
|
174
|
+
return Str.Type.new(...opts);
|
|
207
175
|
}
|
|
208
176
|
Str.Type = StringColumn;
|
|
209
177
|
|
|
@@ -225,8 +193,8 @@ var TimeColumn = class extends Field {
|
|
|
225
193
|
throw "Value must be a Date, string, or 'NOW'.";
|
|
226
194
|
}
|
|
227
195
|
};
|
|
228
|
-
function Time(opts) {
|
|
229
|
-
return Time.Type.new(opts);
|
|
196
|
+
function Time(...opts) {
|
|
197
|
+
return Time.Type.new(...opts);
|
|
230
198
|
}
|
|
231
199
|
Time.Type = TimeColumn;
|
|
232
200
|
|
|
@@ -312,7 +280,7 @@ var Builder = class {
|
|
|
312
280
|
compare(field) {
|
|
313
281
|
const use = (op2) => (right, orEqual) => this.where(field, orEqual ? op2 + "=" : op2, right);
|
|
314
282
|
return {
|
|
315
|
-
|
|
283
|
+
equal: use("="),
|
|
316
284
|
in: use("IN"),
|
|
317
285
|
not: use("<>"),
|
|
318
286
|
over: use(">"),
|
|
@@ -344,7 +312,7 @@ var Builder = class {
|
|
|
344
312
|
return local;
|
|
345
313
|
}
|
|
346
314
|
use(type, arg2, ...rest) {
|
|
347
|
-
const { fields
|
|
315
|
+
const { fields, schema } = type;
|
|
348
316
|
const { tables } = this;
|
|
349
317
|
const proxy = {};
|
|
350
318
|
const table = {
|
|
@@ -364,7 +332,7 @@ var Builder = class {
|
|
|
364
332
|
table.name = schema + "." + table.name;
|
|
365
333
|
}
|
|
366
334
|
tables.set(proxy, table);
|
|
367
|
-
|
|
335
|
+
fields.forEach((field, key) => {
|
|
368
336
|
field = create(field);
|
|
369
337
|
field.query = this;
|
|
370
338
|
field.table = table;
|
|
@@ -449,7 +417,9 @@ var Builder = class {
|
|
|
449
417
|
for (const step of path)
|
|
450
418
|
target = target[step] || (target[step] = {});
|
|
451
419
|
const value2 = raw[column];
|
|
452
|
-
|
|
420
|
+
if (value2 === void 0)
|
|
421
|
+
return;
|
|
422
|
+
target[property] = value2 === null && field instanceof Field && field.nullable ? null : field instanceof Field ? field.get(value2) : selects instanceof Parameter ? selects.digest(value2) : value2;
|
|
453
423
|
});
|
|
454
424
|
return values2;
|
|
455
425
|
}
|
|
@@ -703,7 +673,7 @@ var Generator = class {
|
|
|
703
673
|
}
|
|
704
674
|
});
|
|
705
675
|
const mode = optional ? "LEFT" : "INNER";
|
|
706
|
-
const as = name + (alias ? ` ${alias}` : "");
|
|
676
|
+
const as = this.escape(name) + (alias ? ` ${this.escape(alias)}` : "");
|
|
707
677
|
const conds = joins.map((x) => this.filter(x.left, x.op, x.right));
|
|
708
678
|
output.push(`${mode} JOIN ${as} ON ${conds.join(" AND ")}`);
|
|
709
679
|
}
|
|
@@ -776,7 +746,9 @@ var Generator = class {
|
|
|
776
746
|
const [main] = tables.values();
|
|
777
747
|
const selection = returns instanceof Map ? (
|
|
778
748
|
// TODO: simplify this
|
|
779
|
-
Array.from(returns).
|
|
749
|
+
Array.from(returns).filter(([_alias, field]) => {
|
|
750
|
+
return field instanceof Field ? !field.absent : true;
|
|
751
|
+
}).map(([alias, field]) => this.value(field) + ` AS "${alias}"`).join(", ")
|
|
780
752
|
) : returns ? this.reference(returns) : "COUNT(*)";
|
|
781
753
|
return [
|
|
782
754
|
"SELECT",
|
|
@@ -901,17 +873,67 @@ function pretty(sql) {
|
|
|
901
873
|
}
|
|
902
874
|
Connection.None = new NoConnection();
|
|
903
875
|
|
|
904
|
-
// src/type/
|
|
905
|
-
var
|
|
876
|
+
// src/type/Primary.ts
|
|
877
|
+
var PrimaryColumn = class extends Field {
|
|
878
|
+
constructor() {
|
|
879
|
+
super(...arguments);
|
|
880
|
+
this.type = "integer";
|
|
881
|
+
this.nullable = false;
|
|
882
|
+
this.optional = true;
|
|
883
|
+
this.primary = true;
|
|
884
|
+
this.unique = true;
|
|
885
|
+
this.increment = true;
|
|
886
|
+
}
|
|
887
|
+
init(property, parent) {
|
|
888
|
+
const { tableName, tableSchema, columnName } = this;
|
|
889
|
+
if (tableName)
|
|
890
|
+
parent.table = typeof tableName == "function" ? tableName(parent) : tableName;
|
|
891
|
+
if (tableSchema)
|
|
892
|
+
parent.schema = typeof tableSchema == "function" ? tableSchema(parent) : tableSchema;
|
|
893
|
+
if (columnName)
|
|
894
|
+
parent.fields.forEach((field, property2) => {
|
|
895
|
+
field.column = columnName(property2, field, parent);
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
function Primary(...opts) {
|
|
900
|
+
return PrimaryColumn.new(...opts);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// src/type/Table.ts
|
|
904
|
+
var REGISTER = /* @__PURE__ */ new Map();
|
|
905
|
+
var _Table = class _Table {
|
|
906
906
|
constructor() {
|
|
907
907
|
/**
|
|
908
908
|
* Primary key of this entity.
|
|
909
909
|
* May be any name in the actual database, however requred to be `id` as property of this type.
|
|
910
910
|
*/
|
|
911
|
-
this.id =
|
|
911
|
+
this.id = Primary();
|
|
912
912
|
}
|
|
913
913
|
static get fields() {
|
|
914
|
-
|
|
914
|
+
let fields = REGISTER.get(this);
|
|
915
|
+
if (!fields) {
|
|
916
|
+
fields = /* @__PURE__ */ new Map();
|
|
917
|
+
defineProperty(this, "fields", { value: fields });
|
|
918
|
+
const reference = new this();
|
|
919
|
+
for (const key in reference) {
|
|
920
|
+
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
921
|
+
if (value instanceof Field) {
|
|
922
|
+
fields.set(key, value);
|
|
923
|
+
if (!value.column)
|
|
924
|
+
value.column = underscore(key);
|
|
925
|
+
} else throw new Error(
|
|
926
|
+
`Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
|
|
927
|
+
);
|
|
928
|
+
}
|
|
929
|
+
fields.forEach((field, key) => {
|
|
930
|
+
field.parent = this;
|
|
931
|
+
field.property = key;
|
|
932
|
+
if (field.init)
|
|
933
|
+
field.init(key, this);
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
return fields;
|
|
915
937
|
}
|
|
916
938
|
static get table() {
|
|
917
939
|
return this.table = underscore(this.name.replace(/\d+$/, ""));
|
|
@@ -923,7 +945,7 @@ var _Type = class _Type {
|
|
|
923
945
|
return this.name;
|
|
924
946
|
}
|
|
925
947
|
static is(obj) {
|
|
926
|
-
return typeof obj === "function" && obj.prototype instanceof
|
|
948
|
+
return typeof obj === "function" && obj.prototype instanceof _Table;
|
|
927
949
|
}
|
|
928
950
|
static insert(arg1, arg2) {
|
|
929
951
|
let data;
|
|
@@ -960,32 +982,21 @@ var _Type = class _Type {
|
|
|
960
982
|
if (typeof query == "function")
|
|
961
983
|
return query(self, where) || self;
|
|
962
984
|
if (typeof query == "number")
|
|
963
|
-
where(self.id).
|
|
985
|
+
where(self.id).equal(query);
|
|
964
986
|
else
|
|
965
987
|
where(self.id).desc();
|
|
966
988
|
return self;
|
|
967
989
|
}).one(true);
|
|
968
990
|
}
|
|
969
991
|
};
|
|
970
|
-
|
|
971
|
-
var
|
|
972
|
-
var Primary = class extends Field {
|
|
973
|
-
constructor() {
|
|
974
|
-
super(...arguments);
|
|
975
|
-
this.type = "integer";
|
|
976
|
-
this.increment = true;
|
|
977
|
-
this.optional = true;
|
|
978
|
-
this.nullable = false;
|
|
979
|
-
this.primary = true;
|
|
980
|
-
this.unique = true;
|
|
981
|
-
}
|
|
982
|
-
};
|
|
992
|
+
_Table.schema = "";
|
|
993
|
+
var Table = _Table;
|
|
983
994
|
|
|
984
995
|
// src/query/Query.ts
|
|
985
996
|
function Query(factory) {
|
|
986
997
|
let connection = void 0;
|
|
987
998
|
function where(arg1) {
|
|
988
|
-
if (
|
|
999
|
+
if (Table.is(arg1)) {
|
|
989
1000
|
if (!connection)
|
|
990
1001
|
connection = arg1.connection;
|
|
991
1002
|
else if (connection !== arg1.connection)
|
|
@@ -1064,17 +1075,6 @@ function Query(factory) {
|
|
|
1064
1075
|
}
|
|
1065
1076
|
Query.Builder = Builder;
|
|
1066
1077
|
Query.fn = { ...MathOps, bit: BitWise };
|
|
1067
|
-
|
|
1068
|
-
// src/type/Table.ts
|
|
1069
|
-
function Table(arg1, arg2) {
|
|
1070
|
-
return Field.does((parent) => {
|
|
1071
|
-
if (typeof arg1 == "string") {
|
|
1072
|
-
const [table, schema] = arg1.split(".").reverse();
|
|
1073
|
-
arg1 = { schema, ...arg2, table };
|
|
1074
|
-
}
|
|
1075
|
-
assign(parent, arg1);
|
|
1076
|
-
});
|
|
1077
|
-
}
|
|
1078
1078
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1079
1079
|
0 && (module.exports = {
|
|
1080
1080
|
Bool,
|
|
@@ -1086,9 +1086,9 @@ function Table(arg1, arg2) {
|
|
|
1086
1086
|
Num,
|
|
1087
1087
|
One,
|
|
1088
1088
|
Parameter,
|
|
1089
|
+
Primary,
|
|
1089
1090
|
Query,
|
|
1090
1091
|
Str,
|
|
1091
1092
|
Table,
|
|
1092
|
-
Time
|
|
1093
|
-
Type
|
|
1093
|
+
Time
|
|
1094
1094
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -11,8 +11,8 @@ function underscore(str) {
|
|
|
11
11
|
function escape(value) {
|
|
12
12
|
if (typeof value != "string")
|
|
13
13
|
return value;
|
|
14
|
-
|
|
15
|
-
return `'${
|
|
14
|
+
const escaped = 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 `'${escaped}'`;
|
|
16
16
|
}
|
|
17
17
|
var {
|
|
18
18
|
assign,
|
|
@@ -26,31 +26,21 @@ var {
|
|
|
26
26
|
} = Object;
|
|
27
27
|
|
|
28
28
|
// src/type/Field.ts
|
|
29
|
-
var REGISTER = /* @__PURE__ */ new Map();
|
|
30
29
|
var Field = class {
|
|
31
30
|
constructor() {
|
|
32
31
|
this.type = "";
|
|
33
32
|
this.unique = false;
|
|
34
33
|
this.nullable = false;
|
|
35
34
|
this.optional = false;
|
|
35
|
+
this.absent = false;
|
|
36
36
|
this.increment = false;
|
|
37
37
|
}
|
|
38
38
|
/** Real datatype of this field in database. */
|
|
39
39
|
get datatype() {
|
|
40
40
|
return this.type;
|
|
41
41
|
}
|
|
42
|
-
static new(options) {
|
|
43
|
-
|
|
44
|
-
options = { nullable: options };
|
|
45
|
-
return Object.assign(new this(), options);
|
|
46
|
-
}
|
|
47
|
-
create(property, parent) {
|
|
48
|
-
const field = create(this);
|
|
49
|
-
field.parent = parent;
|
|
50
|
-
field.property = property;
|
|
51
|
-
if (!field.column)
|
|
52
|
-
field.column = underscore(property);
|
|
53
|
-
return field;
|
|
42
|
+
static new(...options) {
|
|
43
|
+
return Object.assign(new this(), ...options.map((x) => typeof x === "string" ? { column: x } : x === null ? { nullable: true } : x));
|
|
54
44
|
}
|
|
55
45
|
/**
|
|
56
46
|
* This method dictates behavior of this field when converted from a javascript context to SQL.
|
|
@@ -73,27 +63,6 @@ var Field = class {
|
|
|
73
63
|
defineProperty(Field, "does", {
|
|
74
64
|
value: (callback) => callback
|
|
75
65
|
});
|
|
76
|
-
function fields(from) {
|
|
77
|
-
let fields2 = REGISTER.get(from);
|
|
78
|
-
if (!fields2) {
|
|
79
|
-
fields2 = /* @__PURE__ */ new Map();
|
|
80
|
-
REGISTER.set(from, fields2);
|
|
81
|
-
const reference = new from();
|
|
82
|
-
for (const key in reference) {
|
|
83
|
-
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
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
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return fields2;
|
|
96
|
-
}
|
|
97
66
|
|
|
98
67
|
// src/columns/Bool.ts
|
|
99
68
|
var BooleanColumn = class extends Field {
|
|
@@ -108,8 +77,9 @@ var BooleanColumn = class extends Field {
|
|
|
108
77
|
return value ? TRUE : FALSE;
|
|
109
78
|
}
|
|
110
79
|
};
|
|
111
|
-
function Bool(opts) {
|
|
112
|
-
return Bool.Type.new(opts);
|
|
80
|
+
function Bool(...opts) {
|
|
81
|
+
return Bool.Type.new(...opts);
|
|
82
|
+
;
|
|
113
83
|
}
|
|
114
84
|
Bool.Type = BooleanColumn;
|
|
115
85
|
|
|
@@ -121,8 +91,8 @@ var NumericColumn = class extends Field {
|
|
|
121
91
|
return value;
|
|
122
92
|
}
|
|
123
93
|
};
|
|
124
|
-
function Num(opts
|
|
125
|
-
return Num.Type.new(opts);
|
|
94
|
+
function Num(...opts) {
|
|
95
|
+
return Num.Type.new(...opts);
|
|
126
96
|
}
|
|
127
97
|
Num.Type = NumericColumn;
|
|
128
98
|
|
|
@@ -143,13 +113,11 @@ var ForeignColumn = class extends Field {
|
|
|
143
113
|
return table;
|
|
144
114
|
}
|
|
145
115
|
};
|
|
146
|
-
function One(entity,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
const reference = entity.fields.get("id");
|
|
152
|
-
return One.Type.new({ entity, reference, column, ...opts });
|
|
116
|
+
function One(entity, ...options) {
|
|
117
|
+
const field = "id";
|
|
118
|
+
const column = underscore(entity.name) + "_" + field;
|
|
119
|
+
const reference = entity.fields.get(field);
|
|
120
|
+
return One.Type.new({ column, reference }, ...options, { entity });
|
|
153
121
|
}
|
|
154
122
|
One.Type = ForeignColumn;
|
|
155
123
|
|
|
@@ -163,8 +131,8 @@ var StringColumn = class extends Field {
|
|
|
163
131
|
return value;
|
|
164
132
|
}
|
|
165
133
|
};
|
|
166
|
-
function Str(opts) {
|
|
167
|
-
return Str.Type.new(opts);
|
|
134
|
+
function Str(...opts) {
|
|
135
|
+
return Str.Type.new(...opts);
|
|
168
136
|
}
|
|
169
137
|
Str.Type = StringColumn;
|
|
170
138
|
|
|
@@ -186,8 +154,8 @@ var TimeColumn = class extends Field {
|
|
|
186
154
|
throw "Value must be a Date, string, or 'NOW'.";
|
|
187
155
|
}
|
|
188
156
|
};
|
|
189
|
-
function Time(opts) {
|
|
190
|
-
return Time.Type.new(opts);
|
|
157
|
+
function Time(...opts) {
|
|
158
|
+
return Time.Type.new(...opts);
|
|
191
159
|
}
|
|
192
160
|
Time.Type = TimeColumn;
|
|
193
161
|
|
|
@@ -273,7 +241,7 @@ var Builder = class {
|
|
|
273
241
|
compare(field) {
|
|
274
242
|
const use = (op2) => (right, orEqual) => this.where(field, orEqual ? op2 + "=" : op2, right);
|
|
275
243
|
return {
|
|
276
|
-
|
|
244
|
+
equal: use("="),
|
|
277
245
|
in: use("IN"),
|
|
278
246
|
not: use("<>"),
|
|
279
247
|
over: use(">"),
|
|
@@ -305,7 +273,7 @@ var Builder = class {
|
|
|
305
273
|
return local;
|
|
306
274
|
}
|
|
307
275
|
use(type, arg2, ...rest) {
|
|
308
|
-
const { fields
|
|
276
|
+
const { fields, schema } = type;
|
|
309
277
|
const { tables } = this;
|
|
310
278
|
const proxy = {};
|
|
311
279
|
const table = {
|
|
@@ -325,7 +293,7 @@ var Builder = class {
|
|
|
325
293
|
table.name = schema + "." + table.name;
|
|
326
294
|
}
|
|
327
295
|
tables.set(proxy, table);
|
|
328
|
-
|
|
296
|
+
fields.forEach((field, key) => {
|
|
329
297
|
field = create(field);
|
|
330
298
|
field.query = this;
|
|
331
299
|
field.table = table;
|
|
@@ -410,7 +378,9 @@ var Builder = class {
|
|
|
410
378
|
for (const step of path)
|
|
411
379
|
target = target[step] || (target[step] = {});
|
|
412
380
|
const value2 = raw[column];
|
|
413
|
-
|
|
381
|
+
if (value2 === void 0)
|
|
382
|
+
return;
|
|
383
|
+
target[property] = value2 === null && field instanceof Field && field.nullable ? null : field instanceof Field ? field.get(value2) : selects instanceof Parameter ? selects.digest(value2) : value2;
|
|
414
384
|
});
|
|
415
385
|
return values2;
|
|
416
386
|
}
|
|
@@ -664,7 +634,7 @@ var Generator = class {
|
|
|
664
634
|
}
|
|
665
635
|
});
|
|
666
636
|
const mode = optional ? "LEFT" : "INNER";
|
|
667
|
-
const as = name + (alias ? ` ${alias}` : "");
|
|
637
|
+
const as = this.escape(name) + (alias ? ` ${this.escape(alias)}` : "");
|
|
668
638
|
const conds = joins.map((x) => this.filter(x.left, x.op, x.right));
|
|
669
639
|
output.push(`${mode} JOIN ${as} ON ${conds.join(" AND ")}`);
|
|
670
640
|
}
|
|
@@ -737,7 +707,9 @@ var Generator = class {
|
|
|
737
707
|
const [main] = tables.values();
|
|
738
708
|
const selection = returns instanceof Map ? (
|
|
739
709
|
// TODO: simplify this
|
|
740
|
-
Array.from(returns).
|
|
710
|
+
Array.from(returns).filter(([_alias, field]) => {
|
|
711
|
+
return field instanceof Field ? !field.absent : true;
|
|
712
|
+
}).map(([alias, field]) => this.value(field) + ` AS "${alias}"`).join(", ")
|
|
741
713
|
) : returns ? this.reference(returns) : "COUNT(*)";
|
|
742
714
|
return [
|
|
743
715
|
"SELECT",
|
|
@@ -862,17 +834,67 @@ function pretty(sql) {
|
|
|
862
834
|
}
|
|
863
835
|
Connection.None = new NoConnection();
|
|
864
836
|
|
|
865
|
-
// src/type/
|
|
866
|
-
var
|
|
837
|
+
// src/type/Primary.ts
|
|
838
|
+
var PrimaryColumn = class extends Field {
|
|
839
|
+
constructor() {
|
|
840
|
+
super(...arguments);
|
|
841
|
+
this.type = "integer";
|
|
842
|
+
this.nullable = false;
|
|
843
|
+
this.optional = true;
|
|
844
|
+
this.primary = true;
|
|
845
|
+
this.unique = true;
|
|
846
|
+
this.increment = true;
|
|
847
|
+
}
|
|
848
|
+
init(property, parent) {
|
|
849
|
+
const { tableName, tableSchema, columnName } = this;
|
|
850
|
+
if (tableName)
|
|
851
|
+
parent.table = typeof tableName == "function" ? tableName(parent) : tableName;
|
|
852
|
+
if (tableSchema)
|
|
853
|
+
parent.schema = typeof tableSchema == "function" ? tableSchema(parent) : tableSchema;
|
|
854
|
+
if (columnName)
|
|
855
|
+
parent.fields.forEach((field, property2) => {
|
|
856
|
+
field.column = columnName(property2, field, parent);
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
function Primary(...opts) {
|
|
861
|
+
return PrimaryColumn.new(...opts);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// src/type/Table.ts
|
|
865
|
+
var REGISTER = /* @__PURE__ */ new Map();
|
|
866
|
+
var _Table = class _Table {
|
|
867
867
|
constructor() {
|
|
868
868
|
/**
|
|
869
869
|
* Primary key of this entity.
|
|
870
870
|
* May be any name in the actual database, however requred to be `id` as property of this type.
|
|
871
871
|
*/
|
|
872
|
-
this.id =
|
|
872
|
+
this.id = Primary();
|
|
873
873
|
}
|
|
874
874
|
static get fields() {
|
|
875
|
-
|
|
875
|
+
let fields = REGISTER.get(this);
|
|
876
|
+
if (!fields) {
|
|
877
|
+
fields = /* @__PURE__ */ new Map();
|
|
878
|
+
defineProperty(this, "fields", { value: fields });
|
|
879
|
+
const reference = new this();
|
|
880
|
+
for (const key in reference) {
|
|
881
|
+
const { value } = getOwnPropertyDescriptor(reference, key);
|
|
882
|
+
if (value instanceof Field) {
|
|
883
|
+
fields.set(key, value);
|
|
884
|
+
if (!value.column)
|
|
885
|
+
value.column = underscore(key);
|
|
886
|
+
} else throw new Error(
|
|
887
|
+
`Entities do not support normal values, only fields. Did you forget to import \`${capitalize(typeof value)}\`?`
|
|
888
|
+
);
|
|
889
|
+
}
|
|
890
|
+
fields.forEach((field, key) => {
|
|
891
|
+
field.parent = this;
|
|
892
|
+
field.property = key;
|
|
893
|
+
if (field.init)
|
|
894
|
+
field.init(key, this);
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
return fields;
|
|
876
898
|
}
|
|
877
899
|
static get table() {
|
|
878
900
|
return this.table = underscore(this.name.replace(/\d+$/, ""));
|
|
@@ -884,7 +906,7 @@ var _Type = class _Type {
|
|
|
884
906
|
return this.name;
|
|
885
907
|
}
|
|
886
908
|
static is(obj) {
|
|
887
|
-
return typeof obj === "function" && obj.prototype instanceof
|
|
909
|
+
return typeof obj === "function" && obj.prototype instanceof _Table;
|
|
888
910
|
}
|
|
889
911
|
static insert(arg1, arg2) {
|
|
890
912
|
let data;
|
|
@@ -921,32 +943,21 @@ var _Type = class _Type {
|
|
|
921
943
|
if (typeof query == "function")
|
|
922
944
|
return query(self, where) || self;
|
|
923
945
|
if (typeof query == "number")
|
|
924
|
-
where(self.id).
|
|
946
|
+
where(self.id).equal(query);
|
|
925
947
|
else
|
|
926
948
|
where(self.id).desc();
|
|
927
949
|
return self;
|
|
928
950
|
}).one(true);
|
|
929
951
|
}
|
|
930
952
|
};
|
|
931
|
-
|
|
932
|
-
var
|
|
933
|
-
var Primary = class extends Field {
|
|
934
|
-
constructor() {
|
|
935
|
-
super(...arguments);
|
|
936
|
-
this.type = "integer";
|
|
937
|
-
this.increment = true;
|
|
938
|
-
this.optional = true;
|
|
939
|
-
this.nullable = false;
|
|
940
|
-
this.primary = true;
|
|
941
|
-
this.unique = true;
|
|
942
|
-
}
|
|
943
|
-
};
|
|
953
|
+
_Table.schema = "";
|
|
954
|
+
var Table = _Table;
|
|
944
955
|
|
|
945
956
|
// src/query/Query.ts
|
|
946
957
|
function Query(factory) {
|
|
947
958
|
let connection = void 0;
|
|
948
959
|
function where(arg1) {
|
|
949
|
-
if (
|
|
960
|
+
if (Table.is(arg1)) {
|
|
950
961
|
if (!connection)
|
|
951
962
|
connection = arg1.connection;
|
|
952
963
|
else if (connection !== arg1.connection)
|
|
@@ -1025,17 +1036,6 @@ function Query(factory) {
|
|
|
1025
1036
|
}
|
|
1026
1037
|
Query.Builder = Builder;
|
|
1027
1038
|
Query.fn = { ...MathOps, bit: BitWise };
|
|
1028
|
-
|
|
1029
|
-
// src/type/Table.ts
|
|
1030
|
-
function Table(arg1, arg2) {
|
|
1031
|
-
return Field.does((parent) => {
|
|
1032
|
-
if (typeof arg1 == "string") {
|
|
1033
|
-
const [table, schema] = arg1.split(".").reverse();
|
|
1034
|
-
arg1 = { schema, ...arg2, table };
|
|
1035
|
-
}
|
|
1036
|
-
assign(parent, arg1);
|
|
1037
|
-
});
|
|
1038
|
-
}
|
|
1039
1039
|
export {
|
|
1040
1040
|
Bool,
|
|
1041
1041
|
Builder,
|
|
@@ -1046,9 +1046,9 @@ export {
|
|
|
1046
1046
|
Num,
|
|
1047
1047
|
One,
|
|
1048
1048
|
Parameter,
|
|
1049
|
+
Primary,
|
|
1049
1050
|
Query,
|
|
1050
1051
|
Str,
|
|
1051
1052
|
Table,
|
|
1052
|
-
Time
|
|
1053
|
-
Type
|
|
1053
|
+
Time
|
|
1054
1054
|
};
|