-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongodbEngine.js
79 lines (62 loc) · 2.12 KB
/
MongodbEngine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var mongoose = require('mongoose')
var config = require('./config.json');
function connect(callbackvalue) {
mongoose.connect(config.mongodbUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', callbackvalue);
}
function insert(schema, json, callbackvalue) {
var db = mongoose.connection;
var objmodel = db.model(schema.options.collection, schema);
var obj = new objmodel(json);
obj.save(function(err, result) {
callbackvalue(err, result);
});
}
function insertCategory(schema, json, callbackvalue) {
var db = mongoose.connection;
var objmodel = db.model(schema.options.collection, schema);
var obj = new objmodel(json);
obj.save(function(err, result) {
callbackvalue(err, result);
});
}
function fetchOne(schema, query, callbackvalue) {
var db = mongoose.connection;
var objmodel = db.model(schema.options.collection, schema);
objmodel.findOne(query, function(err, result) {
callbackvalue(err, result);
});
}
function fetchAll(schema, query, callbackvalue) {
var db = mongoose.connection;
var objmodel = db.model(schema.options.collection, schema);
objmodel.find(query, function(err, result) {
callbackvalue(err, result);
});
}
function remove(schema, json, callbackvalue) {
var db = mongoose.connection;
var objmodel = db.model(schema.options.collection, schema);
objmodel.remove({
_id: json._id
}, function(err) {
callbackvalue(err);
});
}
function fetchOneAndUpdate(schema, query, update, options, callbackvalue) {
var db = mongoose.connection;
var objmodel = db.model(schema.options.collection, schema);
objmodel.findOneAndUpdate(query, update, options, function(err, result) {
callbackvalue(err, result);
});
}
module.exports.connect = connect;
module.exports.insert = insert;
module.exports.insertCategory = insertCategory;
module.exports.remove = remove;
module.exports.fetchOne = fetchOne;
module.exports.fetchAll = fetchAll;
module.exports.fetchOneAndUpdate = fetchOneAndUpdate;
module.exports.mongoose = mongoose;