Commit 16261f8f authored by Vadym Gidulian's avatar Vadym Gidulian

1.1.0

parents 20d3b8e2 39c439d2
{ {
"name": "storage-mongo-node", "name": "storage-mongo-node",
"version": "1.0.2", "version": "1.1.0",
"dependencies": { "dependencies": {
"express": "^4.16.2", "express": "^4.16.3",
"mongodb": "^3.0.1" "mongodb": "^3.0.7"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^1.14.8" "nodemon": "^1.17.4"
} }
} }
...@@ -44,6 +44,34 @@ app.post('/:collection', (req, res) => { ...@@ -44,6 +44,34 @@ app.post('/:collection', (req, res) => {
}); });
}); });
}); });
app.put('/:collection', (req, res) => {
if (isObjectEmpty(req.body)) {
res.status(400).send('Body is empty');
return;
}
req.body.forEach(record => {
if (record._id) record._id = toId(record._id);
});
mongoSafeConnect(res, client => {
const collection = client.db(DB_NAME).collection(req.params.collection);
collection.insertMany(req.body, {ordered: false}, (err, result) => {
if (err) {
if (err.code == 11000) {
return res.status(409).send();
} else {
throw err;
}
}
const records = result.ops;
res.status(201).send(records);
client.close();
});
});
});
app.put('/:collection/:id', (req, res) => { app.put('/:collection/:id', (req, res) => {
if (isObjectEmpty(req.body)) { if (isObjectEmpty(req.body)) {
res.status(400).send('Body is empty'); res.status(400).send('Body is empty');
...@@ -52,7 +80,7 @@ app.put('/:collection/:id', (req, res) => { ...@@ -52,7 +80,7 @@ app.put('/:collection/:id', (req, res) => {
mongoSafeConnect(res, client => { mongoSafeConnect(res, client => {
const collection = client.db(DB_NAME).collection(req.params.collection); const collection = client.db(DB_NAME).collection(req.params.collection);
collection.findOne({_id: wrapId(req.params.id)}, (err, result) => { collection.findOne({_id: toId(req.params.id)}, (err, result) => {
if (err) throw err; if (err) throw err;
if (result) { if (result) {
...@@ -61,7 +89,7 @@ app.put('/:collection/:id', (req, res) => { ...@@ -61,7 +89,7 @@ app.put('/:collection/:id', (req, res) => {
return; return;
} }
collection.insertOne({...req.body, _id: wrapId(req.params.id)}, (err, result) => { collection.insertOne({...req.body, _id: toId(req.params.id)}, (err, result) => {
if (err) throw err; if (err) throw err;
const record = result.ops[0]; const record = result.ops[0];
...@@ -74,7 +102,7 @@ app.put('/:collection/:id', (req, res) => { ...@@ -74,7 +102,7 @@ app.put('/:collection/:id', (req, res) => {
}); });
app.get('/:collection', (req, res) => { app.get('/:collection', (req, res) => {
mongoSafeConnect(res, client => { mongoSafeConnect(res, client => {
client.db(DB_NAME).collection(req.params.collection).find().toArray((err, result) => { client.db(DB_NAME).collection(req.params.collection).find(req.body.query).toArray((err, result) => {
if (err) throw err; if (err) throw err;
res.status(200).send(result); res.status(200).send(result);
...@@ -85,7 +113,7 @@ app.get('/:collection', (req, res) => { ...@@ -85,7 +113,7 @@ app.get('/:collection', (req, res) => {
}); });
app.get('/:collection/:id', (req, res) => { app.get('/:collection/:id', (req, res) => {
mongoSafeConnect(res, client => { mongoSafeConnect(res, client => {
client.db(DB_NAME).collection(req.params.collection).findOne({_id: wrapId(req.params.id)}, (err, result) => { client.db(DB_NAME).collection(req.params.collection).findOne({_id: toId(req.params.id)}, (err, result) => {
if (err) throw err; if (err) throw err;
if (result) { if (result) {
...@@ -106,7 +134,7 @@ app.patch('/:collection/:id', (req, res) => { ...@@ -106,7 +134,7 @@ app.patch('/:collection/:id', (req, res) => {
mongoSafeConnect(res, client => { mongoSafeConnect(res, client => {
delete req.body._id; // Immutable properties shouldn't be tried to change delete req.body._id; // Immutable properties shouldn't be tried to change
client.db(DB_NAME).collection(req.params.collection).findOneAndUpdate({_id: wrapId(req.params.id)}, {$set: req.body}, {returnOriginal: false}, (err, result) => { client.db(DB_NAME).collection(req.params.collection).findOneAndUpdate({_id: toId(req.params.id)}, {$set: req.body}, {returnOriginal: false}, (err, result) => {
if (err) throw err; if (err) throw err;
if (result.value) { if (result.value) {
...@@ -121,7 +149,7 @@ app.patch('/:collection/:id', (req, res) => { ...@@ -121,7 +149,7 @@ app.patch('/:collection/:id', (req, res) => {
}); });
app.delete('/:collection/:id', (req, res) => { app.delete('/:collection/:id', (req, res) => {
mongoSafeConnect(res, client => { mongoSafeConnect(res, client => {
client.db(DB_NAME).collection(req.params.collection).findOneAndDelete({_id: wrapId(req.params.id)}, (err, result) => { client.db(DB_NAME).collection(req.params.collection).findOneAndDelete({_id: toId(req.params.id)}, (err, result) => {
if (err) throw err; if (err) throw err;
if (result.value) { if (result.value) {
...@@ -161,6 +189,15 @@ function mongoSafeConnect(serverResponse, callback) { ...@@ -161,6 +189,15 @@ function mongoSafeConnect(serverResponse, callback) {
}); });
} }
function wrapId(id) { function toId(id) {
return ObjectID.isValid(id) ? new ObjectID(id) : id; if (+id == id) id = +id;
switch (typeof id) {
case 'number': return id;
case 'object':
if (Array.isArray(id)) throw new Error('Array is not a valid object id type');
return id;
case 'string': return ObjectID.isValid(id) ? new ObjectID(id) : id;
default: throw new Error('Invalid object id');
}
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment