@adityadarma/adonis-database-cryptable
v1.1.0
Published
Package to allows developers store data with encrypted and consume again with data decrypted
Downloads
126
Maintainers
Readme
Adonis Database Encryption
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
Usage
Configuration
You can configuration encryption data from file config. for now only support mysql or mariadb
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'],
})
export default cryptableConfig
Adding to Model
You can choise what column will you encrypt with decorator @columnCryptable()
.
@columnCryptable()
declare name: string
@columnCryptable()
declare email: string
@column()
declare emailVerifiedAt: DateTime
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
})
})
)
Credits
This package was inspired from the following:
License
Adonis Datatables is open-sourced software licensed under the MIT license.