-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
45 lines (39 loc) · 1.21 KB
/
db.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
const environment = process.env.NODE_ENV || 'development'
const config = require('./knexfile')[environment]
const connection = require('knex')(config)
module.exports = {
getNames,
getName,
getCompliments,
sendCompliment,
getPhrase
}
// function to get people from the database
function getNames (testConn) {
const conn = testConn || connection
return conn('people').select()
}
// function that passes in a id from our get route and matches the params with the
// id in our database and returns the first match of everything that relates to the id
function getName (id, testConn) {
const conn = testConn || connection
return conn('people').where('id', id).first().select('name')
}
// function to get complliments from the database
function getCompliments (id, testConn) {
const conn = testConn || connection
return conn('compliments').where('id', id).first()
}
function sendCompliment (receiverId, phrase, testConn) {
const conn = testConn || connection
return conn('compliments').insert({
phrase: phrase,
receiver_id: receiverId
})
}
function getPhrase (id, testConn) {
const conn = testConn || connection
return conn('compliments')
.where('receiver_id', id)
.select('phrase')
}