wikijs-fork/server/core/system.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

const _ = require('lodash')
const cfgHelper = require('../helpers/config')
const Promise = require('bluebird')
2019-01-26 23:35:56 +00:00
const fs = require('fs-extra')
const path = require('path')
/* global WIKI */
2017-02-13 05:59:52 +00:00
module.exports = {
2019-01-13 20:37:45 +00:00
updates: {
channel: 'BETA',
version: WIKI.version,
releaseDate: WIKI.releaseDate,
minimumVersionRequired: '2.0.0-beta.0',
minimumNodeRequired: '10.12.0'
},
init() {
2019-01-26 23:35:56 +00:00
// Clear content cache
fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'cache'))
2019-01-26 23:35:56 +00:00
2019-01-13 20:37:45 +00:00
return this
},
2017-02-14 04:30:42 +00:00
/**
* Upgrade from WIKI.js 1.x - MongoDB database
2017-02-14 04:30:42 +00:00
*
* @param {Object} opts Options object
2017-02-14 04:30:42 +00:00
*/
async upgradeFromMongo (opts) {
WIKI.logger.info('Upgrading from MongoDB...')
2017-12-17 04:41:16 +00:00
let mongo = require('mongodb').MongoClient
let parsedMongoConStr = cfgHelper.parseConfigValue(opts.mongoCnStr)
2017-02-13 05:59:52 +00:00
return new Promise((resolve, reject) => {
// Connect to MongoDB
mongo.connect(parsedMongoConStr, {
autoReconnect: false,
reconnectTries: 2,
reconnectInterval: 1000,
connectTimeoutMS: 5000,
socketTimeoutMS: 5000
}, async (err, db) => {
try {
if (err !== null) { throw err }
let users = db.collection('users')
// Check if users table is populated
let userCount = await users.count()
if (userCount < 2) {
throw new Error('MongoDB Upgrade: Users table is empty!')
2017-02-13 05:59:52 +00:00
}
// Import all users
let userData = await users.find({
email: {
$not: 'guest'
}
}).toArray()
await WIKI.models.User.bulkCreate(_.map(userData, usr => {
return {
email: usr.email,
name: usr.name || 'Imported User',
password: usr.password || '',
provider: usr.provider || 'local',
providerId: usr.providerId || '',
role: 'user',
createdAt: usr.createdAt
}
}))
2017-02-14 04:30:42 +00:00
resolve(true)
2019-09-08 16:39:05 +00:00
} catch (errc) {
reject(errc)
}
db.close()
})
2017-02-13 05:59:52 +00:00
})
}
}