47ng / 47ng/prisma-field-encryption

NestJS PrismaService and Extending it With Encryption

Aperta
#78 9 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

I am trying to encrypt a particular field in my Prisma model.

Here is the full codes of my `PrismaService` residing my in NestJS's `PrismaModule`:
```
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client'
import { OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { fieldEncryptionExtension } from 'prisma-field-encryption';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleDestroy, OnModuleInit {

constructor() {
super({
log: ['query'],
errorFormat: 'pretty',
});
// Extend PrismaClient with fieldEncryptionExtension
this.$extends(fieldEncryptionExtension()); // extending `PrismaClient`
}

async onModuleInit() {
await this.$connect();
}

async onModuleDestroy() {
await this.$disconnect();
}
}
```

On my `prisma.schema`:
```
model User {
telegramId Int @id @map("telegram_id")
mnemonics String @unique @db.VarChar(1024) /// @encrypted

createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
}
```

Within my database, after using `prismaService.user.create`, the resulting entry in the PostgreSQL database is NOT encrypted.
```
import { Injectable } from '@nestjs/common';
import { User as UserModel } from '@prisma/client';
import { PrismaService } from 'src/prisma/services/prisma/prisma.service';
import { Mnemonic } from 'src/common/entities/mnemonic/mnemonic';

@Injectable()
export class AuthService {

constructor(private readonly prismaSvc: PrismaService) {}

async signUp(telegramId: number): Promise> {
const mnemonic = Mnemonic.createNew()
const user = await this.prismaSvc.user.create({data: {telegramId: telegramId, mnemonics: mnemonic.toString()}, select: {telegramId: true}});
return user
}
}
```

Any idea how does it work? Thank you.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

The issue is about integrating prisma-field-encryption with a NestJS PrismaService. Start by examining the PrismaService extension code and the prisma.schema model. Check the prisma-field-encryption documentation for setup and configuration. Verify the encryption middleware is correctly applied and that the field is marked for encryption. Look at the test suite for examples of working encryption. The goal is to have the 'mnemonics' field stored encrypted in PostgreSQL.

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

Valutazione

Stack tecnologico
postgresql, typescript
Ambito
backend, databases, security
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.