Comparing version 1.3.0 to 1.3.1
90
index.js
@@ -16,3 +16,2 @@ /*! | ||
const colors = require("./lib/colors"); | ||
const model = require("./lib/Model"); | ||
@@ -52,5 +51,2 @@ function formatDateTime(date) { | ||
// Initialize models object | ||
this.models = {}; | ||
this.init(); | ||
@@ -84,6 +80,6 @@ } | ||
} catch (readError) { | ||
this.handleError(`Failed to create log file: ${readError}`); | ||
this.logError(`Failed to create log file: ${readError}`); | ||
} | ||
} else { | ||
this.handleError(`Failed to save logs: ${error}`); | ||
this.logError(`Failed to save logs: ${error}`); | ||
} | ||
@@ -93,3 +89,3 @@ } | ||
handleError(message) { | ||
logError(message) { | ||
if (this.enableLogToConsoleAndFile) { | ||
@@ -124,44 +120,4 @@ this.logToConsoleAndFile( | ||
// Add a method to create a model using a model | ||
model(modelName, schemaDefinition) { | ||
// Create a model instance and store it in the models object | ||
this.models[modelName] = new Model(modelName, schemaDefinition); | ||
// Return an object with methods like .find(), .save(), .delete(), and .update() | ||
const modelInstance = this.models[modelName]; | ||
const modelAPI = { | ||
find: async (query) => { | ||
const result = await this.find(modelName, query); | ||
return result; | ||
}, | ||
save: async (data) => { | ||
try { | ||
await modelInstance.save(data); | ||
return Promise.resolve(`${modelName} saved successfully`); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}, | ||
delete: async (query) => { | ||
try { | ||
await modelInstance.delete(query); | ||
return Promise.resolve(`${modelName} deleted successfully`); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}, | ||
update: async (query, updates) => { | ||
try { | ||
await modelInstance.update(query, updates); | ||
return Promise.resolve(`${modelName} updated successfully`); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}, | ||
}; | ||
return modelAPI; | ||
} | ||
// Encrypt sensitive data | ||
@@ -208,3 +164,3 @@ encrypt(data, secretKey) { | ||
} catch (readError) { | ||
this.handleError( | ||
this.logError( | ||
`Failed to create data backup for ${dataName}: ${readError}` | ||
@@ -215,3 +171,3 @@ ); | ||
} else { | ||
this.handleError( | ||
this.logError( | ||
`Failed to create data backup for ${dataName}: ${error}` | ||
@@ -223,3 +179,3 @@ ); | ||
} else { | ||
this.handleError(`Failed to create data backup for ${dataName}`); | ||
this.logError(`Failed to create data backup for ${dataName}`); | ||
} | ||
@@ -239,3 +195,3 @@ } | ||
} catch (error) { | ||
this.handleError( | ||
this.logError( | ||
`Failed to restore data from backup: ${backupFileName}: ${error}` | ||
@@ -273,3 +229,3 @@ ); | ||
} catch (deleteError) { | ||
this.handleError( | ||
this.logError( | ||
`Failed to delete backup: ${backupFile}: ${deleteError}` | ||
@@ -312,3 +268,3 @@ ); | ||
} catch (error) { | ||
this.handleError(`The path "${this.dataFolderPath}" doesn't exist.`); | ||
this.logError(`The path "${this.dataFolderPath}" doesn't exist.`); | ||
const answer = await this.askForConfirmation( | ||
@@ -323,6 +279,6 @@ `Do you want to create the path folder? (Y/N): ` | ||
} catch (error) { | ||
this.handleError(`Creating path folder: ${error}`); | ||
this.logError(`Creating path folder: ${error}`); | ||
} | ||
} else { | ||
this.handleError(`Path folder not created.`); | ||
this.logError(`Path folder not created.`); | ||
} | ||
@@ -336,3 +292,3 @@ } | ||
} catch (error) { | ||
this.handleError(`The file "${filePath}" doesn't exist.`); | ||
this.logError(`The file "${filePath}" doesn't exist.`); | ||
const answer = await this.askForConfirmation( | ||
@@ -347,6 +303,6 @@ `${colors.bright}${colors.fg.yellow}[Question]:${colors.reset} Do you want to create the file? (Y/N): ` | ||
} catch (error) { | ||
this.handleError(`Creating file: ${error}`); | ||
this.logError(`Creating file: ${error}`); | ||
} | ||
} else { | ||
this.handleError(`File not created.`); | ||
this.logError(`File not created.`); | ||
} | ||
@@ -486,3 +442,3 @@ } | ||
await this.initFile(filePath).catch((initError) => { | ||
this.handleError(`Initializing file: ${initError}`); | ||
this.logError(`Initializing file: ${initError}`); | ||
}); | ||
@@ -497,7 +453,7 @@ // Retry reading the file | ||
} catch (readError) { | ||
this.handleError(`Reading file ${filePath}: ${readError}`); | ||
this.logError(`Reading file ${filePath}: ${readError}`); | ||
return null; | ||
} | ||
} else { | ||
this.handleError(`Reading file ${filePath}: ${error}`); | ||
this.logError(`Reading file ${filePath}: ${error}`); | ||
return null; | ||
@@ -525,3 +481,3 @@ } | ||
} catch (error) { | ||
this.handleError(`Writing to file ${filePath}: ${error}`); | ||
this.logError(`Writing to file ${filePath}: ${error}`); | ||
} | ||
@@ -537,3 +493,3 @@ // Invalidate the cache for this data name | ||
} catch (error) { | ||
this.handleError(`Writing to item with ID: ${filePath}: ${error}`); | ||
this.logError(`Writing to item with ID: ${filePath}: ${error}`); | ||
} | ||
@@ -560,3 +516,3 @@ } | ||
} else { | ||
this.handleError(`Item with ID ${id} not found.`); | ||
this.logError(`Item with ID ${id} not found.`); | ||
} | ||
@@ -587,3 +543,3 @@ } | ||
} catch (error) { | ||
this.handleError( | ||
this.logError( | ||
`Failed to update data in DB: ${dataName}\nError: ${error}` | ||
@@ -620,3 +576,3 @@ ); | ||
} catch (error) { | ||
this.handleError(`Deleting data:`, error); | ||
this.logError(`Deleting data:`, error); | ||
} | ||
@@ -655,3 +611,3 @@ } | ||
} catch (error) { | ||
this.handleError(`Deleting data:`, error); | ||
this.logError(`Deleting data:`, error); | ||
} | ||
@@ -658,0 +614,0 @@ } |
{ | ||
"name": "jsonverse", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"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", |
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
29017
6
575