Comparing version 1.2.3 to 1.2.4
224
index.js
/*! | ||
* | ||
* Copyright(c) 2023 Mark Maher Ewdia | ||
* | ||
* Copyright(c) 2023 Mark Maher Ewida | ||
* MIT Licensed | ||
*/ | ||
'use strict'; | ||
"use strict"; | ||
@@ -28,3 +28,3 @@ const fs = require("fs").promises; | ||
const currentDate = formatDateTime(new Date()); | ||
const currentTime = currentDate.replace(/\D/g, ''); // Remove non-numeric characters | ||
const currentTime = currentDate.replace(/\D/g, ""); // Remove non-numeric characters | ||
@@ -36,6 +36,7 @@ class jsonverse { | ||
this.backupFolderPath = path.join(this.dataFolderPath, "Backup"); | ||
this.logFilePath = path.join(this.logFolderPath, "app.log"); | ||
this.logFilePath = path.join(this.logFolderPath, "data.log"); | ||
this.enableLogToConsoleAndFile = activateLogs; | ||
this.searchIndex = {}; | ||
this.init(); | ||
this.searchIndex = {}; | ||
} | ||
@@ -46,28 +47,62 @@ | ||
function removeAnsiEscapeCodes(input) { | ||
return input.replace(/\x1b\[\d+m/g, ''); | ||
return input.replace(/\x1b\[\d+m/g, ""); | ||
} | ||
// Log to the file | ||
const logFilePath = path.join(this.logFolderPath, "app.log"); | ||
if (this.enableLogToConsoleAndFile) { | ||
try { | ||
console.log(message); | ||
try { | ||
await fs.mkdir(this.logFolderPath, { recursive: true }); | ||
await fs.appendFile( | ||
logFilePath, | ||
`${currentDate} ${removeAnsiEscapeCodes(message)}\n`, | ||
"utf8" | ||
); | ||
} catch (error) { | ||
if (error.code === "ENOENT") { | ||
await fs.mkdir(this.logFolderPath, { recursive: true }); | ||
await fs.appendFile(logFilePath, `${currentDate} ${removeAnsiEscapeCodes(message)}\n`, "utf8"); | ||
} catch (error) { | ||
if (error.code === "ENOENT") { | ||
await fs.mkdir(this.logFolderPath, { recursive: true }); | ||
await fs.writeFile(logFilePath, `${removeAnsiEscapeCodes(message)}\n`, "utf8"); | ||
} else { | ||
console.log( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to save the logs: ${error}` | ||
try { | ||
await fs.writeFile( | ||
logFilePath, | ||
`${removeAnsiEscapeCodes(message)}\n`, | ||
"utf8" | ||
); | ||
} catch (readError) { | ||
this.handleError(`Failed to create log file: ${readError}`); | ||
} | ||
} else { | ||
this.handleError(`Failed to save logs: ${error}`); | ||
} | ||
} | ||
} | ||
handleError(message) { | ||
if (this.enableLogToConsoleAndFile) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} ${message}` | ||
); | ||
console.log( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} ${message}` | ||
); | ||
} else { | ||
console.log(message); | ||
console.log( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} ${message}` | ||
); | ||
} | ||
} | ||
} | ||
logSuccess(message) { | ||
if (this.enableLogToConsoleAndFile) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} ${message}` | ||
); | ||
console.log( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} ${message}` | ||
); | ||
} else { | ||
console.log( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} ${message}` | ||
); | ||
} | ||
} | ||
// Encrypt sensitive data | ||
@@ -102,5 +137,3 @@ encrypt(data, secretKey) { | ||
); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data backup created: ${backupFileName}` | ||
); | ||
this.logSuccess(`Data backup created: ${backupFileName}`); | ||
} catch (error) { | ||
@@ -115,8 +148,6 @@ if (error.code === "ENOENT") { | ||
); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data backup created: ${backupFileName}` | ||
); | ||
this.logSuccess(`Data backup created: ${backupFileName}`); | ||
} catch (readError) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to create data backup for ${dataName}: ${readError}` | ||
this.handleError( | ||
`Failed to create data backup for ${dataName}: ${readError}` | ||
); | ||
@@ -126,4 +157,4 @@ return null; | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to create data backup for ${dataName}: ${error}` | ||
this.handleError( | ||
`Failed to create data backup for ${dataName}: ${error}` | ||
); | ||
@@ -134,5 +165,3 @@ return null; | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to create data backup for ${dataName}` | ||
); | ||
this.handleError(`Failed to create data backup for ${dataName}`); | ||
} | ||
@@ -150,8 +179,6 @@ } | ||
await this.writeDataByFileName(dataName, JSON.parse(backupData)); // Replace data in the file | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data restored from backup: ${backupFileName}` | ||
); | ||
this.logSuccess(`Data restored from backup: ${backupFileName}`); | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to restore data from backup: ${backupFileName}: ${error}` | ||
this.handleError( | ||
`Failed to restore data from backup: ${backupFileName}: ${error}` | ||
); | ||
@@ -186,8 +213,6 @@ } | ||
await fs.unlink(backupFilePath); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Backup deleted: ${backupFile}` | ||
); | ||
this.logSuccess(`Backup deleted: ${backupFile}`); | ||
} catch (deleteError) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to delete backup: ${backupFile}: ${deleteError}` | ||
this.handleError( | ||
`Failed to delete backup: ${backupFile}: ${deleteError}` | ||
); | ||
@@ -229,7 +254,5 @@ } | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} The path "${this.dataFolderPath}" doesn't exist.` | ||
); | ||
this.handleError(`The path "${this.dataFolderPath}" doesn't exist.`); | ||
const answer = await this.askForConfirmation( | ||
`${colors.bright}${colors.fg.yellow}[Question]:${colors.reset} Do you want to create the path folder? (Y/N): ` | ||
`Do you want to create the path folder? (Y/N): ` | ||
); | ||
@@ -240,18 +263,10 @@ | ||
await fs.mkdir(this.dataFolderPath, { recursive: true }); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Path folder created successfully.` | ||
); | ||
this.logSuccess(`Path folder created successfully.`); | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Creating path folder: ${error}` | ||
); | ||
this.handleError(`Creating path folder: ${error}`); | ||
} | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Path folder not created.` | ||
); | ||
this.handleError(`Path folder not created.`); | ||
} | ||
} | ||
return this.dataFolderPath; | ||
} | ||
@@ -263,5 +278,3 @@ | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} The file "${filePath}" doesn't exist.` | ||
); | ||
this.handleError(`The file "${filePath}" doesn't exist.`); | ||
const answer = await this.askForConfirmation( | ||
@@ -274,14 +287,8 @@ `${colors.bright}${colors.fg.yellow}[Question]:${colors.reset} Do you want to create the file? (Y/N): ` | ||
await fs.writeFile(filePath, "[]"); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} File created successfully.` | ||
); | ||
this.logSuccess(`File created successfully.`); | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Creating file: ${error}` | ||
); | ||
this.handleError(`Creating file: ${error}`); | ||
} | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} File not created.` | ||
); | ||
this.handleError(`File not created.`); | ||
} | ||
@@ -319,5 +326,3 @@ } | ||
await fs.writeFile(filePath, JSON.stringify(data, null, 2), "utf8"); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data exported to JSON: ${filePath}` | ||
); | ||
this.logSuccess(`Data exported to JSON: ${filePath}`); | ||
} else if (format === "csv") { | ||
@@ -334,5 +339,3 @@ // Export as CSV | ||
await fs.writeFile(filePath, csvString, "utf8"); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data exported to CSV: ${filePath}` | ||
); | ||
this.logSuccess(`Data exported to CSV: ${filePath}`); | ||
} | ||
@@ -348,5 +351,3 @@ } | ||
await this.writeDataByFileName(dataName, newData); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data imported from JSON: ${filePath}` | ||
); | ||
this.logSuccess(`Data imported from JSON: ${filePath}`); | ||
} else if (format === "csv") { | ||
@@ -356,5 +357,3 @@ // Import CSV | ||
await this.writeDataByFileName(dataName, csvData); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data imported from CSV: ${filePath}` | ||
); | ||
this.logSuccess(`Data imported from CSV: ${filePath}`); | ||
} | ||
@@ -368,5 +367,3 @@ } | ||
await this.writeDataByFileName(dataName, transformedData); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data transformed and saved.` | ||
); | ||
this.logSuccess(`Data transformed and saved.`); | ||
} | ||
@@ -408,5 +405,3 @@ | ||
await this.initFile(filePath).catch((initError) => { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Initializing file: ${initError}` | ||
); | ||
this.handleError(`Initializing file: ${initError}`); | ||
}); | ||
@@ -421,11 +416,7 @@ // Retry reading the file | ||
} catch (readError) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Reading file ${filePath}: ${readError}` | ||
); | ||
this.handleError(`Reading file ${filePath}: ${readError}`); | ||
return null; | ||
} | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Reading file ${filePath}: ${error}` | ||
); | ||
this.handleError(`Reading file ${filePath}: ${error}`); | ||
return null; | ||
@@ -453,5 +444,3 @@ } | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Writing to file ${filePath}: ${error}` | ||
); | ||
this.handleError(`Writing to file ${filePath}: ${error}`); | ||
} | ||
@@ -465,5 +454,3 @@ } | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Writing to item with ID: ${filePath}: ${error}` | ||
); | ||
this.handleError(`Writing to item with ID: ${filePath}: ${error}`); | ||
} | ||
@@ -488,9 +475,5 @@ } | ||
await this.writeDataById(id, existingData); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Item with ID ${id} has been edited.` | ||
); | ||
this.logSuccess(`Item with ID ${id} has been edited.`); | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Item with ID ${id} not found.` | ||
); | ||
this.handleError(`Item with ID ${id} not found.`); | ||
} | ||
@@ -514,10 +497,6 @@ } | ||
await this.writeDataByFileName(dataName, existingData).then(() => { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} New Data added to DB: ${dataName}` | ||
); | ||
this.logSuccess(`New Data added to DB: ${dataName}`); | ||
}); | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Data failed to be added to the DB: ${dataName}` | ||
); | ||
this.handleError(`Data failed to be added to the DB: ${dataName}`); | ||
} | ||
@@ -537,7 +516,5 @@ } | ||
this.logSuccess(`Item has been deleted.`); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Item has been deleted.` | ||
); | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Deleted item:`, | ||
`${colors.bright}${colors.fg.blue}[Info]:${colors.reset} Deleted item:`, | ||
dataItemToDelete | ||
@@ -547,16 +524,11 @@ ); | ||
if (typeof window == "undefined") { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Item Deleted successfully!` | ||
); | ||
this.logSuccess(`Item Deleted successfully!`); | ||
} | ||
} else { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Item is already deleted.` | ||
`${colors.bright}${colors.fg.blue}[Info]:${colors.reset} Item is already deleted.` | ||
); | ||
} | ||
} catch (error) { | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Deleting data:`, | ||
error | ||
); | ||
this.handleError(`Deleting data:`, error); | ||
} | ||
@@ -563,0 +535,0 @@ } |
@@ -0,1 +1,9 @@ | ||
/*! | ||
* | ||
* Copyright(c) 2023 Mark Maher Ewida | ||
* MIT Licensed | ||
*/ | ||
"use strict"; | ||
// Define color codes | ||
@@ -2,0 +10,0 @@ const colors = { |
{ | ||
"name": "jsonverse", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "jsonVerse is a lightweight JSON-based database package for Node.js. It provides a simple interface to store, retrieve, and manage data using JSON files.", | ||
@@ -70,4 +70,4 @@ "main": "index.js", | ||
"fuse.js": "^6.6.2", | ||
"winston": "^3.10.0" | ||
"json2csv": "^6.0.0-alpha.2" | ||
} | ||
} |
@@ -225,2 +225,2 @@ # jsonVerse | ||
The jsonVerse package simplifies the management of JSON data files within a specified folder. With the provided examples and usage instructions, you'll be able to efficiently integrate the jsonVerse package into your projects to streamline data operations. | ||
The jsonVerse package simplifies the management of JSON data files within a specified folder. With the provided examples and usage instructions, you'll be able to efficiently integrate the jsonVerse package into your projects to streamline data operations. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26535
509
225
+ Addedjson2csv@^6.0.0-alpha.2
+ Added@streamparser/json@0.0.6(transitive)
+ Addedcommander@6.2.1(transitive)
+ Addedjson2csv@6.0.0-alpha.2(transitive)
+ Addedlodash.get@4.4.2(transitive)
- Removedwinston@^3.10.0
- Removed@colors/colors@1.6.0(transitive)
- Removed@dabh/diagnostics@2.0.3(transitive)
- Removed@types/triple-beam@1.3.5(transitive)
- Removedasync@3.2.6(transitive)
- Removedcolor@3.2.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcolor-string@1.9.1(transitive)
- Removedcolorspace@1.1.4(transitive)
- Removedenabled@2.0.0(transitive)
- Removedfecha@4.2.3(transitive)
- Removedfn.name@1.1.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.3.2(transitive)
- Removedis-stream@2.0.1(transitive)
- Removedkuler@2.0.0(transitive)
- Removedlogform@2.7.0(transitive)
- Removedms@2.1.3(transitive)
- Removedone-time@1.0.0(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafe-stable-stringify@2.5.0(transitive)
- Removedsimple-swizzle@0.2.2(transitive)
- Removedstack-trace@0.0.10(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedtext-hex@1.0.0(transitive)
- Removedtriple-beam@1.4.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedwinston@3.17.0(transitive)
- Removedwinston-transport@4.9.0(transitive)