-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
130 lines (111 loc) · 3.92 KB
/
db.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var Datastore = require('nedb'),
path = require('path');
var db = {};
db.measures = new Datastore( path.resolve(__dirname,'measures.db') );
db.measures.loadDatabase();
//db.settings.persistence.setAutocompactionInterval(60000);
function _processGet( dbName, req, res ) {
var tag = req.query.tag,
label = req.query.label;
if ( tag ) {
db[dbName].find( { "tag": tag, "label": label}).sort({ created: -1 }).exec( function(err, result) {
if ( err ) {
res.status(500).json( {success: false, error: err} );
} else {
res.status(200).json( result );
}
} );
} else {
res.status(400).json( { success: false, error: "tag or label params not found" } );
}
}
function _processUpdateCreate ( dbName, req, res ) {
var id = req.params.id;
if ( id ) {
db[dbName].update( { "_id": id } , req.body, {"upsert": true}, function(err, affected, affectedDocuments) {
if (err){
res.status(500).json( { success: false, error: err } );
} else {
res.status(200).json( {affected:affected, affectedDocuments:affectedDocuments} );
}
} );
} else {
res.status(400).json( { success: false, error: "Id Not found" } );
}
}
function processNewMeasure (req, res ) {
var id = req.params.id;
if ( id ) {
db['measures'].insert( req.body, function(err, newDoc) {
if (err){
res.status(500).json( { success: false, error: err } );
} else {
res.status(200).json( newDoc );
}
} );
} else {
res.status(400).json( { success: false, error: "Id Not found" } );
}
}
function processUpdateCreateMeasure( req, res ) {
_processUpdateCreate("measures", req, res);
}
function processGetAllMeasures( req, res ) {
_processGet("measures", req, res);
}
function nedb ( req, res ) {
if ( !req.body || !req.body.collection || !req.body.filter || !req.body.method ) {
res.status(400).json( {success: false, error: "Query params missing"} );
return;
}
if ( req.body.method === "get" ){
db[req.body.collection].find( req.body.filter , function(err, result) {
if ( err ) {
res.status(500).json( {success: false, error: err} );
} else {
res.status(200).json( result );
}
} );
} else if ( req.body.method === "delete" ) {
db[req.body.collection].remove( req.body.filter, { multi: true }, function (err, numRemoved) {
if ( err ) {
res.status(500).json( {success: false, error: err} );
} else {
res.status(200).json( numRemoved );
}
});
}
}
function processDelete ( req, res ) {
var id = req.params.id;
if ( id ) {
db['measures'].remove({ _id: id }, {}, function (err, numRemoved) {
if ( err ) {
res.status(500).json( {success: false, error: err} );
} else {
res.status(200).json( {success: true, numRemoved: numRemoved} );
}
});
} else {
res.status(400).json( { success: false, error: "Id Not found" } );
}
}
exports.processUpdateCreateMeasure = processUpdateCreateMeasure;
exports.processGetAllMeasures = processGetAllMeasures;
exports.processNewMeasure = processNewMeasure;
exports.processDeleteMeasure = processDeleteMeasure;
exports.processGetMeasure = function( req, res ){
var id = req.params.id;
if ( id ) {
db['measures'].findOne( { "_id": id } , function(err, result) {
if ( err ) {
res.status(500).json( {success: false, error: err} );
} else {
res.status(200).json( result );
}
} );
} else {
res.status(400).json( { success: false, error: "Id Not found" } );
}
};
exports.nedb = nedb;