-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjectMongodb.js
152 lines (136 loc) · 3.57 KB
/
objectMongodb.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* /lib/_objectMongodb.js
*
*/
'use strict';
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var oidRegExp = new RegExp("/^[a-fA-F0-9]{24}$/");
module.exports = function newModel(url, collectionName) {
var collection;
// [START translate]
function fromMongo(item) {
if (Array.isArray(item) && item.length) {
item = item[0];
}
item.id = item._id;
delete item._id;
return item;
}
function toMongo(item) {
delete item.id;
return item;
}
// [END translate]
function getCollection(cb) {
if (collection) {
setImmediate(function () { cb(null, collection); });
return;
}
MongoClient.connect(url, function (err, db) {
if (err) {
return cb(err);
}
collection = db.collection(collectionName);
cb(null, collection);
});
}
// [START list]
function list(qs, limit, token, cb) {
token = token ? parseInt(token, 10) : 0;
if (isNaN(token)) {
return cb(new Error('invalid token'));
}
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.find({})
.skip(token)
.limit(limit)
.toArray(function (err, results) {
if (err) { return cb(err); }
var hasMore =
results.length === limit ? token + results.length : false;
cb(null, results.map(fromMongo), hasMore);
//cb(null, results.map(fromMongo), hasMore, results.length);
});
});
}
// [END list]
// [START listFilter]
/*function listFilter(field, limit, token, cb) {
//token = token ? parseInt(token, 100) : 0;
token = 0;
if (isNaN(token)) {
return cb(new Error('invalid token'));
}
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.distinct(field, function (err, results) {
if (err) { return cb(err); }
cb(null, results.map(fromMongo));
});
});
}*/
// [END listFilter]
// [START create]
function create(data, cb) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.insert(data, {w: 1}, function (err, result) {
if (err) { return cb(err); }
var item = fromMongo(result.ops);
cb(null, item);
});
});
}
// [END create]
function read(id, cb) {
if (!/^[a-fA-F0-9]{24}$/.test(id)) {return cb ({code: 404, message: 'Not valid ObjectId. Must be 24 hex string or 12 hex binary sequence.'})};
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.findOne({
_id: new ObjectID(id)
}, function (err, result) {
if (err) { return cb(err); }
if (!result) {
return cb({
code: 404,
message: 'Not found'
});
}
cb(null, fromMongo(result));
});
});
}
// [START update]
function update(id, data, cb) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.update(
{ _id: new ObjectID(id) },
{ '$set': toMongo(data) },
{ w: 1 },
function (err) {
if (err) { return cb(err); }
return read(id, cb);
}
);
});
}
// [END update]
function _delete(id, cb) {
getCollection(function (err, collection) {
if (err) { return cb(err); }
collection.remove({
_id: new ObjectID(id)
}, cb);
});
}
return {
create: create,
read: read,
update: update,
delete: _delete,
list: list
};
};