-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
executable file
·99 lines (61 loc) · 2.22 KB
/
database.js
File metadata and controls
executable file
·99 lines (61 loc) · 2.22 KB
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
/*Toggle these functions and observe their output*/
/* Accessing database */
function do_database(){
var pg = require('pg');
pg.defaults.ssl = true; //always keep true!!!
//determine where the database is
var conString = "postgres://ujjikxvfoeetmy:9cebcbMX6DYH_S6LvM0sm6utUs@ec2-54-243-47-83.compute-1.amazonaws.com:5432/d2jl3gj15vn53e";
//eshtablish client connection
var client = new pg.Client(conString);
client.connect(function(err) {
//error
if (err) {
return console.error('could not connect to postgres', err);
}
//success - execute the query and read in the results
//feel free to modify this and observer the results
//note: the semicolon delimeter is not needed but is semantic in that it tells me when the SQL part ends
client.query('SELECT * FROM "Users";', function(err, result) {
if (err) {
return console.error('error running query', err);
}
//the 'results.rows' is a JSON object
for (i = 0; i < result.rows.length; i++) {
console.log(result.rows[i]);
}
console.log("OK");
//kill the client connection
client.end();
});
});
}
/* VIEW database tables */
function view_tables(){
var pg = require('pg');
pg.defaults.ssl = true; //always keep true!!!
//determine where the database is
var conString = "postgres://ujjikxvfoeetmy:9cebcbMX6DYH_S6LvM0sm6utUs@ec2-54-243-47-83.compute-1.amazonaws.com:5432/d2jl3gj15vn53e";
//eshtablish client connection
var client = new pg.Client(conString);
client.connect(function(err) {
//error
if (err) {
return console.error('could not connect to postgres', err);
}
//find database tables
client.query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';", function(err, result) {
if (err) {
return console.error('error running query', err);
}
//the 'results.rows' is a JSON object
for (i = 0; i < result.rows.length; i++) {
console.log(result.rows[i]);
}
console.log("OK");
//kill the client connection
client.end();
});
});
}
do_database();
view_tables();