Skip to content

Instantly share code, notes, and snippets.

@bryzettler
Created October 15, 2018 14:17
Show Gist options
  • Save bryzettler/81d9020c50ada835e89d73fd2a6dee71 to your computer and use it in GitHub Desktop.
Save bryzettler/81d9020c50ada835e89d73fd2a6dee71 to your computer and use it in GitHub Desktop.
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