NodeJS Mongodb 创建数据库、集合、数据库用户自动化脚本

NodeJS Mongodb 创建数据库、集合、数据库用户自动化脚本

关注本人微信公众号,目前已经发布了完整Docker教程,未来继续努力分享更多资源给您,谢谢!

NodeJS Mongodb 创建数据库、集合、数据库用户自动化脚本_第1张图片

/**
 * Created by lzy on 2017/8/1.
 *
 * system config
 */
var SysConfig = {};
var DB = {};
var USER_ADMIN = {};
var DB_ADMIN = {};

/*当前项目数据库配置*/
DB.ip = '127.0.0.1';
DB.port = '27017';
DB.database = 'blogger';
DB.username = 'lzy';
DB.password = '123456';

/*用户管理员账号*/
USER_ADMIN.username = 'useradmin';
USER_ADMIN.password = '123456';

/*数据库管理员账号*/
DB_ADMIN.username = 'dbadmin';
DB_ADMIN.password = '123456';

SysConfig.DB = DB;
SysConfig.UA = USER_ADMIN;
SysConfig.DA = DB_ADMIN;

module.exports = SysConfig;

 

 

 

/**
 * Created by lzy on 2017/8/1.
 *
 * 创建数据库、用户、以及集合脚本
 *
 * version info:
 * npm 3.10.10
 * node v6.11.1
 * mongodb v3.4.6
 */
var dbConfig = require('../config/sys-config').DB;
var userAdmin = require('../config/sys-config').UA;
var dbAdmin = require('../config/sys-config').DA;
var mongoClient = require('mongodb').MongoClient;

var mongoUri = "mongodb://" + dbAdmin.username + ':' + dbAdmin.password + '@' + dbConfig.ip + ':' + dbConfig.port + '/admin';

mongoClient.connect(
    mongoUri,
    connectDbCallback
);

/*连接后回调*/
function connectDbCallback(err, db){
    if (err){
        console.log('Connection Failed!');
    }else {
        console.log('Connection Success!');
        try{
            connectSuccessCallback(db);
        }catch (e){
            console.log('Oper Database Error:' + e);
            db.close();
            return;
        }
    }
}

/*连接成功调用*/
function connectSuccessCallback(db){
    /*删除数据库*/
    dropDb(db);
}

/*添加用户*/
function addUser(db){
    var admin = db.admin();
    var bloggerDB = db.db(dbConfig.database);
    admin.authenticate(userAdmin.username, userAdmin.password, function(err, results){
        if (err){
            console.log('Authenticate userAdmin Failed! result:' + err);
        }else {
            console.log('Authenticate userAdmin Success!');
            bloggerDB.removeUser(dbConfig.username, function(err, results){
                if (err){
                    console.log('Delete user Failed! result:' + err);
                }else{
                    console.log('Delete user Success!');
                }
                bloggerDB.addUser(dbConfig.username, dbConfig.password, function(err, results){
                    if (err){
                        console.log('Add user Failed! result:' + err);
                        closeDB(db);
                        return;
                    }else {
                        console.log('Add user Success!');
                        bloggerDB.authenticate(dbConfig.username, dbConfig.password, function(err, results){
                            if (err){
                                console.log('Auth user Failed! result:' + err);
                                closeDB(db);
                                return;
                            }else {
                                console.log('Auth user Success!');
                                closeDB(db);
                            }
                        })
                    }
                })
            });
        }
    })
}

/*是否创建成功*/
function isCreateSuccess(db){
    var admin = db.admin();
    admin.listDatabases(function(err, results){
        if (err){
            console.log('show dbs error');
            closeDB(db);
            return;
        }
        var found = false;
        try{
            console.log('Current Databases:' + results.databases);
            results.databases.forEach(function(element, index) {
                if (element.name == dbConfig.database){
                    found = true;
                }
            })
        }catch (e) {
            console.log('show dbs error: errMsg:' + e);
            closeDB(db);
            return;
        }

        if (found){
            console.log('Create Database ' + dbConfig.database + ' Success!');
            addUser(db);
        }else {
            console.log('Create Database ' + dbConfig.database + ' Failed!');
            closeDB(db);
            return;
        }
    })
}

/*删除DB*/
function dropDb(db){
    db.db(dbConfig.database).dropDatabase(function(err, results){
        if (!err){
            console.log('Drop ' + dbConfig.database + ' Database Success');
            /*显示删除后的数据库列表*/
            showDbs(db);
        }else {
            console.log('Drop ' + dbConfig.database + ' Database Failed; result: ' + err);
            closeDB(db);
        }
    })
}

/*显示数据库列表*/
function showDbs(db){
    var admin = db.admin();
    admin.listDatabases(function(err, results){
        if (err){
            console.log("Show dbs error");
            closeDB(db);
            return;
        }
        console.log("Current Database List: ");
        console.log(results.databases);
        createDbAndCol(db);
    })
}


/*创建数据库和集合*/
function createDbAndCol(db){
    var bloggerDB = db.db(dbConfig.database);
    bloggerDB.createCollection('example', function(err, col){
        if (err){
            console.log('Create Database ' + dbConfig.database + ' and example ' + ' Col Failed!' + err);
            closeDB(db);
            return;
        }
        console.log('Create Database ' + dbConfig.database + ' and example ' + ' Col Success!');
        isCreateSuccess(db);
    })
}

/*关闭DB*/
function closeDB(db){
    db.close(true, function(err, results){
        if (!err){
            console.log('Close DB Success!');
        }else {
            console.log('Close DB Failed! Msg:' + err);
        }
    });
}

 

你可能感兴趣的:(NodeJS Mongodb 创建数据库、集合、数据库用户自动化脚本)