Commit 2dbdc34f authored by Vadym Gidulian's avatar Vadym Gidulian

Added inserting of multiple records

parent 20d3b8e2
......@@ -3,10 +3,10 @@
"version": "1.0.2",
"dependencies": {
"express": "^4.16.2",
"mongodb": "^3.0.1"
"express": "^4.16.3",
"mongodb": "^3.0.7"
},
"devDependencies": {
"nodemon": "^1.14.8"
"nodemon": "^1.17.4"
}
}
......@@ -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) => {
if (isObjectEmpty(req.body)) {
res.status(400).send('Body is empty');
......@@ -52,7 +80,7 @@ app.put('/:collection/:id', (req, res) => {
mongoSafeConnect(res, client => {
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 (result) {
......@@ -61,7 +89,7 @@ app.put('/:collection/:id', (req, res) => {
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;
const record = result.ops[0];
......@@ -85,7 +113,7 @@ app.get('/:collection', (req, res) => {
});
app.get('/:collection/:id', (req, res) => {
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 (result) {
......@@ -106,7 +134,7 @@ app.patch('/:collection/:id', (req, res) => {
mongoSafeConnect(res, client => {
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 (result.value) {
......@@ -121,7 +149,7 @@ app.patch('/:collection/:id', (req, res) => {
});
app.delete('/:collection/:id', (req, res) => {
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 (result.value) {
......@@ -161,6 +189,15 @@ function mongoSafeConnect(serverResponse, callback) {
});
}
function wrapId(id) {
return ObjectID.isValid(id) ? new ObjectID(id) : id;
function toId(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