这个DB类也算是我经历了3个实际项目应用的,现分享出来,有需要的请借鉴批评。
上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的。
关于mongoose的安装就是 npm install -g mongoose
这个DB类的数据库配置是基于auth认证的,如果您的数据库没有账号与密码则留空即可。
var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
var logger = require('pomelo-logger').getLogger('mongodb-log');
var options = {
db_user: "game",
db_pwd: "12345678",
db_host: "192.168.2.20",
db_port: 27017,
db_name: "dbname"
};
var dbURL = "mongodb://" + options.db_user + ":" + options.db_pwd + "@" + options.db_host + ":" + options.db_port + "/" + options.db_name;
mongoose.connect(dbURL);
mongoose.connection.on('connected', function (err) {
if (err) logger.error('Database connection failure');
});
mongoose.connection.on('error', function (err) {
logger.error('Mongoose connected error ' + err);
});
mongoose.connection.on('disconnected', function () {
logger.error('Mongoose disconnected');
});
process.on('SIGINT', function () {
mongoose.connection.close(function () {
logger.info('Mongoose disconnected through app termination');
process.exit(0);
});
});
var DB = function () {
this.mongoClient = {};
var filename = path.join(path.dirname(__dirname).replace('app', ''), 'config/table.json');
this.tabConf = JSON.parse(fs.readFileSync(path.normalize(filename)));
};
DB.prototype.getConnection = function (table_name) {
if (!table_name) return;
if (!this.tabConf[table_name]) {
logger.error('No table structure');
return false;
}
var client = this.mongoClient[table_name];
if (!client) {
//构建用户信息表结构
var nodeSchema = new mongoose.Schema(this.tabConf[table_name]);
//构建model
client = mongoose.model(table_name, nodeSchema, table_name);
this.mongoClient[table_name] = client;
}
return client;
};
DB.prototype.save = function (table_name, fields, callback) {
if (!fields) {
if (callback) callback({msg: 'Field is not allowed for null'});
return false;
}
var err_num = 0;
for (var i in fields) {
if (!this.tabConf[table_name][i]) err_num ++;
}
if (err_num > 0) {
if (callback) callback({msg: 'Wrong field name'});
return false;
}
var node_model = this.getConnection(table_name);
var mongooseEntity = new node_model(fields);
mongooseEntity.save(function (err, res) {
if (err) {
if (callback) callback(err);
} else {
if (callback) callback(null, res);
}
});
};
DB.prototype.update = function (table_name, conditions, update_fields, callback) {
if (!update_fields || !conditions) {
if (callback) callback({msg: 'Parameter error'});
return;
}
var node_model = this.getConnection(table_name);
node_model.update(conditions, {$set: update_fields}, {multi: true, upsert: true}, function (err, res) {
if (err) {
if (callback) callback(err);
} else {
if (callback) callback(null, res);
}
});
};
DB.prototype.updateData = function (table_name, conditions, update_fields, callback) {
if (!update_fields || !conditions) {
if (callback) callback({msg: 'Parameter error'});
return;
}
var node_model = this.getConnection(table_name);
node_model.findOneAndUpdate(conditions, update_fields, {multi: true, upsert: true}, function (err, data) {
if (callback) callback(err, data);
});
};
DB.prototype.remove = function (table_name, conditions, callback) {
var node_model = this.getConnection(table_name);
node_model.remove(conditions, function (err, res) {
if (err) {
if (callback) callback(err);
} else {
if (callback) callback(null, res);
}
});
};
DB.prototype.find = function (table_name, conditions, fields, callback) {
var node_model = this.getConnection(table_name);
node_model.find(conditions, fields || null, {}, function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res);
}
});
};
DB.prototype.findOne = function (table_name, conditions, callback) {
var node_model = this.getConnection(table_name);
node_model.findOne(conditions, function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res);
}
});
};
DB.prototype.findById = function (table_name, _id, callback) {
var node_model = this.getConnection(table_name);
node_model.findById(_id, function (err, res){
if (err) {
callback(err);
} else {
callback(null, res);
}
});
};
DB.prototype.count = function (table_name, conditions, callback) {
var node_model = this.getConnection(table_name);
node_model.count(conditions, function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res);
}
});
};
DB.prototype.distinct = function (table_name, field, conditions, callback) {
var node_model = this.getConnection(table_name);
node_model.distinct(field, conditions, function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res);
}
});
};
DB.prototype.where = function (table_name, conditions, options, callback) {
var node_model = this.getConnection(table_name);
node_model.find(conditions)
.select(options.fields || '')
.sort(options.sort || {})
.limit(options.limit || {})
.exec(function (err, res) {
if (err) {
callback(err);
} else {
callback(null, res);
}
});
};
module.exports = new DB();
这个类库使用方法如下:
//先包含进来
var MongoDB = require('./mongodb');
//查询一条数据
MongoDB.findOne('user_info', {_id: user_id}, function (err, res) {
console.log(res);
});
//查询多条数据
MongoDB.find('user_info', {type: 1}, {}, function (err, res) {
console.log(res);
});
//更新数据并返回结果集合
MongoDB.updateData('user_info', {_id: user_info._id}, {$set: update_data}, function(err, user_info) {
callback(null, user_info);
});
//删除数据
MongoDB.remove('user_data', {user_id: 1});
就先举这些例子,更多的可亲自尝试吧!
其中配置中的 config/table.json 是数据库集合的配置项,结构如下:
{
"user_stats_data": {
"user_id": "Number",
"platform": "Number",
"user_first_time": "Number",
"create_time": "Number"
},
"room_data": {
"room_id": "String",
"room_type": "Number",
"user_id": "Number",
"player_num": "Number",
"diamond_num": "Number",
"normal_settle": "Number",
"single_settle": "Number",
"create_time": "Number"
},
"online_data": {
"server_id": "String",
"pf": "Number",
"player_num": "Number",
"room_list": "String",
"update_time": "Number"
}
}
记得每次给添加字段时,要往这个table.json里面添加。由于nodejs这个服务器的改动,更改table.json往往需要重启游戏服务的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。