-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrud.js
More file actions
141 lines (126 loc) · 4.25 KB
/
crud.js
File metadata and controls
141 lines (126 loc) · 4.25 KB
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
131
132
133
134
135
136
137
138
139
140
141
// handle specific ajax requests here
// @jfreeman
var mongo = require('mongodb'),
Server = mongo.Server,
ObjectID = mongo.ObjectID,
Db = mongo.Db;
var server = new Server('localhost', 27017, {auto_reconnect: true});
var db = new Db('GrannyDB',server);
var respond = function(response, data, err) {
var responseObj = {data: data, error: err};
response.writeHead(200, {"Content-Type":"application/json"});
response.write(JSON.stringify(responseObj));
response.end();
};
exports.getAddresses = function(response){
// TODO query the collection and return the list of addresses
// Since we're dealing with a relatively small amount of data,
// use toArray(). If more data is involved, stream it!
db.open( function(err, db) {
if(!err) {
db.collection('addresses', function(err, coll) {
if(!err) {
var cursor = coll.find();
cursor.toArray(function(err, items) {
respond(response, items);
});
}
else {
console.log('error opening the collection in crud.js');
}
});
}
else {
console.log('error opening the db in crud.js');
}
db.close();
});
};
exports.getAddress = function(response, data, pathparts){
var theid = pathparts.length > 2 ? pathparts[2] : data.data;
// TODO query the collection with an ID and return a single address entry
db.open( function(err, db) {
if(!err) {
db.collection('addresses', function(err, coll) {
if(!err) {
coll.findOne({_id: ObjectID(theid)}, function(err, item){
//handles after the query for one
respond(response, item);
});
}
else {
console.log('error opening the collection in crud.js');
}
});
}
else {
console.log('error opening the db in crud.js');
}
db.close();
});
};
exports.updateAddress = function(response, data){
db.open( function(err, db) {
if(!err) {
db.collection('addresses', function(err, coll) {
if(!err) {
coll.update({_id: ObjectID(data._id)}, {$set: recreateAddressWithoutId(data)}, {safe:true}, function(err, result){
});
respond(response);
}
else {
console.log('error opening the collection in crud.js');
}
});
}
else {
console.log('error opening the db in crud.js');
}
db.close();
});
};
exports.createAddress = function(response, newEntry){
db.open( function(err, db) {
if(!err) {
db.collection('addresses', function(err, coll) {
if(!err) {
coll.insert(newEntry, {safe:true}, function(err, result){
});
respond(response, {}, {});
}
else {
console.log('error opening the collection in crud.js');
}
});
}
else {
console.log('error opening the db in crud.js');
}
db.close();
});
};
exports.deleteAddress = function(response, data){
//TODO delete a specified address from the collection
db.open( function(err, db) {
if(!err) {
db.collection('addresses', function(err, coll) {
if(!err) {
coll.remove({_id: ObjectID(data._id)}, {safe:true}, function(err, result){
// handles after the remove
});
respond(response, {}, {});
}
else {
console.log('error opening the collection in crud.js');
}
});
}
else {
console.log('error opening the db in crud.js');
}
db.close();
});
};
recreateAddressWithoutId = function(address) {
return {name: address.name, address: address.address, phone: address.phone, email: address.email};
}