feat: Create user model

This commit is contained in:
Faruk AYDIN
2021-10-06 23:37:45 +02:00
committed by Ali BARIN
parent 3f56da5efb
commit d03e34e9cc
4 changed files with 102 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
import { Model, QueryContext, ModelOptions } from 'objection';
import bcrypt from 'bcrypt';
class User extends Model {
id!: number
email!: string
password!: string
static tableName = 'users';
static jsonSchema = {
type: 'object',
required: ['email', 'password'],
properties: {
id: { type: 'integer' },
email: { type: 'email', minLength: 1, maxLength: 255 },
password: { type: 'string', minLength: 1, maxLength: 255 },
}
}
async generateHash() {
this.password = await bcrypt.hash(this.password, 10);
}
async $beforeInsert(queryContext: QueryContext) {
await super.$beforeInsert(queryContext);
await this.generateHash()
}
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
await super.$beforeUpdate(opt, queryContext);
if(this.password) {
await this.generateHash()
}
}
}
export default User;