remult 0.14.2 → 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.d.ts +13 -0
- package/index.js +25 -55
- package/inputTypes.js +9 -16
- package/package.json +6 -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
|
@@ -1,393 +1,194 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var filter_consumer_bridge_to_sql_request_1 = require("../src/filter/filter-consumer-bridge-to-sql-request");
|
|
10
|
-
var remult3_1 = require("../src/remult3");
|
|
11
|
-
var PostgresDataProvider = /** @class */ (function () {
|
|
12
|
-
function PostgresDataProvider(pool) {
|
|
1
|
+
import { SqlDatabase } from '../';
|
|
2
|
+
import { Pool } from 'pg';
|
|
3
|
+
import { allEntities, Remult } from '../src/context';
|
|
4
|
+
import { postgresColumnSyntax } from './postgresColumnSyntax';
|
|
5
|
+
import { getDbNameProvider } from '../src/filter/filter-consumer-bridge-to-sql-request';
|
|
6
|
+
import { isAutoIncrement } from '../src/remult3';
|
|
7
|
+
export class PostgresDataProvider {
|
|
8
|
+
constructor(pool) {
|
|
13
9
|
this.pool = pool;
|
|
14
10
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return [2 /*return*/];
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
PostgresDataProvider.prototype.getLimitSqlSyntax = function (limit, offset) {
|
|
11
|
+
async entityIsUsedForTheFirstTime(entity) {
|
|
12
|
+
}
|
|
13
|
+
getLimitSqlSyntax(limit, offset) {
|
|
23
14
|
return ' limit ' + limit + ' offset ' + offset;
|
|
24
|
-
}
|
|
25
|
-
|
|
15
|
+
}
|
|
16
|
+
createCommand() {
|
|
26
17
|
return new PostgresBridgeToSQLCommand(this.pool);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
case 2:
|
|
38
|
-
_a.trys.push([2, 6, 8, 10]);
|
|
39
|
-
return [4 /*yield*/, client.query('BEGIN')];
|
|
40
|
-
case 3:
|
|
41
|
-
_a.sent();
|
|
42
|
-
return [4 /*yield*/, action({
|
|
43
|
-
createCommand: function () { return new PostgresBridgeToSQLCommand(client); },
|
|
44
|
-
entityIsUsedForTheFirstTime: this.entityIsUsedForTheFirstTime,
|
|
45
|
-
transaction: function () { throw "nested transactions not allowed"; },
|
|
46
|
-
getLimitSqlSyntax: this.getLimitSqlSyntax
|
|
47
|
-
})];
|
|
48
|
-
case 4:
|
|
49
|
-
_a.sent();
|
|
50
|
-
return [4 /*yield*/, client.query('COMMIT')];
|
|
51
|
-
case 5:
|
|
52
|
-
_a.sent();
|
|
53
|
-
return [3 /*break*/, 10];
|
|
54
|
-
case 6:
|
|
55
|
-
err_1 = _a.sent();
|
|
56
|
-
return [4 /*yield*/, client.query('ROLLBACK')];
|
|
57
|
-
case 7:
|
|
58
|
-
_a.sent();
|
|
59
|
-
throw err_1;
|
|
60
|
-
case 8: return [4 /*yield*/, client.release()];
|
|
61
|
-
case 9:
|
|
62
|
-
_a.sent();
|
|
63
|
-
return [7 /*endfinally*/];
|
|
64
|
-
case 10: return [2 /*return*/];
|
|
65
|
-
}
|
|
18
|
+
}
|
|
19
|
+
async transaction(action) {
|
|
20
|
+
let client = await this.pool.connect();
|
|
21
|
+
try {
|
|
22
|
+
await client.query('BEGIN');
|
|
23
|
+
await action({
|
|
24
|
+
createCommand: () => new PostgresBridgeToSQLCommand(client),
|
|
25
|
+
entityIsUsedForTheFirstTime: this.entityIsUsedForTheFirstTime,
|
|
26
|
+
transaction: () => { throw "nested transactions not allowed"; },
|
|
27
|
+
getLimitSqlSyntax: this.getLimitSqlSyntax
|
|
66
28
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
29
|
+
await client.query('COMMIT');
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
await client.query('ROLLBACK');
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
await client.release();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
class PostgresBridgeToSQLCommand {
|
|
41
|
+
constructor(source) {
|
|
74
42
|
this.source = source;
|
|
75
43
|
this.values = [];
|
|
76
44
|
}
|
|
77
|
-
|
|
45
|
+
addParameterAndReturnSqlToken(val) {
|
|
78
46
|
this.values.push(val);
|
|
79
47
|
return '$' + this.values.length;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return this.source.query(sql, this.values).then(
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
function PostgresBridgeToSQLQueryResult(r) {
|
|
48
|
+
}
|
|
49
|
+
execute(sql) {
|
|
50
|
+
return this.source.query(sql, this.values).then(r => new PostgresBridgeToSQLQueryResult(r));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class PostgresBridgeToSQLQueryResult {
|
|
54
|
+
constructor(r) {
|
|
88
55
|
this.r = r;
|
|
89
56
|
this.rows = r.rows;
|
|
90
57
|
}
|
|
91
|
-
|
|
58
|
+
getColumnKeyInResultForIndexInSelect(index) {
|
|
92
59
|
return this.r.fields[index].name;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
98
|
-
return tslib_1.__generator(this, function (_a) {
|
|
99
|
-
switch (_a.label) {
|
|
100
|
-
case 0: return [4 /*yield*/, new PostgresSchemaBuilder(db).verifyStructureOfAllEntities(remult)];
|
|
101
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export async function verifyStructureOfAllEntities(db, remult) {
|
|
63
|
+
return await new PostgresSchemaBuilder(db).verifyStructureOfAllEntities(remult);
|
|
105
64
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
function PostgresSchemaBuilder(pool, schema) {
|
|
65
|
+
export class PostgresSchemaBuilder {
|
|
66
|
+
constructor(pool, schema) {
|
|
109
67
|
this.pool = pool;
|
|
110
68
|
this.additionalWhere = '';
|
|
111
69
|
if (schema) {
|
|
112
70
|
this.additionalWhere = ' and table_schema=\'' + schema + '\'';
|
|
113
71
|
}
|
|
114
72
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
allEntities_1 = tslib_1.__values(context_1.allEntities), allEntities_1_1 = allEntities_1.next();
|
|
127
|
-
_b.label = 2;
|
|
128
|
-
case 2:
|
|
129
|
-
if (!!allEntities_1_1.done) return [3 /*break*/, 11];
|
|
130
|
-
entityClass = allEntities_1_1.value;
|
|
131
|
-
entity = remult.repo(entityClass).metadata;
|
|
132
|
-
return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(entity)];
|
|
133
|
-
case 3:
|
|
134
|
-
e = _b.sent();
|
|
135
|
-
_b.label = 4;
|
|
136
|
-
case 4:
|
|
137
|
-
_b.trys.push([4, 9, , 10]);
|
|
138
|
-
if (!!entity.options.sqlExpression) return [3 /*break*/, 8];
|
|
139
|
-
return [4 /*yield*/, e.entityName];
|
|
140
|
-
case 5:
|
|
141
|
-
if (!((_b.sent()).toLowerCase().indexOf('from ') < 0)) return [3 /*break*/, 8];
|
|
142
|
-
return [4 /*yield*/, this.createIfNotExist(entity)];
|
|
143
|
-
case 6:
|
|
144
|
-
_b.sent();
|
|
145
|
-
return [4 /*yield*/, this.verifyAllColumns(entity)];
|
|
146
|
-
case 7:
|
|
147
|
-
_b.sent();
|
|
148
|
-
_b.label = 8;
|
|
149
|
-
case 8: return [3 /*break*/, 10];
|
|
150
|
-
case 9:
|
|
151
|
-
err_2 = _b.sent();
|
|
152
|
-
console.log("failed verify structure of " + e.entityName + " ", err_2);
|
|
153
|
-
return [3 /*break*/, 10];
|
|
154
|
-
case 10:
|
|
155
|
-
allEntities_1_1 = allEntities_1.next();
|
|
156
|
-
return [3 /*break*/, 2];
|
|
157
|
-
case 11: return [3 /*break*/, 14];
|
|
158
|
-
case 12:
|
|
159
|
-
e_1_1 = _b.sent();
|
|
160
|
-
e_1 = { error: e_1_1 };
|
|
161
|
-
return [3 /*break*/, 14];
|
|
162
|
-
case 13:
|
|
163
|
-
try {
|
|
164
|
-
if (allEntities_1_1 && !allEntities_1_1.done && (_a = allEntities_1.return)) _a.call(allEntities_1);
|
|
165
|
-
}
|
|
166
|
-
finally { if (e_1) throw e_1.error; }
|
|
167
|
-
return [7 /*endfinally*/];
|
|
168
|
-
case 14: return [2 /*return*/];
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
};
|
|
173
|
-
PostgresSchemaBuilder.prototype.createIfNotExist = function (entity) {
|
|
174
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
175
|
-
var c, e;
|
|
176
|
-
var _this = this;
|
|
177
|
-
return tslib_1.__generator(this, function (_a) {
|
|
178
|
-
switch (_a.label) {
|
|
179
|
-
case 0:
|
|
180
|
-
c = this.pool.createCommand();
|
|
181
|
-
return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(entity)];
|
|
182
|
-
case 1:
|
|
183
|
-
e = _a.sent();
|
|
184
|
-
return [4 /*yield*/, c.execute("select 1 from information_Schema.tables where table_name=" + c.addParameterAndReturnSqlToken((e.entityName).toLowerCase()) + this.additionalWhere).then(function (r) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
185
|
-
var result, _a, _b, x, sql;
|
|
186
|
-
var e_2, _c;
|
|
187
|
-
return tslib_1.__generator(this, function (_d) {
|
|
188
|
-
switch (_d.label) {
|
|
189
|
-
case 0:
|
|
190
|
-
if (!(r.rows.length == 0)) return [3 /*break*/, 2];
|
|
191
|
-
result = '';
|
|
192
|
-
try {
|
|
193
|
-
for (_a = tslib_1.__values(entity.fields), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
194
|
-
x = _b.value;
|
|
195
|
-
if (!e.isDbReadonly(x) || remult3_1.isAutoIncrement(x)) {
|
|
196
|
-
if (result.length != 0)
|
|
197
|
-
result += ',';
|
|
198
|
-
result += '\r\n ';
|
|
199
|
-
if (remult3_1.isAutoIncrement(x))
|
|
200
|
-
result += e.nameOf(x) + ' serial';
|
|
201
|
-
else {
|
|
202
|
-
result += postgresColumnSyntax_1.postgresColumnSyntax(x, e.nameOf(x));
|
|
203
|
-
if (x == entity.idMetadata.field)
|
|
204
|
-
result += ' primary key';
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
210
|
-
finally {
|
|
211
|
-
try {
|
|
212
|
-
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
213
|
-
}
|
|
214
|
-
finally { if (e_2) throw e_2.error; }
|
|
215
|
-
}
|
|
216
|
-
sql = 'create table ' + e.entityName + ' (' + result + '\r\n)';
|
|
217
|
-
//console.log(sql);
|
|
218
|
-
return [4 /*yield*/, this.pool.execute(sql)];
|
|
219
|
-
case 1:
|
|
220
|
-
//console.log(sql);
|
|
221
|
-
_d.sent();
|
|
222
|
-
_d.label = 2;
|
|
223
|
-
case 2: return [2 /*return*/];
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}); })];
|
|
227
|
-
case 2:
|
|
228
|
-
_a.sent();
|
|
229
|
-
return [2 /*return*/];
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
};
|
|
234
|
-
PostgresSchemaBuilder.prototype.addColumnIfNotExist = function (entity, c) {
|
|
235
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
236
|
-
var e, cmd, colName, sql, err_3;
|
|
237
|
-
return tslib_1.__generator(this, function (_a) {
|
|
238
|
-
switch (_a.label) {
|
|
239
|
-
case 0: return [4 /*yield*/, filter_consumer_bridge_to_sql_request_1.getDbNameProvider(entity)];
|
|
240
|
-
case 1:
|
|
241
|
-
e = _a.sent();
|
|
242
|
-
if (e.isDbReadonly(c(entity)))
|
|
243
|
-
return [2 /*return*/];
|
|
244
|
-
_a.label = 2;
|
|
245
|
-
case 2:
|
|
246
|
-
_a.trys.push([2, 6, , 7]);
|
|
247
|
-
cmd = this.pool.createCommand();
|
|
248
|
-
colName = e.nameOf(c(entity));
|
|
249
|
-
return [4 /*yield*/, cmd.execute("select 1 \n FROM information_schema.columns \n WHERE table_name=" + cmd.addParameterAndReturnSqlToken((e.entityName).toLocaleLowerCase()) + " and column_name=" + cmd.addParameterAndReturnSqlToken((colName).toLocaleLowerCase()) + this.additionalWhere)];
|
|
250
|
-
case 3:
|
|
251
|
-
if (!((_a.sent()).rows.length == 0)) return [3 /*break*/, 5];
|
|
252
|
-
sql = "alter table " + e.entityName + " add column " + postgresColumnSyntax_1.postgresColumnSyntax(c(entity), colName);
|
|
253
|
-
//console.log(sql);
|
|
254
|
-
return [4 /*yield*/, this.pool.execute(sql)];
|
|
255
|
-
case 4:
|
|
256
|
-
//console.log(sql);
|
|
257
|
-
_a.sent();
|
|
258
|
-
_a.label = 5;
|
|
259
|
-
case 5: return [3 /*break*/, 7];
|
|
260
|
-
case 6:
|
|
261
|
-
err_3 = _a.sent();
|
|
262
|
-
console.log(err_3);
|
|
263
|
-
return [3 /*break*/, 7];
|
|
264
|
-
case 7: return [2 /*return*/];
|
|
73
|
+
async verifyStructureOfAllEntities(remult) {
|
|
74
|
+
console.log("start verify structure");
|
|
75
|
+
for (const entityClass of allEntities) {
|
|
76
|
+
let entity = remult.repo(entityClass).metadata;
|
|
77
|
+
let e = await getDbNameProvider(entity);
|
|
78
|
+
try {
|
|
79
|
+
if (!entity.options.sqlExpression) {
|
|
80
|
+
if ((await e.entityName).toLowerCase().indexOf('from ') < 0) {
|
|
81
|
+
await this.createIfNotExist(entity);
|
|
82
|
+
await this.verifyAllColumns(entity);
|
|
83
|
+
}
|
|
265
84
|
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
case 4:
|
|
290
|
-
if (!!_b.done) return [3 /*break*/, 7];
|
|
291
|
-
col = _b.value;
|
|
292
|
-
if (!!e.isDbReadonly(col)) return [3 /*break*/, 6];
|
|
293
|
-
if (!!cols.includes(e.nameOf(col).toLocaleLowerCase())) return [3 /*break*/, 6];
|
|
294
|
-
sql = "alter table " + e.entityName + " add column " + postgresColumnSyntax_1.postgresColumnSyntax(col, e.nameOf(col));
|
|
295
|
-
//console.log(sql);
|
|
296
|
-
return [4 /*yield*/, this.pool.execute(sql)];
|
|
297
|
-
case 5:
|
|
298
|
-
//console.log(sql);
|
|
299
|
-
_d.sent();
|
|
300
|
-
_d.label = 6;
|
|
301
|
-
case 6:
|
|
302
|
-
_b = _a.next();
|
|
303
|
-
return [3 /*break*/, 4];
|
|
304
|
-
case 7: return [3 /*break*/, 10];
|
|
305
|
-
case 8:
|
|
306
|
-
e_3_1 = _d.sent();
|
|
307
|
-
e_3 = { error: e_3_1 };
|
|
308
|
-
return [3 /*break*/, 10];
|
|
309
|
-
case 9:
|
|
310
|
-
try {
|
|
311
|
-
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
console.log("failed verify structure of " + e.entityName + " ", err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async createIfNotExist(entity) {
|
|
92
|
+
var c = this.pool.createCommand();
|
|
93
|
+
let e = await getDbNameProvider(entity);
|
|
94
|
+
await c.execute("select 1 from information_Schema.tables where table_name=" + c.addParameterAndReturnSqlToken((e.entityName).toLowerCase()) + this.additionalWhere).then(async (r) => {
|
|
95
|
+
if (r.rows.length == 0) {
|
|
96
|
+
let result = '';
|
|
97
|
+
for (const x of entity.fields) {
|
|
98
|
+
if (!e.isDbReadonly(x) || isAutoIncrement(x)) {
|
|
99
|
+
if (result.length != 0)
|
|
100
|
+
result += ',';
|
|
101
|
+
result += '\r\n ';
|
|
102
|
+
if (isAutoIncrement(x))
|
|
103
|
+
result += e.nameOf(x) + ' serial';
|
|
104
|
+
else {
|
|
105
|
+
result += postgresColumnSyntax(x, e.nameOf(x));
|
|
106
|
+
if (x == entity.idMetadata.field)
|
|
107
|
+
result += ' primary key';
|
|
312
108
|
}
|
|
313
|
-
|
|
314
|
-
return [7 /*endfinally*/];
|
|
315
|
-
case 10: return [3 /*break*/, 12];
|
|
316
|
-
case 11:
|
|
317
|
-
err_4 = _d.sent();
|
|
318
|
-
console.log(err_4);
|
|
319
|
-
return [3 /*break*/, 12];
|
|
320
|
-
case 12: return [2 /*return*/];
|
|
109
|
+
}
|
|
321
110
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
return PostgresSchemaBuilder;
|
|
326
|
-
}());
|
|
327
|
-
exports.PostgresSchemaBuilder = PostgresSchemaBuilder;
|
|
328
|
-
function preparePostgresQueueStorage(sql) {
|
|
329
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
330
|
-
var c, JobsInQueueEntity, e;
|
|
331
|
-
return tslib_1.__generator(this, function (_a) {
|
|
332
|
-
switch (_a.label) {
|
|
333
|
-
case 0:
|
|
334
|
-
c = new context_1.Remult();
|
|
335
|
-
c.setDataProvider(sql);
|
|
336
|
-
return [4 /*yield*/, Promise.resolve().then(function () { return require('../server/expressBridge'); })];
|
|
337
|
-
case 1:
|
|
338
|
-
JobsInQueueEntity = (_a.sent()).JobsInQueueEntity;
|
|
339
|
-
e = c.repo(JobsInQueueEntity);
|
|
340
|
-
return [4 /*yield*/, new PostgresSchemaBuilder(sql).createIfNotExist(e.metadata)];
|
|
341
|
-
case 2:
|
|
342
|
-
_a.sent();
|
|
343
|
-
return [4 /*yield*/, new PostgresSchemaBuilder(sql).verifyAllColumns(e.metadata)];
|
|
344
|
-
case 3:
|
|
345
|
-
_a.sent();
|
|
346
|
-
return [4 /*yield*/, Promise.resolve().then(function () { return require('../server/expressBridge'); })];
|
|
347
|
-
case 4: return [2 /*return*/, new (_a.sent()).EntityQueueStorage(c.repo(JobsInQueueEntity))];
|
|
111
|
+
let sql = 'create table ' + e.entityName + ' (' + result + '\r\n)';
|
|
112
|
+
//console.log(sql);
|
|
113
|
+
await this.pool.execute(sql);
|
|
348
114
|
}
|
|
349
115
|
});
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
116
|
+
}
|
|
117
|
+
async addColumnIfNotExist(entity, c) {
|
|
118
|
+
let e = await getDbNameProvider(entity);
|
|
119
|
+
if (e.isDbReadonly(c(entity)))
|
|
120
|
+
return;
|
|
121
|
+
try {
|
|
122
|
+
let cmd = this.pool.createCommand();
|
|
123
|
+
const colName = e.nameOf(c(entity));
|
|
124
|
+
if ((await cmd.execute(`select 1
|
|
125
|
+
FROM information_schema.columns
|
|
126
|
+
WHERE table_name=${cmd.addParameterAndReturnSqlToken((e.entityName).toLocaleLowerCase())} and column_name=${cmd.addParameterAndReturnSqlToken((colName).toLocaleLowerCase())}` + this.additionalWhere)).rows.length == 0) {
|
|
127
|
+
let sql = `alter table ${e.entityName} add column ${postgresColumnSyntax(c(entity), colName)}`;
|
|
128
|
+
//console.log(sql);
|
|
129
|
+
await this.pool.execute(sql);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
console.log(err);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async verifyAllColumns(entity) {
|
|
137
|
+
try {
|
|
138
|
+
let cmd = this.pool.createCommand();
|
|
139
|
+
let e = await getDbNameProvider(entity);
|
|
140
|
+
let cols = (await cmd.execute(`select column_name
|
|
141
|
+
FROM information_schema.columns
|
|
142
|
+
WHERE table_name=${cmd.addParameterAndReturnSqlToken((e.entityName).toLocaleLowerCase())} ` + this.additionalWhere)).rows.map(x => x.column_name);
|
|
143
|
+
for (const col of entity.fields) {
|
|
144
|
+
if (!e.isDbReadonly(col))
|
|
145
|
+
if (!cols.includes(e.nameOf(col).toLocaleLowerCase())) {
|
|
146
|
+
let sql = `alter table ${e.entityName} add column ${postgresColumnSyntax(col, e.nameOf(col))}`;
|
|
147
|
+
//console.log(sql);
|
|
148
|
+
await this.pool.execute(sql);
|
|
379
149
|
}
|
|
380
|
-
db = new __1.SqlDatabase(new PostgresDataProvider(new pg_1.Pool(config)));
|
|
381
|
-
remult = new context_1.Remult();
|
|
382
|
-
remult.setDataProvider(db);
|
|
383
|
-
if (!(options.autoCreateTables === undefined || options.autoCreateTables)) return [3 /*break*/, 2];
|
|
384
|
-
return [4 /*yield*/, verifyStructureOfAllEntities(db, remult)];
|
|
385
|
-
case 1:
|
|
386
|
-
_a.sent();
|
|
387
|
-
_a.label = 2;
|
|
388
|
-
case 2: return [2 /*return*/, db];
|
|
389
150
|
}
|
|
390
|
-
}
|
|
391
|
-
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
console.log(err);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export async function preparePostgresQueueStorage(sql) {
|
|
158
|
+
let c = new Remult();
|
|
159
|
+
c.setDataProvider(sql);
|
|
160
|
+
let JobsInQueueEntity = (await import('../server/expressBridge')).JobsInQueueEntity;
|
|
161
|
+
let e = c.repo(JobsInQueueEntity);
|
|
162
|
+
await new PostgresSchemaBuilder(sql).createIfNotExist(e.metadata);
|
|
163
|
+
await new PostgresSchemaBuilder(sql).verifyAllColumns(e.metadata);
|
|
164
|
+
return new (await import('../server/expressBridge')).EntityQueueStorage(c.repo(JobsInQueueEntity));
|
|
165
|
+
}
|
|
166
|
+
export async function createPostgresConnection(options) {
|
|
167
|
+
if (!options)
|
|
168
|
+
options = {};
|
|
169
|
+
let config = {};
|
|
170
|
+
if (options.configuration)
|
|
171
|
+
if (options.configuration == "heroku") {
|
|
172
|
+
config = {
|
|
173
|
+
connectionString: process.env.DATABASE_URL,
|
|
174
|
+
ssl: process.env.NODE_ENV !== "production" && !options.sslInDev ? false : {
|
|
175
|
+
rejectUnauthorized: false
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
else
|
|
180
|
+
config = options.configuration;
|
|
181
|
+
else {
|
|
182
|
+
if (!options.connectionString)
|
|
183
|
+
options.connectionString = process.env.DATABASE_URL;
|
|
184
|
+
}
|
|
185
|
+
if (!config.connectionString && options.connectionString) {
|
|
186
|
+
config.connectionString = options.connectionString;
|
|
187
|
+
}
|
|
188
|
+
const db = new SqlDatabase(new PostgresDataProvider(new Pool(config)));
|
|
189
|
+
let remult = new Remult();
|
|
190
|
+
remult.setDataProvider(db);
|
|
191
|
+
if (options.autoCreateTables === undefined || options.autoCreateTables)
|
|
192
|
+
await verifyStructureOfAllEntities(db, remult);
|
|
193
|
+
return db;
|
|
392
194
|
}
|
|
393
|
-
exports.createPostgresConnection = createPostgresConnection;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var valueConverters_1 = require("../valueConverters");
|
|
5
|
-
function postgresColumnSyntax(x, dbName) {
|
|
6
|
-
var result = dbName;
|
|
1
|
+
import { ValueConverters } from '../valueConverters';
|
|
2
|
+
export function postgresColumnSyntax(x, dbName) {
|
|
3
|
+
let result = dbName;
|
|
7
4
|
if (x.valueType == Number) {
|
|
8
5
|
if (!x.valueConverter.fieldTypeInDb)
|
|
9
6
|
result += " numeric" + (x.allowNull ? "" : " default 0 not null");
|
|
@@ -12,7 +9,7 @@ function postgresColumnSyntax(x, dbName) {
|
|
|
12
9
|
}
|
|
13
10
|
else if (x.valueType == Date) {
|
|
14
11
|
if (!x.valueConverter.fieldTypeInDb)
|
|
15
|
-
if (x.valueConverter ==
|
|
12
|
+
if (x.valueConverter == ValueConverters.DateOnly)
|
|
16
13
|
result += " date";
|
|
17
14
|
else
|
|
18
15
|
result += " timestamp";
|
|
@@ -31,4 +28,3 @@ function postgresColumnSyntax(x, dbName) {
|
|
|
31
28
|
result += " varchar" + (x.allowNull ? "" : " default '' not null");
|
|
32
29
|
return result;
|
|
33
30
|
}
|
|
34
|
-
exports.postgresColumnSyntax = postgresColumnSyntax;
|
package/remult-express.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var expressBridge_1 = require("./server/expressBridge");
|
|
6
|
-
function remultExpress(options) {
|
|
7
|
-
var app = express.Router();
|
|
1
|
+
import * as express from 'express';
|
|
2
|
+
import { remultMiddlewareBase } from './server/expressBridge';
|
|
3
|
+
export function remultExpress(options) {
|
|
4
|
+
let app = express.Router();
|
|
8
5
|
if (!options) {
|
|
9
6
|
options = {};
|
|
10
7
|
}
|
|
11
8
|
if (options.bodySizeLimit === undefined) {
|
|
12
9
|
options.bodySizeLimit = '10mb';
|
|
13
10
|
}
|
|
14
|
-
if (
|
|
11
|
+
if (options?.bodyParser !== false) {
|
|
15
12
|
app.use(express.json({ limit: options.bodySizeLimit }));
|
|
16
13
|
app.use(express.urlencoded({ extended: true, limit: options.bodySizeLimit }));
|
|
17
14
|
}
|
|
18
|
-
return
|
|
15
|
+
return remultMiddlewareBase(app, options);
|
|
19
16
|
}
|
|
20
|
-
exports.remultExpress = remultExpress;
|