const { Model } = require('objection'); const { format } = require('date-fns'); class BaseModel extends Model { static get useLimitInFirst() { return true; } $beforeInsert() { super.$beforeInsert(); if (this.constructor.hasColumn('created')) { this.created = this.constructor.mysqlTimestamp(false); } if (this.constructor.hasColumn('modified')) { this.modified = this.constructor.mysqlTimestamp(false); } } $beforeUpdate() { super.$beforeUpdate(); if (this.constructor.hasColumn('modified')) { this.modified = this.constructor.mysqlTimestamp(false); } } static hasColumn(columnName) { if (this.jsonSchema && this.jsonSchema.properties && this.jsonSchema.properties[columnName] ) { return true; } return false; } static mysqlTimestamp(schemaDefinition = true) { const formattedDate = format(new Date(), 'yyyy-MM-dd HH:mm:ss'); return schemaDefinition ? { type: 'string', default: formattedDate } : formattedDate; } } module.exports = BaseModel;