@godspeedsystems/prisma-deterministic-search-field-encryption
Version:
Transparent and customizable field-level encryption at rest for Prisma based on prisma-field-encryption package
25 lines (24 loc) • 1.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.fieldEncryptionMiddleware = void 0;
const dmmf_1 = require("./dmmf");
const encryption_1 = require("./encryption");
function fieldEncryptionMiddleware(config = {}) {
var _a;
const { cipherFunctions, keys, method } = (0, encryption_1.configureKeysAndFunctions)(config);
const models = (0, dmmf_1.analyseDMMF)((_a = config.dmmf) !== null && _a !== void 0 ? _a : require('@prisma/client').Prisma.dmmf);
return async function fieldEncryptionMiddleware(params, next) {
if (!params.model) {
// Unsupported operation
return await next(params);
}
const operation = `${params.model}.${params.action}`;
// Params are mutated in-place for modifications to occur.
// See https://github.com/prisma/prisma/issues/9522
const encryptedParams = (0, encryption_1.encryptOnWrite)(params, models, operation, method, keys, cipherFunctions === null || cipherFunctions === void 0 ? void 0 : cipherFunctions.encryptFn);
let result = await next(encryptedParams);
(0, encryption_1.decryptOnRead)(encryptedParams, result, models, operation, method, keys, cipherFunctions === null || cipherFunctions === void 0 ? void 0 : cipherFunctions.decryptFn);
return result;
};
}
exports.fieldEncryptionMiddleware = fieldEncryptionMiddleware;
;