-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented CLI functionality for generating and deleting CRUD …
…operations. Updated README with CLI documentation
- Loading branch information
1 parent
bf9b221
commit 95bde0d
Showing
9 changed files
with
217 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
## CLI Commands | ||
|
||
### How to Use CRUD Operations | ||
|
||
To generate or delete CRUD operations, you can use the CLI provided in this project. The CLI allows you to automate the process of creating or removing CRUD (Create, Read, Update, Delete) operations for entities within a MongoDB database. | ||
|
||
### Usage | ||
|
||
#### Running the CLI | ||
|
||
To run the CLI, execute the following command: | ||
|
||
```bash | ||
npm run crud-operation | ||
``` | ||
|
||
Selecting Action | ||
Upon running the CLI, you will be prompted to select an action to perform. You can choose between generating CRUD operations or deleting existing CRUD operations. | ||
|
||
#### Generating CRUD Operations | ||
|
||
If you select the option to generate CRUD operations, the CLI will guide you through the process. You will be prompted to enter the name of the entity for which you want to generate CRUD operations. Additionally, you will need to specify the type of CRUD operations you want to generate, whether empty or minimal. | ||
|
||
#### Empty CRUD Operations | ||
|
||
Empty CRUD operations include the basic structure for Create, Read, Update, and Delete operations. This option provides a starting point for implementing custom logic for each operation. | ||
|
||
#### Minimal CRUD Operations | ||
|
||
Minimal CRUD operations include a simplified version of Create, Read, Update, and Delete operations. This option provides basic functionality with minimal boilerplate code. | ||
|
||
#### Deleting CRUD Operations | ||
|
||
If you select the option to delete CRUD operations, the CLI will prompt you to enter the name of the entity for which you want to delete CRUD operations. Once confirmed, the CLI will remove all associated CRUD files for the specified entity. | ||
|
||
### Example | ||
|
||
Here's an example of how you can use the CLI to generate CRUD operations: | ||
|
||
Run the CLI: | ||
|
||
```bash | ||
npm run crud-operation | ||
``` | ||
|
||
1. Select the action "Generate CRUD." | ||
2. Enter the name of the entity (e.g., "user") for which you want to generate CRUD operations. | ||
3. Choose the type of CRUD operations (empty or minimal). | ||
4. Follow the prompts to complete the generation process. | ||
|
||
### Note | ||
|
||
Ensure that you have the necessary permissions and dependencies installed before running the CLI. Additionally, make sure to review the generated CRUD files and modify them according to your application's requirements. | ||
|
||
This documentation provides a clear overview of how to use the CLI to generate or delete CRUD operations in the project. Adjustments have been made to reflect the changes made in this version, including the implementation of delete CRUD and the change from `npm run generate-crud` to `npm run crud-operation`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
|
||
/** | ||
* Function to delete CRUD. | ||
* @param {string} entity - The entity to delete CRUD for. | ||
* @returns {Promise<void>} - A Promise that resolves when CRUD is deleted successfully | ||
*/ | ||
async function deleteCrud(entity) { | ||
try { | ||
// Define the paths for model, controller, and route files | ||
const modelPath = path.join( | ||
__dirname, | ||
'..', | ||
'src', | ||
'models', | ||
`${entity}.model.js`, | ||
); | ||
const controllerPath = path.join( | ||
__dirname, | ||
'..', | ||
'src', | ||
'controllers', | ||
`${entity}.controller.js`, | ||
); | ||
const routePath = path.join( | ||
__dirname, | ||
'..', | ||
'src', | ||
'routes', | ||
`${entity}.route.js`, | ||
); | ||
|
||
// Remove the model, controller, and route files | ||
await Promise.all([ | ||
fs.unlink(modelPath), | ||
fs.unlink(controllerPath), | ||
fs.unlink(routePath), | ||
]); | ||
|
||
// Remove routes reference from server.js | ||
const serverFilePath = path.join(__dirname, '..', 'server.js'); | ||
let serverFileContent = await fs.readFile(serverFilePath, 'utf-8'); | ||
serverFileContent = serverFileContent.replace( | ||
`const ${entity}Routes = require('./src/routes/${entity}.route');\napp.use('/v1/api', ${entity}Routes);\n`, | ||
'', | ||
); | ||
await fs.writeFile(serverFilePath, serverFileContent); | ||
|
||
console.log('CRUD deleted successfully for entity:', entity); | ||
} catch (error) { | ||
// Handle errors | ||
console.error('An error occurred while deleting CRUD:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
module.exports = { deleteCrud }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters