This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
732 lines (640 loc) · 21.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*!
*
* Copyright(c) 2023 Mark Maher Ewida(Marco5dev)
* MIT Licensed
*/
"use strict";
// ! NPM Packages
const fs = require("fs").promises;
const path = require("path");
const { randomUUID } = require("crypto");
const csv = require("csv-parser");
const crypto = require("crypto-js");
const Fuse = require("fuse.js");
const ExcelJS = require("exceljs");
const json2xls = require("json2xls");
// ! Lib files
const colors = require("./lib/colors");
const Schema = require("./lib/schema");
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 currentDate = formatDateTime(new Date());
const currentTime = currentDate.replace(/\D/g, ""); // *Remove non-numeric characters
class jsonverse {
constructor(options) {
// * Default configuration options
const defaultOptions = {
dataFolderPath: "./Data",
logFolderPath: "./Logs",
activateLogs: true,
};
throw new Error('Package is deprecated please install the latest version of the new database called "verse.db"')
// * Merge the default options with the provided options
this.options = { ...defaultOptions, ...options };
this.dataFolderPath = this.options.dataFolderPath;
this.logFolderPath = path.join(this.options.logFolderPath);
this.backupFolderPath = path.join(this.dataFolderPath, "Backup");
this.logFilePath = path.join(this.logFolderPath, "data.log");
this.enableLogToConsoleAndFile = this.options.activateLogs;
this.searchIndex = {};
this.cache = {};
this.data = {};
this.init();
}
async logToConsoleAndFile(message) {
// *Function to remove ANSI escape codes from a string
function removeAnsiEscapeCodes(input) {
return input.replace(/\x1b\[\d+m/g, "");
}
// *TODO: Log to the file
const logFilePath = path.join(this.logFolderPath, "app.log");
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 });
try {
await fs.writeFile(
logFilePath,
`${removeAnsiEscapeCodes(message)}\n`,
"utf8"
);
} catch (readError) {
this.logError(`Failed to create log file: ${readError}`);
}
} else {
this.logError(`Failed to save logs: ${error}`);
}
}
}
logError(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(
`${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}`
);
}
}
logWarning(message) {
if (this.enableLogToConsoleAndFile) {
this.logToConsoleAndFile(
`${colors.bright}${colors.fg.yellow}[Warning]:${colors.reset} ${message}`
);
console.log(
`${colors.bright}${colors.fg.yellow}[Warning]:${colors.reset} ${message}`
);
} else {
console.log(
`${colors.bright}${colors.fg.yellow}[Warning]:${colors.reset} ${message}`
);
}
}
logInfo(message) {
if (this.enableLogToConsoleAndFile) {
this.logToConsoleAndFile(
`${colors.bright}${colors.fg.blue}[Info]:${colors.reset} ${message}`
);
console.log(
`${colors.bright}${colors.fg.blue}[Info]:${colors.reset} ${message}`
);
} else {
console.log(
`${colors.bright}${colors.fg.blue}[Info]:${colors.reset} ${message}`
);
}
}
// TODO: Encrypt sensitive data
encrypt(data, secretKey) {
const encryptedData = crypto.AES.encrypt(
JSON.stringify(data),
secretKey
).toString();
return encryptedData;
}
// TODO: Decrypt sensitive data
decrypt(encryptedData, secretKey) {
const decryptedData = crypto.AES.decrypt(encryptedData, secretKey).toString(
crypto.enc.Utf8
);
return JSON.parse(decryptedData);
}
// TODO: Create Data Backup
async backupCreate(dataName) {
const currentData = await this.readData(dataName);
if (currentData !== null) {
const backupFileName = `./Backup/${dataName}_${currentTime}.json`;
const backupFilePath = path.join(this.dataFolderPath, backupFileName);
try {
await fs.writeFile(
backupFilePath,
JSON.stringify(currentData, null, 2),
"utf8"
);
this.logSuccess(`Data backup created: ${backupFileName}`);
} catch (error) {
if (error.code === "ENOENT") {
await fs.mkdir("./Data/Backup", { recursive: true });
try {
await fs.writeFile(
backupFilePath,
JSON.stringify(currentData, null, 2),
"utf8"
);
this.logSuccess(`Data backup created: ${backupFileName}`);
} catch (readError) {
this.logError(
`Failed to create data backup for ${dataName}: ${readError}`
);
return null;
}
} else {
this.logError(
`Failed to create data backup for ${dataName}: ${error}`
);
return null;
}
}
} else {
this.logError(`Failed to create data backup for ${dataName}`);
}
}
// TODO: Restore Data from Backup
async backupRestore(dataName, backupFileName) {
const backupFilePath = path.join(
this.dataFolderPath + "/Backup",
backupFileName + ".json"
);
try {
const backupData = await fs.readFile(backupFilePath, "utf8");
await this.writeDataByFileName(dataName, JSON.parse(backupData)); // * Replace data in the file
this.logSuccess(`Data restored from backup: ${backupFileName}`);
} catch (error) {
this.logError(
`Failed to restore data from backup: ${backupFileName}: ${error}`
);
}
}
// TODO: Cleanup Old Backups
async backupDelete(dataName, retentionDays) {
const backupFiles = await this.getBackupFiles(dataName);
const currentDate = new Date(currentTime);
for (const backupFile of backupFiles) {
const backupDateStr = backupFile
.split(dataName + "_")[1]
.split(".json")[0];
const year = parseInt(backupDateStr.slice(0, 4));
const month = parseInt(backupDateStr.slice(4, 6)) - 1; // * Months are 0-indexed
const day = parseInt(backupDateStr.slice(6, 8));
const hour = parseInt(backupDateStr.slice(8, 10));
const minute = parseInt(backupDateStr.slice(10, 12));
const second = parseInt(backupDateStr.slice(12, 14));
const backupDate = new Date(year, month, day, hour, minute, second);
if (backupDate > retentionDays) {
const backupFilePath = path.join(
this.dataFolderPath + "/Backup",
backupFile
);
try {
await fs.unlink(backupFilePath);
this.logSuccess(`Backup deleted: ${backupFile}`);
} catch (deleteError) {
this.logError(
`Failed to delete backup: ${backupFile}: ${deleteError}`
);
}
}
}
}
// TODO: Get Backup Files
async getBackupFiles(dataName) {
const files = await fs.readdir(this.dataFolderPath + "/Backup");
const backupFiles = files.filter((file) => file.startsWith(`${dataName}_`));
return backupFiles;
}
// TODO: Initialize search index for a dataName
initSearchIndex(dataName, options) {
const data = this.readData(dataName);
if (data !== null) {
const fuse = new Fuse(data, options);
this.searchIndex[dataName] = fuse;
}
}
// TODO: Search data by query
searchData(dataName, query) {
if (this.searchIndex[dataName]) {
const fuse = this.searchIndex[dataName];
return fuse.search(query);
} else {
return [];
}
}
async init() {
try {
await fs.access(this.dataFolderPath);
} catch (error) {
this.logInfo(`The path "${this.dataFolderPath}" doesn't exist.`);
try {
await fs.mkdir(this.dataFolderPath, { recursive: true });
this.logSuccess(`Path folder created successfully.`);
} catch (error) {
this.logError(`Creating path folder: ${error}`);
}
}
}
async initFile(filePath) {
try {
await fs.access(filePath);
} catch (error) {
this.logInfo(`The file "${filePath}" doesn't exist.`);
try {
await fs.writeFile(filePath, "[]");
this.logSuccess(`File created successfully.`);
} catch (error) {
this.logError(`Creating file: ${error}`);
}
}
}
async randomID() {
const uuid = randomUUID();
const idWithoutHyphens = uuid.replace(/-/g, "");
return idWithoutHyphens;
}
// TODO: Export Data
async exportData(dataName, format = "json") {
const data = await this.readData(dataName);
if (format === "json") {
// *Export as JSON
const filePath = this.getFilePath(dataName + ".json");
await fs.writeFile(filePath, JSON.stringify(data, null, 2), "utf8");
this.logSuccess(`Data exported to JSON: ${filePath}`);
} else if (format === "csv") {
// *Export as CSV
const filePath = this.getFilePath(dataName + ".csv");
const csvData = data.map((item) => {
return {
id: item.id,
...item, // *Include other properties in CSV
};
});
const csvString = await this.convertToCSV(csvData);
await fs.writeFile(filePath, csvString, "utf8");
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);
}
}
// TODO: Import Data
async importData(dataName, format = "json", filePath) {
if (format === "json") {
// * Import JSON
const rawData = await fs.readFile(filePath, "utf8");
const newData = JSON.parse(rawData);
await this.writeDataByFileName(dataName, newData);
this.logSuccess(`Data imported from JSON: ${filePath}`);
} else if (format === "csv") {
// * Import CSV
const csvData = await this.readCSV(filePath);
await this.writeDataByFileName(dataName, csvData);
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}`);
}
}
// TODO: Data Transformation (if needed)
async transformData(dataName, transformFunction) {
const data = await this.readData(dataName);
const transformedData = data.map(transformFunction);
await this.writeDataByFileName(dataName, transformedData);
this.logSuccess(`Data transformed and saved.`);
}
// TODO: Helper method to convert data to CSV
async convertToCSV(data) {
const { Parser } = require("json2csv");
const parser = new Parser();
return parser.parse(data);
}
// TODO: Helper method to read CSV
async readCSV(filePath) {
const results = [];
const stream = fs.createReadStream(filePath).pipe(csv());
return new Promise((resolve, reject) => {
stream
.on("data", (data) => results.push(data))
.on("end", () => resolve(results))
.on("error", (error) => reject(error));
});
}
// TODO: 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;
}
}
// TODO: 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}`);
}
// TODO: 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}`);
}
}
// TODO: 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) {
const CACHE_EXPIRATION_TIME = 10 * 60 * 1000; // * 10 minutes in milliseconds
if (
this.cache[dataName] &&
Date.now() - this.cache[dataName].timestamp > CACHE_EXPIRATION_TIME
) {
// * Cache has expired
return true;
}
return false;
}
getFilePath(dataName) {
return path.join(this.dataFolderPath, `${dataName}.json`);
}
async readData(dataName) {
const filePath = await this.getFilePath(dataName);
// * Check if data is already in the cache and not expired
if (this.cache[dataName] && !this.isCacheExpired(dataName)) {
return this.cache[dataName].data;
}
try {
const data = await fs.readFile(filePath, "utf8");
if (data.trim() === "") {
return [];
}
// * Parse and cache the data with a timestamp
const parsedData = JSON.parse(data);
this.cache[dataName] = {
data: parsedData,
timestamp: Date.now(),
};
return parsedData;
} catch (error) {
if (error.code === "ENOENT") {
await this.initFile(filePath).catch((initError) => {
this.logError(`Initializing file: ${initError}`);
});
// * Retry reading the file
try {
const newData = await fs.readFile(filePath, "utf8");
if (newData.trim() === "") {
return [];
}
return JSON.parse(newData);
} catch (readError) {
this.logError(`Reading file ${filePath}: ${readError}`);
return null;
}
} else {
this.logError(`Reading file ${filePath}: ${error}`);
return null;
}
}
}
async findByID(id) {
const allData = await this.allData();
return allData.find((dataItem) => dataItem.id === id);
}
async writeDataByFileName(dataName, newData) {
const filePath = this.getFilePath(dataName);
try {
let existingData = await this.readData(dataName);
if (existingData === null) {
existingData = [];
}
existingData.push(...newData);
await fs.writeFile(filePath, JSON.stringify(newData, null, 2), "utf8");
} catch (error) {
this.logError(`Writing to file ${filePath}: ${error}`);
}
// * Invalidate the cache for this data name
delete this.cache[dataName];
}
async writeDataById(id, newData) {
const filePath = this.findByID(id);
try {
await fs.writeFile(filePath, JSON.stringify(newData, null, 2), "utf8");
} catch (error) {
this.logError(`Writing to item with ID: ${filePath}: ${error}`);
}
}
async editByID(id, updatedData) {
// * Read existing data from the file
const existingData = await this.findByID(id);
if (existingData) {
// * Find the index of the item with the specified ID
const itemIndex = existingData.findIndex((item) => item.id === id);
if (itemIndex !== -1) {
// * Update the existing data with the new data
existingData[itemIndex] = {
...existingData[itemIndex],
...updatedData,
};
// * Write the updated data back to the file
await this.writeDataById(id, existingData);
this.logSuccess(`Item with ID ${id} has been edited.`);
} else {
this.logError(`Item with ID ${id} not found.`);
}
}
}
// TODO: Add Data
async saveData(dataName, newData) {
try {
const existingData = (await this.readData(dataName)) || [];
const newId = await this.randomID();
const newDataWithId = {
id: newId,
...newData,
};
// * Remove any existing data with the same ID
const updatedData = existingData.filter((item) => item.id !== newId);
// * Add the new data with the same ID
updatedData.push(newDataWithId);
// * Write the updated data to the file
await this.writeDataByFileName(dataName, updatedData);
this.logSuccess(`Data updated in DB: ${dataName}`);
} catch (error) {
this.logError(
`Failed to update data in DB: ${dataName}\nError: ${error}`
);
}
}
async delByID(id) {
try {
const dataItemToDelete = await this.findByID(id);
if (dataItemToDelete) {
const date = new Date(dataItemToDelete.date);
const dataName = `${date.getFullYear()}`;
const data = await this.readData(dataName);
const newData = data.filter((item) => item.id !== id);
await this.writeDataByFileName(dataName, newData);
this.logSuccess(`Item has been deleted.`);
this.logInfo(`Deleted item:`, dataItemToDelete);
if (typeof window == "undefined") {
this.logSuccess(`Item Deleted successfully!`);
}
} else {
this.logInfo(`Item is already deleted.`);
}
} catch (error) {
this.logError(`Deleting data:`, error);
}
}
async delByDataName(dataName, id) {
try {
// * Read the data from the specified JSON file
const data = await this.readData(dataName);
if (data) {
// * Find the item with the specified ID
const itemToDelete = data.find((item) => item.id === id);
if (itemToDelete) {
// * Filter out the item with the matching ID
const newData = data.filter((item) => item.id !== id);
// * Write the updated data back to the JSON file
await this.writeDataByFileName(dataName, newData);
this.logSuccess(
`Item with ID ${id} has been deleted from ${dataName}.json`
);
} else {
this.logInfo(`Item with ID ${id} not found in ${dataName}.json.`);
}
} else {
this.logInfo(`No data found in ${dataName}.json.`);
}
} catch (error) {
this.logError(`Deleting data:`, error);
}
}
async allData() {
const files = await fs.readdir(this.dataFolderPath);
const allData = [];
for (const file of files) {
if (file.endsWith(".json")) {
const dataName = file.slice(0, -5);
const data = await this.readData(dataName);
if (Array.isArray(data) && data.length > 0) {
allData.push(...data);
}
}
}
return allData;
}
// TODO: model
model(dataName, schema) {
const filePath = this.getFilePath(dataName);
return {
save: async function (newData) {
const validationErrors = schema.validate(newData);
if (validationErrors) {
return Promise.reject(validationErrors);
}
return this.saveData(dataName, newData);
}.bind(this), // Bind the function to the current context
delete: async function (id) {
return this.delByDataName(dataName, id);
}.bind(this),
update: async function (id, updatedData) {
return this.editByID(id, updatedData);
}.bind(this),
find: async function (id) {
return this.findByID(id);
}.bind(this),
findAll: async function () {
return this.readData(dataName);
}.bind(this),
};
}
}
module.exports = {
Schema,
jsonverse,
};