Created
October 15, 2018 14:17
-
-
Save bryzettler/81d9020c50ada835e89d73fd2a6dee71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Model } from 'objection'; | |
import AuditLog from './AuditLog'; | |
export default class BaseModel extends Model { | |
static auditKeys = []; | |
static auditSanitize = modelObj => ( | |
// upto other models to implent their own logic | |
// for auditSanitize | |
this.constructor.auditSanitize | |
? this.constructor.auditSanitize(modelObj) | |
: modelObj | |
); | |
async $afterInsert(queryContext) { | |
await super.$afterInsert(queryContext); | |
if (this.constructor.auditKeys.length) { | |
await AuditLog.log( | |
queryContext.transaction, | |
{ verb: 'insert', model: this } | |
); | |
} | |
} | |
async $afterUpdate(opt, queryContext) { | |
const { old } = opt; | |
await super.$afterUpdate(opt, queryContext); | |
if (this.constructor.auditKeys.length) { | |
await AuditLog.log( | |
queryContext.transaction, | |
{ verb: 'update', model: old, updatedModel: this } | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment