47ng / 47ng/prisma-field-encryption

Doesn't work with the Prisma fluent API

Aperta
#80 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
TypeScript
Stelle
306
Fork
40
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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);
});
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
postgresql, typescript
Ambito
backend, databases
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.