refactor: migrate to objection.js + knex
This commit is contained in:
@@ -18,7 +18,7 @@ module.exports = {
|
||||
})
|
||||
|
||||
passport.deserializeUser(function (id, done) {
|
||||
WIKI.db.User.findById(id).then((user) => {
|
||||
WIKI.db.users.query().findById(id).then((user) => {
|
||||
if (user) {
|
||||
done(null, user)
|
||||
} else {
|
||||
@@ -58,57 +58,6 @@ module.exports = {
|
||||
WIKI.logger.info(`Authentication Provider ${strategy.title}: [ OK ]`)
|
||||
})
|
||||
|
||||
// Create Guest account for first-time
|
||||
|
||||
WIKI.db.User.findOne({
|
||||
where: {
|
||||
provider: 'local',
|
||||
email: 'guest@example.com'
|
||||
}
|
||||
}).then((c) => {
|
||||
if (c < 1) {
|
||||
return WIKI.db.User.create({
|
||||
provider: 'local',
|
||||
email: 'guest@example.com',
|
||||
name: 'Guest',
|
||||
password: '',
|
||||
role: 'guest'
|
||||
}).then(() => {
|
||||
WIKI.logger.info('[AUTH] Guest account created successfully!')
|
||||
return true
|
||||
}).catch((err) => {
|
||||
WIKI.logger.error('[AUTH] An error occured while creating guest account:')
|
||||
WIKI.logger.error(err)
|
||||
return err
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// .then(() => {
|
||||
// if (process.env.WIKI_JS_HEROKU) {
|
||||
// return WIKI.db.User.findOne({ provider: 'local', email: process.env.WIKI_ADMIN_EMAIL }).then((c) => {
|
||||
// if (c < 1) {
|
||||
// // Create root admin account (HEROKU ONLY)
|
||||
|
||||
// return WIKI.db.User.create({
|
||||
// provider: 'local',
|
||||
// email: process.env.WIKI_ADMIN_EMAIL,
|
||||
// name: 'Administrator',
|
||||
// password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default)
|
||||
// role: 'admin'
|
||||
// }).then(() => {
|
||||
// WIKI.logger.info('[AUTH] Root admin account created successfully!')
|
||||
// return true
|
||||
// }).catch((err) => {
|
||||
// WIKI.logger.error('[AUTH] An error occured while creating root admin account:')
|
||||
// WIKI.logger.error(err)
|
||||
// return err
|
||||
// })
|
||||
// } else { return true }
|
||||
// })
|
||||
// } else { return true }
|
||||
// })
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
@@ -59,17 +59,10 @@ module.exports = {
|
||||
subsets = WIKI.data.configNamespaces
|
||||
}
|
||||
|
||||
let results = await WIKI.db.Setting.findAll({
|
||||
attributes: ['key', 'config'],
|
||||
where: {
|
||||
key: {
|
||||
$in: subsets
|
||||
}
|
||||
}
|
||||
})
|
||||
let results = await WIKI.db.settings.query().select(['key', 'value']).whereIn('key', subsets)
|
||||
if (_.isArray(results) && results.length === subsets.length) {
|
||||
results.forEach(result => {
|
||||
WIKI.config[result.key] = result.config
|
||||
WIKI.config[result.key] = result.value
|
||||
})
|
||||
return true
|
||||
} else {
|
||||
@@ -88,14 +81,18 @@ module.exports = {
|
||||
subsets = WIKI.data.configNamespaces
|
||||
}
|
||||
|
||||
let trx = await WIKI.db.Objection.transaction.start(WIKI.db.knex)
|
||||
|
||||
try {
|
||||
for (let set of subsets) {
|
||||
await WIKI.db.Setting.upsert({
|
||||
key: set,
|
||||
config: _.get(WIKI.config, set, {})
|
||||
})
|
||||
console.info(set)
|
||||
await WIKI.db.settings.query(trx).patch({
|
||||
value: _.get(WIKI.config, set, {})
|
||||
}).where('key', set)
|
||||
}
|
||||
await trx.commit()
|
||||
} catch (err) {
|
||||
await trx.rollback(err)
|
||||
WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
|
||||
return false
|
||||
}
|
||||
|
@@ -1,55 +1,18 @@
|
||||
const _ = require('lodash')
|
||||
const fs = require('fs')
|
||||
const autoload = require('auto-load')
|
||||
const path = require('path')
|
||||
const Promise = require('bluebird')
|
||||
const Sequelize = require('sequelize')
|
||||
const Knex = require('knex')
|
||||
const Objection = require('objection')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
const operatorsAliases = {
|
||||
$eq: Sequelize.Op.eq,
|
||||
$ne: Sequelize.Op.ne,
|
||||
$gte: Sequelize.Op.gte,
|
||||
$gt: Sequelize.Op.gt,
|
||||
$lte: Sequelize.Op.lte,
|
||||
$lt: Sequelize.Op.lt,
|
||||
$not: Sequelize.Op.not,
|
||||
$in: Sequelize.Op.in,
|
||||
$notIn: Sequelize.Op.notIn,
|
||||
$is: Sequelize.Op.is,
|
||||
$like: Sequelize.Op.like,
|
||||
$notLike: Sequelize.Op.notLike,
|
||||
$iLike: Sequelize.Op.iLike,
|
||||
$notILike: Sequelize.Op.notILike,
|
||||
$regexp: Sequelize.Op.regexp,
|
||||
$notRegexp: Sequelize.Op.notRegexp,
|
||||
$iRegexp: Sequelize.Op.iRegexp,
|
||||
$notIRegexp: Sequelize.Op.notIRegexp,
|
||||
$between: Sequelize.Op.between,
|
||||
$notBetween: Sequelize.Op.notBetween,
|
||||
$overlap: Sequelize.Op.overlap,
|
||||
$contains: Sequelize.Op.contains,
|
||||
$contained: Sequelize.Op.contained,
|
||||
$adjacent: Sequelize.Op.adjacent,
|
||||
$strictLeft: Sequelize.Op.strictLeft,
|
||||
$strictRight: Sequelize.Op.strictRight,
|
||||
$noExtendRight: Sequelize.Op.noExtendRight,
|
||||
$noExtendLeft: Sequelize.Op.noExtendLeft,
|
||||
$and: Sequelize.Op.and,
|
||||
$or: Sequelize.Op.or,
|
||||
$any: Sequelize.Op.any,
|
||||
$all: Sequelize.Op.all,
|
||||
$values: Sequelize.Op.values,
|
||||
$col: Sequelize.Op.col
|
||||
}
|
||||
|
||||
/**
|
||||
* PostgreSQL DB module
|
||||
*/
|
||||
module.exports = {
|
||||
Sequelize,
|
||||
Op: Sequelize.Op,
|
||||
|
||||
Objection,
|
||||
knex: null,
|
||||
/**
|
||||
* Initialize DB
|
||||
*
|
||||
@@ -57,65 +20,63 @@ module.exports = {
|
||||
*/
|
||||
init() {
|
||||
let self = this
|
||||
let dbModelsPath = path.join(WIKI.SERVERPATH, 'models')
|
||||
|
||||
// Define Sequelize instance
|
||||
|
||||
this.inst = new this.Sequelize(WIKI.config.db.db, WIKI.config.db.user, WIKI.config.db.pass, {
|
||||
let dbClient = null
|
||||
const dbConfig = (!_.isEmpty(process.env.WIKI_DB_CONNSTR)) ? process.env.WIKI_DB_CONNSTR : {
|
||||
host: WIKI.config.db.host,
|
||||
user: WIKI.config.db.user,
|
||||
password: WIKI.config.db.pass,
|
||||
database: WIKI.config.db.db,
|
||||
port: WIKI.config.db.port,
|
||||
dialect: WIKI.config.db.type,
|
||||
storage: WIKI.config.db.storage,
|
||||
pool: {
|
||||
max: 10,
|
||||
min: 0,
|
||||
idle: 10000
|
||||
},
|
||||
logging: log => { WIKI.logger.log('debug', log) },
|
||||
operatorsAliases
|
||||
filename: WIKI.config.db.storage
|
||||
}
|
||||
|
||||
switch (WIKI.config.db.type) {
|
||||
case 'postgres':
|
||||
dbClient = 'pg'
|
||||
break
|
||||
case 'mysql':
|
||||
dbClient = 'mysql2'
|
||||
break
|
||||
case 'mssql':
|
||||
dbClient = 'mssql'
|
||||
break
|
||||
case 'sqlite':
|
||||
dbClient = 'sqlite3'
|
||||
break
|
||||
default:
|
||||
WIKI.logger.error('Invalid DB Type')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
this.knex = Knex({
|
||||
client: dbClient,
|
||||
useNullAsDefault: true,
|
||||
connection: dbConfig,
|
||||
debug: WIKI.IS_DEBUG
|
||||
})
|
||||
|
||||
// Attempt to connect and authenticate to DB
|
||||
|
||||
this.inst.authenticate().then(() => {
|
||||
WIKI.logger.info(`Database (${WIKI.config.db.type}) connection: [ OK ]`)
|
||||
}).catch(err => {
|
||||
WIKI.logger.error(`Failed to connect to ${WIKI.config.db.type} instance.`)
|
||||
WIKI.logger.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
Objection.Model.knex(this.knex)
|
||||
|
||||
// Load DB Models
|
||||
|
||||
fs
|
||||
.readdirSync(dbModelsPath)
|
||||
.filter(file => {
|
||||
return (file.indexOf('.') !== 0 && file.indexOf('_') !== 0)
|
||||
})
|
||||
.forEach(file => {
|
||||
let modelName = _.upperFirst(_.camelCase(_.split(file, '.')[0]))
|
||||
self[modelName] = self.inst.import(path.join(dbModelsPath, file))
|
||||
})
|
||||
|
||||
// Associate DB Models
|
||||
|
||||
require(path.join(dbModelsPath, '_relations.js'))(self)
|
||||
const models = autoload(path.join(WIKI.SERVERPATH, 'db/models'))
|
||||
|
||||
// Set init tasks
|
||||
|
||||
let initTasks = {
|
||||
// -> Sync DB Schemas
|
||||
syncSchemas() {
|
||||
return self.inst.sync({
|
||||
force: false,
|
||||
logging: log => { WIKI.logger.log('debug', log) }
|
||||
// -> Migrate DB Schemas
|
||||
async syncSchemas() {
|
||||
return self.knex.migrate.latest({
|
||||
directory: path.join(WIKI.SERVERPATH, 'db/migrations'),
|
||||
tableName: 'migrations'
|
||||
})
|
||||
},
|
||||
// -> Set Connection App Name
|
||||
setAppName() {
|
||||
async setAppName() {
|
||||
switch (WIKI.config.db.type) {
|
||||
case 'postgres':
|
||||
return self.inst.query(`set application_name = 'WIKI.js'`, { raw: true })
|
||||
return self.knex.raw(`set application_name = 'Wiki.js'`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,6 +92,9 @@ module.exports = {
|
||||
|
||||
this.onReady = Promise.each(initTasksQueue, t => t()).return(true)
|
||||
|
||||
return this
|
||||
return {
|
||||
...this,
|
||||
...models
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -52,11 +52,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
async loadLocale(locale, opts = { silent: false }) {
|
||||
const res = await WIKI.db.Locale.findOne({
|
||||
where: {
|
||||
code: locale
|
||||
}
|
||||
})
|
||||
const res = await WIKI.db.locales.query().findOne('code', locale)
|
||||
if (res) {
|
||||
if (_.isPlainObject(res.strings)) {
|
||||
_.forOwn(res.strings, (data, ns) => {
|
||||
|
Reference in New Issue
Block a user