47ng / 47ng/prisma-field-encryption

NestJS PrismaService and Extending it With Encryption

Offen
#78 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
TypeScript
Sterne
306
Forks
40
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
postgresql, typescript
Bereich
backend, databases, security
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.