-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
184 lines (159 loc) · 4.35 KB
/
Copy pathcontext.ts
File metadata and controls
184 lines (159 loc) · 4.35 KB
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
/**
* Node.js GraphQL API Starter Kit
* https://github.com/kriasoft/nodejs-api-starter
* Copyright © 2016-present Kriasoft | MIT License
*/
import DataLoader from 'dataloader';
import { Request } from 'express';
import db from './db';
import { Validator } from './validator';
import { mapTo, mapToMany, mapToValues } from './utils';
import { UnauthorizedError, ForbiddenError, ValidationError } from './errors';
export class Context {
errors = [];
private req: Request;
constructor(req: Request) {
this.req = req;
if (req.user) {
// Add user object to the cache
this.userById.prime(req.user.id, req.user);
this.userByUsername.prime(req.user.username, req.user);
// Convert snake_case fields to camelCase for convinience
Object.keys(req.user).forEach(key => {
req.user[key.replace(/_\w/g, x => x[1].toUpperCase())] = req.user[key];
});
}
}
get user() {
return this.req.user;
}
get logIn() {
return this.req.logIn;
}
get logOut() {
return this.req.logOut;
}
/*
* Authorization
* ------------------------------------------------------------------------ */
ensureAuthorized(check: (user: any) => Boolean) {
// if (!this.user) {
// throw new UnauthorizedError();
// }
// if (check && !check(this.user)) {
// throw new ForbiddenError();
// }
}
/*
* Validation
* ------------------------------------------------------------------------ */
addError(key: string, message: string) {
this.errors.push({ key, message });
}
validate(input: any, mode: 'create' | 'update') {
const validator = new Validator(input, mode, errors => {
throw new ValidationError(errors);
});
return transform => {
transform(validator);
return validator.validate();
};
}
/*
* Data loaders
* ------------------------------------------------------------------------ */
userById = new DataLoader(keys =>
db
.table('users')
.whereIn('id', keys as string[])
.select()
.then((rows: any[]) =>
rows.map(x => {
this.userByUsername.prime(x.username, x);
return x;
}),
)
.then(mapTo(keys, (x: any) => x.id)),
);
userByUsername = new DataLoader(keys =>
db
.table('users')
.whereIn('username', keys as string[])
.select()
.then(rows =>
rows.map((x: any) => {
this.userById.prime(x.id, x);
return x;
}),
)
.then(mapTo(keys, (x: any) => x.username)),
);
identitiesByUserId = new DataLoader(keys =>
db
.table('user_identities')
.whereIn('user_id', keys as string[])
.select()
.then(mapToMany(keys, (x: any) => x.user_id)),
);
storyById = new DataLoader(keys =>
db
.table('stories')
.whereIn('id', keys as string[])
.select()
.then(rows => {
rows.forEach((x: any) => this.storyBySlug.prime(x.slug, x));
return rows;
})
.then(mapTo(keys, (x: any) => x.id)),
);
storyBySlug = new DataLoader(keys =>
db
.table('stories')
.whereIn('slug', keys as string[])
.select()
.then((rows: any[]) => {
rows.forEach(x => this.storyById.prime(x.id, x));
return rows;
})
.then(mapTo(keys, (x: any) => x.slug)),
);
storyPointsCount = new DataLoader(keys =>
db
.table('stories')
.leftJoin('story_points', 'story_points.story_id', 'stories.id')
.whereIn('stories.id', keys as string[])
.groupBy('stories.id')
.select('stories.id', db.raw('count(story_points.user_id)::int'))
.then(
mapToValues(
keys,
x => x.id,
x => parseInt(x.count, 10),
),
),
);
storyPointGiven = new DataLoader(keys => {
const currentUser = this.user as any;
const userId = currentUser && currentUser.id;
return db
.table('stories')
.leftJoin('story_points', function join() {
this.on('story_points.story_id', 'stories.id').andOn(
'story_points.user_id',
db.raw('?', [userId]),
);
})
.whereIn('stories.id', keys as string[])
.select(
'stories.id',
db.raw('(story_points.user_id IS NOT NULL) AS given'),
)
.then(
mapToValues(
keys,
x => x.id,
x => x.given,
),
);
});
}