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,46 +1,85 @@
exports.up = knex => {
return knex.schema
// -------------------------------------
// GROUPS
// -------------------------------------
// =====================================
// MODEL TABLES
// =====================================
// ASSETS ------------------------------
.createTable('assets', table => {
table.increments('id').primary()
table.string('filename').notNullable()
table.string('basename').notNullable()
table.string('ext').notNullable()
table.enum('kind', ['binary', 'image']).notNullable().defaultTo('binary')
table.string('mime').notNullable().defaultTo('application/octet-stream')
table.integer('fileSize').unsigned().comment('In kilobytes')
table.jsonb('metadata')
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// ASSET FOLDERS -----------------------
.createTable('assetFolders', table => {
table.increments('id').primary()
table.string('name').notNullable()
table.string('slug').notNullable()
table.integer('parentId').unsigned().references('id').inTable('assetFolders')
})
// COMMENTS ----------------------------
.createTable('comments', table => {
table.increments('id').primary()
table.text('content').notNullable()
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// GROUPS ------------------------------
.createTable('groups', table => {
table.increments('id').primary()
table.string('name').notNullable()
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// -------------------------------------
// LOCALES
// -------------------------------------
// LOCALES -----------------------------
.createTable('locales', table => {
table.increments('id').primary()
table.string('code', 2).notNullable().unique()
table.json('strings')
table.jsonb('strings')
table.boolean('isRTL').notNullable().defaultTo(false)
table.string('name').notNullable()
table.string('nativeName').notNullable()
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// -------------------------------------
// SETTINGS
// -------------------------------------
.createTable('settings', table => {
// PAGES -------------------------------
.createTable('pages', table => {
table.increments('id').primary()
table.string('key').notNullable().unique()
table.json('value')
table.string('path').notNullable()
table.string('title').notNullable()
table.string('description')
table.boolean('isPublished').notNullable().defaultTo(false)
table.string('publishStartDate')
table.string('publishEndDate')
table.text('content')
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// -------------------------------------
// USERS
// -------------------------------------
// SETTINGS ----------------------------
.createTable('settings', table => {
table.increments('id').primary()
table.string('key').notNullable().unique()
table.jsonb('value')
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// TAGS --------------------------------
.createTable('tags', table => {
table.increments('id').primary()
table.string('tag').notNullable().unique()
table.string('title')
table.string('createdAt').notNullable()
table.string('updatedAt').notNullable()
})
// USERS -------------------------------
.createTable('users', table => {
table.increments('id').primary()
table.string('email').notNullable()
table.string('name').notNullable()
table.string('provider').notNullable().defaultTo('local')
@@ -54,22 +93,52 @@ exports.up = knex => {
table.unique(['provider', 'email'])
})
// -------------------------------------
// USER GROUPS
// -------------------------------------
// =====================================
// RELATION TABLES
// =====================================
// PAGE TAGS ---------------------------
.createTable('pageTags', table => {
table.increments('id').primary()
table.integer('pageId').unsigned().references('id').inTable('pages')
table.integer('tagId').unsigned().references('id').inTable('tags')
})
// USER GROUPS -------------------------
.createTable('userGroups', table => {
table.increments('id').primary()
table.integer('userId').unsigned().references('id').inTable('users')
table.integer('groupId').unsigned().references('id').inTable('groups')
})
// =====================================
// REFERENCES
// =====================================
.table('assets', table => {
table.integer('folderId').unsigned().references('id').inTable('assetFolders')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('comments', table => {
table.integer('pageId').unsigned().references('id').inTable('pages')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('pages', table => {
table.string('locale', 2).references('code').inTable('locales')
table.integer('authorId').unsigned().references('id').inTable('users')
})
.table('users', table => {
table.string('locale', 2).references('code').inTable('locales')
})
}
exports.down = knex => {
return knex.schema
.dropTableIfExists('userGroups')
.dropTableIfExists('pageTags')
.dropTableIfExists('assets')
.dropTableIfExists('assetFolders')
.dropTableIfExists('comments')
.dropTableIfExists('groups')
.dropTableIfExists('locales')
.dropTableIfExists('pages')
.dropTableIfExists('settings')
.dropTableIfExists('tags')
.dropTableIfExists('users')
}

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()