0.12.7 ā€¢ Published 6 months ago

prisma-crypto v0.12.7

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

Prisma-Crypto: Automated Encryption for Prisma ORM

npm version NPM Downloads GitHub issues Code Coverage

The prisma-crypto is an extension for the Prisma ORM that simplifies the implementation of encryption in your database models. With a simple annotation and some configurations, you can ensure that your data is stored securely while still maintaining the ability to query these data efficiently.


šŸ“‘ Table of Contents


šŸš€ Installation

npm install prisma-crypto

or

yarn add prisma-crypto

šŸŒ Environment Configuration

Before starting, set up the following environment variables:

- PRISMA_CRYPTO_SECRET_KEY="" #Your secret key for encryption. Must be 32 characters
- PRISMA_CRYPTO_DIRECT_DB="" #Direct connection to the database. Useful for development environments with Docker.
- PRISMA_CRYPTO_WRITE_DB="" #Connection to the write instance. Used for write operations via Prisma Client.
- PRISMA_CRYPTO_READ_DB="" #Connection to the read instance. Used for read operations via Prisma Client.
- PRISMA_CRYPTO_DEBUG=false #Activate to get detailed logs of the package's operation.

In scenarios where the prisma client has not yet been initialized - as in the case of a project that has just been cloned - it will be necessary to do so. We recommend that you configure a post-installation script, as follows:

{ // package.json
  "scripts": {
    "postinstall": "npx prisma generate --generator client",
    // other scripts here
  },
  // other configs here
}

This way, whenever you run an npm i your prisma client will automatically be initialized. If you don't want to add the script, just run the command npx prisma generate --generator client manually via CLI.


šŸ“ Schema Configuration

In your schema.prisma, setup a new generator and add the @encrypt annotation to the fields you want to encrypt.

generator encrypt {
    provider = "prisma-crypto"
}

model User {
  id       Int     @id @default(autoincrement())
  email    String  @unique // @encrypt
  password String  // @encrypt
}

šŸ›  Usage

With prisma-crypto set up, run your Prisma operations as usual. The extension will handle encryption and decryption for you.

import { PrismaCrypto } from "prisma-crypto";

const prisma = new PrismaCrypto({
    debug: true // It is possible to control the level of granularity of the debug by activating only the client and deactivating the env(general)
}).getPrismaClient();

const newUser = {
  email: 'example@example.com',
  password: 'securePassword',
};

await prisma.user.create({
  data: newUser,
});

When retrieving the user, the encrypted fields will be automatically decrypted:

import { PrismaCrypto } from "prisma-crypto";

const prisma = new PrismaCrypto().getPrismaClient();

const userEmail = 'example@example.com';

const user = await prisma.user.findUnique({
  where: {
    email: userEmail,
  },
});

console.log(user.password); // 'securePassword'

If necessary, you can call Prisma Crypto's encryption/decryption methods manually:

import { EncryptionMethods } from "@paipe/prisma-crypto";

const encryptedString = EncryptionMethods.encryptData("test");
const decryptedString = EncryptionMethods.decryptData("test");

šŸ“– Technical Details

Encryption Algorithm

The prisma-crypto uses the aes-256-gcm algorithm for encryption. This is a symmetric encryption algorithm that is widely recognized for its security and efficiency.

Deterministic Encryption

To allow queries on encrypted fields, the prisma-crypto uses a deterministic approach, where the same input will always produce the same encrypted output. This is achieved through the use of hashes.

Limitations

  • Only string or string[] fields can be encrypted.
  • The package has been optimized for use with PostgreSQL.
  • Operations like LIKE and IN are not supported on encrypted fields.

šŸŽÆ Use Cases

Saving Data with Encryption

When creating or updating records, fields marked with @encrypt will be automatically encrypted.

Querying Encrypted Data

When querying encrypted data, the prisma-crypto applies encryption to the query values to ensure the correct results are returned.

Retrieving Encrypted Data

When retrieving records, the encrypted fields will be automatically decrypted.

Change History for Data Encryption

Keep a record of all changes made to encrypted data, including which data was added or removed from the encryption list.


šŸ¤ Contribution

Contributions are welcome! Check the contribution guide for details.


šŸ“œ License

This project is licensed under the MIT license.


Developed with ā¤ļø by Lucas Servo.
šŸ“§ Contact: l.servo@hotmail.com

0.12.7

6 months ago

0.12.0

7 months ago

0.12.1

7 months ago

0.12.3

7 months ago

0.12.4

7 months ago

0.12.5

7 months ago

0.11.0

8 months ago

0.11.1

7 months ago

0.10.1

8 months ago

0.10.0

8 months ago

0.9.8

8 months ago

0.9.7

8 months ago

0.9.6

8 months ago

0.9.3

8 months ago

0.9.2

8 months ago

0.9.1

8 months ago

0.9.0

9 months ago

0.8.1

9 months ago

0.6.25

9 months ago

0.6.13

9 months ago

0.6.12

9 months ago

0.6.1

9 months ago

0.6.0

9 months ago

0.0.35

9 months ago

0.0.29

9 months ago

0.0.28

9 months ago

0.0.27

9 months ago

0.0.26

9 months ago

0.0.25

9 months ago

0.0.24

9 months ago

0.0.23

9 months ago

0.0.22

9 months ago

0.0.21

9 months ago

0.0.20

9 months ago

0.0.19

9 months ago

0.0.18

9 months ago

0.0.17

9 months ago

0.0.16

9 months ago

0.0.15

9 months ago

0.0.14

9 months ago

0.0.13

9 months ago

0.0.11

9 months ago

0.0.10

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago