-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection-pool.js
254 lines (232 loc) · 5.33 KB
/
connection-pool.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const { URL } = require('url');
const { SQLDataSource } = require('datasource-sql');
const HOUR = 3600;
const MINUTE = 60;
const NOTHING = 1;
const MAX_WEEK = 19;
class PGDB extends SQLDataSource {
async getTeams() {
const val = await this.knex
.select('*')
.from('teams')
.cache(HOUR);
return val;
}
async getSportsGames(season) {
const val = await this.knex
.select('*')
.from('sports_games')
.where({
'season': season
})
.cache(MINUTE);
return val;
}
async getSportsGamesForWeek(season, week) {
const val = await this.knex
.select('*')
.from('sports_games')
.where({
'season': season,
'week': week
})
.cache(MINUTE);
return val;
}
async getUserByEmail(email) {
const val = await this.knex
.select('*')
.from('users')
.where({
'email': email
})
.limit(1)
.cache(HOUR);
if (val.length) {
return val[0];
}
}
async getUserById(id) {
const val = await this.knex
.select('*')
.from('users')
.where({
'id': id
})
.limit(1)
.cache(HOUR);
if (val.length) {
return val[0];
}
}
async getUserDisplayNameForLeague(userID, leagueID) {
const val = await this.knex
.select('display_name')
.from('memberships')
.innerJoin('fantasy_leagues', 'fantasy_leagues.id', 'memberships.league_id')
.where({
'memberships.user_id': userID,
'fantasy_leagues.id': leagueID
})
.limit(1)
.cache(MINUTE);
if (val.length) {
return val[0];
}
}
async getAllLeagues() {
const val = await this.knex
.select('*')
.from('fantasy_leagues')
.cache(MINUTE);
return val;
}
async getLeaguesForUser(userID) {
const val = await this.knex
.select('*')
.from('fantasy_leagues')
.innerJoin('memberships', 'fantasy_leagues.id', 'memberships.league_id')
.where({
'memberships.user_id': userID
})
.cache(MINUTE);
return val;
}
async getLeagueById(leagueID) {
const val = await this.knex
.select('*')
.from('fantasy_leagues')
.where({
'id': leagueID
})
.cache(MINUTE);
if (val.length) {
return val[0];
}
}
async getTeam(shortName, league) {
const val = await this.knex
.select('*')
.from('teams')
.where({
short_name: shortName,
sports_league: league
})
.limit(1)
.cache(HOUR);
if (val.length) {
return val[0];
}
}
async getTeamById(teamID) {
const val = await this.knex
.select('*')
.from('teams')
.where({
id: teamID
})
.limit(1)
.cache(HOUR);
if (val.length) {
return val[0];
}
}
async getLeagueOwner(leagueId, ownerId) {
const val = await this.knex
.select('*')
.from('users')
.innerJoin('memberships', 'users.id', 'memberships.user_id')
.where({
'memberships.league_id': leagueId,
'users.id': ownerId
})
.limit(1)
.cache(MINUTE);
if (val.length) {
return val[0];
}
}
async getLeagueMembers(leagueID) {
const val = await this.knex
.select('*')
.from('users')
.innerJoin('memberships', 'users.id', 'memberships.user_id')
.where({
'memberships.league_id': leagueID
})
.cache(MINUTE);
return val;
}
async getPicksForLeague(leagueID, leagueConcluded = false) {
const val = await this.knex
.select('*')
.from('picks')
.where({
'league_id': leagueID,
'invalidated_at': null
})
.whereRaw('week <= ?', (leagueConcluded ? [MAX_WEEK] : [parseInt(process.env.REVEALED_WEEK)] ))
.cache(MINUTE);
return val;
}
async getCurrentPick(leagueID, userID, week) {
const val = await this.knex
.select('*')
.from('picks')
.where({
'user_id': userID,
'league_id': leagueID,
'week': week,
'invalidated_at': null
})
.cache(NOTHING);
return val;
}
async getPicksForMember(userID, leagueID) {
const val = await this.knex
.select('*')
.from('picks')
.where({
'user_id': userID,
'league_id': leagueID,
'invalidated_at': null
})
.cache(MINUTE);
return val;
}
async submitPicks(userID, leagueID, teamIDs, week) {
let responseRows = [];
const knex = this.knex;
// DB transaction
await knex.transaction(async function(trx) {
// First, invalidate any previous picks
// for the current week
await knex('picks')
.where({
'invalidated_at': null,
'week': week,
'user_id': userID,
'league_id': leagueID
})
.update({
'invalidated_at': trx.raw('CURRENT_TIMESTAMP')
})
.transacting(trx);
// Then, insert the new picks
for (const teamID of teamIDs) {
const result = await knex('picks')
.insert({
'league_id': leagueID,
'user_id': userID,
'team_id': teamID,
'week': week
})
.transacting(trx)
.returning('*');
responseRows.push(result[0]);
}
})
return responseRows;
}
}
exports.DataSource = PGDB;