Registry / security / prisma-field-encryption

prisma-field-encryption

JSON →
library1.6.0jsnpmunverified

Transparent field-level encryption at rest for Prisma using AES-256-GCM. v1.6.0 requires @prisma/client >= 4.7. Encrypts specified fields automatically via Prisma middleware. Configuration is per-field with a master key. Different from alternatives like prisma-encryption-middleware by supporting key rotation, detecting encrypted fields via schema comments, and being actively maintained. Ships TypeScript types.

npm install prisma-field-encryption
INSTALL
IMPORT
SIG · PRISMA-FIELD-ENCRY
P
prisma-field-encryption
securityjavascriptv1.6.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PrismaFieldEncryption
import { PrismaFieldEncryption } from 'prisma-field-encryption'
import PrismaFieldEncryption from 'prisma-field-encryption'
This is a named export, not default. ESM-only.
config
import { config } from 'prisma-field-encryption/config'
import { config } from 'prisma-field-encryption'
config is exported from a subpath 'prisma-field-encryption/config'.
PrismaClient
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from 'prisma-field-encryption'
PrismaClient is from @prisma/client, not from this package.

Shows how to configure PrismaFieldEncryption middleware to encrypt a field named 'secret' on User model using AES-256-GCM.

import { PrismaClient } from '@prisma/client'; import { PrismaFieldEncryption } from 'prisma-field-encryption'; const prisma = new PrismaClient(); prisma.$use( PrismaFieldEncryption({ encryptionKey: process.env.FIELD_ENCRYPTION_KEY ?? '', fields: { User: { secret: true, }, }, }) ); async function main() { const user = await prisma.user.create({ data: { name: 'Alice', secret: 'my-secret-value' }, }); console.log(user.secret); // Automatically decrypted } main().catch(console.error);
Debug
Known issues
breakingv1.0.0 changed the export from default to named. Old code using import PrismaFieldEncryption from ... breaks.
fix
Change to import { PrismaFieldEncryption } from 'prisma-field-encryption'.
affects: <1.0.0
deprecatedThe field-level encryption key (encryptionKey) must be exactly 32 bytes (256-bit) for AES-256-GCM. Using shorter keys will fail.
fix
Generate a 32-byte key using openssl rand -hex 32 and set it as environment variable.
affects: >=1.0.0
gotchaEncrypted fields cannot be used in WHERE clauses or other filtering operations because they are stored encrypted.
fix
Do not attempt to query with encrypted fields; consider storing a searchable hash separately if needed.
affects: >=1.0.0
gotchaThe middleware must be registered before any queries are executed. Registering it after a query will leave previously created records unencrypted.
fix
Always call prisma.$use() immediately after creating the PrismaClient instance, before any operations.
affects: >=1.0.0
breakingv1.4.0 removed support for @prisma/client <4.7. If using older Prisma versions, upgrade Prisma first.
fix
Upgrade @prisma/client to >=4.7.
affects: <4.7
Errors
Common errors & fixes
Error: Field encryption key must be a 32-byte Buffer or a 64-character hex string.
encryptionKey is not exactly 32 bytes or 64 hex chars.
fix
Set FIELD_ENCRYPTION_KEY=$(openssl rand -hex 32) and ensure it is exactly 64 hex characters.
PrismaFieldEncryption is not a function
Importing incorrectly as default export or using old CommonJS syntax.
fix
Use import { PrismaFieldEncryption } from 'prisma-field-encryption' (ESM only).
Cannot find module 'prisma-field-encryption/config'
Missing dependency or using an older version that does not export subpath.
fix
Ensure you have prisma-field-encryption@^1.0.0 installed. The config subpath was added in v1.0.0.
Upgrade
Version history
1.6.0latest on npm
Audit
Dependencies
@prisma/clientrequiredPeer dependency: uses Prisma Client middleware and runs queries
Agent activity
21 hits · last 30 days
node
20
Resources
prisma-field-encryption — npm install prisma-field-encryption · libregistry