-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d71093c
Showing
6 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var connectionString, db, mongoose, options; | ||
|
||
mongoose = require('mongoose'); | ||
|
||
var host = "127.0.0.1"; | ||
var port = "27017"; | ||
var db = "test-mongoose-dao"; | ||
|
||
connectionString = 'mongodb://' + host + ':' + port + '/' + db + ''; | ||
|
||
options = { | ||
db: { | ||
native_parser: true | ||
}, | ||
server: { | ||
auto_reconnect: true, | ||
poolSize: 5 | ||
} | ||
}; | ||
|
||
mongoose.connect(connectionString, options, function(err, res) { | ||
if (err) { | ||
console.log('[mongoose log] Error connecting to: ', +connectionString + '. ' + err); | ||
return process.exit(1); | ||
} else { | ||
return console.log('[mongoose log] Successfully connected to: ', +connectionString); | ||
} | ||
}); | ||
|
||
db = mongoose.connection; | ||
|
||
db.on('error', console.error.bind(console, 'mongoose connection error:')); | ||
|
||
db.once('open', function() { | ||
return console.log('mongoose open success'); | ||
}); | ||
|
||
|
||
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
function CommonDao (Model){ | ||
/*if(typeof Model === 'undefined' || Model == null) | ||
throw new Error('Model can not be null.'); | ||
*/ | ||
this.model = Model; | ||
} | ||
|
||
CommonDao.prototype.create = function (doc,callback){ | ||
this.model.create(doc, function (error) { | ||
if(error) return callback(error); | ||
|
||
return callback(null); | ||
}); | ||
}; | ||
|
||
CommonDao.prototype.getById = function (id, callback) { | ||
this.model.findOne({_id:id}, function(error, model){ | ||
if(error) return callback(error,null); | ||
|
||
return callback(null,model); | ||
}); | ||
}; | ||
|
||
CommonDao.prototype.countByQuery = function (query, callback) { | ||
this.model.count(query, function(error, model){ | ||
if(error) return callback(error,null); | ||
|
||
return callback(null,model); | ||
}); | ||
}; | ||
|
||
CommonDao.prototype.getByQuery = function (query,fileds,opt,callback) { | ||
this.model.find(query, fileds, opt, function(error,model){ | ||
if(error) return callback(error,null); | ||
|
||
return callback(null,model); | ||
}); | ||
}; | ||
|
||
|
||
CommonDao.prototype.getAll = function (callback) { | ||
this.model.find({}, function(error,model){ | ||
if(error) return callback(error,null); | ||
|
||
return callback(null, model); | ||
}); | ||
}; | ||
/* | ||
CommonDao.prototype.getAllModelByOption = function (opt, callback) { | ||
CommonDao.getModelByQuery({},{},opt, callback); | ||
};*/ | ||
|
||
|
||
CommonDao.prototype.delete = function (query, callback){ | ||
this.model.remove(query, function(error){ | ||
if(error) return callback(error); | ||
|
||
return callback(null); | ||
}); | ||
}; | ||
|
||
CommonDao.prototype.update = function( conditions, update ,options, callback) { | ||
this.model.update(conditions, update, options, function (error) { | ||
if(error) return callback(error); | ||
|
||
return callback(null); | ||
}); | ||
}; | ||
|
||
module.exports = CommonDao; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "mongoosedao", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^2.3.0", | ||
"mocha": "^2.2.5", | ||
"mongoose": "^4.0.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var mongoose = require('mongoose'); | ||
var Schema = mongoose.Schema; | ||
|
||
|
||
UserSchema = new Schema({ | ||
username: { | ||
type: String, | ||
required: true, | ||
index: { | ||
unique: true | ||
} | ||
}, | ||
password: { | ||
type: String, | ||
required: true | ||
}, | ||
avatar: String, | ||
address: String, | ||
created_at: { | ||
type: Date, | ||
"default": Date.now | ||
} | ||
}); | ||
|
||
UserSchema.methods.find_by_name = function(cb) { | ||
return this.model('UserModel').find({ | ||
username: this.username | ||
}, cb); | ||
}; | ||
|
||
UserSchema.methods.is_exist = function(cb) { | ||
var query; | ||
query = { | ||
username: this.username, | ||
password: this.password | ||
}; | ||
return this.model('UserModel').findOne(query, cb); | ||
}; | ||
|
||
UserSchema.statics.delete_by_name = function(name, cb_succ, cb_fail) {}; | ||
|
||
var UserModel = mongoose.model('UserModel', UserSchema); | ||
|
||
var CommonDao = require('../'); | ||
|
||
|
||
var MettingDao = new CommonDao(UserModel); | ||
|
||
module.exports = MettingDao; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require('../db'); | ||
|
||
|
||
Metting = require('./Metting'); | ||
|
||
|
||
Metting.create({"username":"sss","password":"password"},function(err, user){ | ||
console.log(user); | ||
}); | ||
|
||
|
||
|
||
Metting.delete({"username":"sss","password":"password"},function(err, user){ | ||
console.log(user); | ||
}); |