47ng / 47ng/prisma-field-encryption
Doesn't work with the Prisma fluent API
- Dominant language
- TypeScript
- Stars
- 306
- Forks
- 40
- PR merge metrics
- No merged PRs in 30d
Description
Describe the Bug
I have trouble in using prisma-field-encryption middleware when using prisma fluent API. Not decrypted data are returned when I request data through prisma fluent API. To avoid N+1 problem, I want to use [fluent API](https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#fluent-api) which can use [prisma data loader](https://www.prisma.io/docs/guides/performance-and-optimization/query-optimization-performance).
To Reproduce
Add prisma-field-encryption middleware. Make schema which has parent-children relation model and @encrypted field in children model. (see my code below)
Expected Behavior
I expect data which are requested through prisma fluent API to be decrypted.
Environment:
OS: ubuntu 22.04.1
Node 20.2.0
Prisma version 5.2.0
prisma-field-encryption 1.5.0
TypeScript version 5.2.2
Express 4.18.2
Additional Context
This is my [github repository](https://github.com/hightail191/prisma-field-encryption-error-sample)
Prisma schema
```prisma
// schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgresql://postgres:postgres@db:5432/adgame?schema=public"
}
model User {
id Int @id @default(autoincrement())
email String
name String? /// @encrypted
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String? /// @encrypted
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
```
Express Server
```ts
// server.ts
import express from "express";
import { PrismaClient } from "@prisma/client";
import { fieldEncryptionExtension } from "prisma-field-encryption";
const globalClient = new PrismaClient();
const client = globalClient.$extends(
fieldEncryptionExtension({
encryptionKey: "k1.aesgcm256.DbQoar8ZLuUsOHZNyrnjlskInHDYlzF3q6y1KGM7DUM=",
})
);
const app = express();
const server = app.listen(3000, function () {
console.log("start server");
});
// data are decrypted when using normal query
app.get("/userposts1", async (req, res, next) => {
const user = await client.user.findFirst();
const authorId = user?.id || 1;
const posts = await client.post.findMany({
where: {
authorId: authorId,
},
});
console.log(posts);
res.send(posts);
});
// data are not decrypted when using fluent API
app.get("/userposts2", async (req, res, next) => {
const posts = await client.user.findFirst().posts();
console.log(posts);
res.send(posts);
});
```
Contributor guide
Assessment
This issue has not been assessed yet.