forked from stemey/mongomat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaCrud.js
106 lines (95 loc) · 2.86 KB
/
MetaCrud.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var GenericCrud = require('./GenericCrud');
var ejsonHelper = require('./ejsonHelper');
var DbQueryParser = require('./DbQueryParser');
var util = require('util');
var MetaCrud = function (config) {
GenericCrud.call(this, config);
this.queryParser = new DbQueryParser(config);
this.db = config.db;
}
util.inherits(MetaCrud, GenericCrud);
MetaCrud.prototype.insert = function (collection, doc, res, next) {
var me = this;
collection.count({collection: doc.collection}, function (e, result) {
if (result > 0) {
res.status(512).send({status: 512, message: "the collection already exists"})
} else {
var col = me.db(doc.db).createCollection(doc.collection, function (e, results) {
if (e) {
next(e);
} else {
collection.insert(doc, {}, function (e, result) {
if (e) return next(e)
res.send(result)
})
}
});
}
})
}
MetaCrud.prototype.delete = function (collection, id, res, next) {
collection.findById(ejsonHelper.deflateId(id), function (e, result) {
if (e) return next(e);
this.db(result.db).collection(result.collection).drop(function (e, result) {
if (e) return next(e);
collection.removeById(ejsonHelper.deflateId(id), function (e, result) {
if (e) return next(e)
res.send((result === 1) ? {msg: 'success'} : {msg: 'error'})
}.bind(this))
}.bind(this));
}.bind(this));
}
MetaCrud.prototype.update = function (collection, doc, id, res, next) {
var me = this;
collection.count({
collection: doc.collection,
db: doc.db,
_id: {$ne: ejsonHelper.deflateId(id)}
}, function (e, result) {
if (result > 0) {
res.status(512).send({status: 512, message: "the collection already exists"})
} else {
collection.findById(id, function (e, result) {
if (result != null && result.collection !== doc.collection) {
me.db.collection(result.collection).rename(doc.collection, function (e, result) {
if (e) {
next(e);
} else {
collection.updateById(ejsonHelper.deflateId(id), ejsonHelper.deflate(doc), {
safe: true,
multi: false
}, function (e, result) {
if (e) return next(e)
res.send((result === 1) ? {msg: 'success'} : {msg: 'error'})
})
}
});
} else {
collection.updateById(ejsonHelper.deflateId(id), ejsonHelper.deflate(doc), {
safe: true,
multi: false
}, function (e, result) {
if (e) return next(e)
res.send((result === 1) ? {msg: 'success'} : {msg: 'error'})
})
}
});
}
})
};
MetaCrud.prototype.generate = function (collection, params, id, res, next) {
collection.findById(ejsonHelper.deflateId(id), function (e, meta) {
if (e) {
return next(e);
} else {
var dataCollection = db.collection(meta.collection);
dataCollection.find({}, {limit: 100}, function (e, results) {
if (e) {
return next(e);
} else {
}
})
}
})
};
module.exports = MetaCrud;