Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Jun 1, 2015
0 parents commit d71093c
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
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
39 changes: 39 additions & 0 deletions db.js
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;
70 changes: 70 additions & 0 deletions index.js
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;
16 changes: 16 additions & 0 deletions package.json
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"
}
}
50 changes: 50 additions & 0 deletions test/Metting.js
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;

15 changes: 15 additions & 0 deletions test/test.js
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);
});

0 comments on commit d71093c

Please sign in to comment.