Node.js对于MongoDB的操作DAO的封装
在看下面的代码之前,希望你阅读过这个,在阅读完之后,你应该就会明白,我下面的这些代码的整体思想。
首先,下面是代码的大体的样子
var mongoClient = require('mongodb').MongoClient;
function MongoDB(url) {
...
}
...
MongoDB.prototype.insert = function() { ...}
...
module.exports = MongoDB;
我通过module.exports导出了写的一个“类”(函数),然后在这个函数上添加了一些原型方法,然后我还创建了一个connect函数,这个函数的作用就是连接数据库
//插入数据(一条或者多条)
MongoDB.prototype.insert = function(collectionName, data) {
if(!typeof collectionName == 'string' || !typeof data == 'object') {
throw new Error('请检查传入的参数是否正确');
}
connect(this.url, function(err, db) {
if(Array.isArray(data)) {
db.collection(collectionName).insertMany(data).then(function(result) {
if(result) {
console.log('插入多条数据成功');
}else {
console.log('插入多条数据失败');
}
});
}else {
db.collection(collectionName).insertOne(data).then(function(result) {
if(result) {
console.log('插入一条数据成功');
}else {
console.log('插入一条数据失败');
}
});
}
});
}
正向前面我所写的那样,我给connect
,这个函数中传递了一个回掉函数,当打开数据库的时候,调用真正的插入操作
function connect(url, callback) {
if(!url) {
throw new Error('url不符合预期');
return;
}
mongoClient.connect(url, function(err, db) {
callback(err, db);
db.close();
});
}
这样做的目的是因为在多次尝试下的选择,因为在Node.js进行I/O操作的时候就是异步的,刚开始我想的是在MongoDB
这个构造函数里面加一句this.db = null
,然后调用原型方法connect
,当打开连接的时候,给this.db
赋值,结果实践中发现,this.db
始终是null的,所以,其他的原型方法一直不能成功调用。因此,才写了单独的connect
这个函数,而不是原型方法。
完整代码如下所示:
var mongoClient = require('mongodb').MongoClient;
function MongoDB(url) {
this.url = url;
this.db = null;
}
//连接
function connect(url, callback) {
if(!url) {
throw new Error('url不符合预期');
return;
}
mongoClient.connect(url, function(err, db) {
callback(err, db);
db.close();
});
}
//插入数据(一条或者多条)
MongoDB.prototype.insert = function(collectionName, data) {
if(!typeof collectionName == 'string' || !typeof data == 'object') {
throw new Error('请检查传入的参数是否正确');
}
connect(this.url, function(err, db) {
if(Array.isArray(data)) {
db.collection(collectionName).insertMany(data).then(function(result) {
if(result) {
console.log('插入多条数据成功');
}else {
console.log('插入多条数据失败');
}
});
}else {
db.collection(collectionName).insertOne(data).then(function(result) {
if(result) {
console.log('插入一条数据成功');
}else {
console.log('插入一条数据失败');
}
});
}
});
}
//更新数据(falg=true时更新一条,flag=false时更新多条)
MongoDB.prototype.update = function(flag, collectionName, condition, data) {
if(typeof collectionName != 'string') {
throw new Error('请检查传入的参数是否正确');
}
connect(this.url, function(err, db) {
if(flag) {
db.collection(collectionName).updateOne(condition, data).then(function(result) {
console.log('更新成功');
});
}else {
db.collection(collectionName).updateMany(condition, data).then(function(result) {
console.log('更新成功');
});
}
});
}
//删除数据(falg=true时删除一条,flag=false时删除多条)
MongoDB.prototype.delete = function(flag, collectionName, condition) {
if(arguments.length != 3) {
throw new Error('参数的必须为3个');
}
connect(this.url, function(err, db) {
if(flag) {
db.collection(collectionName).deleteOne(condition).then(function(result) {
console.log("删除一条记录成功");
})
}else {
db.collection(collectionName).deleteMany(condition).then(function(result) {
console.log("删除多条记录成功");
})
}
});
}
//查找 可以查找所有文档,也可以根据条件进行查找
MongoDB.prototype.find = function(collectionName, condition, callback) {
var result;
if(arguments.length != 3) {
throw new Error('参数的个数必须为三');
}
connect(this.url, function(err, db) {
var cursor = db.collection(collectionName).find(condition);
cursor.each(function(err, doc) {
if(err) {
callback(err, null);
}
if(doc != null) {
result.push(doc);
}else {
callback(null, result);
}
})
});
}
module.exports = MongoDB;
今天已经将昨天未完工的find部分完工,关于这部分代码地址,下载地址