forked from emmikkelsen/node-lectio-til-ics
-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
42 lines (34 loc) · 942 Bytes
/
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
/* eslint-disable no-console */
import * as fs from 'fs';
import * as path from 'path';
import { parse } from 'fast-csv';
const { stderr } = process;
const _db = () => {
return new Promise((resolve, reject) => {
let result = [];
fs.createReadStream(path.resolve(__dirname, 'db.csv'))
.pipe(parse({ headers: true }))
.on('data', (row) => result.push(row))
.on('end', () => resolve(result))
.on('error', (error) => reject(error));
});
};
const _users = async () => {
try {
return await _db();
} catch (error) {
stderr.write(`db error: ${error}`);
}
};
const findUserBySchool = async (school) => {
return new Promise(async (resolve, reject) => {
const users = await _users();
const keys = Object.keys(users);
keys.forEach((key) => {
const user = users[key];
if (user.school === school) resolve(user);
});
reject();
});
};
export { findUserBySchool };