const async = require('async')
const path = require('path')
const flatlog = require('./flatlog')
const importRegion = require('./phoenix/import-region')
const importScheduler = require('./phoenix/import-scheduler')
/**
- Setup new database
- If database is not exists, create new database then push sample data to it
- If database is early exists, drop database then push sample data to it
@exports module:reincarnate
@param {module:database.Database} db - Instance of database
@param {module:database.AssetDir} assetDir - Path to directory contains
static data
@param {StdCallback} callback - Function will be call after done
@example
// see constructor for detail
const reincarnate = require('./lib/reincarnate')
const mongodb = require('mongodb')
const path = require('path')
var db = mongodb.MongoClient.connect(url)
var asset = path.join(__dirname, 'asset/static')
reincarnate(db, asset, function(err) {
if (err) return
// do something here
})
*/
module.exports = function (db, assetDir, callback) {
const logger = flatlog({caller: 'gwisp'})
var calls = []
calls.push(function (callback) {
db.renew(callback)
})
calls.push(function (callback) {
logger.info('push region data to database')
var isofile = path.join(assetDir, 'region-iso-639.csv')
importRegion({db: db._dbclient, file: isofile}, callback)
})
calls.push(function (callback) {
logger.info('push scheduler to database')
importScheduler({db: db._dbclient, assetDir: assetDir}, callback)
})
async.series(calls, function (err) {
callback(err)
})
}