mirror of
https://github.com/okiba-org/backend.git
synced 2025-02-22 17:22:02 +00:00
refactor: change to async file operations
This commit is contained in:
parent
68eebf1229
commit
f520d16f19
@ -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<boolean> => {
|
||||
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<string> = 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<string> = arr.slice(i, i + chunkSize);
|
||||
const chunk: string[] = arr.slice(i, i + chunkSize);
|
||||
|
||||
const queryStr = {
|
||||
text: `INSERT INTO words (val) VALUES ${expand(
|
||||
chunk.length
|
||||
)}`,
|
||||
text: `INSERT INTO words (val) VALUES ${expand(chunk.length)}`,
|
||||
values: chunk,
|
||||
};
|
||||
|
||||
await db.query(queryStr);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
logSuccess("Successfully populated the database!");
|
||||
};
|
||||
|
@ -7,7 +7,7 @@ import { tableExists, populateDB } from "./db";
|
||||
|
||||
const main = async () => {
|
||||
dotenv.config();
|
||||
createDataDir();
|
||||
await createDataDir();
|
||||
|
||||
// init db
|
||||
const pool = new Pool();
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user