Skip to content

Commit

Permalink
Merge pull request #59 from dolthub/taylor/graphql-fix
Browse files Browse the repository at this point in the history
graphql: Check if store exists
  • Loading branch information
tbantle22 authored Nov 28, 2023
2 parents 450883e + 2e7d278 commit 971a36d
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions graphql-server/src/fileStore/fileStore.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand All @@ -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",
});
}
}

0 comments on commit 971a36d

Please sign in to comment.