Comparing version 1.2.2 to 1.2.3
290
index.js
@@ -1,2 +0,9 @@ | ||
// * Dependencies | ||
/*! | ||
* | ||
* Copyright(c) 2023 Mark Maher Ewdia | ||
* MIT Licensed | ||
*/ | ||
'use strict'; | ||
const fs = require("fs").promises; | ||
@@ -10,33 +17,22 @@ const path = require("path"); | ||
function getDateTime() { | ||
var now = new Date(); | ||
var year = now.getFullYear(); | ||
var month = now.getMonth() + 1; | ||
var day = now.getDate(); | ||
var hour = now.getHours(); | ||
var minute = now.getMinutes(); | ||
var second = now.getSeconds(); | ||
if (month.toString().length == 1) { | ||
month = "0" + month; | ||
} | ||
if (day.toString().length == 1) { | ||
day = "0" + day; | ||
} | ||
if (hour.toString().length == 1) { | ||
hour = "0" + hour; | ||
} | ||
if (minute.toString().length == 1) { | ||
minute = "0" + minute; | ||
} | ||
if (second.toString().length == 1) { | ||
second = "0" + second; | ||
} | ||
var dateTime = year + month + day + hour + minute + second; | ||
return dateTime; | ||
function formatDateTime(date) { | ||
const year = date.getFullYear(); | ||
const month = String(date.getMonth() + 1).padStart(2, "0"); | ||
const day = String(date.getDate()).padStart(2, "0"); | ||
const hour = String(date.getHours()).padStart(2, "0"); | ||
const minute = String(date.getMinutes()).padStart(2, "0"); | ||
const second = String(date.getSeconds()).padStart(2, "0"); | ||
return `${year}/${month}/${day} ${hour}:${minute}:${second}`; | ||
} | ||
const currentTime = getDateTime(); | ||
const currentDate = formatDateTime(new Date()); | ||
const currentTime = currentDate.replace(/\D/g, ''); // Remove non-numeric characters | ||
class jsonverse { | ||
constructor(dataFolderPath) { | ||
constructor(dataFolderPath, activateLogs) { | ||
this.dataFolderPath = dataFolderPath; | ||
this.logFolderPath = path.join("./Logs"); | ||
this.backupFolderPath = path.join(this.dataFolderPath, "Backup"); | ||
this.logFilePath = path.join(this.logFolderPath, "app.log"); | ||
this.enableLogToConsoleAndFile = activateLogs; | ||
this.init(); | ||
@@ -46,4 +42,33 @@ this.searchIndex = {}; | ||
async logToConsoleAndFile(message) { | ||
// Function to remove ANSI escape codes from a string | ||
function removeAnsiEscapeCodes(input) { | ||
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); | ||
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}` | ||
); | ||
} | ||
} | ||
} else { | ||
console.log(message); | ||
} | ||
} | ||
// Encrypt sensitive data | ||
encryptData(data, secretKey) { | ||
encrypt(data, secretKey) { | ||
const encryptedData = crypto.AES.encrypt( | ||
@@ -57,3 +82,3 @@ JSON.stringify(data), | ||
// Decrypt sensitive data | ||
decryptData(encryptedData, secretKey) { | ||
decrypt(encryptedData, secretKey) { | ||
const decryptedData = crypto.AES.decrypt(encryptedData, secretKey).toString( | ||
@@ -66,4 +91,4 @@ crypto.enc.Utf8 | ||
// Create Data Backup | ||
async createDataBackup(dataName) { | ||
const currentData = await this.readDataFromFile(dataName); | ||
async backupCreate(dataName) { | ||
const currentData = await this.readData(dataName); | ||
if (currentData !== null) { | ||
@@ -78,4 +103,4 @@ const backupFileName = `./Backup/${dataName}_${currentTime}.json`; | ||
); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data backup created: ${backupFileName}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data backup created: ${backupFileName}` | ||
); | ||
@@ -91,8 +116,8 @@ } catch (error) { | ||
); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data backup created: ${backupFileName}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data backup created: ${backupFileName}` | ||
); | ||
} catch (readError) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Failed to create data backup for ${dataName}: ${readError}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to create data backup for ${dataName}: ${readError}` | ||
); | ||
@@ -102,4 +127,4 @@ return null; | ||
} else { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Failed to create data backup for ${dataName}: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to create data backup for ${dataName}: ${error}` | ||
); | ||
@@ -110,4 +135,4 @@ return null; | ||
} else { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Failed to create data backup for ${dataName}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to create data backup for ${dataName}` | ||
); | ||
@@ -118,3 +143,3 @@ } | ||
// Restore Data from Backup | ||
async restoreDataFromBackup(dataName, backupFileName) { | ||
async backupRestore(dataName, backupFileName) { | ||
const backupFilePath = path.join( | ||
@@ -127,8 +152,8 @@ this.dataFolderPath + "/Backup", | ||
await this.writeDataByFileName(dataName, JSON.parse(backupData)); // Replace data in the file | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data restored from backup: ${backupFileName}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data restored from backup: ${backupFileName}` | ||
); | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Failed to restore data from backup: ${backupFileName}: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to restore data from backup: ${backupFileName}: ${error}` | ||
); | ||
@@ -139,3 +164,3 @@ } | ||
// Cleanup Old Backups | ||
async cleanupOldBackups(dataName, retentionDays) { | ||
async backupDelete(dataName, retentionDays) { | ||
const backupFiles = await this.getBackupFiles(dataName); | ||
@@ -145,3 +170,5 @@ const currentDate = new Date(currentTime); | ||
for (const backupFile of backupFiles) { | ||
const backupDateStr = backupFile.split("_")[1].split(".json")[0]; | ||
const backupDateStr = backupFile | ||
.split(dataName + "_")[1] | ||
.split(".json")[0]; | ||
const year = parseInt(backupDateStr.slice(0, 4)); | ||
@@ -155,7 +182,4 @@ const month = parseInt(backupDateStr.slice(4, 6)) - 1; // Months are 0-indexed | ||
const backupDate = new Date(year, month, day, hour, minute, second); | ||
const diffInDays = Math.floor( | ||
(currentDate - backupDate) / (1000 * 60 * 60 * 24) | ||
); | ||
if (diffInDays > retentionDays) { | ||
if (backupDate > retentionDays) { | ||
const backupFilePath = path.join( | ||
@@ -167,8 +191,8 @@ this.dataFolderPath + "/Backup", | ||
await fs.unlink(backupFilePath); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Backup deleted: ${backupFile}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Backup deleted: ${backupFile}` | ||
); | ||
} catch (deleteError) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Failed to delete backup: ${backupFile}: ${deleteError}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Failed to delete backup: ${backupFile}: ${deleteError}` | ||
); | ||
@@ -189,3 +213,3 @@ } | ||
initSearchIndex(dataName, options) { | ||
const data = this.readDataFromFile(dataName); | ||
const data = this.readData(dataName); | ||
if (data !== null) { | ||
@@ -211,7 +235,7 @@ const fuse = new Fuse(data, options); | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}The path "${this.dataFolderPath}" doesn't exist.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} The path "${this.dataFolderPath}" doesn't exist.` | ||
); | ||
const answer = await this.askForConfirmation( | ||
`${colors.fg.yellow}[Question]: ${colors.reset}Do you want to create the path folder? (Y/N): ` | ||
`${colors.bright}${colors.fg.yellow}[Question]:${colors.reset} Do you want to create the path folder? (Y/N): ` | ||
); | ||
@@ -222,13 +246,13 @@ | ||
await fs.mkdir(this.dataFolderPath, { recursive: true }); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Path folder created successfully.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Path folder created successfully.` | ||
); | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Creating path folder: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Creating path folder: ${error}` | ||
); | ||
} | ||
} else { | ||
console.log( | ||
`${colors.fg.blue}[Info]: ${colors.reset}Path folder not created.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Path folder not created.` | ||
); | ||
@@ -245,7 +269,7 @@ } | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}The file "${filePath}" doesn't exist.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} The file "${filePath}" doesn't exist.` | ||
); | ||
const answer = await this.askForConfirmation( | ||
`${colors.fg.yellow}[Question]: ${colors.reset}Do you want to create the file? (Y/N): ` | ||
`${colors.bright}${colors.fg.yellow}[Question]:${colors.reset} Do you want to create the file? (Y/N): ` | ||
); | ||
@@ -256,13 +280,13 @@ | ||
await fs.writeFile(filePath, "[]"); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}File created successfully.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} File created successfully.` | ||
); | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Creating file: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Creating file: ${error}` | ||
); | ||
} | ||
} else { | ||
console.log( | ||
`${colors.fg.blue}[Info]: ${colors.reset}File not created.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} File not created.` | ||
); | ||
@@ -295,3 +319,3 @@ } | ||
async exportData(dataName, format = "json") { | ||
const data = await this.readDataFromFile(dataName); | ||
const data = await this.readData(dataName); | ||
@@ -302,4 +326,4 @@ if (format === "json") { | ||
await fs.writeFile(filePath, JSON.stringify(data, null, 2), "utf8"); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data exported to JSON: ${filePath}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data exported to JSON: ${filePath}` | ||
); | ||
@@ -317,4 +341,4 @@ } else if (format === "csv") { | ||
await fs.writeFile(filePath, csvString, "utf8"); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data exported to CSV: ${filePath}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data exported to CSV: ${filePath}` | ||
); | ||
@@ -331,4 +355,4 @@ } | ||
await this.writeDataByFileName(dataName, newData); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data imported from JSON: ${filePath}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data imported from JSON: ${filePath}` | ||
); | ||
@@ -339,4 +363,4 @@ } else if (format === "csv") { | ||
await this.writeDataByFileName(dataName, csvData); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data imported from CSV: ${filePath}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data imported from CSV: ${filePath}` | ||
); | ||
@@ -348,7 +372,7 @@ } | ||
async transformData(dataName, transformFunction) { | ||
const data = await this.readDataFromFile(dataName); | ||
const data = await this.readData(dataName); | ||
const transformedData = data.map(transformFunction); | ||
await this.writeDataByFileName(dataName, transformedData); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Data transformed and saved.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Data transformed and saved.` | ||
); | ||
@@ -380,3 +404,3 @@ } | ||
async readDataFromFile(dataName) { | ||
async readData(dataName) { | ||
const filePath = await this.getFilePath(dataName); | ||
@@ -392,4 +416,4 @@ try { | ||
await this.initFile(filePath).catch((initError) => { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Initializing file: ${initError}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Initializing file: ${initError}` | ||
); | ||
@@ -405,4 +429,4 @@ }); | ||
} catch (readError) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Reading file ${filePath}: ${readError}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Reading file ${filePath}: ${readError}` | ||
); | ||
@@ -412,4 +436,4 @@ return null; | ||
} else { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Reading file ${filePath}: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Reading file ${filePath}: ${error}` | ||
); | ||
@@ -421,4 +445,4 @@ return null; | ||
async findDataById(id) { | ||
const allData = await this.getAllData(); | ||
async findByID(id) { | ||
const allData = await this.allData(); | ||
return allData.find((dataItem) => dataItem.id === id); | ||
@@ -430,3 +454,3 @@ } | ||
try { | ||
let existingData = await this.readDataFromFile(dataName); | ||
let existingData = await this.readData(dataName); | ||
@@ -438,10 +462,6 @@ if (existingData === null) { | ||
existingData.push(...newData); | ||
await fs.writeFile( | ||
filePath, | ||
JSON.stringify(existingData, null, 2), | ||
"utf8" | ||
); | ||
await fs.writeFile(filePath, JSON.stringify(newData, null, 2), "utf8"); | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Writing to file ${filePath}: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Writing to file ${filePath}: ${error}` | ||
); | ||
@@ -452,8 +472,8 @@ } | ||
async writeDataById(id, newData) { | ||
const filePath = this.findDataById(id); | ||
const filePath = this.findByID(id); | ||
try { | ||
await fs.writeFile(filePath, JSON.stringify(newData, null, 2), "utf8"); | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Writing to item with ID: ${filePath}: ${error}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Writing to item with ID: ${filePath}: ${error}` | ||
); | ||
@@ -463,5 +483,5 @@ } | ||
async editData(id, updatedData) { | ||
async editByID(id, updatedData) { | ||
// Read existing data from the file | ||
const existingData = await this.findDataById(id); | ||
const existingData = await this.findByID(id); | ||
@@ -480,8 +500,8 @@ if (existingData) { | ||
await this.writeDataById(id, existingData); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Item with ID ${id} has been edited.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Item with ID ${id} has been edited.` | ||
); | ||
} else { | ||
console.log( | ||
`${colors.fg.blue}[Info]: ${colors.reset}Item with ID ${id} not found.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Item with ID ${id} not found.` | ||
); | ||
@@ -493,4 +513,4 @@ } | ||
// Add Data | ||
async addData(dataName, newData) { | ||
const existingData = await this.readDataFromFile(dataName); | ||
async saveData(dataName, newData) { | ||
const existingData = await this.readData(dataName); | ||
if (existingData !== null) { | ||
@@ -507,9 +527,9 @@ const newId = await this.randomID(); | ||
await this.writeDataByFileName(dataName, existingData).then(() => { | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}New Data added to DB: ${dataName}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} New Data added to DB: ${dataName}` | ||
); | ||
}); | ||
} else { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Data failed to be added to the DB: ${dataName}` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Data failed to be added to the DB: ${dataName}` | ||
); | ||
@@ -519,5 +539,5 @@ } | ||
async deleteDataById(id) { | ||
async delByID(id) { | ||
try { | ||
const dataItemToDelete = await this.findDataById(id); | ||
const dataItemToDelete = await this.findByID(id); | ||
@@ -527,11 +547,11 @@ if (dataItemToDelete) { | ||
const dataName = `${date.getFullYear()}`; | ||
const data = await this.readDataFromFile(dataName); | ||
const data = await this.readData(dataName); | ||
const newData = data.filter((item) => item.id !== id); | ||
await this.writeDataByFileName(dataName, newData); | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Item has been deleted.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Item has been deleted.` | ||
); | ||
console.log( | ||
`${colors.fg.blue}[Info]: ${colors.reset}Deleted item:`, | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Deleted item:`, | ||
dataItemToDelete | ||
@@ -541,14 +561,14 @@ ); | ||
if (typeof window == "undefined") { | ||
console.log( | ||
`${colors.fg.green}[Successful]: ${colors.reset}Item Deleted successfully!` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.green}[Successful]:${colors.reset} Item Deleted successfully!` | ||
); | ||
} | ||
} else { | ||
console.log( | ||
`${colors.fg.blue}[Info]: ${colors.reset}Item is already deleted.` | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Info]:${colors.reset} Item is already deleted.` | ||
); | ||
} | ||
} catch (error) { | ||
console.log( | ||
`${colors.fg.red}[Error]: ${colors.reset}Deleting data:`, | ||
this.logToConsoleAndFile( | ||
`${colors.bright}${colors.fg.red}[Error]:${colors.reset} Deleting data:`, | ||
error | ||
@@ -559,3 +579,3 @@ ); | ||
async getAllData() { | ||
async allData() { | ||
const files = await fs.readdir(this.dataFolderPath); | ||
@@ -567,3 +587,3 @@ const allData = []; | ||
const dataName = file.slice(0, -5); | ||
const data = await this.readDataFromFile(dataName); | ||
const data = await this.readData(dataName); | ||
@@ -570,0 +590,0 @@ if (Array.isArray(data) && data.length > 0) { |
{ | ||
"name": "jsonverse", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"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.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
101
README.md
@@ -31,3 +31,3 @@ # jsonVerse | ||
// Initialize the JSONDatabase instance | ||
const db = new jsonverse("./path/to/your/data/folder"); | ||
const db = new jsonverse("./path/to/your/data/folder", true); // to activate the logs write after the path true to activate it | ||
``` | ||
@@ -42,3 +42,3 @@ | ||
try { | ||
const allData = await db.getAllData(); | ||
const allData = await db.allData(); | ||
// ... (rendering logic) | ||
@@ -51,2 +51,23 @@ } catch (err) { | ||
### Display Data from a specific data json file | ||
you can display data from a specific data file by file name | ||
```javascript | ||
app.get("/", async (req, res) => { | ||
try { | ||
db | ||
.readData(dataName) // dataName: the name of the json file like test.json the data name will be "test" | ||
.then((result) => { | ||
res.send(result); // result is the content of the json file | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
} catch (err) { | ||
// ... (error handling) | ||
} | ||
}); | ||
``` | ||
### Add Data | ||
@@ -59,3 +80,4 @@ | ||
try { | ||
const { dataName, name, social, rank, competition, date, edu, password } = req.body; | ||
const { dataName, name, social, rank, competition, date, edu, password } = | ||
req.body; | ||
const newData = { | ||
@@ -68,5 +90,5 @@ social, | ||
edu, | ||
password: db.encryptData(password, "Your-Secret-Key"), | ||
password: db.encrypt(password, "Your-Secret-Key"), | ||
}; | ||
await db.addData(dataName, newData); | ||
await db.saveData(dataName, newData); | ||
// ... (redirect or response) | ||
@@ -87,7 +109,7 @@ } catch (err) { | ||
try { | ||
const result = await db.findDataById(id); | ||
const result = await db.findById(id); | ||
if (result) { | ||
// ... (rendering logic) | ||
// remember if the retreved data contained encrypted data to use | ||
// db.decryptData(encryptedData, secretKey) | ||
// remember if the retrieved data contained encrypted data to use | ||
// db.decrypt(encryptedData, secretKey) | ||
} else { | ||
@@ -110,3 +132,3 @@ // ... (not found logic) | ||
try { | ||
await db.deleteDataById(id); | ||
await db.delById(id); | ||
// ... (response or redirect) | ||
@@ -136,3 +158,3 @@ } catch (err) { | ||
}; | ||
await db.editDataById(id, updatedData); | ||
await db.editById(id, updatedData); | ||
// ... (response or redirect) | ||
@@ -145,8 +167,24 @@ } catch (err) { | ||
### Encrypt/Decrypt | ||
you can encrypt/decrypt the data that you are saving by this commend | ||
- Encrypt | ||
```javascript | ||
db.encrypt(data, secretKey); | ||
``` | ||
- Decrypt | ||
```javascript | ||
db.decrypt(encryptedData, secretKey); | ||
``` | ||
### Backup | ||
backup the data json files you want by the dataName in Backup folder in Data Folder (the package create it automatically) | ||
backup the data files you want by the dataName in Backup folder in Data Folder (the package create it automatically) | ||
```js | ||
db.createDataBackup(dataName) // the name of the data file you want to backup | ||
```javascript | ||
db.backupCreate(dataName); // dataName: the name of the data file you want to backup | ||
``` | ||
@@ -156,6 +194,6 @@ | ||
Restore the backedup data json files you want by the dataName in Backup folder in Data Folder (the package create it automatically) | ||
Restore the backup data files you want by the dataName in Backup `./Data/Backup` ./Data is the path you add to the data folder | ||
```js | ||
db.restoreDataFromBackup(dataName, backupFileName) // the name of the data file you want to backup and the name of the backedup file you will get the name after backing up | ||
```javascript | ||
db.backupRestore(dataName, backupFileName); // the dataName is the data you want to restore to it & the backupFileName is the backup file name you got after backing up | ||
``` | ||
@@ -165,11 +203,34 @@ | ||
Delete the backedup data json files you want by the dataName in Backup folder in Data Folder (the package create it automatically) | ||
Delete the backup data json files you want by the dataName in Backup folder in Data Folder (the package create it automatically) | ||
```js | ||
db.cleanupOldBackups(dataName, retentionDays) // the name of the data file you want to backup and the time you want to delete the files since then | ||
// like i have backups from the day before i will but the date with YYYYMMDDHHMMss | ||
```javascript | ||
const retentionDays = 5; // write here with days the time you want to delete the backups since then like i want to delete the backup the had been saved the last 5 days | ||
db.backupDelete(dataName, retentionDays); | ||
``` | ||
### Import/Export Data | ||
- Import | ||
you can import data from json files from outside the project files | ||
```javascript | ||
db.importData(dataName, (format = "json"), filePath); | ||
// dataName: the data file you want to import to if exist if doesn't write the new data name instead | ||
// format: to set the format to json you don't need to write it it's json by default | ||
// filePath: where is the file you want to import | ||
``` | ||
- Export | ||
you can Export data from json files | ||
```javascript | ||
db.exportData(dataName, (format = "json")); | ||
// dataName: the data file you want to Export | ||
// format: to set the format of the new exported file like json and csv | ||
``` | ||
## Conclusion | ||
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. |
@@ -9,6 +9,7 @@ # Security Policy | ||
- 1.1.0 to 1.1.2 | ||
- 1.2.0 to 1.2.3 | ||
## Reporting a Vulnerability | ||
If you discover a security vulnerability within the `jsonverse` package, please send an email to marco5dev@example.com. All security vulnerabilities will be promptly addressed. | ||
If you discover a security vulnerability within the `jsonverse` package, please send an email to marco5dev@outlook.com. All security vulnerabilities will be promptly addressed. | ||
@@ -33,6 +34,2 @@ Please do not open GitHub issues for security vulnerabilities. | ||
## Code of Conduct | ||
This project follows a [Code of Conduct](https://github.com/Marco5dev/jsonverse/blob/main/CODE_OF_CONDUCT.md). We expect all contributors and users to adhere to it to ensure a safe and respectful environment for everyone. | ||
Thank you for using the `jsonverse` package. Your security is our priority. |
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
28466
532
226