Comparing version 1.0.3 to 1.0.12
35
index.js
@@ -11,4 +11,6 @@ const fs = require("fs").promises; | ||
async randomID() { | ||
return randomUUID(); | ||
} | ||
const uuid = randomUUID(); | ||
const idWithoutHyphens = uuid.replace(/-/g, ""); | ||
return idWithoutHyphens; | ||
} | ||
@@ -23,2 +25,5 @@ getFilePath(dataName) { | ||
const data = await fs.readFile(filePath, "utf8"); | ||
if (data.trim() === "") { | ||
return []; | ||
} | ||
return JSON.parse(data); | ||
@@ -39,3 +44,14 @@ } catch (error) { | ||
try { | ||
await fs.writeFile(filePath, JSON.stringify(newData, null, 2), "utf8"); | ||
let existingData = await this.readDataFromFile(dataName); | ||
if (existingData === null) { | ||
existingData = []; | ||
} | ||
existingData.push(...newData); | ||
await fs.writeFile( | ||
filePath, | ||
JSON.stringify(existingData, null, 2), | ||
"utf8" | ||
); | ||
} catch (error) { | ||
@@ -81,10 +97,15 @@ console.error(`Error writing to file ${filePath}: ${error}`); | ||
const existingData = await this.readDataFromFile(dataName); | ||
if (existingData) { | ||
if (existingData || existingData !== null) { | ||
// Generate a random and unique ID | ||
const newId = await this.randomID(); | ||
// Add the ID to the newData object | ||
newData.id = newId; | ||
// Create a new object with the ID and newData | ||
const newDataWithId = { | ||
id: newId, | ||
...newData, | ||
}; | ||
existingData.push(newData); | ||
// Insert the newData object with ID at the beginning of the existing data array | ||
existingData.unshift(newDataWithId); | ||
await this.writeDataByFileName(dataName, existingData); | ||
@@ -91,0 +112,0 @@ } |
{ | ||
"name": "jsonverse", | ||
"version": "1.0.3", | ||
"version": "1.0.12", | ||
"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", |
@@ -19,8 +19,6 @@ # jsonVerse | ||
const router = express.Router(); | ||
const jsonVerse = require("jsonVerse"); | ||
// Specify the data folder path | ||
const dataFolderPath = path.join(__dirname, "../Data"); | ||
const jsonverse = require("jsonverse"); | ||
// Initialize the JSONDatabase instance | ||
const db = new jsonVerse(dataFolderPath); | ||
const db = new jsonverse("./path/to/your/data/folder"); // add the path to the data folder for example "./Data" | ||
``` | ||
@@ -43,8 +41,6 @@ ```javascript | ||
const { dataName, name, social, rank, competition, date, edu } = req.body; // here you have to add the dataName which is where you want to save the data in the json files you have, here i store the name of the dataName from the UI (User interface - Front end) | ||
// dataName is where you want to add the data (the json file) | ||
// Generate a random unique ID | ||
// this is a package you have to install | ||
const uniqueID = uuid.v4(); | ||
// the data that you will store from the front end and save in the database | ||
const newData = { | ||
id: uniqueID, | ||
social, | ||
@@ -51,0 +47,0 @@ name, |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
130
0
0
19692
110
1