mongoose的DAO写法问题
mongoose的相关函数
/mongodb/department.js
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://nodeoper:[email protected]:27017/itsys');
db.on('error', function(error) {
console.log(error);
});
var DepartmentSchema = mongoose.Schema({
DepartmentID : {type : String,default:"default ID"},
DepartmentName : {type : String, default:"default Name"},
DepartmentPosition : {type : String, default:"{'p1','p2','p3'}"},
});
DepartmentSchema.methods.findbyID = function(other, callback) {
return this.model('DepartmentModel').find({other: other}, callback);
}
DepartmentSchema.methods.findbyName = function(name, callback) {
return this.model('DepartmentModel').find({name: name}, callback);
}
var DepartmentModel = db.model('DepartmentModel', DepartmentSchema);
//====以下是curd的操作,不同的schema操作的时候,下面的代码很大一部分是重复的=====
function DepartmentSave(doc){
//var OperationModel = DepartmentSchema();
//var OperationEntity = new OperationModel(doc);
var OperationEntity = new DepartmentModel(doc);
OperationEntity.save(function(error) {
if(error) {
console.log(error);
} else {
console.log('saved OK!');
}
db.close();
});
}
function DepartmentCreate(doc){
//var OperationModel = DepartmentSchema();
DepartmentModel.create(doc, function(error){
if(error) {
console.log(error);
} else {
console.log('save ok');
}
db.close();
});
}
function DepartmentUpdate(conditions,update){
var options = {upsert : true};
//var OperationModel = DepartmentSchema();
DepartmentModel.update(conditions, update, options, function(error){
if(error) {
console.log(error);
} else {
console.log('update ok!');
}
db.close();
});
}
function DepartmentFindbyID(){
//var OperationModel = DepartmentSchema();
var OperationEntity = new DepartmentModel();
DepartmentModel.findbyID('model_demo_username', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
db.close();
});
}
function DepartmentFindbyName(){
//var OperationModel = DepartmentSchema();
var OperationEntity = new DepartmentModel();
OperationModel.findbyName('model_demo_username', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
db.close();
});
}
exports.Save = DepartmentSave;
exports.Create = DepartmentCreate;
exports.Update = DepartmentUpdate;
exports.FindbyID = DepartmentFindbyID;
exports.FindbyName = DepartmentFindbyName;
app.js的调用
var departmentDao = require('./routes/mongodb/department');
app.get('/save',function(req, res, next){
var data = "{
DepartmentID : 'emtity_demo_username',
DepartmentName : 'emtity_demo_title',
DepartmentPosition:'{'develop','operation','manager'}'
}"; //为了方便大家看我这里做了格式变动
var result = departmentDao.Save(data);
res.send(result);
});
我的思路是将数据操作封装在一个个dao中,然后由app.js充当service层,调用各种方法,但是我想再加一层单例模式,把数据库操作封起来,但是有几个问题: 1、单例之后,这对nodejs的异步有何影响? 2、除了schema不同,curd的操作代码可否统一?如何把该文件拆开? 3、mongo有一点没太明白,如果看成schema类似于oracle的表结构,那岂不是数据库就是个放json的数据仓库。。。表结构都是程序控制了。。。 4、好好说话,不要喷,欢迎大神指出各种问题 = =
5 回复
@CarlosRen 是的,但我这里的 services 就会是面对一个功能面做的,这个service 会有多个Model或proxy参与,而proxy就是model的扩展,目前是为Model减血,不想将其变成充血模型
这里是我的一个项目里的model层,这里也是用于定义schema,通过 m.model(xxx)的方法取得model的实例,之后通过create、save、find等mongooseAPI 调用 CURD,这个我是从CNODE 里源码里学回来的 `` var m = require(‘mongoose’);
var s = m.Schema;
var schema = new s({
"area_code":String,
"area_name":String,
"area_level":Number,
"area_lat":{type:Number,default:22.518053},
"area_lng":{type:Number,default:113.390857},
"cities":[
{
type: s.Types.ObjectId, ref:'mmArea'
}
],
sort:Number,
createAt : { type:Number , default: Date.now }
});
m.model('mmArea’,schema);``