remult 0.14.6 → 0.14.7
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/assign.js +5 -1
- package/classType.js +2 -1
- package/graphql.js +146 -84
- package/index.js +73 -25
- package/inputTypes.js +16 -9
- package/package.json +1 -2
- package/postgres/index.js +4 -1
- package/postgres/postgres-data-provider.d.ts +4 -1
- package/postgres/postgres-data-provider.js +364 -165
- package/postgres/postgresColumnSyntax.d.ts +1 -1
- package/postgres/postgresColumnSyntax.js +8 -4
- package/remult-express.js +10 -6
- package/remult-knex/index.d.ts +5 -2
- package/remult-knex/index.js +720 -300
- package/remult-middleware.js +29 -24
- package/remult-mongo.js +406 -172
- package/server/JsonEntityFileStorage.js +15 -10
- package/server/expressBridge.d.ts +4 -2
- package/server/expressBridge.js +570 -383
- package/server/index.js +11 -4
- package/server/register-actions-on-server.js +38 -12
- package/server/register-entities-on-server.js +13 -9
- package/src/__EntityValueProvider.js +2 -1
- package/src/__RowsOfDataForTesting.js +2 -1
- package/src/backend-tests/backend-database-test-setup.backend-spec.js +190 -72
- package/src/backend-tests/my-test.backend-spec.js +114 -45
- package/src/column-interfaces.js +6 -2
- package/src/column.js +113 -71
- package/src/context.js +330 -174
- package/src/data-api.js +326 -162
- package/src/data-interfaces.js +11 -7
- package/src/data-providers/array-entity-data-provider.js +286 -171
- package/src/data-providers/in-memory-database.js +27 -12
- package/src/data-providers/json-data-provider.js +77 -41
- package/src/data-providers/rest-data-provider.js +189 -102
- package/src/data-providers/sql-database.js +403 -223
- package/src/data-providers/web-sql-data-provider.js +139 -78
- package/src/entity.js +2 -1
- package/src/filter/filter-consumer-bridge-to-sql-request.d.ts +1 -1
- package/src/filter/filter-consumer-bridge-to-sql-request.js +248 -106
- package/src/filter/filter-consumer-bridge-to-url-builder.js +2 -1
- package/src/filter/filter-interfaces.js +440 -277
- package/src/id-entity.js +17 -8
- package/src/remult3/RepositoryImplementation.js +2367 -1191
- package/src/remult3/index.js +5 -2
- package/src/remult3/remult3.js +2 -1
- package/src/server-action.js +528 -294
- package/src/shared-tests/db-tests-setup.js +153 -49
- package/src/shared-tests/db-tests.db-tests.js +1566 -660
- package/src/shared-tests/deleteAll.js +20 -3
- package/src/shared-tests/entityWithValidations.js +79 -52
- package/src/shared-tests/index.js +6 -4
- package/src/shared-tests/test-custom-id-column.spec.js +85 -37
- package/src/shared-tests/test-special-value.spec.js +104 -64
- package/src/shared-tests/test-update-with-null.spec.js +49 -28
- package/src/sort.js +95 -38
- package/src/sql-command.js +2 -1
- package/src/tests/Done.js +13 -7
- package/src/tests/TestDataApiResponse.js +22 -15
- package/src/tests/c.js +27 -21
- package/src/tests/createData.js +48 -20
- package/src/tests/d.js +27 -19
- package/src/tests/dWithPrefilter.js +44 -31
- package/src/tests/entityForCustomFilter.js +66 -42
- package/src/tests/entityWithValidationsOnColumn.js +29 -21
- package/src/tests/frontend-database-tests-setup.spec.js +98 -35
- package/src/tests/h.js +44 -35
- package/src/tests/remult-3-entities.js +80 -71
- package/src/tests/tasks.js +29 -23
- package/src/tests/testHelper.spec.js +322 -170
- package/src/tests/testModel/models.js +33 -25
- package/src/validators.js +59 -32
- package/urlBuilder.js +14 -8
- package/valueConverters.js +160 -161
package/remult-knex/index.js
CHANGED
|
@@ -1,334 +1,740 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createKnexDataProvider = exports.buildColumn = exports.KnexSchemaBuilder = exports.FilterConsumerBridgeToKnexRequest = exports.KnexDataProvider = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var knex_1 = require("knex");
|
|
6
|
+
var filter_interfaces_1 = require("../src/filter/filter-interfaces");
|
|
7
|
+
var filter_consumer_bridge_to_sql_request_1 = require("../src/filter/filter-consumer-bridge-to-sql-request");
|
|
8
|
+
var context_1 = require("../src/context");
|
|
9
|
+
var remult3_1 = require("../src/remult3");
|
|
10
|
+
var valueConverters_1 = require("../valueConverters");
|
|
11
|
+
var column_1 = require("../src/column");
|
|
12
|
+
var KnexDataProvider = /** @class */ (function () {
|
|
13
|
+
function KnexDataProvider(knex) {
|
|
10
14
|
this.knex = knex;
|
|
11
15
|
}
|
|
12
|
-
getEntityDataProvider(entity) {
|
|
16
|
+
KnexDataProvider.prototype.getEntityDataProvider = function (entity) {
|
|
13
17
|
return new KnexEntityDataProvider(entity, this.knex);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
};
|
|
19
|
+
KnexDataProvider.prototype.transaction = function (action) {
|
|
20
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
21
|
+
var t, err_1;
|
|
22
|
+
return tslib_1.__generator(this, function (_a) {
|
|
23
|
+
switch (_a.label) {
|
|
24
|
+
case 0: return [4 /*yield*/, this.knex.transaction()];
|
|
25
|
+
case 1:
|
|
26
|
+
t = _a.sent();
|
|
27
|
+
_a.label = 2;
|
|
28
|
+
case 2:
|
|
29
|
+
_a.trys.push([2, 5, , 7]);
|
|
30
|
+
return [4 /*yield*/, action(new KnexDataProvider(t))];
|
|
31
|
+
case 3:
|
|
32
|
+
_a.sent();
|
|
33
|
+
return [4 /*yield*/, t.commit()];
|
|
34
|
+
case 4:
|
|
35
|
+
_a.sent();
|
|
36
|
+
return [3 /*break*/, 7];
|
|
37
|
+
case 5:
|
|
38
|
+
err_1 = _a.sent();
|
|
39
|
+
return [4 /*yield*/, t.rollback()];
|
|
40
|
+
case 6:
|
|
41
|
+
_a.sent();
|
|
42
|
+
throw err_1;
|
|
43
|
+
case 7: return [2 /*return*/];
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
KnexDataProvider.customFilter = function (build) {
|
|
49
|
+
var _a;
|
|
50
|
+
return _a = {},
|
|
51
|
+
_a[filter_interfaces_1.customDatabaseFilterToken] = {
|
|
29
52
|
buildKnex: build
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
53
|
+
},
|
|
54
|
+
_a;
|
|
55
|
+
};
|
|
56
|
+
return KnexDataProvider;
|
|
57
|
+
}());
|
|
58
|
+
exports.KnexDataProvider = KnexDataProvider;
|
|
59
|
+
var KnexEntityDataProvider = /** @class */ (function () {
|
|
60
|
+
function KnexEntityDataProvider(entity, knex) {
|
|
36
61
|
this.entity = entity;
|
|
37
62
|
this.knex = knex;
|
|
38
63
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
let query = this.knex(e.entityName).select(cols);
|
|
67
|
-
if (options?.where) {
|
|
68
|
-
const br = new FilterConsumerBridgeToKnexRequest(e);
|
|
69
|
-
options.where.__applyToConsumer(br);
|
|
70
|
-
let r = await br.resolveWhere();
|
|
71
|
-
query.where(b => r.forEach(y => y(b)));
|
|
72
|
-
}
|
|
73
|
-
if (options.orderBy) {
|
|
74
|
-
query = query.orderBy(options.orderBy.Segments.map(s => ({
|
|
75
|
-
column: e.nameOf(s.field),
|
|
76
|
-
order: s.isDescending ? "desc" : "asc"
|
|
77
|
-
})));
|
|
78
|
-
}
|
|
79
|
-
if (options.limit) {
|
|
80
|
-
query = query.limit(options.limit);
|
|
81
|
-
if (options.page)
|
|
82
|
-
query = query.offset((options.page - 1) * options.limit);
|
|
83
|
-
}
|
|
84
|
-
const r = await query;
|
|
85
|
-
return r.map(y => {
|
|
86
|
-
let result = {};
|
|
87
|
-
let i = 0;
|
|
88
|
-
for (let m in y) {
|
|
89
|
-
let field = colKeys[i++];
|
|
90
|
-
try {
|
|
91
|
-
result[field.key] = field.valueConverter.fromDb(y[m]);
|
|
64
|
+
KnexEntityDataProvider.prototype.count = function (where) {
|
|
65
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var e, br, r, result, row, key, element;
|
|
67
|
+
return tslib_1.__generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0: return [4 /*yield*/, this.init()];
|
|
70
|
+
case 1:
|
|
71
|
+
e = _a.sent();
|
|
72
|
+
br = new FilterConsumerBridgeToKnexRequest(e);
|
|
73
|
+
where.__applyToConsumer(br);
|
|
74
|
+
return [4 /*yield*/, br.resolveWhere()];
|
|
75
|
+
case 2:
|
|
76
|
+
r = _a.sent();
|
|
77
|
+
return [4 /*yield*/, this.knex(e.entityName)
|
|
78
|
+
.count().where(function (b) { return r.forEach(function (w) { return w(b); }); })];
|
|
79
|
+
case 3:
|
|
80
|
+
result = (_a.sent());
|
|
81
|
+
row = result[0];
|
|
82
|
+
for (key in row) {
|
|
83
|
+
if (Object.prototype.hasOwnProperty.call(row, key)) {
|
|
84
|
+
element = row[key];
|
|
85
|
+
return [2 /*return*/, +element];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return [2 /*return*/];
|
|
92
89
|
}
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
KnexEntityDataProvider.prototype.find = function (options) {
|
|
94
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
95
|
+
var e, cols, colKeys, _a, _b, x, query, br, r_1, r;
|
|
96
|
+
var e_1, _c;
|
|
97
|
+
return tslib_1.__generator(this, function (_d) {
|
|
98
|
+
switch (_d.label) {
|
|
99
|
+
case 0: return [4 /*yield*/, this.init()];
|
|
100
|
+
case 1:
|
|
101
|
+
e = _d.sent();
|
|
102
|
+
cols = [];
|
|
103
|
+
colKeys = [];
|
|
104
|
+
try {
|
|
105
|
+
for (_a = tslib_1.__values(this.entity.fields), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
106
|
+
x = _b.value;
|
|
107
|
+
if (x.isServerExpression) {
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
cols.push(e.nameOf(x));
|
|
111
|
+
colKeys.push(x);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
116
|
+
finally {
|
|
117
|
+
try {
|
|
118
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
119
|
+
}
|
|
120
|
+
finally { if (e_1) throw e_1.error; }
|
|
121
|
+
}
|
|
122
|
+
query = this.knex(e.entityName).select(cols);
|
|
123
|
+
if (!(options === null || options === void 0 ? void 0 : options.where)) return [3 /*break*/, 3];
|
|
124
|
+
br = new FilterConsumerBridgeToKnexRequest(e);
|
|
125
|
+
options.where.__applyToConsumer(br);
|
|
126
|
+
return [4 /*yield*/, br.resolveWhere()];
|
|
127
|
+
case 2:
|
|
128
|
+
r_1 = _d.sent();
|
|
129
|
+
query.where(function (b) { return r_1.forEach(function (y) { return y(b); }); });
|
|
130
|
+
_d.label = 3;
|
|
131
|
+
case 3:
|
|
132
|
+
if (options.orderBy) {
|
|
133
|
+
query = query.orderBy(options.orderBy.Segments.map(function (s) { return ({
|
|
134
|
+
column: e.nameOf(s.field),
|
|
135
|
+
order: s.isDescending ? "desc" : "asc"
|
|
136
|
+
}); }));
|
|
137
|
+
}
|
|
138
|
+
if (options.limit) {
|
|
139
|
+
query = query.limit(options.limit);
|
|
140
|
+
if (options.page)
|
|
141
|
+
query = query.offset((options.page - 1) * options.limit);
|
|
142
|
+
}
|
|
143
|
+
return [4 /*yield*/, query];
|
|
144
|
+
case 4:
|
|
145
|
+
r = _d.sent();
|
|
146
|
+
return [2 /*return*/, r.map(function (y) {
|
|
147
|
+
var result = {};
|
|
148
|
+
var i = 0;
|
|
149
|
+
for (var m in y) {
|
|
150
|
+
var field = colKeys[i++];
|
|
151
|
+
try {
|
|
152
|
+
result[field.key] = field.valueConverter.fromDb(y[m]);
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
throw new Error("Failed to load from db:" + field.key + "\r\n" + err);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
})];
|
|
95
160
|
}
|
|
96
|
-
}
|
|
97
|
-
return result;
|
|
161
|
+
});
|
|
98
162
|
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(id)).__applyToConsumer(f);
|
|
107
|
-
let resultFilter = this.entity.idMetadata.getIdFilter(id);
|
|
108
|
-
if (data.id != undefined)
|
|
109
|
-
resultFilter = this.entity.idMetadata.getIdFilter(data.id);
|
|
110
|
-
let updateObject = {};
|
|
111
|
-
for (const x of this.entity.fields) {
|
|
112
|
-
if (e.isDbReadonly(x)) { }
|
|
113
|
-
else if (data[x.key] !== undefined) {
|
|
114
|
-
let v = x.valueConverter.toDb(data[x.key]);
|
|
115
|
-
if (v !== undefined) {
|
|
116
|
-
let key = await e.nameOf(x);
|
|
117
|
-
updateObject[key] = v;
|
|
163
|
+
};
|
|
164
|
+
KnexEntityDataProvider.prototype.init = function () {
|
|
165
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
166
|
+
return tslib_1.__generator(this, function (_a) {
|
|
167
|
+
switch (_a.label) {
|
|
168
|
+
case 0: return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(this.entity)];
|
|
169
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
118
170
|
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
KnexEntityDataProvider.prototype.update = function (id, data) {
|
|
175
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
176
|
+
var e, f, resultFilter, updateObject, _a, _b, x, v, key, e_2_1, where;
|
|
177
|
+
var e_2, _c;
|
|
178
|
+
return tslib_1.__generator(this, function (_d) {
|
|
179
|
+
switch (_d.label) {
|
|
180
|
+
case 0: return [4 /*yield*/, this.init()];
|
|
181
|
+
case 1:
|
|
182
|
+
e = _d.sent();
|
|
183
|
+
f = new FilterConsumerBridgeToKnexRequest(e);
|
|
184
|
+
filter_interfaces_1.Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(id)).__applyToConsumer(f);
|
|
185
|
+
resultFilter = this.entity.idMetadata.getIdFilter(id);
|
|
186
|
+
if (data.id != undefined)
|
|
187
|
+
resultFilter = this.entity.idMetadata.getIdFilter(data.id);
|
|
188
|
+
updateObject = {};
|
|
189
|
+
_d.label = 2;
|
|
190
|
+
case 2:
|
|
191
|
+
_d.trys.push([2, 8, 9, 10]);
|
|
192
|
+
_a = tslib_1.__values(this.entity.fields), _b = _a.next();
|
|
193
|
+
_d.label = 3;
|
|
194
|
+
case 3:
|
|
195
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
196
|
+
x = _b.value;
|
|
197
|
+
if (!e.isDbReadonly(x)) return [3 /*break*/, 4];
|
|
198
|
+
return [3 /*break*/, 6];
|
|
199
|
+
case 4:
|
|
200
|
+
if (!(data[x.key] !== undefined)) return [3 /*break*/, 6];
|
|
201
|
+
v = x.valueConverter.toDb(data[x.key]);
|
|
202
|
+
if (!(v !== undefined)) return [3 /*break*/, 6];
|
|
203
|
+
return [4 /*yield*/, e.nameOf(x)];
|
|
204
|
+
case 5:
|
|
205
|
+
key = _d.sent();
|
|
206
|
+
updateObject[key] = v;
|
|
207
|
+
_d.label = 6;
|
|
208
|
+
case 6:
|
|
209
|
+
_b = _a.next();
|
|
210
|
+
return [3 /*break*/, 3];
|
|
211
|
+
case 7: return [3 /*break*/, 10];
|
|
212
|
+
case 8:
|
|
213
|
+
e_2_1 = _d.sent();
|
|
214
|
+
e_2 = { error: e_2_1 };
|
|
215
|
+
return [3 /*break*/, 10];
|
|
216
|
+
case 9:
|
|
217
|
+
try {
|
|
218
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
219
|
+
}
|
|
220
|
+
finally { if (e_2) throw e_2.error; }
|
|
221
|
+
return [7 /*endfinally*/];
|
|
222
|
+
case 10: return [4 /*yield*/, f.resolveWhere()];
|
|
223
|
+
case 11:
|
|
224
|
+
where = _d.sent();
|
|
225
|
+
return [4 /*yield*/, this.knex(e.entityName).update(updateObject).where(function (b) { return where.forEach(function (w) { return w(b); }); })];
|
|
226
|
+
case 12:
|
|
227
|
+
_d.sent();
|
|
228
|
+
return [2 /*return*/, this.find({ where: filter_interfaces_1.Filter.fromEntityFilter(this.entity, resultFilter) }).then(function (y) { return y[0]; })];
|
|
147
229
|
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
let insert = this.knex(e.entityName).insert(insertObject);
|
|
151
|
-
if (isAutoIncrement(this.entity.idMetadata.field)) {
|
|
152
|
-
let result = await insert.returning(this.entity.idMetadata.field.key);
|
|
153
|
-
let newId = result[0].id;
|
|
154
|
-
resultFilter = new Filter(x => x.isEqualTo(this.entity.idMetadata.field, newId));
|
|
155
|
-
}
|
|
156
|
-
else
|
|
157
|
-
await insert;
|
|
158
|
-
return this.find({ where: resultFilter }).then(y => {
|
|
159
|
-
return y[0];
|
|
230
|
+
});
|
|
160
231
|
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
232
|
+
};
|
|
233
|
+
KnexEntityDataProvider.prototype.delete = function (id) {
|
|
234
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
235
|
+
var e, f, where;
|
|
236
|
+
return tslib_1.__generator(this, function (_a) {
|
|
237
|
+
switch (_a.label) {
|
|
238
|
+
case 0: return [4 /*yield*/, this.init()];
|
|
239
|
+
case 1:
|
|
240
|
+
e = _a.sent();
|
|
241
|
+
f = new FilterConsumerBridgeToKnexRequest(e);
|
|
242
|
+
filter_interfaces_1.Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(id)).__applyToConsumer(f);
|
|
243
|
+
return [4 /*yield*/, f.resolveWhere()];
|
|
244
|
+
case 2:
|
|
245
|
+
where = _a.sent();
|
|
246
|
+
return [4 /*yield*/, this.knex(e.entityName).delete().where(function (b) { return where.forEach(function (w) { return w(b); }); })];
|
|
247
|
+
case 3:
|
|
248
|
+
_a.sent();
|
|
249
|
+
return [2 /*return*/];
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
KnexEntityDataProvider.prototype.insert = function (data) {
|
|
255
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
256
|
+
var e, resultFilter, insertObject, _a, _b, x, v, key, e_3_1, insert, result, newId_1;
|
|
257
|
+
var e_3, _c;
|
|
258
|
+
var _this = this;
|
|
259
|
+
return tslib_1.__generator(this, function (_d) {
|
|
260
|
+
switch (_d.label) {
|
|
261
|
+
case 0: return [4 /*yield*/, this.init()];
|
|
262
|
+
case 1:
|
|
263
|
+
e = _d.sent();
|
|
264
|
+
if (this.entity.idMetadata.field instanceof column_1.CompoundIdField)
|
|
265
|
+
resultFilter = this.entity.idMetadata.field.resultIdFilter(undefined, data);
|
|
266
|
+
else
|
|
267
|
+
resultFilter = filter_interfaces_1.Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(data[this.entity.idMetadata.field.key]));
|
|
268
|
+
insertObject = {};
|
|
269
|
+
_d.label = 2;
|
|
270
|
+
case 2:
|
|
271
|
+
_d.trys.push([2, 8, 9, 10]);
|
|
272
|
+
_a = tslib_1.__values(this.entity.fields), _b = _a.next();
|
|
273
|
+
_d.label = 3;
|
|
274
|
+
case 3:
|
|
275
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
276
|
+
x = _b.value;
|
|
277
|
+
if (!e.isDbReadonly(x)) return [3 /*break*/, 4];
|
|
278
|
+
return [3 /*break*/, 6];
|
|
279
|
+
case 4:
|
|
280
|
+
v = x.valueConverter.toDb(data[x.key]);
|
|
281
|
+
if (!(v != undefined)) return [3 /*break*/, 6];
|
|
282
|
+
return [4 /*yield*/, e.nameOf(x)];
|
|
283
|
+
case 5:
|
|
284
|
+
key = _d.sent();
|
|
285
|
+
insertObject[key] = v;
|
|
286
|
+
_d.label = 6;
|
|
287
|
+
case 6:
|
|
288
|
+
_b = _a.next();
|
|
289
|
+
return [3 /*break*/, 3];
|
|
290
|
+
case 7: return [3 /*break*/, 10];
|
|
291
|
+
case 8:
|
|
292
|
+
e_3_1 = _d.sent();
|
|
293
|
+
e_3 = { error: e_3_1 };
|
|
294
|
+
return [3 /*break*/, 10];
|
|
295
|
+
case 9:
|
|
296
|
+
try {
|
|
297
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
298
|
+
}
|
|
299
|
+
finally { if (e_3) throw e_3.error; }
|
|
300
|
+
return [7 /*endfinally*/];
|
|
301
|
+
case 10:
|
|
302
|
+
insert = this.knex(e.entityName).insert(insertObject);
|
|
303
|
+
if (!remult3_1.isAutoIncrement(this.entity.idMetadata.field)) return [3 /*break*/, 12];
|
|
304
|
+
return [4 /*yield*/, insert.returning(this.entity.idMetadata.field.key)];
|
|
305
|
+
case 11:
|
|
306
|
+
result = _d.sent();
|
|
307
|
+
newId_1 = result[0].id;
|
|
308
|
+
resultFilter = new filter_interfaces_1.Filter(function (x) { return x.isEqualTo(_this.entity.idMetadata.field, newId_1); });
|
|
309
|
+
return [3 /*break*/, 14];
|
|
310
|
+
case 12: return [4 /*yield*/, insert];
|
|
311
|
+
case 13:
|
|
312
|
+
_d.sent();
|
|
313
|
+
_d.label = 14;
|
|
314
|
+
case 14: return [2 /*return*/, this.find({ where: resultFilter }).then(function (y) {
|
|
315
|
+
return y[0];
|
|
316
|
+
})];
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
return KnexEntityDataProvider;
|
|
322
|
+
}());
|
|
323
|
+
var FilterConsumerBridgeToKnexRequest = /** @class */ (function () {
|
|
324
|
+
function FilterConsumerBridgeToKnexRequest(nameProvider) {
|
|
165
325
|
this.nameProvider = nameProvider;
|
|
166
326
|
this._addWhere = true;
|
|
167
327
|
this.promises = [];
|
|
168
328
|
this.result = [];
|
|
169
329
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
330
|
+
FilterConsumerBridgeToKnexRequest.prototype.resolveWhere = function () {
|
|
331
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
332
|
+
var p, p_1, p_1_1, pr, e_4_1;
|
|
333
|
+
var e_4, _a;
|
|
334
|
+
return tslib_1.__generator(this, function (_b) {
|
|
335
|
+
switch (_b.label) {
|
|
336
|
+
case 0:
|
|
337
|
+
if (!(this.promises.length > 0)) return [3 /*break*/, 9];
|
|
338
|
+
p = this.promises;
|
|
339
|
+
this.promises = [];
|
|
340
|
+
_b.label = 1;
|
|
341
|
+
case 1:
|
|
342
|
+
_b.trys.push([1, 6, 7, 8]);
|
|
343
|
+
p_1 = (e_4 = void 0, tslib_1.__values(p)), p_1_1 = p_1.next();
|
|
344
|
+
_b.label = 2;
|
|
345
|
+
case 2:
|
|
346
|
+
if (!!p_1_1.done) return [3 /*break*/, 5];
|
|
347
|
+
pr = p_1_1.value;
|
|
348
|
+
return [4 /*yield*/, pr];
|
|
349
|
+
case 3:
|
|
350
|
+
_b.sent();
|
|
351
|
+
_b.label = 4;
|
|
352
|
+
case 4:
|
|
353
|
+
p_1_1 = p_1.next();
|
|
354
|
+
return [3 /*break*/, 2];
|
|
355
|
+
case 5: return [3 /*break*/, 8];
|
|
356
|
+
case 6:
|
|
357
|
+
e_4_1 = _b.sent();
|
|
358
|
+
e_4 = { error: e_4_1 };
|
|
359
|
+
return [3 /*break*/, 8];
|
|
360
|
+
case 7:
|
|
361
|
+
try {
|
|
362
|
+
if (p_1_1 && !p_1_1.done && (_a = p_1.return)) _a.call(p_1);
|
|
363
|
+
}
|
|
364
|
+
finally { if (e_4) throw e_4.error; }
|
|
365
|
+
return [7 /*endfinally*/];
|
|
366
|
+
case 8: return [3 /*break*/, 0];
|
|
367
|
+
case 9: return [2 /*return*/, this.result];
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
};
|
|
372
|
+
FilterConsumerBridgeToKnexRequest.prototype.custom = function (key, customItem) {
|
|
181
373
|
throw new Error("Custom filter should be translated before it gets here");
|
|
182
|
-
}
|
|
183
|
-
or(orElements) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
374
|
+
};
|
|
375
|
+
FilterConsumerBridgeToKnexRequest.prototype.or = function (orElements) {
|
|
376
|
+
var _this = this;
|
|
377
|
+
this.promises.push((function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
378
|
+
var result, _loop_1, this_1, orElements_1, orElements_1_1, element, state_1, e_5_1;
|
|
379
|
+
var e_5, _a;
|
|
380
|
+
return tslib_1.__generator(this, function (_b) {
|
|
381
|
+
switch (_b.label) {
|
|
382
|
+
case 0:
|
|
383
|
+
result = [];
|
|
384
|
+
_loop_1 = function (element) {
|
|
385
|
+
var f, where;
|
|
386
|
+
return tslib_1.__generator(this, function (_a) {
|
|
387
|
+
switch (_a.label) {
|
|
388
|
+
case 0:
|
|
389
|
+
f = new FilterConsumerBridgeToKnexRequest(this_1.nameProvider);
|
|
390
|
+
f._addWhere = false;
|
|
391
|
+
element.__applyToConsumer(f);
|
|
392
|
+
return [4 /*yield*/, f.resolveWhere()];
|
|
393
|
+
case 1:
|
|
394
|
+
where = _a.sent();
|
|
395
|
+
if (where.length > 0) {
|
|
396
|
+
result.push(function (b) {
|
|
397
|
+
b.orWhere(function (b) {
|
|
398
|
+
where.forEach(function (x) { return x(b); });
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
else
|
|
403
|
+
return [2 /*return*/, { value: void 0 }]; //empty or means all rows
|
|
404
|
+
return [2 /*return*/];
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
this_1 = this;
|
|
409
|
+
_b.label = 1;
|
|
410
|
+
case 1:
|
|
411
|
+
_b.trys.push([1, 6, 7, 8]);
|
|
412
|
+
orElements_1 = tslib_1.__values(orElements), orElements_1_1 = orElements_1.next();
|
|
413
|
+
_b.label = 2;
|
|
414
|
+
case 2:
|
|
415
|
+
if (!!orElements_1_1.done) return [3 /*break*/, 5];
|
|
416
|
+
element = orElements_1_1.value;
|
|
417
|
+
return [5 /*yield**/, _loop_1(element)];
|
|
418
|
+
case 3:
|
|
419
|
+
state_1 = _b.sent();
|
|
420
|
+
if (typeof state_1 === "object")
|
|
421
|
+
return [2 /*return*/, state_1.value];
|
|
422
|
+
_b.label = 4;
|
|
423
|
+
case 4:
|
|
424
|
+
orElements_1_1 = orElements_1.next();
|
|
425
|
+
return [3 /*break*/, 2];
|
|
426
|
+
case 5: return [3 /*break*/, 8];
|
|
427
|
+
case 6:
|
|
428
|
+
e_5_1 = _b.sent();
|
|
429
|
+
e_5 = { error: e_5_1 };
|
|
430
|
+
return [3 /*break*/, 8];
|
|
431
|
+
case 7:
|
|
432
|
+
try {
|
|
433
|
+
if (orElements_1_1 && !orElements_1_1.done && (_a = orElements_1.return)) _a.call(orElements_1);
|
|
434
|
+
}
|
|
435
|
+
finally { if (e_5) throw e_5.error; }
|
|
436
|
+
return [7 /*endfinally*/];
|
|
437
|
+
case 8:
|
|
438
|
+
if (result.length > 0) {
|
|
439
|
+
this.result.push(function (b) { return b.where(function (x) { return result.find(function (y) { return y(x); }); }); });
|
|
440
|
+
}
|
|
441
|
+
return [2 /*return*/];
|
|
197
442
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
this.result.push(b
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
isEqualTo(col, val) {
|
|
443
|
+
});
|
|
444
|
+
}); })());
|
|
445
|
+
};
|
|
446
|
+
FilterConsumerBridgeToKnexRequest.prototype.isNull = function (col) {
|
|
447
|
+
var _this = this;
|
|
448
|
+
this.result.push(function (b) { return b.whereNull(_this.nameProvider.nameOf(col)); });
|
|
449
|
+
};
|
|
450
|
+
FilterConsumerBridgeToKnexRequest.prototype.isNotNull = function (col) {
|
|
451
|
+
var _this = this;
|
|
452
|
+
this.result.push(function (b) { return b.whereNotNull(_this.nameProvider.nameOf(col)); });
|
|
453
|
+
};
|
|
454
|
+
FilterConsumerBridgeToKnexRequest.prototype.isIn = function (col, val) {
|
|
455
|
+
var _this = this;
|
|
456
|
+
this.result.push(function (knex) {
|
|
457
|
+
return knex.whereIn(_this.nameProvider.nameOf(col), val.map(function (x) { return col.valueConverter.toDb(x); }));
|
|
458
|
+
});
|
|
459
|
+
};
|
|
460
|
+
FilterConsumerBridgeToKnexRequest.prototype.isEqualTo = function (col, val) {
|
|
216
461
|
this.add(col, val, "=");
|
|
217
|
-
}
|
|
218
|
-
isDifferentFrom(col, val) {
|
|
462
|
+
};
|
|
463
|
+
FilterConsumerBridgeToKnexRequest.prototype.isDifferentFrom = function (col, val) {
|
|
219
464
|
this.add(col, val, "<>");
|
|
220
|
-
}
|
|
221
|
-
isGreaterOrEqualTo(col, val) {
|
|
465
|
+
};
|
|
466
|
+
FilterConsumerBridgeToKnexRequest.prototype.isGreaterOrEqualTo = function (col, val) {
|
|
222
467
|
this.add(col, val, ">=");
|
|
223
|
-
}
|
|
224
|
-
isGreaterThan(col, val) {
|
|
468
|
+
};
|
|
469
|
+
FilterConsumerBridgeToKnexRequest.prototype.isGreaterThan = function (col, val) {
|
|
225
470
|
this.add(col, val, ">");
|
|
226
|
-
}
|
|
227
|
-
isLessOrEqualTo(col, val) {
|
|
471
|
+
};
|
|
472
|
+
FilterConsumerBridgeToKnexRequest.prototype.isLessOrEqualTo = function (col, val) {
|
|
228
473
|
this.add(col, val, "<=");
|
|
229
|
-
}
|
|
230
|
-
isLessThan(col, val) {
|
|
474
|
+
};
|
|
475
|
+
FilterConsumerBridgeToKnexRequest.prototype.isLessThan = function (col, val) {
|
|
231
476
|
this.add(col, val, "<");
|
|
232
|
-
}
|
|
233
|
-
containsCaseInsensitive(col, val) {
|
|
234
|
-
|
|
235
|
-
this.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
477
|
+
};
|
|
478
|
+
FilterConsumerBridgeToKnexRequest.prototype.containsCaseInsensitive = function (col, val) {
|
|
479
|
+
var _this = this;
|
|
480
|
+
this.result.push(function (b) { return b.whereRaw('lower (' + _this.nameProvider.nameOf(col) + ") like lower ('%" + val.replace(/'/g, '\'\'') + "%')"); });
|
|
481
|
+
this.promises.push((function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
482
|
+
return tslib_1.__generator(this, function (_a) {
|
|
483
|
+
return [2 /*return*/];
|
|
484
|
+
});
|
|
485
|
+
}); })());
|
|
486
|
+
};
|
|
487
|
+
FilterConsumerBridgeToKnexRequest.prototype.add = function (col, val, operator) {
|
|
488
|
+
var _this = this;
|
|
489
|
+
this.result.push(function (b) { return b.where(_this.nameProvider.nameOf(col), operator, col.valueConverter.toDb(val)); });
|
|
490
|
+
};
|
|
491
|
+
FilterConsumerBridgeToKnexRequest.prototype.databaseCustom = function (databaseCustom) {
|
|
492
|
+
var _this = this;
|
|
493
|
+
this.promises.push((function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
494
|
+
var _a, _b;
|
|
495
|
+
return tslib_1.__generator(this, function (_c) {
|
|
496
|
+
switch (_c.label) {
|
|
497
|
+
case 0:
|
|
498
|
+
if (!(databaseCustom === null || databaseCustom === void 0 ? void 0 : databaseCustom.buildKnex)) return [3 /*break*/, 2];
|
|
499
|
+
_b = (_a = this.result).push;
|
|
500
|
+
return [4 /*yield*/, databaseCustom.buildKnex()];
|
|
501
|
+
case 1:
|
|
502
|
+
_b.apply(_a, [_c.sent()]);
|
|
503
|
+
_c.label = 2;
|
|
504
|
+
case 2: return [2 /*return*/];
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
}); })());
|
|
508
|
+
};
|
|
509
|
+
return FilterConsumerBridgeToKnexRequest;
|
|
510
|
+
}());
|
|
511
|
+
exports.FilterConsumerBridgeToKnexRequest = FilterConsumerBridgeToKnexRequest;
|
|
512
|
+
var KnexSchemaBuilder = /** @class */ (function () {
|
|
513
|
+
function KnexSchemaBuilder(knex) {
|
|
251
514
|
this.knex = knex;
|
|
252
515
|
this.additionalWhere = '';
|
|
253
516
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
517
|
+
KnexSchemaBuilder.prototype.verifyStructureOfAllEntities = function (remult) {
|
|
518
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
519
|
+
var allEntities_1, allEntities_1_1, entity, metadata, err_2, _a, _b, _c, e_6_1;
|
|
520
|
+
var e_6, _d;
|
|
521
|
+
return tslib_1.__generator(this, function (_e) {
|
|
522
|
+
switch (_e.label) {
|
|
523
|
+
case 0:
|
|
524
|
+
console.log("start verify structure");
|
|
525
|
+
_e.label = 1;
|
|
526
|
+
case 1:
|
|
527
|
+
_e.trys.push([1, 12, 13, 14]);
|
|
528
|
+
allEntities_1 = tslib_1.__values(context_1.allEntities), allEntities_1_1 = allEntities_1.next();
|
|
529
|
+
_e.label = 2;
|
|
530
|
+
case 2:
|
|
531
|
+
if (!!allEntities_1_1.done) return [3 /*break*/, 11];
|
|
532
|
+
entity = allEntities_1_1.value;
|
|
533
|
+
metadata = remult.repo(entity).metadata;
|
|
534
|
+
_e.label = 3;
|
|
535
|
+
case 3:
|
|
536
|
+
_e.trys.push([3, 8, , 10]);
|
|
537
|
+
if (!!metadata.options.sqlExpression) return [3 /*break*/, 7];
|
|
538
|
+
return [4 /*yield*/, metadata.getDbName()];
|
|
539
|
+
case 4:
|
|
540
|
+
if (!((_e.sent()).toLowerCase().indexOf('from ') < 0)) return [3 /*break*/, 7];
|
|
541
|
+
return [4 /*yield*/, this.createIfNotExist(metadata)];
|
|
542
|
+
case 5:
|
|
543
|
+
_e.sent();
|
|
544
|
+
return [4 /*yield*/, this.verifyAllColumns(metadata)];
|
|
545
|
+
case 6:
|
|
546
|
+
_e.sent();
|
|
547
|
+
_e.label = 7;
|
|
548
|
+
case 7: return [3 /*break*/, 10];
|
|
549
|
+
case 8:
|
|
550
|
+
err_2 = _e.sent();
|
|
551
|
+
_b = (_a = console).log;
|
|
552
|
+
_c = "failed verify structure of ";
|
|
553
|
+
return [4 /*yield*/, metadata.getDbName()];
|
|
554
|
+
case 9:
|
|
555
|
+
_b.apply(_a, [_c + (_e.sent()) + " ", err_2]);
|
|
556
|
+
return [3 /*break*/, 10];
|
|
557
|
+
case 10:
|
|
558
|
+
allEntities_1_1 = allEntities_1.next();
|
|
559
|
+
return [3 /*break*/, 2];
|
|
560
|
+
case 11: return [3 /*break*/, 14];
|
|
561
|
+
case 12:
|
|
562
|
+
e_6_1 = _e.sent();
|
|
563
|
+
e_6 = { error: e_6_1 };
|
|
564
|
+
return [3 /*break*/, 14];
|
|
565
|
+
case 13:
|
|
566
|
+
try {
|
|
567
|
+
if (allEntities_1_1 && !allEntities_1_1.done && (_d = allEntities_1.return)) _d.call(allEntities_1);
|
|
568
|
+
}
|
|
569
|
+
finally { if (e_6) throw e_6.error; }
|
|
570
|
+
return [7 /*endfinally*/];
|
|
571
|
+
case 14: return [2 /*return*/];
|
|
264
572
|
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
573
|
+
});
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
KnexSchemaBuilder.prototype.createIfNotExist = function (entity) {
|
|
577
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
578
|
+
var e, cols_1, _a, _b, f;
|
|
579
|
+
var e_7, _c;
|
|
580
|
+
return tslib_1.__generator(this, function (_d) {
|
|
581
|
+
switch (_d.label) {
|
|
582
|
+
case 0: return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(entity)];
|
|
583
|
+
case 1:
|
|
584
|
+
e = _d.sent();
|
|
585
|
+
return [4 /*yield*/, this.knex.schema.hasTable(e.entityName)];
|
|
586
|
+
case 2:
|
|
587
|
+
if (!!(_d.sent())) return [3 /*break*/, 4];
|
|
588
|
+
cols_1 = new Map();
|
|
589
|
+
try {
|
|
590
|
+
for (_a = tslib_1.__values(entity.fields), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
591
|
+
f = _b.value;
|
|
592
|
+
cols_1.set(f, {
|
|
593
|
+
name: e.nameOf(f),
|
|
594
|
+
readonly: e.isDbReadonly(f)
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
599
|
+
finally {
|
|
600
|
+
try {
|
|
601
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
602
|
+
}
|
|
603
|
+
finally { if (e_7) throw e_7.error; }
|
|
290
604
|
}
|
|
291
|
-
|
|
605
|
+
return [4 /*yield*/, logSql(this.knex.schema.createTable(e.entityName, function (b) {
|
|
606
|
+
var e_8, _a;
|
|
607
|
+
try {
|
|
608
|
+
for (var _b = tslib_1.__values(entity.fields), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
609
|
+
var x = _c.value;
|
|
610
|
+
if (!cols_1.get(x).readonly || remult3_1.isAutoIncrement(x)) {
|
|
611
|
+
if (remult3_1.isAutoIncrement(x))
|
|
612
|
+
b.increments(cols_1.get(x).name);
|
|
613
|
+
else {
|
|
614
|
+
buildColumn(x, cols_1.get(x).name, b);
|
|
615
|
+
if (x == entity.idMetadata.field)
|
|
616
|
+
b.primary([cols_1.get(x).name]);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
622
|
+
finally {
|
|
623
|
+
try {
|
|
624
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
625
|
+
}
|
|
626
|
+
finally { if (e_8) throw e_8.error; }
|
|
627
|
+
}
|
|
628
|
+
}))];
|
|
629
|
+
case 3:
|
|
630
|
+
_d.sent();
|
|
631
|
+
_d.label = 4;
|
|
632
|
+
case 4: return [2 /*return*/];
|
|
292
633
|
}
|
|
293
|
-
}));
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
async addColumnIfNotExist(entity, c) {
|
|
297
|
-
let e = await getDbNameProvider(entity);
|
|
298
|
-
if (e.isDbReadonly(c(entity)))
|
|
299
|
-
return;
|
|
300
|
-
let col = c(entity);
|
|
301
|
-
let colName = e.nameOf(col);
|
|
302
|
-
if (!await this.knex.schema.hasColumn(e.entityName, colName)) {
|
|
303
|
-
await this.knex.schema.alterTable(e.entityName, b => {
|
|
304
|
-
buildColumn(col, colName, b);
|
|
305
634
|
});
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
635
|
+
});
|
|
636
|
+
};
|
|
637
|
+
KnexSchemaBuilder.prototype.addColumnIfNotExist = function (entity, c) {
|
|
638
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
639
|
+
var e, col, colName;
|
|
640
|
+
return tslib_1.__generator(this, function (_a) {
|
|
641
|
+
switch (_a.label) {
|
|
642
|
+
case 0: return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(entity)];
|
|
643
|
+
case 1:
|
|
644
|
+
e = _a.sent();
|
|
645
|
+
if (e.isDbReadonly(c(entity)))
|
|
646
|
+
return [2 /*return*/];
|
|
647
|
+
col = c(entity);
|
|
648
|
+
colName = e.nameOf(col);
|
|
649
|
+
return [4 /*yield*/, this.knex.schema.hasColumn(e.entityName, colName)];
|
|
650
|
+
case 2:
|
|
651
|
+
if (!!(_a.sent())) return [3 /*break*/, 4];
|
|
652
|
+
return [4 /*yield*/, this.knex.schema.alterTable(e.entityName, function (b) {
|
|
653
|
+
buildColumn(col, colName, b);
|
|
654
|
+
})];
|
|
655
|
+
case 3:
|
|
656
|
+
_a.sent();
|
|
657
|
+
_a.label = 4;
|
|
658
|
+
case 4: return [2 /*return*/];
|
|
314
659
|
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
};
|
|
663
|
+
KnexSchemaBuilder.prototype.verifyAllColumns = function (entity) {
|
|
664
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
665
|
+
var e, _loop_2, this_2, _a, _b, col, e_9_1, err_3;
|
|
666
|
+
var e_9, _c;
|
|
667
|
+
return tslib_1.__generator(this, function (_d) {
|
|
668
|
+
switch (_d.label) {
|
|
669
|
+
case 0: return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(entity)];
|
|
670
|
+
case 1:
|
|
671
|
+
e = _d.sent();
|
|
672
|
+
_d.label = 2;
|
|
673
|
+
case 2:
|
|
674
|
+
_d.trys.push([2, 11, , 12]);
|
|
675
|
+
_loop_2 = function (col) {
|
|
676
|
+
return tslib_1.__generator(this, function (_a) {
|
|
677
|
+
switch (_a.label) {
|
|
678
|
+
case 0:
|
|
679
|
+
if (!!e.isDbReadonly(col)) return [3 /*break*/, 2];
|
|
680
|
+
return [4 /*yield*/, this_2.addColumnIfNotExist(entity, function () { return col; })];
|
|
681
|
+
case 1:
|
|
682
|
+
_a.sent();
|
|
683
|
+
_a.label = 2;
|
|
684
|
+
case 2: return [2 /*return*/];
|
|
685
|
+
}
|
|
686
|
+
});
|
|
687
|
+
};
|
|
688
|
+
this_2 = this;
|
|
689
|
+
_d.label = 3;
|
|
690
|
+
case 3:
|
|
691
|
+
_d.trys.push([3, 8, 9, 10]);
|
|
692
|
+
_a = tslib_1.__values(entity.fields), _b = _a.next();
|
|
693
|
+
_d.label = 4;
|
|
694
|
+
case 4:
|
|
695
|
+
if (!!_b.done) return [3 /*break*/, 7];
|
|
696
|
+
col = _b.value;
|
|
697
|
+
return [5 /*yield**/, _loop_2(col)];
|
|
698
|
+
case 5:
|
|
699
|
+
_d.sent();
|
|
700
|
+
_d.label = 6;
|
|
701
|
+
case 6:
|
|
702
|
+
_b = _a.next();
|
|
703
|
+
return [3 /*break*/, 4];
|
|
704
|
+
case 7: return [3 /*break*/, 10];
|
|
705
|
+
case 8:
|
|
706
|
+
e_9_1 = _d.sent();
|
|
707
|
+
e_9 = { error: e_9_1 };
|
|
708
|
+
return [3 /*break*/, 10];
|
|
709
|
+
case 9:
|
|
710
|
+
try {
|
|
711
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
712
|
+
}
|
|
713
|
+
finally { if (e_9) throw e_9.error; }
|
|
714
|
+
return [7 /*endfinally*/];
|
|
715
|
+
case 10: return [3 /*break*/, 12];
|
|
716
|
+
case 11:
|
|
717
|
+
err_3 = _d.sent();
|
|
718
|
+
console.log(err_3);
|
|
719
|
+
return [3 /*break*/, 12];
|
|
720
|
+
case 12: return [2 /*return*/];
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
});
|
|
724
|
+
};
|
|
725
|
+
return KnexSchemaBuilder;
|
|
726
|
+
}());
|
|
727
|
+
exports.KnexSchemaBuilder = KnexSchemaBuilder;
|
|
728
|
+
function buildColumn(x, dbName, b) {
|
|
323
729
|
if (x.valueType == Number) {
|
|
324
730
|
if (!x.valueConverter.fieldTypeInDb) {
|
|
325
|
-
|
|
731
|
+
var c = b.decimal(dbName);
|
|
326
732
|
if (!x.allowNull) {
|
|
327
733
|
c.defaultTo(0).notNullable();
|
|
328
734
|
}
|
|
329
735
|
}
|
|
330
736
|
else if (x.valueConverter.fieldTypeInDb == "integer") {
|
|
331
|
-
|
|
737
|
+
var c = b.integer(dbName);
|
|
332
738
|
if (!x.allowNull) {
|
|
333
739
|
c.defaultTo(0).notNullable();
|
|
334
740
|
}
|
|
@@ -338,7 +744,7 @@ export function buildColumn(x, dbName, b) {
|
|
|
338
744
|
}
|
|
339
745
|
else if (x.valueType == Date) {
|
|
340
746
|
if (!x.valueConverter.fieldTypeInDb)
|
|
341
|
-
if (x.valueConverter == ValueConverters.DateOnly)
|
|
747
|
+
if (x.valueConverter == valueConverters_1.ValueConverters.DateOnly)
|
|
342
748
|
b.date(dbName);
|
|
343
749
|
else
|
|
344
750
|
b.dateTime(dbName);
|
|
@@ -348,13 +754,13 @@ export function buildColumn(x, dbName, b) {
|
|
|
348
754
|
b.specificType(dbName, x.valueConverter.fieldTypeInDb);
|
|
349
755
|
}
|
|
350
756
|
else if (x.valueType == Boolean) {
|
|
351
|
-
|
|
757
|
+
var c = b.boolean(dbName);
|
|
352
758
|
if (!x.allowNull)
|
|
353
759
|
c.defaultTo(false).notNullable();
|
|
354
760
|
}
|
|
355
761
|
else if (x.valueConverter.fieldTypeInDb) {
|
|
356
762
|
if (x.valueConverter.fieldTypeInDb == "integer") {
|
|
357
|
-
|
|
763
|
+
var c = b.integer(dbName);
|
|
358
764
|
if (!x.allowNull) {
|
|
359
765
|
c.defaultTo(0).notNullable();
|
|
360
766
|
}
|
|
@@ -363,19 +769,33 @@ export function buildColumn(x, dbName, b) {
|
|
|
363
769
|
b.specificType(dbName, x.valueConverter.fieldTypeInDb);
|
|
364
770
|
}
|
|
365
771
|
else {
|
|
366
|
-
|
|
772
|
+
var c = b.string(dbName, x.options.maxLength);
|
|
367
773
|
if (!x.allowNull)
|
|
368
774
|
c.defaultTo('').notNullable();
|
|
369
775
|
}
|
|
370
776
|
}
|
|
777
|
+
exports.buildColumn = buildColumn;
|
|
371
778
|
function logSql(who) {
|
|
372
779
|
//console.log(who.toSQL().sql);
|
|
373
780
|
return who;
|
|
374
781
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
782
|
+
function createKnexDataProvider(config, autoCreateTables) {
|
|
783
|
+
if (autoCreateTables === void 0) { autoCreateTables = true; }
|
|
784
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
785
|
+
var k, result;
|
|
786
|
+
return tslib_1.__generator(this, function (_a) {
|
|
787
|
+
switch (_a.label) {
|
|
788
|
+
case 0:
|
|
789
|
+
k = knex_1.default(config);
|
|
790
|
+
result = new KnexDataProvider(k);
|
|
791
|
+
if (!autoCreateTables) return [3 /*break*/, 2];
|
|
792
|
+
return [4 /*yield*/, new KnexSchemaBuilder(k).verifyStructureOfAllEntities(new context_1.Remult(result))];
|
|
793
|
+
case 1:
|
|
794
|
+
_a.sent();
|
|
795
|
+
_a.label = 2;
|
|
796
|
+
case 2: return [2 /*return*/, result];
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
});
|
|
381
800
|
}
|
|
801
|
+
exports.createKnexDataProvider = createKnexDataProvider;
|