slonik-trpc
Version:
Slonik tRPC loader
27 lines (26 loc) • 1.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createResultParserInterceptor = void 0;
const slonik_1 = require("slonik");
const createResultParserInterceptor = () => {
return {
// If you are not going to transform results using Zod, then you should use `afterQueryExecution` instead.
// Future versions of Zod will provide a more efficient parser when parsing without transformations.
// You can even combine the two – use `afterQueryExecution` to validate results, and (conditionally)
// transform results as needed in `transformRow`.
transformRow: (executionContext, actualQuery, row) => {
const { log, resultParser } = executionContext;
if (!resultParser) {
return row;
}
const validationResult = resultParser.safeParse(row);
if (!validationResult.success) {
const msg = validationResult.error.issues.map((issue, idx) => `Error #${idx + 1}: Code: ${issue.code} ~ Path: ${issue.path.join('->')} ~ Message: ${issue.message}`);
console.error(msg.join('\n'));
throw new slonik_1.SchemaValidationError(actualQuery, row, validationResult.error.issues);
}
return validationResult.data;
},
};
};
exports.createResultParserInterceptor = createResultParserInterceptor;