refactor: knex remaining models

This commit is contained in:
NGPixel
2018-05-20 18:50:51 -04:00
parent c9b643fbf0
commit 17b2117b39
22 changed files with 426 additions and 447 deletions

View File

@@ -1,7 +1,7 @@
const Model = require('objection').Model
/**
* Settings model
* Groups model
*/
module.exports = class Group extends Model {
static get tableName() { return 'groups' }
@@ -21,11 +21,10 @@ module.exports = class Group extends Model {
}
static get relationMappings() {
const User = require('./users')
return {
users: {
relation: Model.ManyToManyRelation,
modelClass: User,
modelClass: require('./users'),
join: {
from: 'groups.id',
through: {

70
server/db/models/pages.js Normal file
View File

@@ -0,0 +1,70 @@
const Model = require('objection').Model
/**
* Pages model
*/
module.exports = class Page extends Model {
static get tableName() { return 'pages' }
static get jsonSchema () {
return {
type: 'object',
required: ['path', 'title'],
properties: {
id: {type: 'integer'},
path: {type: 'string'},
title: {type: 'string'},
description: {type: 'string'},
isPublished: {type: 'boolean'},
publishStartDate: {type: 'string'},
publishEndDate: {type: 'string'},
content: {type: 'string'},
createdAt: {type: 'string'},
updatedAt: {type: 'string'}
}
}
}
static get relationMappings() {
return {
tags: {
relation: Model.ManyToManyRelation,
modelClass: require('./tags'),
join: {
from: 'pages.id',
through: {
from: 'pageTags.pageId',
to: 'pageTags.tagId'
},
to: 'tags.id'
}
},
author: {
relation: Model.BelongsToOneRelation,
modelClass: require('./users'),
join: {
from: 'pages.authorId',
to: 'users.id'
}
},
locale: {
relation: Model.BelongsToOneRelation,
modelClass: require('./locales'),
join: {
from: 'users.locale',
to: 'locales.code'
}
}
}
}
$beforeUpdate() {
this.updatedAt = new Date().toISOString()
}
$beforeInsert() {
this.createdAt = new Date().toISOString()
this.updatedAt = new Date().toISOString()
}
}

49
server/db/models/tags.js Normal file
View File

@@ -0,0 +1,49 @@
const Model = require('objection').Model
/**
* Tags model
*/
module.exports = class Tag extends Model {
static get tableName() { return 'tags' }
static get jsonSchema () {
return {
type: 'object',
required: ['tag'],
properties: {
id: {type: 'integer'},
tag: {type: 'string'},
title: {type: 'string'},
createdAt: {type: 'string'},
updatedAt: {type: 'string'}
}
}
}
static get relationMappings() {
return {
pages: {
relation: Model.ManyToManyRelation,
modelClass: require('./pages'),
join: {
from: 'tags.id',
through: {
from: 'pageTags.tagId',
to: 'pageTags.pageId'
},
to: 'pages.id'
}
}
}
}
$beforeUpdate() {
this.updatedAt = new Date().toISOString()
}
$beforeInsert() {
this.createdAt = new Date().toISOString()
this.updatedAt = new Date().toISOString()
}
}

View File

@@ -29,6 +29,7 @@ module.exports = class User extends Model {
role: {type: 'string', enum: ['admin', 'guest', 'user']},
tfaIsActive: {type: 'boolean', default: false},
tfaSecret: {type: 'string'},
locale: {type: 'string'},
createdAt: {type: 'string'},
updatedAt: {type: 'string'}
}
@@ -36,11 +37,10 @@ module.exports = class User extends Model {
}
static get relationMappings() {
const Group = require('./groups')
return {
groups: {
relation: Model.ManyToManyRelation,
modelClass: Group,
modelClass: require('./groups'),
join: {
from: 'users.id',
through: {
@@ -79,7 +79,7 @@ module.exports = class User extends Model {
}
async verifyPassword(pwd) {
if (await bcrypt.compare(this.password, pwd) === true) {
if (await bcrypt.compare(pwd, this.password) === true) {
return true
} else {
throw new WIKI.Error.AuthLoginFailed()