-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
45 lines (40 loc) · 1.31 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)
function getPhases (db = connection) {
return db('phases').select()
}
function getMoonPhase (phaseId, db = connection) {
return db('phases_activities')
.join('phases', 'phase_id', '=', 'phases.id')
.join('activities', 'activity_id', '=', 'activities.id')
.where('phases.id', phaseId)
.select('phases.name as phaseName', 'phases.next_date as nextDate', 'image', 'activities.title as activity', 'activities.id as activitiesId', 'phases.id as phaseId')
}
function addActivity (title, db = connection) {
return db('activities')
.insert({title: title})
}
function addPhaseActivity (phaseId, title, errorFunction, db = connection) {
return db('activities')
.where('title', title)
.select('activities.id')
.then(data => {
db('phases_activities')
.insert({phase_id: phaseId, activity_id: data[0].id})
.catch(errorFunction)
})
}
function deletePhaseActivity (delPhaseId, delActId, db = connection) {
return db('phases_activities')
.where('phase_id', delPhaseId)
.where('activity_id', delActId)
.del()
}
module.exports = {
getPhases,
getMoonPhase,
addActivity,
addPhaseActivity,
deletePhaseActivity
}