adonis-audit 2.1.2
Install from the command line:
Learn more about npm packages
$ npm install @ks-labs/adonis-audit@2.1.2
Install via package.json:
"@ks-labs/adonis-audit": "2.1.2"
About this version
Audit models in AdonisJS
- Install npm module:
adonis install @ks-labs/adonis-audit
Once you have installed adonis-audit, make sure to register the provider inside start/app.js
in order to make use of it.
const providers = ['@ks-labs/adonis-audit/providers/AuditProvider']
Add the following to your model's boot
method:
class MyModel extends Model {
static boot() {
super.boot()
this.addTrait('@provider:Auditable')
}
}
This you can start using as follows:
// create
const createdModel = await MyModel.audit().create({ name: 'John' })
this will generate a new entry on audits table with only the name
field :
auditable | auditable_id | event | ...cols | old_data | new_data |
---|---|---|---|---|---|
user |
1 |
create |
... | null |
{ name: 'John' } |
// simple update event
const myModel = await MyModel.find(1)
await myModel.audit().update({ name: 'Simon' })
const myModel = await MyModel.find(1)
this will generate a new update event on audit table with the fields changed :
...cols | event | old_data | new_data |
---|---|---|---|
... | update |
{ name :'John' } |
{ name: 'Simon' } |
// ignore audit event when only specified fields in `ignoreDiff` has changed (default to ['updated_at'])
await myModel.audit().update({ name: 'Simon' }, { ignoreDiff: ['name', 'updated_at'] })
// this audit will not write to the `audits` table because only the `name` field changed and its present in `ignoreDiff`
this will not create an entry on audits table due the ignored changes on name
field:
...cols | event | old_data | new_data |
---|
// save only the specified fields in `onlySave` (default to [])
await myModel.audit().update(
{ name: 'newName', password: '32123' },
{
onlySave: ['name'],
}
)
this will generate a new entry on audits table with only the name
field :
...cols | event | old_data | new_data |
---|---|---|---|
... | update |
{ name :'Simon' } |
{ name: 'newName' } |
// delete
const myModel = await MyModel.find(1)
await myModel.audit().delete()
This will generate an entry with delete event on audits table :
...cols | event | old_data | new_data |
---|---|---|---|
... | delete |
{ name: 'newName' } |
null |
const { request } = ctx
const trx = await Database.beginTransaction()
const myModel = await MyModel.find(1)
await myModel.audit({ ctx }).update({ name: 'Simon' }, trx)
await myModel.audit({ ctx }).delete(trx)
// revert changes and remove audit entries created
await trx.rollback()
const foundMyModel = await MyModel.find(1)
- Legacy AdonisJS 4.1 - The web framework used.
SemVer is used for versioning. For the versions available, see the tags on this repository.
- KS Labs - Fork Owner - kslabs
- Vinny - Currently Mantainer - vinicioslc
- Simon Tong - Creator - simontong
This project is licensed under the MIT License - see the LICENSE file for details.