-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_functions.js
155 lines (135 loc) · 4.6 KB
/
db_functions.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var pg = require('pg');
var format = require('pg-format');
const { Pool, Client } = require('pg')
const pool = new Pool({
host: 'localhost',
user: 'euxdyohv',
max: 1000,
idleTimeoutMillis: 40000,
connectionTimeoutMillis: 3000,
})
const getUniqueID = async () => {
var existingIDs = [];
var conString = "postgres://euxdyohv:[email protected]/euxdyohv" //Can be found in the Details page
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
})
const {rows} = await client.query(`SELECT id FROM bundles`)
const rowsString = JSON.stringify(rows);
const array = rowsString.split('{"id":');
const splicedArray = []
const nonNumbers = []
for(var i=0; i<array.length; i++){
if(array[i].length>2 && array[i].length<4){
splicedArray.push(array[i].substring(0,1))
}
else if(array[i].length>3 && array[i].length<5){
splicedArray.push(array[i].substring(0,2))
}
else if(array[i].length>4 && array[i].length<6){
splicedArray.push(array[i].substring(0,3))
}
else if (array[i].length>5 && array[i].length<7){
splicedArray.push(array[i].substring(0,4))
}
else if (array[i].length>6 && array[i].length<8){
splicedArray.push(array[i].substring(0,5))
}
else if (array[i].length>7 && array[i].length<9){
splicedArray.push(array[i].substring(0,6))
}
else if (array[i].length>8 && array[i].length<10){
splicedArray.push(array[i].substring(0,7))
}
else if (array[i].length>9 && array[i].length<11){
splicedArray.push(array[i].substring(0,8))
}
else if (array[i].length>10 && array[i].length<12){
splicedArray.push(array[i].substring(0,9))
}
else if (array[i].length>11 && array[i].length<13){
splicedArray.push(array[i].substring(0,10))
}
else if (array[i].length>12 && array[i].length<14){
splicedArray.push(array[i].substring(0,11))
}
else {
nonNumbers.push(array[i])
}
}
// console.log(splicedArray)
// console.log(typeof rowsString)
const rowsInt = [];
for(var j = 0; j<splicedArray.length; j++){
rowsInt.push(parseInt(splicedArray[j] + ' '))
}
rowsInt.unshift(' ')
return rowsInt
}
//getUniqueID();
const getBundles = () => {
var conString = "postgres://euxdyohv:[email protected]/euxdyohv" //Can be found in the Details page
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
client.query(`SELECT * FROM bundles`, function(err, result) {
// if(err) {
// return console.error('error running query', err);
// }
console.log("results getBundles" + result.rows);
console.log('results inside getBundles');
});
});
}
const createBundle = async (obj) => {
const unique = obj.values[1]
const name = obj.values[0]
const values = [name, unique]
const text = `INSERT INTO bundles (id, name) VALUES (%1$L, %2$L) RETURNING *`
var sql = format(text, unique, name.toString());
console.log("sqL"+sql);
var conString = "postgres://euxdyohv:[email protected]/euxdyohv" //Can be found in the Details page
var client = new pg.Client(conString);
client.connect(function(err) {
if(err){
console.log('error connecting to client POSTGRESQL');
}
else{
client.query(sql, function(err, result) {
if(err) {
return console.error('error running query!', err);
}
console.log(result.rows);
// >> output: 2018-08-23T14:02:57.117Z
client.end();
})
};
}
)};
const objDelete = {
text: "DELETE FROM bundles WHERE unique= $1",
values: ['uniqueNumber']
}
const deleteBundle = (body) => {
const {text, values} = body
var conString = "postgres://euxdyohv:[email protected]/euxdyohv" //Can be found in the Details page
var client = new pg.Client(conString);
client
.query(text, values)
.then(res => {
console.log(res.rows[0]) // { name: 'brianc', email: '[email protected]' }
})
.catch(e => console.error(e.stack))
client.end();
}
module.exports = {
getBundles,
createBundle,
deleteBundle,
getUniqueID
}