feat: Introduce base model to implement created_at & updated_at logic
This commit is contained in:
21
packages/backend/src/models/base.ts
Normal file
21
packages/backend/src/models/base.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Model, QueryContext, ModelOptions } from 'objection';
|
||||
|
||||
class Base extends Model {
|
||||
created_at!: string;
|
||||
updated_at!: string;
|
||||
|
||||
async $beforeInsert(queryContext: QueryContext) {
|
||||
await super.$beforeInsert(queryContext);
|
||||
|
||||
this.created_at = new Date().toISOString();
|
||||
this.updated_at = new Date().toISOString();
|
||||
}
|
||||
|
||||
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
|
||||
await super.$beforeUpdate(opt, queryContext);
|
||||
|
||||
this.updated_at = new Date().toISOString();
|
||||
}
|
||||
}
|
||||
|
||||
export default Base;
|
@@ -1,7 +1,8 @@
|
||||
import { Model, QueryContext, ModelOptions } from 'objection';
|
||||
import { QueryContext, ModelOptions } from 'objection';
|
||||
import Base from './base'
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
class User extends Model {
|
||||
class User extends Base {
|
||||
id!: number
|
||||
email!: string
|
||||
password!: string
|
||||
@@ -14,7 +15,7 @@ class User extends Model {
|
||||
|
||||
properties: {
|
||||
id: { type: 'integer' },
|
||||
email: { type: 'email', minLength: 1, maxLength: 255 },
|
||||
email: { type: 'string', format: 'email', minLength: 1, maxLength: 255 },
|
||||
password: { type: 'string', minLength: 1, maxLength: 255 },
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user