@adityadarma/adonis-database-cryptable
v2.0.2
Published
Package to allows developers store data with encrypted and consume again with data decrypted
Downloads
24
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-cryptable
if you use postgres, must install
openpgp
package
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/define_config'
import env from '#start/env'
const cryptableConfig = defineConfig({
key: env.get('APP_KEY'),
default: 'mysql',
drivers: ['mysql', 'postgres'],
})
export default cryptableConfig
Adding 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.