47ng / 47ng/prisma-field-encryption
Doesn't work with the Prisma fluent API
- Lenguaje dominante
- TypeScript
- Estrellas
- 306
- Forks
- 40
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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);
});
```
Guía de contribución
Línea de trabajo
The issue is in the middleware extension not handling Prisma's fluent API. Start by examining the extension code in the repository, particularly how it intercepts queries. Look at the provided sample repository to reproduce the bug. The goal is to ensure decryption works when using `client.user.findFirst().posts()`. Check the test suite for existing patterns on query interception.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- postgresql, typescript
- Área
- backend, databases
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 45/100