remult 0.20.3-exp.0 → 0.20.3-exp.2
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/esm/graphql.js +2 -0
- package/esm/postgres/schema-builder.js +7 -3
- package/esm/server/expressBridge.js +30 -22
- package/graphql.js +2 -0
- package/package.json +1 -1
- package/postgres/getEntityTypescriptPostgres.d.ts +1 -0
- package/postgres/getEntityTypescriptPostgres.js +125 -0
- package/postgres/schema-builder.js +6 -3
- package/server/expressBridge.js +31 -22
package/esm/graphql.js
CHANGED
|
@@ -89,6 +89,8 @@ export function remultGraphql(api) {
|
|
|
89
89
|
root[key] = async (arg1, req, a) => {
|
|
90
90
|
const { options, filter } = arg1;
|
|
91
91
|
return new Promise(async (res, error) => {
|
|
92
|
+
if (req.req)
|
|
93
|
+
req = req.req; //TODO - yoga sends its own request object - and in it you get the original request (need to test with svelte and next)
|
|
92
94
|
server.run(req, async () => {
|
|
93
95
|
let dApi = await server.getDataApi(req, meta);
|
|
94
96
|
let result;
|
|
@@ -128,15 +128,19 @@ export class PostgresSchemaBuilder {
|
|
|
128
128
|
let e = await dbNamesOf(entity);
|
|
129
129
|
let cols = (await cmd.execute(`select column_name
|
|
130
130
|
FROM information_schema.columns
|
|
131
|
-
WHERE table_name=${cmd.addParameterAndReturnSqlToken((e.$entityName).toLocaleLowerCase())} ` + this.additionalWhere)).rows.map(x => x.column_name);
|
|
131
|
+
WHERE table_name=${cmd.addParameterAndReturnSqlToken((e.$entityName).toLocaleLowerCase())} ` + this.additionalWhere)).rows.map(x => x.column_name.toLocaleLowerCase());
|
|
132
132
|
for (const col of entity.fields) {
|
|
133
|
-
if (!isDbReadonly(col, e))
|
|
134
|
-
|
|
133
|
+
if (!isDbReadonly(col, e)) {
|
|
134
|
+
let colName = e.$dbNameOf(col).toLocaleLowerCase();
|
|
135
|
+
if (colName.startsWith('"') && colName.endsWith('"'))
|
|
136
|
+
colName = colName.substring(1, colName.length - 1);
|
|
137
|
+
if (!cols.includes(colName)) {
|
|
135
138
|
let sql = `alter table ${e.$entityName} add column ${postgresColumnSyntax(col, e.$dbNameOf(col))}`;
|
|
136
139
|
if (PostgresSchemaBuilder.logToConsole)
|
|
137
140
|
console.info(sql);
|
|
138
141
|
await this.pool.execute(sql);
|
|
139
142
|
}
|
|
143
|
+
}
|
|
140
144
|
}
|
|
141
145
|
}
|
|
142
146
|
catch (err) {
|
|
@@ -416,37 +416,45 @@ export class RemultServerImplementation {
|
|
|
416
416
|
let key = meta.key;
|
|
417
417
|
let parameters = [];
|
|
418
418
|
if (key) {
|
|
419
|
+
let mutationKey = key;
|
|
419
420
|
let properties = {};
|
|
421
|
+
let mutationProperties = {};
|
|
420
422
|
for (const f of meta.fields) {
|
|
421
423
|
let type = f.valueType == String ? "string" :
|
|
422
424
|
f.valueType == Boolean ? "boolean" :
|
|
423
425
|
f.valueType == Date ? "string" :
|
|
424
426
|
f.valueType == Number ? "number" :
|
|
425
427
|
"object";
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
});
|
|
428
|
+
if (f.options.includeInApi !== false) {
|
|
429
|
+
properties[f.key] = {
|
|
430
|
+
type
|
|
431
|
+
};
|
|
432
|
+
if (f.options.allowApiUpdate !== false) {
|
|
433
|
+
mutationProperties[f.key] = {
|
|
434
|
+
type
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
parameters.push({
|
|
438
|
+
"name": f.key,
|
|
439
|
+
"in": "query",
|
|
440
|
+
"description": "filter equal to " + f.key + ". See [more filtering options](https://remult.dev/docs/rest-api.html#filter)",
|
|
441
|
+
"required": false,
|
|
442
|
+
"style": "simple",
|
|
443
|
+
type
|
|
444
|
+
});
|
|
445
|
+
}
|
|
445
446
|
}
|
|
446
447
|
spec.components.schemas[key] = {
|
|
447
448
|
type: "object",
|
|
448
449
|
properties
|
|
449
450
|
};
|
|
451
|
+
if (JSON.stringify(properties) !== JSON.stringify(mutationProperties)) {
|
|
452
|
+
mutationKey += 'Mutation';
|
|
453
|
+
spec.components.schemas[mutationKey] = {
|
|
454
|
+
type: "object",
|
|
455
|
+
properties: mutationProperties
|
|
456
|
+
};
|
|
457
|
+
}
|
|
450
458
|
let definition = {
|
|
451
459
|
"$ref": "#/components/schemas/" + key
|
|
452
460
|
};
|
|
@@ -456,8 +464,8 @@ export class RemultServerImplementation {
|
|
|
456
464
|
condition = meta.options.allowApiCrud;
|
|
457
465
|
return secureBase(condition, def, item);
|
|
458
466
|
};
|
|
459
|
-
let apiPath = spec.paths['/
|
|
460
|
-
let apiPathWithId = spec.paths['/
|
|
467
|
+
let apiPath = spec.paths[this.options.rootPath + '/' + key] = {};
|
|
468
|
+
let apiPathWithId = spec.paths[this.options.rootPath + '/' + key + "/{id}"] = {};
|
|
461
469
|
//https://github.com/2fd/open-api.d.ts
|
|
462
470
|
apiPath.get = secure(meta.options.allowApiRead, true, {
|
|
463
471
|
description: "return an array of " + key + ". supports filter operators. For more info on filtering [see this article](https://remult.dev/docs/rest-api.html#filter)",
|
|
@@ -516,7 +524,7 @@ export class RemultServerImplementation {
|
|
|
516
524
|
"content": {
|
|
517
525
|
"application/json": {
|
|
518
526
|
"schema": {
|
|
519
|
-
"$ref": "#/components/schemas/" +
|
|
527
|
+
"$ref": "#/components/schemas/" + mutationKey
|
|
520
528
|
}
|
|
521
529
|
}
|
|
522
530
|
}
|
package/graphql.js
CHANGED
|
@@ -159,6 +159,8 @@ function remultGraphql(api) {
|
|
|
159
159
|
return [2 /*return*/, new Promise(function (res, error) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
160
160
|
var _this = this;
|
|
161
161
|
return tslib_1.__generator(this, function (_a) {
|
|
162
|
+
if (req.req)
|
|
163
|
+
req = req.req; //TODO - yoga sends its own request object - and in it you get the original request (need to test with svelte and next)
|
|
162
164
|
server.run(req, function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
163
165
|
var dApi, result, err;
|
|
164
166
|
return tslib_1.__generator(this, function (_a) {
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getEntityTypescriptPostgres(table: string, schema?: string): Promise<string>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEntityTypescriptPostgres = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var remult_1 = require("remult");
|
|
6
|
+
function getEntityTypescriptPostgres(table, schema) {
|
|
7
|
+
if (schema === void 0) { schema = 'public'; }
|
|
8
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
9
|
+
var command, cols, props, first, _a, _b, _c, column_name, column_default, data_type, datetime_precision, character_maximum_length, decorator, decoratorArgs, type, defaultVal, e_1_1, r;
|
|
10
|
+
var e_1, _d;
|
|
11
|
+
return tslib_1.__generator(this, function (_e) {
|
|
12
|
+
switch (_e.label) {
|
|
13
|
+
case 0:
|
|
14
|
+
command = remult_1.SqlDatabase.getDb(remult_1.remult).createCommand();
|
|
15
|
+
cols = '';
|
|
16
|
+
props = [];
|
|
17
|
+
props.push('allowApiCrud: true');
|
|
18
|
+
if (table.toLocaleLowerCase() != table) {
|
|
19
|
+
// props.push("dbName: '\"" + table + "\"'");
|
|
20
|
+
}
|
|
21
|
+
first = undefined;
|
|
22
|
+
_e.label = 1;
|
|
23
|
+
case 1:
|
|
24
|
+
_e.trys.push([1, 6, 7, 8]);
|
|
25
|
+
return [4 /*yield*/, command.execute("select * from INFORMATION_SCHEMA.COLUMNS where table_schema=" + command.addParameterAndReturnSqlToken(schema) + " and table_name=" + command.addParameterAndReturnSqlToken(table) + "\n \n order by ordinal_position")];
|
|
26
|
+
case 2:
|
|
27
|
+
_a = tslib_1.__values.apply(void 0, [(_e.sent()).rows]), _b = _a.next();
|
|
28
|
+
_e.label = 3;
|
|
29
|
+
case 3:
|
|
30
|
+
if (!!_b.done) return [3 /*break*/, 5];
|
|
31
|
+
_c = _b.value, column_name = _c.column_name, column_default = _c.column_default, data_type = _c.data_type, datetime_precision = _c.datetime_precision, character_maximum_length = _c.character_maximum_length;
|
|
32
|
+
decorator = '@Fields.string';
|
|
33
|
+
decoratorArgs = '';
|
|
34
|
+
type = '';
|
|
35
|
+
defaultVal = "''";
|
|
36
|
+
switch (data_type) {
|
|
37
|
+
case 'decimal':
|
|
38
|
+
case 'real':
|
|
39
|
+
case 'int':
|
|
40
|
+
case 'smallint':
|
|
41
|
+
case 'tinyint':
|
|
42
|
+
case 'bigint':
|
|
43
|
+
case 'float':
|
|
44
|
+
case 'numeric':
|
|
45
|
+
case 'NUMBER':
|
|
46
|
+
case 'money':
|
|
47
|
+
if (datetime_precision === 0)
|
|
48
|
+
decorator = '@Fields.integer';
|
|
49
|
+
else
|
|
50
|
+
decorator = '@Fields.number';
|
|
51
|
+
defaultVal = '0';
|
|
52
|
+
break;
|
|
53
|
+
case 'nchar':
|
|
54
|
+
case 'nvarchar':
|
|
55
|
+
case 'ntext':
|
|
56
|
+
case 'NVARCHAR2':
|
|
57
|
+
case 'text':
|
|
58
|
+
case 'varchar':
|
|
59
|
+
case 'VARCHAR2':
|
|
60
|
+
break;
|
|
61
|
+
case 'char':
|
|
62
|
+
case 'CHAR':
|
|
63
|
+
console.log({
|
|
64
|
+
character_maximum_length: character_maximum_length,
|
|
65
|
+
column_default: column_default,
|
|
66
|
+
data_type: data_type,
|
|
67
|
+
column_name: column_name
|
|
68
|
+
});
|
|
69
|
+
if (character_maximum_length == 8 && column_default == "('00000000')") {
|
|
70
|
+
decorator = '@Fields.dateOnly';
|
|
71
|
+
type = 'Date';
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
case 'DATE':
|
|
75
|
+
case 'datetime':
|
|
76
|
+
case 'datetime2':
|
|
77
|
+
case 'timestamp without time zone':
|
|
78
|
+
decorator = '@Fields.date';
|
|
79
|
+
type = 'Date';
|
|
80
|
+
break;
|
|
81
|
+
case 'bit':
|
|
82
|
+
decorator = '@Fields.boolean';
|
|
83
|
+
break;
|
|
84
|
+
default:
|
|
85
|
+
console.log(data_type);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
if (column_name.toLocaleLowerCase() != column_name ||
|
|
89
|
+
column_name == 'order')
|
|
90
|
+
decoratorArgs = "{ dbName: '\"" + column_name + "\"' }";
|
|
91
|
+
if (!first)
|
|
92
|
+
first = column_name;
|
|
93
|
+
cols += '\n\n ' + decorator + ("(" + decoratorArgs + ")\n ") + column_name;
|
|
94
|
+
if (!defaultVal) {
|
|
95
|
+
cols += '!';
|
|
96
|
+
cols += ': ';
|
|
97
|
+
cols += type;
|
|
98
|
+
}
|
|
99
|
+
if (defaultVal)
|
|
100
|
+
cols += ' = ' + defaultVal;
|
|
101
|
+
_e.label = 4;
|
|
102
|
+
case 4:
|
|
103
|
+
_b = _a.next();
|
|
104
|
+
return [3 /*break*/, 3];
|
|
105
|
+
case 5: return [3 /*break*/, 8];
|
|
106
|
+
case 6:
|
|
107
|
+
e_1_1 = _e.sent();
|
|
108
|
+
e_1 = { error: e_1_1 };
|
|
109
|
+
return [3 /*break*/, 8];
|
|
110
|
+
case 7:
|
|
111
|
+
try {
|
|
112
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
113
|
+
}
|
|
114
|
+
finally { if (e_1) throw e_1.error; }
|
|
115
|
+
return [7 /*endfinally*/];
|
|
116
|
+
case 8:
|
|
117
|
+
r = "import { Entity, Fields, EntityBase } from \"remult\";\n@Entity<" + table + ">(\"" + table + "\", { \n " + props.join(',\n ') + " \n})\nexport class " + table + " extends EntityBase {" +
|
|
118
|
+
cols +
|
|
119
|
+
'\n}'.replace(' ', '');
|
|
120
|
+
return [2 /*return*/, r];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
exports.getEntityTypescriptPostgres = getEntityTypescriptPostgres;
|
|
@@ -257,7 +257,7 @@ var PostgresSchemaBuilder = /** @class */ (function () {
|
|
|
257
257
|
};
|
|
258
258
|
PostgresSchemaBuilder.prototype.verifyAllColumns = function (entity) {
|
|
259
259
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
260
|
-
var cmd, e, cols, _a, _b, col, sql, e_4_1, err_3;
|
|
260
|
+
var cmd, e, cols, _a, _b, col, colName, sql, e_4_1, err_3;
|
|
261
261
|
var e_4, _c;
|
|
262
262
|
return tslib_1.__generator(this, function (_d) {
|
|
263
263
|
switch (_d.label) {
|
|
@@ -269,7 +269,7 @@ var PostgresSchemaBuilder = /** @class */ (function () {
|
|
|
269
269
|
e = _d.sent();
|
|
270
270
|
return [4 /*yield*/, cmd.execute("select column_name \n FROM information_schema.columns \n WHERE table_name=" + cmd.addParameterAndReturnSqlToken((e.$entityName).toLocaleLowerCase()) + " " + this.additionalWhere)];
|
|
271
271
|
case 2:
|
|
272
|
-
cols = (_d.sent()).rows.map(function (x) { return x.column_name; });
|
|
272
|
+
cols = (_d.sent()).rows.map(function (x) { return x.column_name.toLocaleLowerCase(); });
|
|
273
273
|
_d.label = 3;
|
|
274
274
|
case 3:
|
|
275
275
|
_d.trys.push([3, 8, 9, 10]);
|
|
@@ -279,7 +279,10 @@ var PostgresSchemaBuilder = /** @class */ (function () {
|
|
|
279
279
|
if (!!_b.done) return [3 /*break*/, 7];
|
|
280
280
|
col = _b.value;
|
|
281
281
|
if (!!filter_consumer_bridge_to_sql_request_1.isDbReadonly(col, e)) return [3 /*break*/, 6];
|
|
282
|
-
|
|
282
|
+
colName = e.$dbNameOf(col).toLocaleLowerCase();
|
|
283
|
+
if (colName.startsWith('"') && colName.endsWith('"'))
|
|
284
|
+
colName = colName.substring(1, colName.length - 1);
|
|
285
|
+
if (!!cols.includes(colName)) return [3 /*break*/, 6];
|
|
283
286
|
sql = "alter table " + e.$entityName + " add column " + postgresColumnSyntax(col, e.$dbNameOf(col));
|
|
284
287
|
if (PostgresSchemaBuilder.logToConsole)
|
|
285
288
|
console.info(sql);
|
package/server/expressBridge.js
CHANGED
|
@@ -731,7 +731,9 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
731
731
|
var key = meta.key;
|
|
732
732
|
var parameters = [];
|
|
733
733
|
if (key) {
|
|
734
|
+
var mutationKey = key;
|
|
734
735
|
var properties = {};
|
|
736
|
+
var mutationProperties = {};
|
|
735
737
|
try {
|
|
736
738
|
for (var _b = (e_7 = void 0, tslib_1.__values(meta.fields)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
737
739
|
var f = _c.value;
|
|
@@ -740,25 +742,24 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
740
742
|
f.valueType == Date ? "string" :
|
|
741
743
|
f.valueType == Number ? "number" :
|
|
742
744
|
"object";
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
});
|
|
745
|
+
if (f.options.includeInApi !== false) {
|
|
746
|
+
properties[f.key] = {
|
|
747
|
+
type: type
|
|
748
|
+
};
|
|
749
|
+
if (f.options.allowApiUpdate !== false) {
|
|
750
|
+
mutationProperties[f.key] = {
|
|
751
|
+
type: type
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
parameters.push({
|
|
755
|
+
"name": f.key,
|
|
756
|
+
"in": "query",
|
|
757
|
+
"description": "filter equal to " + f.key + ". See [more filtering options](https://remult.dev/docs/rest-api.html#filter)",
|
|
758
|
+
"required": false,
|
|
759
|
+
"style": "simple",
|
|
760
|
+
type: type
|
|
761
|
+
});
|
|
762
|
+
}
|
|
762
763
|
}
|
|
763
764
|
}
|
|
764
765
|
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
@@ -772,6 +773,13 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
772
773
|
type: "object",
|
|
773
774
|
properties: properties
|
|
774
775
|
};
|
|
776
|
+
if (JSON.stringify(properties) !== JSON.stringify(mutationProperties)) {
|
|
777
|
+
mutationKey += 'Mutation';
|
|
778
|
+
spec.components.schemas[mutationKey] = {
|
|
779
|
+
type: "object",
|
|
780
|
+
properties: mutationProperties
|
|
781
|
+
};
|
|
782
|
+
}
|
|
775
783
|
var definition = {
|
|
776
784
|
"$ref": "#/components/schemas/" + key
|
|
777
785
|
};
|
|
@@ -781,8 +789,8 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
781
789
|
condition = meta.options.allowApiCrud;
|
|
782
790
|
return secureBase(condition, def, item);
|
|
783
791
|
};
|
|
784
|
-
var apiPath = spec.paths['/
|
|
785
|
-
var apiPathWithId = spec.paths['/
|
|
792
|
+
var apiPath = spec.paths[this_2.options.rootPath + '/' + key] = {};
|
|
793
|
+
var apiPathWithId = spec.paths[this_2.options.rootPath + '/' + key + "/{id}"] = {};
|
|
786
794
|
//https://github.com/2fd/open-api.d.ts
|
|
787
795
|
apiPath.get = secure(meta.options.allowApiRead, true, {
|
|
788
796
|
description: "return an array of " + key + ". supports filter operators. For more info on filtering [see this article](https://remult.dev/docs/rest-api.html#filter)",
|
|
@@ -841,7 +849,7 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
841
849
|
"content": {
|
|
842
850
|
"application/json": {
|
|
843
851
|
"schema": {
|
|
844
|
-
"$ref": "#/components/schemas/" +
|
|
852
|
+
"$ref": "#/components/schemas/" + mutationKey
|
|
845
853
|
}
|
|
846
854
|
}
|
|
847
855
|
}
|
|
@@ -903,6 +911,7 @@ var RemultServerImplementation = /** @class */ (function () {
|
|
|
903
911
|
});
|
|
904
912
|
}
|
|
905
913
|
};
|
|
914
|
+
var this_2 = this;
|
|
906
915
|
try {
|
|
907
916
|
for (var _c = tslib_1.__values(this.getEntities()), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
908
917
|
var meta = _d.value;
|