-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
42 lines (38 loc) · 1.27 KB
/
index.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
import { ReadableApp } from "@scramjet/types";
import {
connect,
createConnection,
createTodos,
disconnect,
insertTodo,
} from "./lib/db";
import { chunksToLines } from "./lib/utils";
const app: ReadableApp<string, [string, string, string, string]> =
async function* (input) {
const connection = createConnection({
host: this.config.host as string,
user: this.config.user as string,
password: this.config.password as string,
database: this.config.database as string,
});
try {
this.logger.debug("Connecting to database");
await connect(connection);
this.logger.debug("Checking for todos table");
await createTodos(connection);
this.logger.debug("Awaiting inserts");
for await (const line of chunksToLines(input)) {
try {
this.logger.debug(`Inserting ${line}`);
await insertTodo(connection)(line);
} catch (err) {
this.logger.error("Error while inserting", err);
}
}
} catch (err) {
this.logger.error("Error while starting", err);
this.logger.debug("Disconnecting");
await disconnect(connection);
}
};
export default app;