@restura/core 0.1.1 → 0.1.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/dist/index.d.mts +43 -42
- package/dist/index.d.ts +43 -42
- package/dist/index.js +29 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +26 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +20 -19
package/dist/index.mjs
CHANGED
|
@@ -934,7 +934,7 @@ function convertTable(table) {
|
|
|
934
934
|
let modelString = ` export interface ${StringUtils2.capitalizeFirst(table.name)} {
|
|
935
935
|
`;
|
|
936
936
|
for (const column of table.columns) {
|
|
937
|
-
modelString += ` ${column.name}
|
|
937
|
+
modelString += ` ${column.name}: ${SqlUtils.convertDatabaseTypeToTypescript(column.type, column.value)}${column.isNullable ? " | null" : ""};
|
|
938
938
|
`;
|
|
939
939
|
}
|
|
940
940
|
modelString += ` }
|
|
@@ -1538,6 +1538,7 @@ import pg from "pg";
|
|
|
1538
1538
|
// src/restura/sql/PsqlConnection.ts
|
|
1539
1539
|
import crypto from "crypto";
|
|
1540
1540
|
import format3 from "pg-format";
|
|
1541
|
+
import { format as sqlFormat } from "sql-formatter";
|
|
1541
1542
|
|
|
1542
1543
|
// src/restura/sql/PsqlUtils.ts
|
|
1543
1544
|
import format2 from "pg-format";
|
|
@@ -1618,8 +1619,10 @@ var PsqlConnection = class {
|
|
|
1618
1619
|
this.logSqlStatement(formattedQuery, options, meta);
|
|
1619
1620
|
const queryMetadata = `--QUERY_METADATA(${JSON.stringify(meta)})
|
|
1620
1621
|
`;
|
|
1622
|
+
const startTime = process.hrtime();
|
|
1621
1623
|
try {
|
|
1622
1624
|
const response = await this.query(queryMetadata + formattedQuery, options);
|
|
1625
|
+
this.logQueryDuration(startTime);
|
|
1623
1626
|
if (response.rows.length === 0) throw new RsError("NOT_FOUND", "No results found");
|
|
1624
1627
|
else if (response.rows.length > 1) throw new RsError("DUPLICATE", "More than one result found");
|
|
1625
1628
|
return response.rows[0];
|
|
@@ -1638,8 +1641,10 @@ var PsqlConnection = class {
|
|
|
1638
1641
|
this.logSqlStatement(formattedQuery, options, meta);
|
|
1639
1642
|
const queryMetadata = `--QUERY_METADATA(${JSON.stringify(meta)})
|
|
1640
1643
|
`;
|
|
1644
|
+
const startTime = process.hrtime();
|
|
1641
1645
|
try {
|
|
1642
1646
|
const response = await this.query(queryMetadata + formattedQuery, options);
|
|
1647
|
+
this.logQueryDuration(startTime);
|
|
1643
1648
|
return response.rows;
|
|
1644
1649
|
} catch (error) {
|
|
1645
1650
|
if ((error == null ? void 0 : error.routine) === "_bt_check_unique") {
|
|
@@ -1648,6 +1653,13 @@ var PsqlConnection = class {
|
|
|
1648
1653
|
throw new RsError("DATABASE_ERROR", `${error.message}`);
|
|
1649
1654
|
}
|
|
1650
1655
|
}
|
|
1656
|
+
logQueryDuration(startTime) {
|
|
1657
|
+
if (logger.level === "silly") {
|
|
1658
|
+
const [seconds, nanoseconds] = process.hrtime(startTime);
|
|
1659
|
+
const duration = seconds * 1e3 + nanoseconds / 1e6;
|
|
1660
|
+
logger.silly(`Query duration: ${duration.toFixed(2)}ms`);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1651
1663
|
logSqlStatement(query, options, queryMetadata, prefix = "") {
|
|
1652
1664
|
if (logger.level !== "silly") return;
|
|
1653
1665
|
let sqlStatement = "";
|
|
@@ -1661,12 +1673,20 @@ var PsqlConnection = class {
|
|
|
1661
1673
|
return format3.literal(value);
|
|
1662
1674
|
});
|
|
1663
1675
|
}
|
|
1676
|
+
const formattedSql = sqlFormat(sqlStatement, {
|
|
1677
|
+
language: "postgresql",
|
|
1678
|
+
linesBetweenQueries: 2,
|
|
1679
|
+
indentStyle: "standard",
|
|
1680
|
+
keywordCase: "upper",
|
|
1681
|
+
useTabs: true,
|
|
1682
|
+
tabWidth: 4
|
|
1683
|
+
});
|
|
1664
1684
|
let initiator = "Anonymous";
|
|
1665
1685
|
if ("userId" in queryMetadata && queryMetadata.userId)
|
|
1666
1686
|
initiator = `User Id (${queryMetadata.userId.toString()})`;
|
|
1667
1687
|
if ("isSystemUser" in queryMetadata && queryMetadata.isSystemUser) initiator = "SYSTEM";
|
|
1668
1688
|
logger.silly(`${prefix}query by ${initiator}, Query ->
|
|
1669
|
-
|
|
1689
|
+
${formattedSql}`);
|
|
1670
1690
|
}
|
|
1671
1691
|
};
|
|
1672
1692
|
|
|
@@ -2261,10 +2281,10 @@ var PsqlEngine = class extends SqlEngine {
|
|
|
2261
2281
|
for (const assignment of routeData.assignments) {
|
|
2262
2282
|
const column = table.columns.find((column2) => column2.name === assignment.name);
|
|
2263
2283
|
if (!column) continue;
|
|
2264
|
-
const
|
|
2284
|
+
const assignmentEscaped = escapeColumnName(assignment.name);
|
|
2265
2285
|
if (SqlUtils.convertDatabaseTypeToTypescript(column.type) === "number")
|
|
2266
|
-
bodyNoId[
|
|
2267
|
-
else bodyNoId[
|
|
2286
|
+
bodyNoId[assignmentEscaped] = Number(assignment.value);
|
|
2287
|
+
else bodyNoId[assignmentEscaped] = assignment.value;
|
|
2268
2288
|
}
|
|
2269
2289
|
const whereClause = this.generateWhereClause(req, routeData.where, routeData, sqlParams);
|
|
2270
2290
|
const query = updateObjectQuery(routeData.table, bodyNoId, whereClause);
|
|
@@ -2298,7 +2318,7 @@ DELETE FROM "${routeData.table}" ${joinStatement} ${whereClause}`;
|
|
|
2298
2318
|
throw new RsError("UNAUTHORIZED", "You do not have permission to access this table");
|
|
2299
2319
|
if (item.custom) {
|
|
2300
2320
|
const customReplaced = this.replaceParamKeywords(item.custom, routeData, req, sqlParams);
|
|
2301
|
-
joinStatements += ` ${item.type} JOIN ${escapeColumnName(item.table)} ON ${customReplaced}
|
|
2321
|
+
joinStatements += ` ${item.type} JOIN ${escapeColumnName(item.table)}${item.alias ? `AS "${item.alias}"` : ""} ON ${customReplaced}
|
|
2302
2322
|
`;
|
|
2303
2323
|
} else {
|
|
2304
2324
|
joinStatements += ` ${item.type} JOIN ${escapeColumnName(item.table)}${item.alias ? `AS "${item.alias}"` : ""} ON "${baseTable}"."${item.localColumnName}" = ${escapeColumnName(item.alias ? item.alias : item.table)}.${escapeColumnName(
|