Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing operations to MongoDB IN Node to emit status on all operations #517

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 118 additions & 63 deletions storage/mongodb/66-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,74 +105,16 @@ module.exports = function(RED) {
delete msg._topic;
delete msg.collection;
if (node.operation === "store") {
if (node.payonly) {
if (typeof msg.payload !== "object") {
msg.payload = {"payload": msg.payload};
}
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
msg.payload._id = msg._id;
}
coll.save(msg.payload,function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
else {
coll.save(msg,function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
storeInDb(node, msg, coll);
}
else if (node.operation === "insert") {
if (node.payonly) {
if (typeof msg.payload !== "object") {
msg.payload = {"payload": msg.payload};
}
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
msg.payload._id = msg._id;
}
coll.insert(msg.payload, function(err, item) {
if (err) {
node.error(err,msg);
}
});
}
else {
coll.insert(msg, function(err,item) {
if (err) {
node.error(err,msg);
}
});
}
insertInDb(node, msg, coll);
}
else if (node.operation === "update") {
if (typeof msg.payload !== "object") {
msg.payload = {"payload": msg.payload};
}
var query = msg.query || {};
var payload = msg.payload || {};
var options = {
upsert: node.upsert,
multi: node.multi
};
if (ObjectID.isValid(msg.query._id)) {
msg.query._id = new ObjectID(msg.query._id);
}
coll.update(query, payload, options, function(err, item) {
if (err) {
node.error(err,msg);
}
});
updateInDb(node, msg, coll);
}
else if (node.operation === "delete") {
coll.remove(msg.payload, function(err, items) {
if (err) {
node.error(err,msg);
}
});
deleteInDb(node, msg, coll);
}
});
}
Expand All @@ -195,6 +137,9 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.collection = n.collection;
this.mongodb = n.mongodb;
this.payonly = n.payonly || false;
this.upsert = n.upsert || false;
this.multi = n.multi || false;
this.operation = n.operation || "find";
this.mongoConfig = RED.nodes.getNode(this.mongodb);
this.status({fill:"grey",shape:"ring",text:RED._("mongodb.status.connecting")});
Expand Down Expand Up @@ -292,6 +237,18 @@ module.exports = function(RED) {
}
});
}
else if (node.operation === "store") {
storeInDb(node, msg, coll);
}
else if (node.operation === "insert") {
insertInDb(node, msg, coll);
}
else if (node.operation === "update") {
updateInDb(node, msg, coll);
}
else if (node.operation === "delete") {
deleteInDb(node, msg, coll);
}
});
}
});
Expand All @@ -306,5 +263,103 @@ module.exports = function(RED) {
if (node.clientDb) { node.clientDb.close(); }
});
}
RED.nodes.registerType("mongodb in",MongoInNode);
RED.nodes.registerType("mongodb in", MongoInNode);

function insertInDb(node, msg, coll) {
if (node.payonly) {
if (typeof msg.payload !== "object") {
msg.payload = { "payload": msg.payload };
}
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
msg.payload._id = msg._id;
}
coll.insert(msg.payload, function (err, item) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = item;
node.send(msg);
}
});
}
else {
coll.insert(msg, function (err, item) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = item;
node.send(msg);
}
});
}
}

function updateInDb(node, msg, coll) {
if (typeof msg.payload !== "object") {
msg.payload = { "payload": msg.payload };
}
var query = msg.query || {};
var payload = msg.payload || {};
var options = {
upsert: node.upsert,
multi: node.multi
};
if (ObjectID.isValid(msg.query._id)) {
msg.query._id = new ObjectID(msg.query._id);
}
coll.update(query, payload, options, function (err, item) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = item;
node.send(msg);
}
});
}

function deleteInDb(node, msg, coll) {
coll.remove(msg.payload, function (err, items) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = items;
node.send(msg);
}
});
}

function storeInDb(node, msg, coll) {
if (node.payonly) {
if (typeof msg.payload !== "object") {
msg.payload = { "payload": msg.payload };
}
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
msg.payload._id = msg._id;
}
coll.save(msg.payload, function (err, item) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = item;
node.send(msg);
}
});
}
else {
coll.save(msg, function (err, item) {
if (err) {
node.error(err, msg);
}
else {
msg.payload = item;
node.send(msg);
}
});
}
}
}