@restura/core 1.0.4 → 1.0.6
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.ts +4 -2
- package/dist/index.js +36 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -934,8 +934,10 @@ declare abstract class PsqlConnection {
|
|
|
934
934
|
readonly instanceId: UUID;
|
|
935
935
|
protected constructor(instanceId?: UUID);
|
|
936
936
|
protected abstract query<R extends QueryResultRow = QueryResultRow, T extends Array<unknown> = unknown[]>(query: string, values?: QueryConfigValues<T>): Promise<QueryResult<R>>;
|
|
937
|
-
queryOne<T>(query: string, options:
|
|
938
|
-
|
|
937
|
+
queryOne<T>(query: string, options: unknown[], requesterDetails: RequesterDetails): Promise<T>;
|
|
938
|
+
queryOneSchema<T>(query: string, params: unknown[], requesterDetails: RequesterDetails, zodSchema: z.ZodSchema<T>): Promise<T>;
|
|
939
|
+
runQuery<T>(query: string, options: unknown[], requesterDetails: RequesterDetails): Promise<T[]>;
|
|
940
|
+
runQuerySchema<T>(query: string, params: unknown[], requesterDetails: RequesterDetails, zodSchema: z.ZodSchema<T>): Promise<T[]>;
|
|
939
941
|
private logQueryDuration;
|
|
940
942
|
private logSqlStatement;
|
|
941
943
|
}
|
package/dist/index.js
CHANGED
|
@@ -1403,7 +1403,11 @@ function requestValidator(req, routeData, validationSchema) {
|
|
|
1403
1403
|
if (!routeData.requestType) throw new RsError("BAD_REQUEST", `No request type defined for custom request.`);
|
|
1404
1404
|
const currentInterface = validationSchema[routeData.requestType];
|
|
1405
1405
|
const validator = new jsonschema.Validator();
|
|
1406
|
-
const
|
|
1406
|
+
const strictSchema = {
|
|
1407
|
+
...currentInterface,
|
|
1408
|
+
additionalProperties: false
|
|
1409
|
+
};
|
|
1410
|
+
const executeValidation = validator.validate(req.data, strictSchema);
|
|
1407
1411
|
if (!executeValidation.valid) {
|
|
1408
1412
|
throw new RsError(
|
|
1409
1413
|
"BAD_REQUEST",
|
|
@@ -1617,6 +1621,7 @@ import pg from "pg";
|
|
|
1617
1621
|
import crypto from "crypto";
|
|
1618
1622
|
import format3 from "pg-format";
|
|
1619
1623
|
import { format as sqlFormat } from "sql-formatter";
|
|
1624
|
+
import { z as z6 } from "zod/v4";
|
|
1620
1625
|
|
|
1621
1626
|
// src/restura/sql/PsqlUtils.ts
|
|
1622
1627
|
import format2 from "pg-format";
|
|
@@ -1691,7 +1696,6 @@ var PsqlConnection = class {
|
|
|
1691
1696
|
constructor(instanceId) {
|
|
1692
1697
|
this.instanceId = instanceId || crypto.randomUUID();
|
|
1693
1698
|
}
|
|
1694
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1695
1699
|
async queryOne(query, options, requesterDetails) {
|
|
1696
1700
|
const formattedQuery = questionMarksToOrderedParams(query);
|
|
1697
1701
|
const meta = { connectionInstanceId: this.instanceId, ...requesterDetails };
|
|
@@ -1713,7 +1717,21 @@ var PsqlConnection = class {
|
|
|
1713
1717
|
throw new RsError("DATABASE_ERROR", `${error.message}`);
|
|
1714
1718
|
}
|
|
1715
1719
|
}
|
|
1716
|
-
|
|
1720
|
+
async queryOneSchema(query, params, requesterDetails, zodSchema) {
|
|
1721
|
+
const result = await this.queryOne(query, params, requesterDetails);
|
|
1722
|
+
try {
|
|
1723
|
+
return zodSchema.parse(result);
|
|
1724
|
+
} catch (error) {
|
|
1725
|
+
if (error instanceof z6.ZodError) {
|
|
1726
|
+
logger.error("Invalid data returned from database:");
|
|
1727
|
+
logger.silly("\n" + JSON.stringify(result, null, 2));
|
|
1728
|
+
logger.error("\n" + z6.prettifyError(error));
|
|
1729
|
+
} else {
|
|
1730
|
+
logger.error(error);
|
|
1731
|
+
}
|
|
1732
|
+
throw new RsError("DATABASE_ERROR", `Invalid data returned from database`);
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1717
1735
|
async runQuery(query, options, requesterDetails) {
|
|
1718
1736
|
const formattedQuery = questionMarksToOrderedParams(query);
|
|
1719
1737
|
const meta = { connectionInstanceId: this.instanceId, ...requesterDetails };
|
|
@@ -1732,6 +1750,21 @@ var PsqlConnection = class {
|
|
|
1732
1750
|
throw new RsError("DATABASE_ERROR", `${error.message}`);
|
|
1733
1751
|
}
|
|
1734
1752
|
}
|
|
1753
|
+
async runQuerySchema(query, params, requesterDetails, zodSchema) {
|
|
1754
|
+
const result = await this.runQuery(query, params, requesterDetails);
|
|
1755
|
+
try {
|
|
1756
|
+
return z6.array(zodSchema).parse(result);
|
|
1757
|
+
} catch (error) {
|
|
1758
|
+
if (error instanceof z6.ZodError) {
|
|
1759
|
+
logger.error("Invalid data returned from database:");
|
|
1760
|
+
logger.silly("\n" + JSON.stringify(result, null, 2));
|
|
1761
|
+
logger.error("\n" + z6.prettifyError(error));
|
|
1762
|
+
} else {
|
|
1763
|
+
logger.error(error);
|
|
1764
|
+
}
|
|
1765
|
+
throw new RsError("DATABASE_ERROR", `Invalid data returned from database`);
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1735
1768
|
logQueryDuration(startTime) {
|
|
1736
1769
|
if (logger.level === "silly") {
|
|
1737
1770
|
const [seconds, nanoseconds] = process.hrtime(startTime);
|