remult 0.14.4 → 0.14.5
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 +1 -5
- package/classType.js +1 -2
- package/graphql.js +84 -146
- package/index.js +25 -73
- package/inputTypes.js +9 -16
- package/package.json +2 -1
- package/postgres/index.js +1 -4
- package/postgres/postgres-data-provider.js +165 -364
- package/postgres/postgresColumnSyntax.js +4 -8
- package/remult-express.js +6 -10
- package/remult-knex/index.js +300 -720
- package/remult-middleware.js +24 -30
- package/remult-mongo.js +172 -406
- package/server/JsonEntityFileStorage.js +10 -15
- package/server/expressBridge.js +383 -569
- package/server/index.js +4 -11
- package/server/register-actions-on-server.js +12 -38
- package/server/register-entities-on-server.js +9 -13
- package/src/__EntityValueProvider.js +1 -2
- package/src/__RowsOfDataForTesting.js +1 -2
- package/src/backend-tests/backend-database-test-setup.backend-spec.js +72 -190
- package/src/backend-tests/my-test.backend-spec.js +45 -114
- package/src/column-interfaces.js +2 -6
- package/src/column.js +71 -113
- package/src/context.js +174 -330
- package/src/data-api.js +162 -326
- package/src/data-interfaces.js +7 -11
- package/src/data-providers/array-entity-data-provider.js +171 -286
- package/src/data-providers/in-memory-database.js +12 -27
- package/src/data-providers/json-data-provider.js +41 -77
- package/src/data-providers/rest-data-provider.js +102 -189
- package/src/data-providers/sql-database.js +223 -403
- package/src/data-providers/web-sql-data-provider.js +78 -139
- package/src/entity.js +1 -2
- package/src/filter/filter-consumer-bridge-to-sql-request.js +106 -248
- package/src/filter/filter-consumer-bridge-to-url-builder.js +1 -2
- package/src/filter/filter-interfaces.js +277 -440
- package/src/id-entity.js +8 -17
- package/src/remult3/RepositoryImplementation.js +1191 -2367
- package/src/remult3/index.js +2 -5
- package/src/remult3/remult3.js +1 -2
- package/src/server-action.js +294 -528
- package/src/shared-tests/db-tests-setup.js +49 -153
- package/src/shared-tests/db-tests.db-tests.js +660 -1566
- package/src/shared-tests/deleteAll.js +3 -20
- package/src/shared-tests/entityWithValidations.js +52 -79
- package/src/shared-tests/index.js +4 -6
- package/src/shared-tests/test-custom-id-column.spec.js +37 -85
- package/src/shared-tests/test-special-value.spec.js +64 -104
- package/src/shared-tests/test-update-with-null.spec.js +28 -49
- package/src/sort.js +38 -95
- package/src/sql-command.js +1 -2
- package/src/tests/Done.js +7 -13
- package/src/tests/TestDataApiResponse.js +15 -22
- package/src/tests/c.js +21 -27
- package/src/tests/createData.js +20 -48
- package/src/tests/d.js +19 -27
- package/src/tests/dWithPrefilter.js +31 -44
- package/src/tests/entityForCustomFilter.js +42 -66
- package/src/tests/entityWithValidationsOnColumn.js +21 -29
- package/src/tests/frontend-database-tests-setup.spec.js +35 -98
- package/src/tests/h.js +35 -44
- package/src/tests/remult-3-entities.js +71 -80
- package/src/tests/tasks.js +23 -29
- package/src/tests/testHelper.spec.js +170 -322
- package/src/tests/testModel/models.js +25 -33
- package/src/validators.js +32 -59
- package/urlBuilder.js +8 -14
- package/valueConverters.js +161 -160
package/remult-knex/index.js
CHANGED
|
@@ -1,740 +1,334 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var remult3_1 = require("../src/remult3");
|
|
11
|
-
var valueConverters_1 = require("../valueConverters");
|
|
12
|
-
var KnexDataProvider = /** @class */ (function () {
|
|
13
|
-
function KnexDataProvider(knex) {
|
|
1
|
+
import { CompoundIdField, Filter, Remult } from "..";
|
|
2
|
+
import knex from 'knex';
|
|
3
|
+
import { customDatabaseFilterToken } from "../src/filter/filter-interfaces";
|
|
4
|
+
import { getDbNameProvider } from "../src/filter/filter-consumer-bridge-to-sql-request";
|
|
5
|
+
import { allEntities } from "../src/context";
|
|
6
|
+
import { isAutoIncrement } from "../src/remult3";
|
|
7
|
+
import { ValueConverters } from "../valueConverters";
|
|
8
|
+
export class KnexDataProvider {
|
|
9
|
+
constructor(knex) {
|
|
14
10
|
this.knex = knex;
|
|
15
11
|
}
|
|
16
|
-
|
|
12
|
+
getEntityDataProvider(entity) {
|
|
17
13
|
return new KnexEntityDataProvider(entity, this.knex);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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] = {
|
|
14
|
+
}
|
|
15
|
+
async transaction(action) {
|
|
16
|
+
let t = await this.knex.transaction();
|
|
17
|
+
try {
|
|
18
|
+
await action(new KnexDataProvider(t));
|
|
19
|
+
await t.commit();
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
await t.rollback();
|
|
23
|
+
throw err;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
static customFilter(build) {
|
|
27
|
+
return {
|
|
28
|
+
[customDatabaseFilterToken]: {
|
|
52
29
|
buildKnex: build
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var KnexEntityDataProvider = /** @class */ (function () {
|
|
60
|
-
function KnexEntityDataProvider(entity, knex) {
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class KnexEntityDataProvider {
|
|
35
|
+
constructor(entity, knex) {
|
|
61
36
|
this.entity = entity;
|
|
62
37
|
this.knex = knex;
|
|
63
38
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
})];
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
});
|
|
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()];
|
|
39
|
+
async count(where) {
|
|
40
|
+
const e = await this.init();
|
|
41
|
+
const br = new FilterConsumerBridgeToKnexRequest(e);
|
|
42
|
+
where.__applyToConsumer(br);
|
|
43
|
+
let r = await br.resolveWhere();
|
|
44
|
+
const result = (await this.knex(e.entityName)
|
|
45
|
+
.count().where(b => r.forEach(w => w(b))));
|
|
46
|
+
var row = result[0];
|
|
47
|
+
for (const key in row) {
|
|
48
|
+
if (Object.prototype.hasOwnProperty.call(row, key)) {
|
|
49
|
+
const element = row[key];
|
|
50
|
+
return +element;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async find(options) {
|
|
55
|
+
const e = await this.init();
|
|
56
|
+
let cols = [];
|
|
57
|
+
let colKeys = [];
|
|
58
|
+
for (const x of this.entity.fields) {
|
|
59
|
+
if (x.isServerExpression) {
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
cols.push(e.nameOf(x));
|
|
63
|
+
colKeys.push(x);
|
|
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]);
|
|
170
92
|
}
|
|
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
|
-
__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: __1.Filter.fromEntityFilter(this.entity, resultFilter) }).then(function (y) { return y[0]; })];
|
|
93
|
+
catch (err) {
|
|
94
|
+
throw new Error("Failed to load from db:" + field.key + "\r\n" + err);
|
|
229
95
|
}
|
|
230
|
-
}
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
231
98
|
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
99
|
+
}
|
|
100
|
+
async init() {
|
|
101
|
+
return await getDbNameProvider(this.entity);
|
|
102
|
+
}
|
|
103
|
+
async update(id, data) {
|
|
104
|
+
const e = await this.init();
|
|
105
|
+
let f = new FilterConsumerBridgeToKnexRequest(e);
|
|
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;
|
|
250
118
|
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
return
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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 __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
|
-
})];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
let where = await f.resolveWhere();
|
|
122
|
+
await this.knex(e.entityName).update(updateObject).where(b => where.forEach(w => w(b)));
|
|
123
|
+
return this.find({ where: Filter.fromEntityFilter(this.entity, resultFilter) }).then(y => y[0]);
|
|
124
|
+
}
|
|
125
|
+
async delete(id) {
|
|
126
|
+
const e = await this.init();
|
|
127
|
+
let f = new FilterConsumerBridgeToKnexRequest(e);
|
|
128
|
+
Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(id)).__applyToConsumer(f);
|
|
129
|
+
let where = await f.resolveWhere();
|
|
130
|
+
await this.knex(e.entityName).delete().where(b => where.forEach(w => w(b)));
|
|
131
|
+
}
|
|
132
|
+
async insert(data) {
|
|
133
|
+
const e = await this.init();
|
|
134
|
+
let resultFilter;
|
|
135
|
+
if (this.entity.idMetadata.field instanceof CompoundIdField)
|
|
136
|
+
resultFilter = this.entity.idMetadata.field.resultIdFilter(undefined, data);
|
|
137
|
+
else
|
|
138
|
+
resultFilter = Filter.fromEntityFilter(this.entity, this.entity.idMetadata.getIdFilter(data[this.entity.idMetadata.field.key]));
|
|
139
|
+
let insertObject = {};
|
|
140
|
+
for (const x of this.entity.fields) {
|
|
141
|
+
if (e.isDbReadonly(x)) { }
|
|
142
|
+
else {
|
|
143
|
+
let v = x.valueConverter.toDb(data[x.key]);
|
|
144
|
+
if (v != undefined) {
|
|
145
|
+
let key = await e.nameOf(x);
|
|
146
|
+
insertObject[key] = v;
|
|
317
147
|
}
|
|
318
|
-
}
|
|
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];
|
|
319
160
|
});
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
function FilterConsumerBridgeToKnexRequest(nameProvider) {
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export class FilterConsumerBridgeToKnexRequest {
|
|
164
|
+
constructor(nameProvider) {
|
|
325
165
|
this.nameProvider = nameProvider;
|
|
326
166
|
this._addWhere = true;
|
|
327
167
|
this.promises = [];
|
|
328
168
|
this.result = [];
|
|
329
169
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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) {
|
|
170
|
+
async resolveWhere() {
|
|
171
|
+
while (this.promises.length > 0) {
|
|
172
|
+
let p = this.promises;
|
|
173
|
+
this.promises = [];
|
|
174
|
+
for (const pr of p) {
|
|
175
|
+
await pr;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return this.result;
|
|
179
|
+
}
|
|
180
|
+
custom(key, customItem) {
|
|
373
181
|
throw new Error("Custom filter should be translated before it gets here");
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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*/];
|
|
182
|
+
}
|
|
183
|
+
or(orElements) {
|
|
184
|
+
this.promises.push((async () => {
|
|
185
|
+
const result = [];
|
|
186
|
+
for (const element of orElements) {
|
|
187
|
+
let f = new FilterConsumerBridgeToKnexRequest(this.nameProvider);
|
|
188
|
+
f._addWhere = false;
|
|
189
|
+
element.__applyToConsumer(f);
|
|
190
|
+
let where = await f.resolveWhere();
|
|
191
|
+
if (where.length > 0) {
|
|
192
|
+
result.push(b => {
|
|
193
|
+
b.orWhere(b => {
|
|
194
|
+
where.forEach(x => x(b));
|
|
195
|
+
});
|
|
196
|
+
});
|
|
442
197
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
this.result.push(
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
|
|
198
|
+
else
|
|
199
|
+
return; //empty or means all rows
|
|
200
|
+
}
|
|
201
|
+
if (result.length > 0) {
|
|
202
|
+
this.result.push(b => b.where(x => result.find(y => y(x))));
|
|
203
|
+
}
|
|
204
|
+
})());
|
|
205
|
+
}
|
|
206
|
+
isNull(col) {
|
|
207
|
+
this.result.push(b => b.whereNull(this.nameProvider.nameOf(col)));
|
|
208
|
+
}
|
|
209
|
+
isNotNull(col) {
|
|
210
|
+
this.result.push(b => b.whereNotNull(this.nameProvider.nameOf(col)));
|
|
211
|
+
}
|
|
212
|
+
isIn(col, val) {
|
|
213
|
+
this.result.push(knex => knex.whereIn(this.nameProvider.nameOf(col), val.map(x => col.valueConverter.toDb(x))));
|
|
214
|
+
}
|
|
215
|
+
isEqualTo(col, val) {
|
|
461
216
|
this.add(col, val, "=");
|
|
462
|
-
}
|
|
463
|
-
|
|
217
|
+
}
|
|
218
|
+
isDifferentFrom(col, val) {
|
|
464
219
|
this.add(col, val, "<>");
|
|
465
|
-
}
|
|
466
|
-
|
|
220
|
+
}
|
|
221
|
+
isGreaterOrEqualTo(col, val) {
|
|
467
222
|
this.add(col, val, ">=");
|
|
468
|
-
}
|
|
469
|
-
|
|
223
|
+
}
|
|
224
|
+
isGreaterThan(col, val) {
|
|
470
225
|
this.add(col, val, ">");
|
|
471
|
-
}
|
|
472
|
-
|
|
226
|
+
}
|
|
227
|
+
isLessOrEqualTo(col, val) {
|
|
473
228
|
this.add(col, val, "<=");
|
|
474
|
-
}
|
|
475
|
-
|
|
229
|
+
}
|
|
230
|
+
isLessThan(col, val) {
|
|
476
231
|
this.add(col, val, "<");
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
this.
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
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) {
|
|
232
|
+
}
|
|
233
|
+
containsCaseInsensitive(col, val) {
|
|
234
|
+
this.result.push(b => b.whereRaw('lower (' + this.nameProvider.nameOf(col) + ") like lower ('%" + val.replace(/'/g, '\'\'') + "%')"));
|
|
235
|
+
this.promises.push((async () => {
|
|
236
|
+
})());
|
|
237
|
+
}
|
|
238
|
+
add(col, val, operator) {
|
|
239
|
+
this.result.push(b => b.where(this.nameProvider.nameOf(col), operator, col.valueConverter.toDb(val)));
|
|
240
|
+
}
|
|
241
|
+
databaseCustom(databaseCustom) {
|
|
242
|
+
this.promises.push((async () => {
|
|
243
|
+
if (databaseCustom?.buildKnex) {
|
|
244
|
+
this.result.push(await databaseCustom.buildKnex());
|
|
245
|
+
}
|
|
246
|
+
})());
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
export class KnexSchemaBuilder {
|
|
250
|
+
constructor(knex) {
|
|
514
251
|
this.knex = knex;
|
|
515
252
|
this.additionalWhere = '';
|
|
516
253
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
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*/];
|
|
254
|
+
async verifyStructureOfAllEntities(remult) {
|
|
255
|
+
console.log("start verify structure");
|
|
256
|
+
for (const entity of allEntities) {
|
|
257
|
+
let metadata = remult.repo(entity).metadata;
|
|
258
|
+
try {
|
|
259
|
+
if (!metadata.options.sqlExpression) {
|
|
260
|
+
if ((await metadata.getDbName()).toLowerCase().indexOf('from ') < 0) {
|
|
261
|
+
await this.createIfNotExist(metadata);
|
|
262
|
+
await this.verifyAllColumns(metadata);
|
|
263
|
+
}
|
|
572
264
|
}
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
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; }
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
console.log("failed verify structure of " + await metadata.getDbName() + " ", err);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async createIfNotExist(entity) {
|
|
272
|
+
const e = await getDbNameProvider(entity);
|
|
273
|
+
if (!await this.knex.schema.hasTable(e.entityName)) {
|
|
274
|
+
let cols = new Map();
|
|
275
|
+
for (const f of entity.fields) {
|
|
276
|
+
cols.set(f, {
|
|
277
|
+
name: e.nameOf(f),
|
|
278
|
+
readonly: e.isDbReadonly(f)
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
await logSql(this.knex.schema.createTable(e.entityName, b => {
|
|
282
|
+
for (const x of entity.fields) {
|
|
283
|
+
if (!cols.get(x).readonly || isAutoIncrement(x)) {
|
|
284
|
+
if (isAutoIncrement(x))
|
|
285
|
+
b.increments(cols.get(x).name);
|
|
286
|
+
else {
|
|
287
|
+
buildColumn(x, cols.get(x).name, b);
|
|
288
|
+
if (x == entity.idMetadata.field)
|
|
289
|
+
b.primary([cols.get(x).name]);
|
|
604
290
|
}
|
|
605
|
-
|
|
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*/];
|
|
633
|
-
}
|
|
634
|
-
});
|
|
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*/];
|
|
291
|
+
}
|
|
659
292
|
}
|
|
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);
|
|
660
305
|
});
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
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*/];
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
async verifyAllColumns(entity) {
|
|
309
|
+
let e = await getDbNameProvider(entity);
|
|
310
|
+
try {
|
|
311
|
+
for (const col of entity.fields) {
|
|
312
|
+
if (!e.isDbReadonly(col)) {
|
|
313
|
+
await this.addColumnIfNotExist(entity, () => col);
|
|
721
314
|
}
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
console.log(err);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
export function buildColumn(x, dbName, b) {
|
|
729
323
|
if (x.valueType == Number) {
|
|
730
324
|
if (!x.valueConverter.fieldTypeInDb) {
|
|
731
|
-
|
|
325
|
+
let c = b.decimal(dbName);
|
|
732
326
|
if (!x.allowNull) {
|
|
733
327
|
c.defaultTo(0).notNullable();
|
|
734
328
|
}
|
|
735
329
|
}
|
|
736
330
|
else if (x.valueConverter.fieldTypeInDb == "integer") {
|
|
737
|
-
|
|
331
|
+
let c = b.integer(dbName);
|
|
738
332
|
if (!x.allowNull) {
|
|
739
333
|
c.defaultTo(0).notNullable();
|
|
740
334
|
}
|
|
@@ -744,7 +338,7 @@ function buildColumn(x, dbName, b) {
|
|
|
744
338
|
}
|
|
745
339
|
else if (x.valueType == Date) {
|
|
746
340
|
if (!x.valueConverter.fieldTypeInDb)
|
|
747
|
-
if (x.valueConverter ==
|
|
341
|
+
if (x.valueConverter == ValueConverters.DateOnly)
|
|
748
342
|
b.date(dbName);
|
|
749
343
|
else
|
|
750
344
|
b.dateTime(dbName);
|
|
@@ -754,13 +348,13 @@ function buildColumn(x, dbName, b) {
|
|
|
754
348
|
b.specificType(dbName, x.valueConverter.fieldTypeInDb);
|
|
755
349
|
}
|
|
756
350
|
else if (x.valueType == Boolean) {
|
|
757
|
-
|
|
351
|
+
let c = b.boolean(dbName);
|
|
758
352
|
if (!x.allowNull)
|
|
759
353
|
c.defaultTo(false).notNullable();
|
|
760
354
|
}
|
|
761
355
|
else if (x.valueConverter.fieldTypeInDb) {
|
|
762
356
|
if (x.valueConverter.fieldTypeInDb == "integer") {
|
|
763
|
-
|
|
357
|
+
let c = b.integer(dbName);
|
|
764
358
|
if (!x.allowNull) {
|
|
765
359
|
c.defaultTo(0).notNullable();
|
|
766
360
|
}
|
|
@@ -769,33 +363,19 @@ function buildColumn(x, dbName, b) {
|
|
|
769
363
|
b.specificType(dbName, x.valueConverter.fieldTypeInDb);
|
|
770
364
|
}
|
|
771
365
|
else {
|
|
772
|
-
|
|
366
|
+
let c = b.string(dbName, x.options.maxLength);
|
|
773
367
|
if (!x.allowNull)
|
|
774
368
|
c.defaultTo('').notNullable();
|
|
775
369
|
}
|
|
776
370
|
}
|
|
777
|
-
exports.buildColumn = buildColumn;
|
|
778
371
|
function logSql(who) {
|
|
779
372
|
//console.log(who.toSQL().sql);
|
|
780
373
|
return who;
|
|
781
374
|
}
|
|
782
|
-
function createKnexDataProvider(config, autoCreateTables) {
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
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 __1.Remult(result))];
|
|
793
|
-
case 1:
|
|
794
|
-
_a.sent();
|
|
795
|
-
_a.label = 2;
|
|
796
|
-
case 2: return [2 /*return*/, result];
|
|
797
|
-
}
|
|
798
|
-
});
|
|
799
|
-
});
|
|
375
|
+
export async function createKnexDataProvider(config, autoCreateTables = true) {
|
|
376
|
+
let k = knex(config);
|
|
377
|
+
let result = new KnexDataProvider(k);
|
|
378
|
+
if (autoCreateTables)
|
|
379
|
+
await new KnexSchemaBuilder(k).verifyStructureOfAllEntities(new Remult(result));
|
|
380
|
+
return result;
|
|
800
381
|
}
|
|
801
|
-
exports.createKnexDataProvider = createKnexDataProvider;
|