Skip to content

Commit 8b93d6e

Browse files
update node with treeSoftware changes
1 parent 7f850f7 commit 8b93d6e

File tree

5 files changed

+2161
-882
lines changed

5 files changed

+2161
-882
lines changed

node-red-contrib-treeview/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
"homepage": "https://github.com/MobileTarget/treeViewPackage#readme",
2626
"dependencies": {
2727
"axios": "^0.17.1",
28-
"cloudant": "^1.10.0",
2928
"mongoid-js": "^1.1.3",
30-
"querystring": "^0.2.0",
31-
"underscore": "^1.8.3",
32-
"url": "^0.11.0"
29+
"underscore": "^1.8.3"
3330
}
3431
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
var axios = require('axios');
2+
3+
var db = {
4+
databaseName: "",
5+
databaseUsername: "",
6+
databasePassword: "",
7+
databaseUrl: "",
8+
init: function(databaseName, databaseUsername, databasePassword){
9+
this.databaseName = databaseName;
10+
this.databaseUsername = databaseUsername;
11+
this.databasePassword = databasePassword;
12+
this.databaseUrl = `https://${this.databaseUsername}.cloudant.com/${this.databaseName}`;
13+
},
14+
httpRequest: function(url, method, payload, callback){
15+
var config = {
16+
auth: {
17+
username: this.databaseUsername,
18+
password: this.databasePassword
19+
} , //basic auth credentails used for cloudant database
20+
timeout: 30000 , //if the response not comes in 30sec axios will abort the request;
21+
url: url, // cloudant database url for views, query's
22+
method: method, //method used to communicate with cloudant database i.e get,post,put,delete,patch
23+
};
24+
25+
switch(method) {
26+
case "GET":
27+
axios
28+
.request(config)
29+
.then(function(response){
30+
callback(response.data, 'success');
31+
}).catch(function(exception){
32+
if (exception.response) {
33+
callback(exception.response.data, 'error');
34+
} else if (exception.request) {
35+
callback(exception.request, 'error');
36+
} else {
37+
callback(exception.message, 'error');
38+
}
39+
});
40+
break;
41+
42+
case "POST":
43+
config.data = payload ;
44+
axios
45+
.request(config)
46+
.then(function(response){
47+
callback(response.data, 'success');
48+
}).catch(function(exception){
49+
if (exception.response) {
50+
callback(exception.response.data, 'error');
51+
} else if (exception.request) {
52+
callback(exception.request, 'error');
53+
} else {
54+
callback(exception.message, 'error');
55+
}
56+
});
57+
break;
58+
59+
case "PUT":
60+
config.data = payload ;
61+
axios
62+
.request(config)
63+
.then(function(response){
64+
callback(response.data, 'success');
65+
}).catch(function(exception){
66+
if (exception.response) {
67+
callback(exception.response.data, 'error');
68+
} else if (exception.request) {
69+
callback(exception.request, 'error');
70+
} else {
71+
callback(exception.message, 'error');
72+
}
73+
});
74+
break;
75+
76+
case "DELETE":
77+
axios
78+
.request(config)
79+
.then(function(response){
80+
callback(response.data, 'success');
81+
}).catch(function(exception){
82+
if (exception.response) {
83+
callback(exception.response.data, 'error');
84+
} else if (exception.request) {
85+
callback(exception.request, 'error');
86+
} else {
87+
callback(exception.message, 'error');
88+
}
89+
});
90+
break;
91+
92+
default:
93+
callback("Exception raised: No such method is defined.");
94+
break;
95+
}
96+
},
97+
sendResponse: function(data, status, callerFn, callback){
98+
if(status == "error"){
99+
callback(null);
100+
}else if(status == "success"){
101+
if(callerFn == "searchRecordsFromDatabase"){
102+
var payload = {};
103+
if(data.rows){
104+
data.rows.forEach((obj)=>{
105+
payload[obj.value._id] = obj.value ;
106+
});
107+
callback(payload);
108+
}else if(data.docs){
109+
callback(data.docs);
110+
}else{
111+
callback(payload);
112+
}
113+
}else{
114+
callback(data);
115+
}
116+
}else{
117+
throw new Error(`Exception raised inside ${callerFn}:- Failed to fetch data from database.`);
118+
}
119+
},
120+
resetDatabase: function(callback){
121+
var url = `https://${this.databaseUsername}.cloudant.com/_all_dbs`, self = this;
122+
123+
this.httpRequest(url, 'GET', null, function(data, status){
124+
if(status == "success" && data){
125+
if(data.indexOf(self.databaseName) > -1){
126+
self.httpRequest(self.databaseUrl, 'DELETE', null, function(data, status){
127+
if(status == "success" && data.ok){
128+
self.httpRequest(self.databaseUrl, 'PUT', null, function(data, staus){
129+
if(staus == "success" && data.ok){
130+
callback({isReset: true, msg: "Database reset successfully."});
131+
}else{
132+
callback({isReset: false, msg: "Error while creating empty database"});
133+
}
134+
});
135+
} else{
136+
callback({isReset: false, msg: "Error while removing database."});
137+
}
138+
});
139+
}else{
140+
self.httpRequest(self.databaseUrl, 'PUT', null, function(data, staus){
141+
if(staus == "success" && data.ok){
142+
callback({isReset: true, msg: "Database reset successfully."});
143+
}else{
144+
callback({isReset: false, msg: "Error while creating empty database"});
145+
}
146+
});
147+
}
148+
}else{
149+
callback({isReset: false, msg: "Error while creating empty database"});
150+
}
151+
});
152+
},
153+
searchNodeById: function(id, callback){
154+
var url = `${this.databaseUrl}/${id}`, self = this;
155+
this.httpRequest(url, "GET", null, function(data, status){
156+
self.sendResponse(data, status, 'getById', callback);
157+
});
158+
},
159+
saveNode: function(obj, callback){
160+
var self = this;
161+
this.httpRequest(this.databaseUrl, 'POST', obj, function(data, status){
162+
console.info("Node saved successfully with following object", data);
163+
self.sendResponse(data, status, 'saveNode', callback);
164+
});
165+
},
166+
saveArray: function(arr, callback){
167+
var self = this, url = `${this.databaseUrl}/_bulk_docs`;
168+
this.httpRequest(url, 'POST', {docs: arr}, function(data, status){
169+
self.sendResponse(JSON.parse(JSON.stringify(arr)), status, 'saveArray', callback);
170+
});
171+
},
172+
deleteArray: function(arr, callback){
173+
var self = this, url = `${this.databaseUrl}/_bulk_docs`, payload = [];
174+
175+
arr.forEach((obj)=>{
176+
if(obj._id && obj._rev){
177+
payload.push({ _id: obj._id, _rev: obj._rev, _deleted: true });
178+
}
179+
});
180+
181+
this.httpRequest(url, 'POST', { docs: payload}, function(data, status){
182+
self.sendResponse(data, status, 'deleteArray', callback);
183+
});
184+
},
185+
searchRecordsFromDatabase: function(queryFields, callback){
186+
var self = this, url = `${this.databaseUrl}/_find`;
187+
188+
this.httpRequest(url, 'POST', queryFields, function(data, status){
189+
self.sendResponse(data, status, 'searchRecordsFromDatabase', callback);
190+
});
191+
}
192+
193+
};
194+
195+
196+
module.exports = db;

node-red-contrib-treeview/treeView/treeView.html

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -35,78 +35,11 @@
3535
<label for="node-input-name"><i class="fa fa-lock"></i> Password</label>
3636
<input type="password" id="node-input-password" placeholder="Password">
3737
</div>
38-
<div class="form-row">
39-
<label for="node-input-topic"><i class="fa fa-wrench"></i> Operation</label>
40-
<select type="text" id="node-input-operation" style="width:68%;">
41-
<option value="null">Select</option>
42-
<option value="all">All</option>
43-
<option value="insert">Save</option>
44-
<option value="search">Search</option>
45-
<option value="delete">Delete</option>
46-
<option value="process_msg">Process msg</option>
47-
</select>
48-
</div>
49-
38+
5039
</script>
5140

5241
<script type="text/x-red" data-help-name="Tree View">
5342
<p>Custom treeView strcture of data saved into cloudantDb for Dri-list application.<br/></p>
54-
<h3>Details</h3>
55-
<p>
56-
The <b>database name</b> must follow these rules:
57-
<ul>
58-
<li>No spaces</li>
59-
<li>All letters in small caps</li>
60-
<li>The first character can't be <code>_</code></li>
61-
</ul>
62-
</p>
63-
<p>
64-
Your document should avoid having top-level fields that start with
65-
<code>_</code>, with exceptions for <code>_id</code>, <code>_rev</code>
66-
and other <a href="https://wiki.apache.org/couchdb/HTTP_Document_API#Special_Fields">
67-
CouchDB reserved words</a>.
68-
</p>
69-
<p>
70-
Fetching documents can be done in two modes: directly by using the
71-
document's <b>_id</b>, or retrieving <b>all documents</b> stored in your database.
72-
1) To fetch document by <b>_id</b> you need to select <b>"Operation"</b> as fetch and
73-
need to add <b>"mode"</b> property in msg object for <b>e.g: msg.mode = "byId"</b> <br/>
74-
2) To fetch document by cloudant <b>_designs</b> search filters for which you need to follow the following
75-
<a href="https://cloudant.com/for-developers/search/" target="_blank">link</a>.<br/>
76-
3) To fetch all document stored into database i.e default limit is enable to fetch all record is 200 and you need
77-
to set the <b>"mode"</b> property of "msg" object to "all" i.e. <b>msg.mode = "all"</b>.<br/>
78-
</p>
79-
<p>
80-
When querying using the <b>_id</b> option, the value for the document's
81-
<code>_id</code> should be passed in the <code>msg.payload</code> as a
82-
string.
83-
</p>
84-
<p>
85-
The last method to retrieve documents is to simply get all of them by
86-
selecting the option <b>all documents</b>.
87-
</p>
88-
89-
<hr/>
90-
91-
<p>Searching document can be done in tow modes:
92-
1) To search data by using search/filters index stored into database.
93-
by using an existing <a href="https://cloudant.com/for-developers/search/" target="_blank">Search Index</a>
94-
To use an existing <b>Search Index</b> stored on the desired database,
95-
the query argument should be passed on the <code>msg.payload</code> as a
96-
string following the <code>indexName:value</code> pattern. Keep in mind
97-
that <i>the index must be created beforehand in the database.</i> and
98-
referenced here by its <code>design document/index name</code>. </p>
99-
<p> When querying using a <b>Search Index</b> you can pass the search parameters as an object in <code>msg.payload</code>. </p>
100-
<p> For example, you can pass an object like this: <p> <code>{ query: "abc*", limit: 100 }</code> </p>
101-
102-
<p> to change the value of <code>limit</code>. You can find more information about <b>Search Index</b> parameters in the
103-
<a href="https://docs.cloudant.com/api.html?http#queries" target="_blank"> official Cloudant documentation</a>. </p><br/>
104-
<strong>Note: To use cloudantDb design filter need to add "mode" property value in msg object. i.e "msg.mode" = "searchFilter"</strong> <br/><br/>
105-
2) To search data by using the cloudant query method.
106-
For more information please visit the following link.
107-
<a target="_blank" href="https://examples.cloudant.com/docs-examples/_design/couchapp/api/search.html">Cloudant search query</a><br/><br/>
108-
109-
<strong>Note: To use cloudantDb design filter need to add "mode" property value in msg object. i.e "msg.mode" = "searchQuery"</strong><br/>
11043
<p>Some more information about the node. plz vist the following link</p>
11144
<a href="https://github.com/MobileTarget/treeViewPackage">Tree view github</a>
11245
</script>

0 commit comments

Comments
 (0)