47ng / 47ng/prisma-field-encryption
Doesn't work with the Prisma fluent API
- 主要言語
- TypeScript
- スター
- 306
- フォーク
- 40
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
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);
});
```
コントリビューションガイド
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- postgresql, typescript
- 領域
- backend, databases
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 45/100