Skip to content

Commit

Permalink
Merge pull request #9 from Mirasaki/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Mirasaki authored Jun 8, 2024
2 parents b594a26 + d6e409a commit 7f313f1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/modules/json-database/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getAllJSONDataController = async (
}

// Resolve data
const allJSONData = getAllJSONFiles(cfg.DIRECTORY);
const allJSONData = await getAllJSONFiles(cfg.DIRECTORY);

// Don't handle error (empty),
// provide empty array for null fallback
Expand All @@ -41,7 +41,7 @@ export const getJSONDataController = async (

// Resolve data
const { identifier } = req.params;
const jsonData = getJSONFile(cfg.DIRECTORY, identifier);
const jsonData = await getJSONFile(cfg.DIRECTORY, identifier);

// Handle errors
if (jsonData === null) {
Expand All @@ -68,7 +68,7 @@ export const deleteJsonDataController = async (

// Resolve data
const { identifier } = req.params;
const jsonData = getJSONFile(cfg.DIRECTORY, identifier);
const jsonData = await getJSONFile(cfg.DIRECTORY, identifier);

// Handle errors
if (jsonData === null) {
Expand All @@ -77,7 +77,7 @@ export const deleteJsonDataController = async (
}

// Delete the file/data
deleteJSONFile(cfg.DIRECTORY, identifier);
await deleteJSONFile(cfg.DIRECTORY, identifier);

// Ok, send the response
res.json({ data: jsonData });
Expand All @@ -99,7 +99,7 @@ export const putJSONDataController = async (
// Resolve data
const { identifier } = req.params;
const newJSONContent = req.body;
const response = putJSONFile(cfg.DIRECTORY, identifier, newJSONContent);
const response = await putJSONFile(cfg.DIRECTORY, identifier, newJSONContent);

// Handle errors - target to overwrite doesn't exist
if (response === null) {
Expand Down Expand Up @@ -127,7 +127,7 @@ export const patchJSONDataController = async (
// Resolve data
const { identifier } = req.params;
const updatedJSONFields = req.body;
const jsonData = getJSONFile(cfg.DIRECTORY, identifier);
const jsonData = await getJSONFile(cfg.DIRECTORY, identifier);
if (!jsonData) {
next(resourceNotFound(`JSON_DATABASE(${name})-${identifier}`));
return;
Expand All @@ -139,7 +139,7 @@ export const patchJSONDataController = async (
}

// Reflect in file - can't 404
putJSONFile(cfg.DIRECTORY, identifier, jsonData);
await putJSONFile(cfg.DIRECTORY, identifier, jsonData);

// Ok, send the response
res.json({ data: jsonData });
Expand Down
35 changes: 20 additions & 15 deletions src/modules/json-database/helper.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,72 @@
import { readFileSync, writeFileSync, rmSync } from 'fs';
import { getFiles } from '../files';
import { debugLog } from '../../debug';
import { rm, writeFile, readFile } from 'fs/promises';

export const getAllJSONFiles = (targetPath: string) => {
export const getAllJSONFiles = async (targetPath: string) => {
const allJSONFiles = getFiles(targetPath, [ '.json' ]);
debugLog(`Found ${allJSONFiles.length} JSON files in ${targetPath}`);
return allJSONFiles
.map((pathToJSON) => {
const textData = readFileSync(pathToJSON, { encoding: 'utf-8' });
return (await Promise.all(
allJSONFiles.map(async (pathToJSON) => {
const textData = await readFile(pathToJSON, { encoding: 'utf-8' });
try {
return JSON.parse(textData);
}
catch {
catch (err) {
console.error(`Error parsing JSON file at ${pathToJSON}`, err);
// Either invalid syntax in JSON or not a file
// full path is shown in those errors, avoid
return null;
}
})
.filter((e) => !!e); // Keep only truthy
)).filter((e) => !!e); // Keep only truthy
};

export const getJSONFile = (targetPath: string, identifier: string) => {
export const getJSONFile = async (targetPath: string, identifier: string) => {
const finalPath = targetPath + `/${ identifier }.json`;
debugLog(`Attempting to get JSON file from ${finalPath}`);
try {
const textData = readFileSync(finalPath, { encoding: 'utf-8' });
const textData = await readFile(finalPath, { encoding: 'utf-8' });
return JSON.parse(textData);
}
catch (err) {
console.error(`Error reading JSON file from ${finalPath}`, err);
// Either invalid syntax in JSON or not a file
// full path is shown in those errors, avoid
return null;
}
};

export const putJSONFile = (
export const putJSONFile = async (
targetPath: string,
identifier: string,
newJSONContent: unknown
) => {
const finalPath = targetPath + `/${ identifier }.json`;
debugLog(`Attempting to put JSON file to ${finalPath}`);
try {
writeFileSync(
await writeFile(
finalPath,
JSON.stringify(newJSONContent, null, 2),
{ encoding: 'utf-8' }
{ encoding: 'utf-8'}
);
return true;
}
catch {
catch (err) {
console.error(`Error writing JSON file to ${finalPath}`, err);
// Either invalid syntax in JSON or not a file
// full path is shown in those errors, avoid
return null;
}
};

export const deleteJSONFile = (targetPath: string, identifier: string) => {
export const deleteJSONFile = async (targetPath: string, identifier: string) => {
const finalPath = targetPath + `/${ identifier }.json`;
debugLog(`Attempting to delete JSON file at ${finalPath}`);
try {
rmSync(finalPath, { recursive: false, force: false });
await rm(finalPath, { recursive: false, force: false });
}
catch (err) {
console.error(`Error deleting JSON file at ${finalPath}`, err);
// Either invalid syntax in JSON or not a file
// full path is shown in those errors, avoid
return null;
Expand Down

0 comments on commit 7f313f1

Please sign in to comment.