diff --git a/graphql-server/src/fileStore/fileStore.service.ts b/graphql-server/src/fileStore/fileStore.service.ts index 493c7137..ab30e7eb 100644 --- a/graphql-server/src/fileStore/fileStore.service.ts +++ b/graphql-server/src/fileStore/fileStore.service.ts @@ -3,17 +3,19 @@ import * as fs from "fs"; import { resolve } from "path"; import { DatabaseConnection } from "../databases/database.model"; +const storePath = resolve(__dirname, "../../store/store.json"); + @Injectable() export class FileStoreService { // eslint-disable-next-line class-methods-use-this getStore(): DatabaseConnection[] { + if (!fs.existsSync(storePath)) { + return []; + } try { - const file = fs.readFileSync( - resolve(__dirname, "../../store/store.json"), - { - encoding: "utf8", - }, - ); + const file = fs.readFileSync(storePath, { + encoding: "utf8", + }); if (!file) { return []; } @@ -40,24 +42,16 @@ export class FileStoreService { fs.mkdirSync(resolve(__dirname, "../../store")); } - fs.writeFileSync( - resolve(__dirname, "../../store/store.json"), - JSON.stringify(store), - { - encoding: "utf8", - }, - ); + fs.writeFileSync(storePath, JSON.stringify(store), { + encoding: "utf8", + }); } removeItemFromStore(name: string): void { const store = this.getStore(); const newStore = store.filter(item => item.name !== name); - fs.writeFileSync( - resolve(__dirname, "../../store/store.json"), - JSON.stringify(newStore), - { - encoding: "utf8", - }, - ); + fs.writeFileSync(storePath, JSON.stringify(newStore), { + encoding: "utf8", + }); } }