forked from arjun555/project3-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
57 lines (48 loc) · 1.19 KB
/
library.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
// start up server with npm start
// read more: https://expressjs.com/
const express = require('express');
const app = express();
const port = 8080;
// pg for interfacing to database
// read more: https://node-postgres.com/
const { Client } = require('pg')
const client = new Client({
database: 'project3'
})
// Database Parameters.
// Data Table names, etc.
const dt_words = "words"
const dt_tags = "tags"
const dt_leaderboard = "leaderboard"
function connect(){
if(!client.connection.stream.connecting){
client.connect()
}
console.log(`Connection Status: ${client.connection.stream.connecting}`)
}
function disconnect(){
client.end()
console.log(`Connection Status: ${client.connection.stream.connecting}`)
}
// Get all words
function getWords(){
connect()
client.query(`SELECT * FROM ${dt_words}`)
.then(res => {
return ((res.rows))
})
.catch(e => console.log(e))
}
// Get all words
function getWord(word){
connect()
client.query(`SELECT * FROM ${dt_words} WHERE word = '${word}'`)
.then(res => {
return(res.rows[0])
})
.catch(e => console.log(e))
}
module.exports = {
getWords: getWords,
getWord: getWord
}