47ng / 47ng/prisma-field-encryption
Doesn't work with the Prisma fluent API
- Vorherrschende Sprache
- TypeScript
- Sterne
- 306
- Forks
- 40
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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);
});
```
Beitragsleitfaden
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- postgresql, typescript
- Bereich
- backend, databases
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100