-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
48 lines (39 loc) · 1.16 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
46
const knex = require('knex')
const environment = process.env.NODE_ENV || 'development'
const config = require('./knexfile')[environment]
const connection = knex(config);
module.exports = {
setStartingSentence,
getStartingSentence,
addSentence,
getUserSentence,
getStory
}
var startingSentenceIdx = 0
function setStartingSentence(db = connection) {
db.seed.run()
startingSentenceIdx = Math.floor(Math.random() * 6)
return startingSentenceIdx
}
function getStartingSentence(db = connection) {
return db('starter-sentences')
.select('starter-sentences.sentence as startingSentence')
.then((result) => {
return { startingSentence: result[startingSentenceIdx].startingSentence }
})
}
function addSentence(sentence, db = connection) {
return db('user-sentences')
.insert({ sentence })
}
function getUserSentence(db = connection) {
return db('user-sentences')
.select('user-sentences.sentence as userSentence')
.then((result) => {
return { userSentence: result[result.length - 1].userSentence }
})
}
function getStory(db = connection) {
return db('user-sentences')
.select('user-sentences.sentence as userSentence')
}