1 |
|
2 |
|
3 |
|
4 | var mongoose = require('../modules/mongoose.js');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function base(className, schema) {
|
11 | var SpeSchema = mongoose.Schema(schema);
|
12 | this.model = mongoose.model(className, SpeSchema);
|
13 | }
|
14 | base.prototype.add = function(d, success, fail){
|
15 | var s = new this.model(d);
|
16 | s.save(function (err, o) {
|
17 | if (err) {
|
18 | console.error(err);
|
19 | fail && fail(err);
|
20 | } else {
|
21 | success && success(o);
|
22 | }
|
23 | });
|
24 | };
|
25 | base.prototype.find = function(d, success, fail) {
|
26 | if(typeof d === 'function'){
|
27 | fail = success,
|
28 | success = d;
|
29 | this.model.find(_do);
|
30 | }else{
|
31 | this.model.find(d, _do);
|
32 | }
|
33 | function _do(err, list) {
|
34 | if (err) {
|
35 | console.error(err);
|
36 | fail && fail(err);
|
37 | } else {
|
38 | success && success(list);
|
39 | }
|
40 | }
|
41 | };
|
42 | base.prototype.getMongoose = function(d){
|
43 | return mongoose;
|
44 | };
|
45 | base.prototype.getObjectId = function () {
|
46 | return new mongoose.Types.ObjectId;
|
47 | }
|
48 | module.exports = base;
|