@adityadarma/adonis-database-cryptable
v2.2.0
Published
Package to allows developers store data with encrypted and consume again with data decrypted
Maintainers
Readme
Adonis Database Encryption
This version 2.x is Mayor update, please check parameter in model
Database Encryption is a feature that allows developers to store data with encrypted and consume again with data decrypted. This feature provides a structured and organized approach to managing application database, making it easier to query.
Installation
node ace add @adityadarma/adonis-database-cryptableif you use postgres, must install
openpgppackage andpgcryptoextension
Usage
Configuration
You can configuration encryption data from file config. for now only support mysql or postgres database.
import { defineConfig } from '@adityadarma/adonis-database-cryptable'
import env from '#start/env'
const cryptableConfig = defineConfig({
key: env.get('APP_KEY'),
default: 'mysql', // available mysql or postgres
})
export default cryptableConfigAdding to Model
You can add what column will you encrypt with parameter in your model.
$cryptable: string[] = ['name']Searching Encrypted Fields Example:
Searching encrypted field can be done by calling the whereEncrypted and orWhereEncrypted functions
similar to laravel eloquent where and orWhere. Ordering encrypted data can be calling orderByEncrypted laravel eloquent orderBy.
export default class UsersController {
async index() {
const user = await User.query()
.whereEncrypted('first_name', 'john')
.orWhereEncrypted('last_name', '!=', 'Doe')
.orderByEncrypted('last_name', 'asc')
.first()
return user
}
}Validate value
Validate data encrypted in database in VineJS. You can apply on unique or exists method.
export const updateUserValidator = vine.compile(
vine.object({
email: vine.string().unique(async (db, value, field) => {
const user = await db
.from('users')
.whereNot('id', field.meta.userId)
.whereEncrypted('email', value)
.first()
return !user
}),
})
)License
Adonis Datatables is open-sourced software licensed under the MIT license.
