wikijs-fork/server/core/system.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

const _ = require('lodash')
const cfgHelper = require('../helpers/config')
const Promise = require('bluebird')
/* global wiki */
2017-02-13 05:59:52 +00:00
module.exports = {
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.telemetry.sendEvent('setup', 'upgradeFromMongo')
2017-02-13 05:59:52 +00:00
2017-12-17 04:41:16 +00:00
wiki.logger.info('Upgrading from MongoDB...')
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.db.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)
} catch (err) {
reject(err)
}
db.close()
})
2017-02-13 05:59:52 +00:00
})
}
}