@restura/core 0.1.0-alpha.25 → 0.1.0-alpha.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +16 -8
- package/dist/index.d.ts +16 -8
- package/dist/index.js +56 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +56 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -269,7 +269,6 @@ import cookieParser from "cookie-parser";
|
|
|
269
269
|
import * as express from "express";
|
|
270
270
|
import fs4 from "fs";
|
|
271
271
|
import path4 from "path";
|
|
272
|
-
import pg3 from "pg";
|
|
273
272
|
import * as prettier3 from "prettier";
|
|
274
273
|
|
|
275
274
|
// src/restura/RsError.ts
|
|
@@ -428,10 +427,10 @@ var compareSchema = new CompareSchema();
|
|
|
428
427
|
var compareSchema_default = compareSchema;
|
|
429
428
|
|
|
430
429
|
// src/restura/customApiFactory.ts
|
|
431
|
-
import { fileUtils } from "@restura/internal";
|
|
432
430
|
import Bluebird2 from "bluebird";
|
|
433
431
|
import fs from "fs";
|
|
434
432
|
import path from "path";
|
|
433
|
+
import { FileUtils } from "@restura/internal";
|
|
435
434
|
var CustomApiFactory = class {
|
|
436
435
|
constructor() {
|
|
437
436
|
this.customApis = {};
|
|
@@ -440,7 +439,7 @@ var CustomApiFactory = class {
|
|
|
440
439
|
const apiVersions = ["v1"];
|
|
441
440
|
for (const apiVersion of apiVersions) {
|
|
442
441
|
const apiVersionFolderPath = path.join(baseFolderPath, apiVersion);
|
|
443
|
-
const directoryExists = await
|
|
442
|
+
const directoryExists = await FileUtils.existDir(apiVersionFolderPath);
|
|
444
443
|
if (!directoryExists) continue;
|
|
445
444
|
await this.addDirectory(apiVersionFolderPath, apiVersion);
|
|
446
445
|
}
|
|
@@ -926,7 +925,7 @@ function resturaGlobalTypesGenerator() {
|
|
|
926
925
|
return `/** Auto generated file. DO NOT MODIFY **/
|
|
927
926
|
/** This file contains types that may be used in the CustomTypes of Restura **/
|
|
928
927
|
/** For example export interface MyPagedQuery extends Restura.PageQuery { } **/
|
|
929
|
-
|
|
928
|
+
|
|
930
929
|
declare namespace Restura {
|
|
931
930
|
export type StandardOrderTypes = 'ASC' | 'DESC' | 'RAND' | 'NONE';
|
|
932
931
|
export interface PageQuery {
|
|
@@ -1283,7 +1282,7 @@ import { ObjectUtils as ObjectUtils2 } from "@redskytech/core-utils";
|
|
|
1283
1282
|
import jsonschema from "jsonschema";
|
|
1284
1283
|
import { z as z4 } from "zod";
|
|
1285
1284
|
|
|
1286
|
-
// src/restura/utils/
|
|
1285
|
+
// src/restura/utils/utils.ts
|
|
1287
1286
|
function addQuotesToStrings(variable) {
|
|
1288
1287
|
if (typeof variable === "string") {
|
|
1289
1288
|
return `'${variable}'`;
|
|
@@ -1294,6 +1293,17 @@ function addQuotesToStrings(variable) {
|
|
|
1294
1293
|
return variable;
|
|
1295
1294
|
}
|
|
1296
1295
|
}
|
|
1296
|
+
function sortObjectKeysAlphabetically(obj) {
|
|
1297
|
+
if (Array.isArray(obj)) {
|
|
1298
|
+
return obj.map(sortObjectKeysAlphabetically);
|
|
1299
|
+
} else if (obj !== null && typeof obj === "object") {
|
|
1300
|
+
return Object.keys(obj).sort().reduce((sorted, key) => {
|
|
1301
|
+
sorted[key] = sortObjectKeysAlphabetically(obj[key]);
|
|
1302
|
+
return sorted;
|
|
1303
|
+
}, {});
|
|
1304
|
+
}
|
|
1305
|
+
return obj;
|
|
1306
|
+
}
|
|
1297
1307
|
|
|
1298
1308
|
// src/restura/validators/requestValidator.ts
|
|
1299
1309
|
function requestValidator(req, routeData, validationSchema) {
|
|
@@ -1508,7 +1518,22 @@ function escapeColumnName(columnName) {
|
|
|
1508
1518
|
}
|
|
1509
1519
|
function questionMarksToOrderedParams(query) {
|
|
1510
1520
|
let count = 1;
|
|
1511
|
-
|
|
1521
|
+
let inSingleQuote = false;
|
|
1522
|
+
let inDoubleQuote = false;
|
|
1523
|
+
return query.replace(/('|"|\?)/g, (char) => {
|
|
1524
|
+
if (char === "'") {
|
|
1525
|
+
inSingleQuote = !inSingleQuote && !inDoubleQuote;
|
|
1526
|
+
return char;
|
|
1527
|
+
}
|
|
1528
|
+
if (char === '"') {
|
|
1529
|
+
inDoubleQuote = !inDoubleQuote && !inSingleQuote;
|
|
1530
|
+
return char;
|
|
1531
|
+
}
|
|
1532
|
+
if (char === "?" && !inSingleQuote && !inDoubleQuote) {
|
|
1533
|
+
return `$${count++}`;
|
|
1534
|
+
}
|
|
1535
|
+
return char;
|
|
1536
|
+
});
|
|
1512
1537
|
}
|
|
1513
1538
|
function insertObjectQuery(table, obj) {
|
|
1514
1539
|
const keys = Object.keys(obj);
|
|
@@ -1838,7 +1863,7 @@ var filterPsqlParser = peg.generate(filterSqlGrammar, {
|
|
|
1838
1863
|
var filterPsqlParser_default = filterPsqlParser;
|
|
1839
1864
|
|
|
1840
1865
|
// src/restura/sql/PsqlEngine.ts
|
|
1841
|
-
var { Client } = pg2;
|
|
1866
|
+
var { Client, types } = pg2;
|
|
1842
1867
|
var systemUser = {
|
|
1843
1868
|
role: "",
|
|
1844
1869
|
host: "",
|
|
@@ -1849,6 +1874,7 @@ var PsqlEngine = class extends SqlEngine {
|
|
|
1849
1874
|
constructor(psqlConnectionPool, shouldListenForDbTriggers = false) {
|
|
1850
1875
|
super();
|
|
1851
1876
|
this.psqlConnectionPool = psqlConnectionPool;
|
|
1877
|
+
this.setupPgReturnTypes();
|
|
1852
1878
|
if (shouldListenForDbTriggers) {
|
|
1853
1879
|
this.setupTriggerListeners = this.listenForDbTriggers();
|
|
1854
1880
|
}
|
|
@@ -1858,6 +1884,16 @@ var PsqlEngine = class extends SqlEngine {
|
|
|
1858
1884
|
await this.triggerClient.end();
|
|
1859
1885
|
}
|
|
1860
1886
|
}
|
|
1887
|
+
setupPgReturnTypes() {
|
|
1888
|
+
const TIMESTAMPTZ_OID = 1184;
|
|
1889
|
+
types.setTypeParser(TIMESTAMPTZ_OID, (val) => {
|
|
1890
|
+
return val === null ? null : new Date(val).toISOString();
|
|
1891
|
+
});
|
|
1892
|
+
const BIGINT_OID = 20;
|
|
1893
|
+
types.setTypeParser(BIGINT_OID, (val) => {
|
|
1894
|
+
return val === null ? null : Number(val);
|
|
1895
|
+
});
|
|
1896
|
+
}
|
|
1861
1897
|
async listenForDbTriggers() {
|
|
1862
1898
|
this.triggerClient = new Client({
|
|
1863
1899
|
user: this.psqlConnectionPool.poolConfig.user,
|
|
@@ -1911,7 +1947,7 @@ var PsqlEngine = class extends SqlEngine {
|
|
|
1911
1947
|
const tableColumns = [];
|
|
1912
1948
|
for (const column of table.columns) {
|
|
1913
1949
|
let columnSql = "";
|
|
1914
|
-
columnSql += ` "${column.name}" ${schemaToPsqlType(column)}`;
|
|
1950
|
+
columnSql += ` "${column.name}" ${this.schemaToPsqlType(column)}`;
|
|
1915
1951
|
let value = column.value;
|
|
1916
1952
|
if (column.type === "JSON") value = "";
|
|
1917
1953
|
if (column.type === "JSONB") value = "";
|
|
@@ -2380,10 +2416,14 @@ CREATE TRIGGER "${tableName}_insert"
|
|
|
2380
2416
|
EXECUTE FUNCTION notify_${tableName}_insert();
|
|
2381
2417
|
`;
|
|
2382
2418
|
}
|
|
2419
|
+
schemaToPsqlType(column) {
|
|
2420
|
+
if (column.hasAutoIncrement) return "BIGSERIAL";
|
|
2421
|
+
if (column.type === "ENUM") return `TEXT`;
|
|
2422
|
+
if (column.type === "DATETIME") return "TIMESTAMPTZ";
|
|
2423
|
+
if (column.type === "MEDIUMINT") return "INT";
|
|
2424
|
+
return column.type;
|
|
2425
|
+
}
|
|
2383
2426
|
};
|
|
2384
|
-
__decorateClass([
|
|
2385
|
-
boundMethod
|
|
2386
|
-
], PsqlEngine.prototype, "handleTrigger", 1);
|
|
2387
2427
|
__decorateClass([
|
|
2388
2428
|
boundMethod
|
|
2389
2429
|
], PsqlEngine.prototype, "createUpdateTrigger", 1);
|
|
@@ -2393,26 +2433,19 @@ __decorateClass([
|
|
|
2393
2433
|
__decorateClass([
|
|
2394
2434
|
boundMethod
|
|
2395
2435
|
], PsqlEngine.prototype, "createInsertTriggers", 1);
|
|
2396
|
-
function schemaToPsqlType(column) {
|
|
2397
|
-
if (column.hasAutoIncrement) return "BIGSERIAL";
|
|
2398
|
-
if (column.type === "ENUM") return `TEXT`;
|
|
2399
|
-
if (column.type === "DATETIME") return "TIMESTAMPTZ";
|
|
2400
|
-
if (column.type === "MEDIUMINT") return "INT";
|
|
2401
|
-
return column.type;
|
|
2402
|
-
}
|
|
2403
2436
|
|
|
2404
2437
|
// src/restura/utils/TempCache.ts
|
|
2405
2438
|
import fs3 from "fs";
|
|
2406
2439
|
import path3 from "path";
|
|
2407
2440
|
import { DateUtils } from "@redskytech/core-utils";
|
|
2441
|
+
import { FileUtils as FileUtils2 } from "@restura/internal";
|
|
2408
2442
|
import Bluebird3 from "bluebird";
|
|
2409
2443
|
import * as os2 from "os";
|
|
2410
|
-
import { fileUtils as fileUtils2 } from "@restura/internal";
|
|
2411
2444
|
var TempCache = class {
|
|
2412
2445
|
constructor(location) {
|
|
2413
2446
|
this.maxDurationDays = 7;
|
|
2414
2447
|
this.location = location || os2.tmpdir();
|
|
2415
|
-
|
|
2448
|
+
FileUtils2.ensureDir(this.location).catch((e) => {
|
|
2416
2449
|
throw e;
|
|
2417
2450
|
});
|
|
2418
2451
|
}
|
|
@@ -2434,7 +2467,6 @@ var TempCache = class {
|
|
|
2434
2467
|
};
|
|
2435
2468
|
|
|
2436
2469
|
// src/restura/restura.ts
|
|
2437
|
-
var { types } = pg3;
|
|
2438
2470
|
var ResturaEngine = class {
|
|
2439
2471
|
constructor() {
|
|
2440
2472
|
this.publicEndpoints = {
|
|
@@ -2457,7 +2489,6 @@ var ResturaEngine = class {
|
|
|
2457
2489
|
new TempCache(this.resturaConfig.fileTempCachePath);
|
|
2458
2490
|
this.psqlConnectionPool = psqlConnectionPool;
|
|
2459
2491
|
this.psqlEngine = new PsqlEngine(this.psqlConnectionPool, true);
|
|
2460
|
-
setupPgReturnTypes();
|
|
2461
2492
|
await customApiFactory_default.loadApiFiles(this.resturaConfig.customApiFolderPath);
|
|
2462
2493
|
this.authenticationHandler = authenticationHandler;
|
|
2463
2494
|
app.use(compression());
|
|
@@ -2608,7 +2639,7 @@ var ResturaEngine = class {
|
|
|
2608
2639
|
}
|
|
2609
2640
|
async updateSchema(req, res) {
|
|
2610
2641
|
try {
|
|
2611
|
-
this.schema = req.data;
|
|
2642
|
+
this.schema = sortObjectKeysAlphabetically(req.data);
|
|
2612
2643
|
await this.storeFileSystemSchema();
|
|
2613
2644
|
await this.reloadEndpoints();
|
|
2614
2645
|
await this.updateTypes();
|
|
@@ -2770,22 +2801,11 @@ __decorateClass([
|
|
|
2770
2801
|
__decorateClass([
|
|
2771
2802
|
boundMethod
|
|
2772
2803
|
], ResturaEngine.prototype, "runCustomRouteLogic", 1);
|
|
2773
|
-
function setupPgReturnTypes() {
|
|
2774
|
-
const TIMESTAMPTZ_OID = 1184;
|
|
2775
|
-
types.setTypeParser(TIMESTAMPTZ_OID, (val) => {
|
|
2776
|
-
return val === null ? null : new Date(val).toISOString();
|
|
2777
|
-
});
|
|
2778
|
-
const BIGINT_OID = 20;
|
|
2779
|
-
types.setTypeParser(BIGINT_OID, (val) => {
|
|
2780
|
-
return val === null ? null : Number(val);
|
|
2781
|
-
});
|
|
2782
|
-
}
|
|
2783
|
-
setupPgReturnTypes();
|
|
2784
2804
|
var restura = new ResturaEngine();
|
|
2785
2805
|
|
|
2786
2806
|
// src/restura/sql/PsqlTransaction.ts
|
|
2787
|
-
import
|
|
2788
|
-
var { Client: Client2 } =
|
|
2807
|
+
import pg3 from "pg";
|
|
2808
|
+
var { Client: Client2 } = pg3;
|
|
2789
2809
|
var PsqlTransaction = class extends PsqlConnection {
|
|
2790
2810
|
constructor(clientConfig) {
|
|
2791
2811
|
super();
|