Comparing version
90
index.js
@@ -16,2 +16,4 @@ /*! | ||
const colors = require("./lib/colors"); | ||
const ExcelJS = require("exceljs"); | ||
const json2xls = require("json2xls"); | ||
@@ -117,4 +119,2 @@ function formatDateTime(date) { | ||
// Encrypt sensitive data | ||
@@ -341,2 +341,6 @@ encrypt(data, secretKey) { | ||
this.logSuccess(`Data exported to CSV: ${filePath}`); | ||
} else if (format === "xlsx") { | ||
// Export as XLSX using exceljs | ||
const filePath = this.getFilePath(dataName + ".xlsx"); | ||
await this.writeDataToXLSX(filePath, data); | ||
} | ||
@@ -358,2 +362,10 @@ } | ||
this.logSuccess(`Data imported from CSV: ${filePath}`); | ||
} else if (format === "xlsx") { | ||
// Import XLSX data using exceljs | ||
const xlsxData = await this.readXLSX(filePath); | ||
await this.writeDataByFileName(dataName, xlsxData); | ||
this.logSuccess(`Data imported from XLSX: ${filePath}`); | ||
} else { | ||
// Handle unsupported file format | ||
this.logError(`Unsupported file format: ${format}`); | ||
} | ||
@@ -389,2 +401,76 @@ } | ||
// Function to read data from an XLSX file using exceljs | ||
async readXLSX(filePath) { | ||
try { | ||
const workbook = new ExcelJS.Workbook(); | ||
await workbook.xlsx.readFile(filePath); | ||
const worksheet = workbook.getWorksheet(1); // Assuming there's only one sheet | ||
const data = []; | ||
worksheet.eachRow((row, rowNumber) => { | ||
if (rowNumber !== 1) { | ||
const rowData = row.values.map((cell) => cell); | ||
data.push(rowData); | ||
} | ||
}); | ||
return data; | ||
} catch (error) { | ||
this.logError(`Reading XLSX file: ${error}`); | ||
return null; | ||
} | ||
} | ||
// Helper method to write data to an XLSX file using exceljs | ||
async writeDataToXLSX(filePath, data) { | ||
const workbook = new ExcelJS.Workbook(); | ||
const worksheet = workbook.addWorksheet("Sheet1"); | ||
// Add headers to the worksheet | ||
if (data.length > 0) { | ||
const headers = Object.keys(data[0]); | ||
worksheet.addRow(headers); | ||
} | ||
// Add data rows to the worksheet | ||
data.forEach((row) => { | ||
worksheet.addRow(Object.values(row)); | ||
}); | ||
await workbook.xlsx.writeFile(filePath); | ||
this.logSuccess(`Data exported to XLSX: ${filePath}`); | ||
} | ||
// Export Data to XLSX | ||
async exportDataToXLSX(dataName) { | ||
const data = await this.readData(dataName); | ||
// Check if the data is empty | ||
if (data.length === 0) { | ||
this.logError(`No data to export in ${dataName}.`); | ||
return; | ||
} | ||
// Create an XLSX object from the JSON data | ||
const xls = json2xls(data); | ||
// Specify the file path where the XLSX file will be saved | ||
const filePath = this.getFilePath(dataName + ".xlsx"); | ||
try { | ||
// Save the XLSX data to the file | ||
fs.writeFileSync(filePath, xls, "binary"); | ||
this.logSuccess(`Data exported to XLSX: ${filePath}`); | ||
} catch (error) { | ||
this.logError(`Failed to export data to XLSX: ${error}`); | ||
} | ||
} | ||
// Function to convert XLS data to JSON | ||
async xlsToJSON(xlsData) { | ||
const wb = XLSX.read(xlsData, { type: "buffer" }); | ||
const ws = wb.Sheets[wb.SheetNames[0]]; // Assuming there's only one sheet | ||
return XLSX.utils.sheet_to_json(ws); | ||
} | ||
isCacheExpired(dataName) { | ||
@@ -391,0 +477,0 @@ const CACHE_EXPIRATION_TIME = 10 * 60 * 1000; // 10 minutes in milliseconds |
{ | ||
"name": "jsonverse", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"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.", | ||
@@ -25,3 +25,3 @@ "main": "index.js", | ||
], | ||
"homepage": "https://github.com/Marco5dev/jsonverse#readme", | ||
"homepage": "https://jsonversedb.web.app", | ||
"keywords": [ | ||
@@ -70,4 +70,6 @@ "JSON", | ||
"dateformat": "^5.0.3", | ||
"exceljs": "^4.3.0", | ||
"fuse.js": "^6.6.2", | ||
"json2csv": "^6.0.0-alpha.2" | ||
"json2csv": "^6.0.0-alpha.2", | ||
"json2xls": "^0.1.2" | ||
}, | ||
@@ -74,0 +76,0 @@ "devDependencies": { |
@@ -5,3 +5,9 @@ # jsonVerse | ||
[](https://github.com/Marco5dev/jsonverse/actions/workflows/npm-publish.yml) | ||
[](https://github.com/Marco5dev/jsonverse/actions/workflows/npm-publish.yml) | ||
[](https://www.npmjs.com/package/jsonverse) | ||
[](https://www.npmjs.com/package/jsonverse) | ||
[](https://github.com/Marco5dev/jsonverse) | ||
[](https://github.com/Marco5dev/jsonverse/blob/master/LICENSE) | ||
[](https://github.com/Marco5dev/jsonverse/graphs/contributors) | ||
[](https://github.com/Marco5dev/jsonverse/commits/master) | ||
@@ -8,0 +14,0 @@ ## Introduction |
Sorry, the diff of this file is not supported yet
32391
11.63%650
13.04%235
2.62%7
40%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added