diff --git a/src/db/index.ts b/src/db/index.ts index 9c4df56..f8c3d1b 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,7 +1,8 @@ import { logError, logSuccess, projectRoot } from "../utils"; import fs from "fs"; import path from "path"; -import { Client, QueryResult } from "pg"; +import { Client } from "pg"; +import fsp from "fs/promises"; export const tableExists = async (db: Client): Promise => { const result = await db @@ -47,33 +48,22 @@ export const populateDB = async (db: Client) => { ) .catch((err) => err); - fs.readFile( - filepath, - { encoding: "utf-8" }, - async (err: Error | null, data: string) => { - if (err != null) { - logError(err.message); - throw err; - } + let data = await fsp.readFile(filepath, { encoding: "utf-8" }); - let arr: Array = data.split("\r\n"); + let arr: string[] = data.split("\r\n"); - // 1k entries per query - const chunkSize = 1000; - for (let i = 0; i < arr.length; i += chunkSize) { - const chunk: Array = arr.slice(i, i + chunkSize); + // 1k entries per query + const chunkSize = 1000; + for (let i = 0; i < arr.length; i += chunkSize) { + const chunk: string[] = arr.slice(i, i + chunkSize); - const queryStr = { - text: `INSERT INTO words (val) VALUES ${expand( - chunk.length - )}`, - values: chunk, - }; + const queryStr = { + text: `INSERT INTO words (val) VALUES ${expand(chunk.length)}`, + values: chunk, + }; - await db.query(queryStr); - } - } - ); + await db.query(queryStr); + } logSuccess("Successfully populated the database!"); }; diff --git a/src/main.ts b/src/main.ts index 6700a43..6d6f447 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import { tableExists, populateDB } from "./db"; const main = async () => { dotenv.config(); - createDataDir(); + await createDataDir(); // init db const pool = new Pool(); diff --git a/src/routes/bin.ts b/src/routes/bin.ts index 06361ae..6fd59dc 100644 --- a/src/routes/bin.ts +++ b/src/routes/bin.ts @@ -3,6 +3,7 @@ import { Pool, QueryResult } from "pg"; import fs from "fs"; import path from "path"; import { Word } from "../utils/types"; +import fsp from "fs/promises"; import { projectRoot } from "../utils"; import { getAvailableWord, setWordTaken } from "../db/bin"; @@ -18,10 +19,9 @@ export default function BinRouter(db: Pool) { const word: Word | undefined = await getAvailableWord(db); if (word != undefined) { - fs.writeFile( + await fsp.writeFile( path.join(projectRoot, "data", word.val + ".txt"), - body, - (err) => err + body ); await setWordTaken(db, word.id); diff --git a/src/utils/index.ts b/src/utils/index.ts index 80668f6..4212250 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,6 +2,7 @@ import chalk from "chalk"; import { ErrorRequestHandler } from "express"; import path from "path"; import fs from "fs"; +import fsp from "fs/promises"; // logs export const logError = (msg: string) => console.error(chalk.bold.red(msg)); @@ -16,10 +17,10 @@ export const errorHandler: ErrorRequestHandler = (err, _, res, __) => { return res.status(500).json({ message: "Internal server error!" }); }; -export const createDataDir = () => { +export const createDataDir = async () => { let dir = path.join(projectRoot, "data"); if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); + await fsp.mkdir(dir); } };