UNPKG

1.11 kBJavaScriptView Raw
1/**
2 * Created by Administrator on 2016/1/15.
3 */
4var mongoose = require('../modules/mongoose.js');
5/**
6 * 基类
7 * @param className
8 * @param schema
9 */
10function base(className, schema) {
11 var SpeSchema = mongoose.Schema(schema);
12 this.model = mongoose.model(className, SpeSchema);
13}
14base.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};
25base.prototype.find = function(d, success, fail) {
26 if (d) {
27 this.model.find(d, _do);
28 } else {
29 this.model.find(_do);
30 }
31 function _do(err, list) {
32 if (err) {
33 console.error(err);
34 fail && fail(err);
35 } else {
36 success && success(list);
37 }
38 }
39};
40base.prototype.getMongoose = function(d){
41 return mongoose;
42};
43base.prototype.getObjectId = function () {
44 return new mongoose.Types.ObjectId;
45}
46module.exports = base;