[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / storage / 66-mongodb.js
1 /**
2  * Copyright 2013,2014 IBM Corp.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  **/
16
17 module.exports = function(RED) {
18     "use strict";
19     var mongo = require('mongodb');
20     var MongoClient = mongo.MongoClient;
21
22     function MongoNode(n) {
23         RED.nodes.createNode(this,n);
24         this.hostname = n.hostname;
25         this.port = n.port;
26         this.db = n.db;
27         this.name = n.name;
28
29         var url = "mongodb://";
30         if (this.credentials && this.credentials.user && this.credentials.password) {
31             url += this.credentials.user+":"+this.credentials.password+"@";
32         }
33         url += this.hostname+":"+this.port+"/"+this.db;
34
35         this.url = url;
36     }
37
38     RED.nodes.registerType("mongodb",MongoNode,{
39         credentials: {
40             user: {type:"text"},
41             password: {type: "password"}
42         }
43     });
44
45     function ensureValidSelectorObject(selector) {
46         if (selector != null && (typeof selector != 'object' || Buffer.isBuffer(selector))) {
47             return {};
48         }
49         return selector;
50     }
51         
52     
53     function MongoOutNode(n) {
54         RED.nodes.createNode(this,n);
55         this.collection = n.collection;
56         this.mongodb = n.mongodb;
57         this.payonly = n.payonly || false;
58         this.upsert = n.upsert || false;
59         this.multi = n.multi || false;
60         this.operation = n.operation;
61         this.mongoConfig = RED.nodes.getNode(this.mongodb);
62
63         if (this.mongoConfig) {
64             var node = this;
65             MongoClient.connect(this.mongoConfig.url, function(err, db) {
66                 if (err) {
67                     node.error(err);
68                 } else {
69                     node.clientDb = db;
70                     var coll;
71                     if (node.collection) {
72                         coll = db.collection(node.collection);
73                     }
74                     node.on("input",function(msg) {
75                         if (!coll) {
76                             if (msg.collection) {
77                                 coll = db.collection(msg.collection);
78                             } else {
79                                 node.error("No collection defined");
80                                 return;
81                             }
82                         }
83                         delete msg._topic;
84                         delete msg.collection;
85                         if (node.operation === "store") {
86                             if (node.payonly) {
87                                 if (typeof msg.payload !== "object") {
88                                     msg.payload = {"payload": msg.payload};
89                                 }
90                                 coll.save(msg.payload,function(err, item) {
91                                     if (err) {
92                                         node.error(err);
93                                     }
94                                 });
95                             } else {
96                                 coll.save(msg,function(err, item) {
97                                     if (err) {
98                                         node.error(err);
99                                     }
100                                 });
101                             }
102                         } else if (node.operation === "insert") {
103                             if (node.payonly) {
104                                 if (typeof msg.payload !== "object") {
105                                     msg.payload = {"payload": msg.payload};
106                                 }
107                                 coll.insert(msg.payload, function(err, item) {
108                                     if (err) {
109                                         node.error(err);
110                                     }
111                                 });
112                             } else {
113                                 coll.insert(msg, function(err,item) {
114                                     if (err) {
115                                         node.error(err);
116                                     }
117                                 });
118                             }
119                         } else if (node.operation === "update") {
120                             if (typeof msg.payload !== "object") {
121                                 msg.payload = {"payload": msg.payload};
122                             }
123                             var query = msg.query || {};
124                             var payload = msg.payload || {};
125                             var options = {
126                                 upsert: node.upsert,
127                                 multi: node.multi
128                             };
129
130                             coll.update(query, payload, options, function(err, item) {
131                                 if (err) {
132                                     node.error(err + " " + payload);
133                                 }
134                             });
135                         } else if (node.operation === "delete") {
136                             coll.remove(msg.payload, function(err, items) {
137                                 if (err) {
138                                     node.error(err);
139                                 }
140                             });
141                         }
142                     });
143                 }
144             });
145         } else {
146             this.error("missing mongodb configuration");
147         }
148
149         this.on("close", function() {
150             if (this.clientDb) {
151                 this.clientDb.close();
152             }
153         });
154     }
155     RED.nodes.registerType("mongodb out",MongoOutNode);
156
157     function MongoInNode(n) {
158         RED.nodes.createNode(this,n);
159         this.collection = n.collection;
160         this.mongodb = n.mongodb;
161         this.operation = n.operation || "find";
162         this.mongoConfig = RED.nodes.getNode(this.mongodb);
163
164         if (this.mongoConfig) {
165             var node = this;
166             MongoClient.connect(this.mongoConfig.url, function(err,db) {
167                 if (err) {
168                     node.error(err);
169                 } else {
170                     node.clientDb = db;
171                     var coll;
172                     if (node.collection) {
173                         coll = db.collection(node.collection);
174                     }
175                     node.on("input", function(msg) {
176                         if (!coll) {
177                             if (msg.collection) {
178                                 coll = db.collection(msg.collection);
179                             } else {
180                                 node.error("No collection defined");
181                                 return;
182                             }
183                         }
184                         if (node.operation === "find") {
185                             msg.projection = msg.projection || {};
186                             var selector = ensureValidSelectorObject(msg.payload);
187                             coll.find(selector,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) {
188                                 if (err) {
189                                     node.error(err);
190                                 } else {
191                                     msg.payload = items;
192                                     delete msg.projection;
193                                     delete msg.sort;
194                                     delete msg.limit;
195                                     node.send(msg);
196                                 }
197                             });
198                         } else if (node.operation === "count") {
199                             var selector = ensureValidSelectorObject(msg.payload);
200                             coll.count(selector, function(err, count) {
201                                 if (err) {
202                                     node.error(err);
203                                 } else {
204                                     msg.payload = count;
205                                     node.send(msg);
206                                 }
207                             });
208                         } else if (node.operation === "aggregate") {
209                             msg.payload = (msg.payload instanceof Array) ? msg.payload : [];
210                             coll.aggregate(msg.payload, function(err, result) {
211                                 if (err) {
212                                     node.error(err);
213                                 } else {
214                                     msg.payload = result;
215                                     node.send(msg);
216                                 }
217                             });
218                         }
219                     });
220                 }
221             });
222         } else {
223             this.error("missing mongodb configuration");
224         }
225
226         this.on("close", function() {
227             if (this.clientDb) {
228                 this.clientDb.close();
229             }
230         });
231     }
232     RED.nodes.registerType("mongodb in",MongoInNode);
233 }