DemocracyOS/democracyos

View on GitHub
bin/dos-db

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

/**
 * Module dependencies.
 */

var fs = require('fs')
var dir = fs.readdirSync
var read = fs.readFileSync
var exists = fs.existsSync
var path = require('path')
var program = require('commander')
var resolve = path.resolve
var parse = JSON.parse
var mongoose = require('mongoose')
var prompt = require('co-prompt')
var confirm = prompt.confirm

program
  .option('-u, --user <name>', 'MongoDB username.')
  .option('-p, --password <password>', 'MongoDB password.')
  .option('-h, --host <name>', 'MongoDB host. [localhost]', String, 'localhost')
  .option('-p, --port <port>', 'MongoDB port. [27017]', Number, 27017)
  .option('-d, --database <name>', 'MongoDB database name.', String, 'DemocracyOS-dev')
  .option('-U, --url <url>', 'MongoDB URL. (mongodb://user:pass@localhost:27017/DemocracyOS)')

program
  .command('load <model> <source>')
  .description("Loads model's collection from .json source path.")
  .action(function (model, source) {
    var mPath = resolve('./lib/models/' + model.toLowerCase() + '.js')
    var sPath = resolve(source)

    if (!exists(mPath)) return write('Invalid api "' + model + '"')
    if (!exists(sPath)) return write('Invalid source "' + source + '"')

    var data = parse(read(sPath))
    if (!data.length) return write('No data in file.')

    connect(function () {
      models()
      var api = require('lib/db-api')
      var c = data.length
      data.forEach(function (doc) {
        api[model.toLowerCase()].create(doc, function (err) {
          if (err) return error('Error %s while saving %j', err, doc)
          console.log('Saved %j', doc)
          if (--c === 0) disconnect(exit)
        })
      })
    })
  })

program
  .command('drop')
  .description('Fully drop database.')
  .action(function () {
    confirm('This will delete all your data. Continue?\n')(function (err, ok) {
      if (err) return error('Whoops, %s', err)
      if (!ok) return write('There you go! =)')

      connect(function () {
        mongoose.connection.db.dropDatabase(function (err) {
          if (err) return error('Whoops, %s', err)
          console.log('Database dropped.')
          disconnect(exit)
        })
      })
    })
  })

program.parse(process.argv)

/**
 * Write `msg` to stdout
 *
 * @param {String} msg
 * @requires process
 * @api private
 */

function write (msg) {
  process.stdout.write(msg)
  process.stdout.write('\n')
}

/**
 * Write err `msg` to console
 *
 * @param {String} msg
 * @requires process
 * @api private
 */

function error () {
  console.log.apply(console, arguments)
  process.exit(1)
}

/**
 * Exit from process
 *
 * @requires process
 * @api private
 */

function exit () {
  process.exit(0)
}

/**
 * Connects to mongoDB
 *
 * @param {Function} fn
 * @requires program
 * @requires mongoose
 * @api private
 */

function connect (fn) {
  write('Connecting to ' + mongoURL())
  fn = fn || function () {}
  mongoose.connect(mongoURL(), fn)
}

/**
 * Registers al available models
 *
 * @param {Function} fn
 * @requires path.
 */
function models (fn) {
  write('Register mongoose models.')
  var except = ['index.js', 'forum-reserved-names.js']
  var files = dir(resolve('./lib/models/'))
  files && files.forEach(function (f) {
    if (~except.indexOf(f)) return
    require(resolve('./lib/models/' + f))(mongoose)
  })
  fn && fn()
}

/**
 * Disconnects from mongoDB
 *
 * @param {Function} fn
 * @requires program
 * @requires mongoose
 * @api private
 */

function disconnect (fn) {
  write('Disconnecting from ' + mongoURL())
  if (!mongoose.connections.length) return console.log('Not connected to DB!')
  fn = fn || function () {}
  mongoose.disconnect(fn)
}

/**
 * Make mongodb url from program
 *
 * @requires program from upper scope
 * @api private
 */

function mongoURL () {
  return program.url || 'mongodb://' +
  (program.user ? program.user + ':' + program.password + '@' : '') +
  program.host +
  ':' +
  program.port +
  '/' +
  program.database
}