﻿var fs = require("fs");
var _ = require("lodash");
var path = require("path");
var async = require("async");
var config = require("config");
var cp = require("child_process");
var mongoose = require("mongoose");

/**
 * 启动数据库（Server）。
 * @param {Function(err)} [p_callback]
 */
exports.start = function (p_callback)
{
    var dbpath = path.resolve(__dirname, "../db");
    if (!fs.existsSync(dbpath))
    {
        fs.mkdirSync(dbpath);
    }
    
    if (fs.existsSync(dbpath))
    {
        cp.execFile("mongod", ["--dbpath", dbpath]);
        setTimeout(p_callback, 1000);
    }
    else
    {
        p_callback(new Error("Can not find/create database dir: " + dbpath));
    }
};

/**
 * 连接数据库。
 * @param {Function(err)} [p_callback]
 */
exports.connect = function (p_callback)
{
    mongoose.connect(
        config.db,
        {
            server: {
                auto_reconnect: config.auto_reconnect,
                poolSize: config.poolSize
            },
        },
        function (err)
        {
            _connected = !err;
            _monitor();
            if (_.isFunction(p_callback))
            {
                p_callback(err);
            }
        }
    );
};

var _connected = false;
/**
 * 检查数据库是否已连接。
 */
 exports.connected = function ()
{
    return _connected;
};

/**
 * 删除所有集合。
 * @param {Function(err)} [p_callback]
 */
 exports.dropAllCollections = function (p_callback)
{
    async.each(mongoose.connection.collections, function (collection, done)
    {
        collection.drop(done);
    },
    function (err)
    {
        if (_.isFunction(p_callback))
        {
            if (!err || err.message == "ns not found")
            {
                p_callback();
            }
            else
            {
                p_callback(err);
            }
        }
    });
};

/**
 * 监控数据库状态。
 */
var _monitor = function ()
{
    var db = mongoose.connection.db;
    db.on("reconnect", function ()
    {
        _connected = true;
    });
    
    db.on("close", function ()
    {
        _connected = false;
    });
    
    db.on("error", function ()
    {
        _connected = false;
    });
};
@@ -1,12 +1,31 @@
 %EF%BB%BF
-var
+%22use strict%22;%0D%0A%0D%0Aconst
  fs = re
@@ -38,19 +38,21 @@
 %22fs%22);%0D%0A
-var
+const
  _ = req
@@ -68,19 +68,21 @@
 ash%22);%0D%0A
-var
+const
  path = 
@@ -99,19 +99,21 @@
 ath%22);%0D%0A
-var
+const
  async =
@@ -132,19 +132,21 @@
 ync%22);%0D%0A
-var
+const
  config 
@@ -167,19 +167,21 @@
 fig%22);%0D%0A
-var
+const
  cp = re
@@ -205,19 +205,21 @@
 ess%22);%0D%0A
-var
+const
  mongoos
@@ -307,32 +307,39 @@
 callback%5D%0D%0A */%0D%0A
+module.
 exports.start = 
@@ -368,19 +368,21 @@
 %0A%7B%0D%0A    
-var
+const
  dbpath 
@@ -496,20 +496,16 @@
 %0A    %7D%0D%0A
-    
 %0D%0A    if
@@ -807,16 +807,23 @@
 %5D%0D%0A */%0D%0A
+module.
 exports.
@@ -1066,38 +1066,32 @@
 %7D,%0D%0A        
-function 
 (err)
+ =%3E
 %0D%0A        %7B%0D
@@ -1280,19 +1280,19 @@
 %0D%0A%7D;%0D%0A%0D%0A
-var
+let
  _connec
@@ -1327,25 +1327,31 @@
 %E5%90%A6%E5%B7%B2%E8%BF%9E%E6%8E%A5%E3%80%82%0D%0A */%0D%0A
- 
+module.
 exports.conn
@@ -1466,17 +1466,23 @@
 %5D%0D%0A */%0D%0A
- 
+module.
 exports.
@@ -1575,25 +1575,16 @@
 ections,
- function
  (collec
@@ -1594,16 +1594,19 @@
 n, done)
+ =%3E
 %0D%0A    %7B%0D
@@ -1654,22 +1654,16 @@
     
-function 
 (err)
+ =%3E
 %0D%0A  
@@ -1965,19 +1965,24 @@
 %E3%80%82%0D%0A */%0D%0A
-var
+function
  _monito
@@ -1982,28 +1982,16 @@
 _monitor
- = function 
 ()%0D%0A%7B%0D%0A 
@@ -1997,11 +1997,13 @@
     
-var
+const
  db 
@@ -2052,27 +2052,21 @@
 nnect%22, 
-function ()
+() =%3E
 %0D%0A    %7B%0D
@@ -2095,36 +2095,32 @@
 true;%0D%0A    %7D);%0D%0A
-    
 %0D%0A    db.on(%22clo
@@ -2124,27 +2124,21 @@
 close%22, 
-function ()
+() =%3E
 %0D%0A    %7B%0D
@@ -2176,20 +2176,16 @@
    %7D);%0D%0A
-    
 %0D%0A    db
@@ -2197,27 +2197,21 @@
 error%22, 
-function ()
+() =%3E
 %0D%0A    %7B%0D

update for es6 of node default