-
Notifications
You must be signed in to change notification settings - Fork 36
/
db.ts
454 lines (410 loc) · 9.19 KB
/
db.ts
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import clientPromise from "./db_conn";
import {
ReceivedArticle,
ReceivedStaff,
DepartmentsArray,
mongoObjectId,
ReceivedArticleExtra,
} from "./ts_types/db_types";
import { Db, ObjectId } from "mongodb";
async function applyTextImageCredit(
v: ReceivedArticle,
db: Db
): Promise<ReceivedArticle> {
// get extras
let extras_collection = db.collection("article_extras");
const article_extras = await extras_collection
.aggregate<ReceivedArticleExtra>([
{
$match: {
article: new ObjectId(String(v._id)),
},
},
{
$lookup: {
from: "staffs",
localField: "contributors",
foreignField: "_id",
as: "contributors",
},
},
])
.toArray();
let text = v.text;
const regex = /<div.*class=.content_img.*?>/g; // just match the opening tag of '<div class="content_img">'
let array1: RegExpExecArray | null;
let img_index = 0;
while ((array1 = regex.exec(text)) !== null) {
const current_extra = article_extras.find(
(v) => v.index === img_index && v.type == "image"
);
if (!current_extra) {
throw new Error(
"No current extra found matching index " + img_index + " !"
);
}
const current_contributor = current_extra.contributors[0];
const generated_text =
`<img src="${current_extra.image_src}" alt="Body image for the article: ${v.title}" />` +
`<a class="img_credit" href="/staff/${current_contributor.slug}"/>By <span class="discrete-link">${current_contributor.name}</span></a>`;
text =
text.slice(0, regex.lastIndex) +
generated_text +
text.slice(regex.lastIndex);
img_index++;
v.text = text;
}
return v;
}
// articles
async function get_articles_by_department(
department: string,
num?: number,
fetch_text?: boolean
): Promise<[ReceivedArticle]> {
const db = (await clientPromise).db();
let articles_collection = await db.collection("articles");
const limit = num || 10;
const projection = fetch_text ? {} : { text: 0 };
const department_id = DepartmentsArray.findIndex((a) => a == department);
let articles = (await articles_collection
.aggregate<ReceivedArticle>([
{
$match: { section_id: department_id },
},
{ $sort: { volume: -1, issue: -1, rank: -1 } },
{
$lookup: {
from: "staffs",
localField: "contributors",
foreignField: "_id",
as: "contributors",
},
},
{
$lookup: {
from: "staffs",
localField: "cover_image_contributor",
foreignField: "_id",
as: "cover_image_contributor",
},
},
{
$project: {
contributors: { password: 0 },
cover_image_contributor: { password: 0 },
...projection,
},
},
])
.limit(limit)
.toArray()) as [ReceivedArticle];
return articles;
}
async function get_article_by_id(article_id: string): Promise<ReceivedArticle> {
const db = (await clientPromise).db();
let articles_collection = db.collection("articles");
const article = (
await articles_collection
.aggregate<ReceivedArticle>([
{
$match: { _id: new ObjectId(article_id) },
},
{
$lookup: {
from: "staffs",
localField: "contributors",
foreignField: "_id",
as: "contributors",
},
},
{
$lookup: {
from: "staffs",
localField: "cover_image_contributor",
foreignField: "_id",
as: "cover_image_contributor",
},
},
{
$project: {
contributors: { password: 0 },
cover_image_contributor: { password: 0 },
},
},
])
.toArray()
)[0];
return article;
}
async function get_article_by_slug(
article_slug: string
): Promise<ReceivedArticle> {
const db = (await clientPromise).db();
let articles_collection = await db.collection("articles");
let article = await (
await articles_collection
.aggregate<ReceivedArticle>([
{
$match: { slug: article_slug },
},
{
$lookup: {
from: "staffs",
localField: "contributors",
foreignField: "_id",
as: "contributors",
},
},
{
$lookup: {
from: "staffs",
localField: "cover_image_contributor",
foreignField: "_id",
as: "cover_image_contributor",
},
},
])
.toArray()
).map((v) => applyTextImageCredit(v, db))[0];
return article;
}
async function get_articles_by_author(
author_id: mongoObjectId,
num?: number
): Promise<ReceivedArticle[]> {
let articles = await get_articles_by_query(
{
contributors: new ObjectId(String(author_id)),
},
num
);
return articles;
}
async function get_articles_by_query(
query: any,
num?: number,
skip_num?: number,
fetch_text?: boolean,
db_param?: Db
): Promise<ReceivedArticle[]> {
const db = db_param ?? (await clientPromise).db();
let articles_collection = await db.collection("articles");
const limit = num || 10;
const skip = skip_num || 0;
const projection = fetch_text ? {} : { text: 0 };
let articles = await articles_collection
.aggregate<ReceivedArticle>([
{ $match: query },
{ $sort: { volume: -1, issue: -1, rank: -1 } },
{
$lookup: {
from: "staffs",
localField: "contributors",
foreignField: "_id",
as: "contributors",
},
},
{
$lookup: {
from: "staffs",
localField: "cover_image_contributor",
foreignField: "_id",
as: "cover_image_contributor",
},
},
{
$project: {
contributors: { password: 0 },
cover_image_contributor: { password: 0 },
...projection,
},
},
])
.skip(skip)
.limit(limit)
.toArray();
return articles;
}
async function get_articles_by_string_query(
query: string,
num?: number
): Promise<ReceivedArticle[]> {
const db = (await clientPromise).db();
let articles_collection = await db.collection("articles");
const limit = num || 10;
let articles = await articles_collection
.aggregate<ReceivedArticle>([
{
$search: {
index: "articles_search_index",
text: {
query: query,
path: {
wildcard: "*",
},
},
},
},
{
$lookup: {
from: "staffs",
localField: "contributors",
foreignField: "_id",
as: "contributors",
},
},
{
$lookup: {
from: "staffs",
localField: "cover_image_contributor",
foreignField: "_id",
as: "cover_image_contributor",
},
},
{
$project: {
contributors: { password: 0 },
cover_image_contributor: { password: 0 },
},
},
])
.limit(limit)
.toArray();
return articles;
}
async function get_media_by_author(author_id: mongoObjectId, num?: number) {
const db = (await clientPromise).db();
let articles = await get_articles_by_query(
{
cover_image_contributor: new ObjectId(String(author_id)),
},
num,
0,
false,
db // use cached DB
);
let media = articles.map((v) => {
return {
cover_image: v.cover_image,
article_slug: v.slug,
};
});
let extras_collection = await db.collection("article_extras");
type ExtraWithArticle = Omit<ReceivedArticleExtra, "article"> & {
article: ReceivedArticle[];
};
const article_extras = (
await extras_collection
.aggregate<ExtraWithArticle>([
{
$match: {
contributors: new ObjectId(String(author_id)),
},
},
{
$lookup: {
from: "articles",
localField: "article",
foreignField: "_id",
as: "article",
},
},
{
$project: {
article: { text: 0 },
},
},
])
.toArray()
)
.filter((v) => v.type == "image")
.map((v) => {
return {
cover_image: v.image_src,
article_slug: v.article[0].slug,
};
});
media = media.concat(article_extras);
return media;
}
// staff
async function get_staff_by_id(_id: string): Promise<ReceivedStaff> {
const db = (await clientPromise).db();
let staff_collection = await db.collection("staffs");
let staff = (
await staff_collection
.aggregate([
{ $match: { _id: new ObjectId(_id) } },
{
$project: {
password: 0,
},
},
])
.toArray()
)[0] as ReceivedStaff;
return staff;
}
async function get_staff_by_slug(slug: string): Promise<ReceivedStaff> {
const db = (await clientPromise).db();
let staff_collection = await db.collection("staffs");
let staff = (
await staff_collection
.aggregate([
{ $match: { slug: slug } },
{
$project: {
password: 0,
},
},
])
.toArray()
)[0] as ReceivedStaff;
return staff;
}
async function get_staff_by_position(position: string): Promise<ReceivedStaff> {
const db = (await clientPromise).db();
let staff_collection = await db.collection("staffs");
let staff = (
await staff_collection
.aggregate([
{ $match: { position: position } },
{
$project: {
password: 0,
},
},
])
.toArray()
)[0] as ReceivedStaff;
return staff;
}
async function get_staffs_by_query(query: any): Promise<ReceivedStaff[]> {
const db = (await clientPromise).db();
let staff_collection = await db.collection("staffs");
let staff = (await staff_collection
.aggregate([
{ $match: query },
{
$project: {
password: 0,
},
},
])
.toArray()) as ReceivedStaff[];
return staff;
}
export {
get_articles_by_department,
get_article_by_id,
get_article_by_slug,
get_articles_by_author,
get_articles_by_query,
get_staff_by_id,
get_staff_by_position,
get_staff_by_slug,
get_staffs_by_query,
get_media_by_author,
get_articles_by_string_query,
};