-
Notifications
You must be signed in to change notification settings - Fork 2
/
smartFarmDB_tools.js
99 lines (79 loc) · 2.3 KB
/
smartFarmDB_tools.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//adapted from
//https://github.com/eh3rrera/rethinkdb-example/blob/master/models/movies.js
var db = module.exports;// use this to call db functions from elsewhere
var r = require('rethinkdb');
var connection = null;
/*
* 1 Database: pc_farmDB
* 4 tables, Air / Ground / historical / wateringHistory
*/
var dbName = "pc_farmDB";
var tName ="ERROR_ON_WRONG_TABLE";
var query=null;
var hostOptions={host:'localhost',port: 28015};
//==================================================
// DB input
// throw a json object data into the table
function insert_DB( tableName, data )
{
tName=tableName;
r.connect( hostOptions, function(err, conn) {
if (err) throw err;
connection = conn;
//insert the data
r.table(tName).insert([data]).run(connection,function(err,res){
if(err) throw err;
console.log("INSERT SUCCESS");
});
connection.close(function(err) { if (err) throw err; })
)};
}
//==================================================
// DB output all
// return all JSON objects on request
function getAllData_DB( tableName)
{
var output=null;
r.connect( hostOptions, function(err, conn) {
if (err) throw err;
connection = conn;
r.table(tableName).run(connection,function(err,res){
if(err) throw err;
res.toArray(function(err,out){
if (err) throw err;
output=out;
});
});
connection.close(function(err) { if (err) throw err; })
)};
return output;
}
//==================================================
// DB output query
// return JSON objects from row on equal
// SELECT * FROM TABLE WHERE COND ==TRUE
function queryData(tableName, rw, eql)
{
var output = null;
r.connect( hostOptions, function(err, conn) {
if (err) throw err;
connection = conn;
r.table(tableName).filter(r.row(rw).eq(eql)).run(connection,function(err,res){
if(err) throw err;
res.toArray(function(err,out){
if (err) throw err;
output=out;
});
});
connection.close(function(err) { if (err) throw err; })
)};
return output;
}
//==================================================
// DB output query
// return JSON objects from row on equal
//SELECT data1,data2 FROM table
//
function getFields(tableName,data1 ){
return ouput
}