@eqxjs/mongodb-encrypt-data
v1.0.5
Published
Mongodb CSFLE for nestjs
Downloads
353
Keywords
Readme
Mongo Encrypt Data
Description
This project provides a set of tools and libraries to enable encryption of data stored in MongoDB. It ensures that sensitive information is securely encrypted before being stored in the database, and decrypted when retrieved.
Features
- Data Encryption: Automatically encrypts data before storing it in MongoDB.
- Data Decryption: Automatically decrypts data when retrieving it from MongoDB.
- Seamless Integration: Easily integrates with existing MongoDB applications.
- Configurable Encryption Algorithms: Supports various encryption algorithms to meet different security requirements.
Installation
To install the library, use the following command:
npm install @eqxjs/mongodb-encrypt-data
Usage
Here is a basic example of how to use the library: Register on module
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MongoEncryptModule } from '@eqxjs/mongodb-encrypt-data'
@Module({
imports: [MongoEncryptModule.register('<Mogodb Connection String>',
{
AzureKeyvaultUrl: '<Azure Keyvault URL>',
AzureKeyvaultName: '<Azure Keyvault Name>',
keyVaultNamespace: '<Mongo db name>.<Mongo db collection>',
keyAltName: '<Key name>'
}
)],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Usage on service
import { Injectable } from '@nestjs/common';
import { MongoEncryptService } from '@eqxjs/mongodb-encrypt-data'
import { Collection, MongoClient } from 'mongodb';
@Injectable()
export class AppService {
private mongodbClient: MongoClient;
private mongodbCollection: Collection;
constructor(private readonly mongodb: MongoEncryptService) {
this.mongodbClient = new MongoClient('<Mogodb Connection String>');
this.mongodbClient.connect().then(() => {
this.mongodbCollection = this.mongodbClient.db('<Mongo db name>').collection('<Mongo db collection>');
});
}
async getHello() {
await this.mongodbCollection.insertOne({
"id": "sfegtfvbsrgtwavb",
"identificationIdEncrypted": await this.mongodb.encryptField('12345678903070')
});
let result = await this.mongodbCollection.findOne({ "id": "sfegtfvbsrgtwavb" });
console.log('data before decrypt', result);
result.identificationIdEncrypted = await this.mongodb.decryptField(result.identificationIdEncrypted);
return result;
}
}
Contributing
Contributions are welcome!
Contact
For any questions or issues, please open an issue on the GitHub repository.