🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More
Socket
Sign inDemoInstall
Socket

saksh-crud

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

saksh-crud

A simple Node.js package for generating CRUD operations using Mongoose.

1.0.5
unpublished
latest
npm
Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

saksh-crud

A CRUD generator for Node.js applications.

Features

  • Generates CRUD routes based on a model file.
  • Option to include authentication checks.
  • Customizable templates for route generation.
  • Saves configuration preferences for future use.
  • Emits events during the generation process.

Installation

You can install the package using npm:

npm install saksh-crud

Usage

To use the CRUD generator, run the script and follow the prompts:

node saksh-crud.js

Configuration

You can configure the generator by creating a saksh-crud.config.json file in the root directory. Here is an example configuration:

{
  "defaultOutputDir": "./output",
  "includeAuth": true,
  "customTemplateDir": "./templates"
}

Example

Here’s how to use the generator:

  • Run the script:

    node saksh-crud.js
    
  • Follow the prompts to enter the model file path, choose whether to include authentication, and specify the output directory and custom template directory.

  • The generated routes file will be saved in the specified output directory.

How to Customize

You can customize the CRUD generator to fit your specific needs:

Custom Templates

  • Create your own templates for route generation.
  • Save your custom templates in a directory and specify this directory when prompted or in the saksh-crud.config.json file.
  • The templates should include placeholders like {{modelName}}, {{modelFile}}, and {{modelNameLower}}, which will be replaced with actual values during generation.

Configuration File

  • Modify the saksh-crud.config.json file to set default values for the output directory, authentication inclusion, and custom template directory.

Example configuration:

{
  "defaultOutputDir": "./customOutput",
  "includeAuth": false,
  "customTemplateDir": "./myTemplates"
}

Events

The CRUD generator emits several events during its execution. You can listen to these events to perform custom actions:

beforeGenerate

  • Emitted before the routes file is generated.

    Example:

    generator.on('beforeGenerate', (modelName) => {
      console.log(`Generating routes for model: ${modelName}`);
    });
    

afterGenerate

  • Emitted after the routes file has been generated.

    Example:

    generator.on('afterGenerate', (outputPath) => {
      console.log(`Routes file generated at: ${outputPath}`);
    });
    

error

  • Emitted when an error occurs during the generation process.

    Example:

    generator.on('error', (err) => {
      console.error('An error occurred:', err);
    });
    

Events in Generated Code

The generated CRUD routes also emit events that you can listen to:

beforeCreate

  • Emitted before a new record is created.

    Example:

    router.post('/', async (req, res, next) => {
      try {
        router.emit('beforeCreate', req.body);
        const newRecord = await Model.create(req.body);
        router.emit('afterCreate', newRecord);
        res.status(201).json(newRecord);
      } catch (err) {
        router.emit('error', err);
        next(err);
      }
    });
    

afterCreate

  • Emitted after a new record has been created.

    Example:

    router.post('/', async (req, res, next) => {
      try {
        const newRecord = await Model.create(req.body);
        router.emit('afterCreate', newRecord);
        res.status(201).json(newRecord);
      } catch (err) {
        router.emit('error', err);
        next(err);
      }
    });
    

beforeUpdate

  • Emitted before an existing record is updated.

    Example:

    router.put('/:id', async (req, res, next) => {
      try {
        router.emit('beforeUpdate', req.params.id, req.body);
        const updatedRecord = await Model.findByIdAndUpdate(req.params.id, req.body, { new: true });
        router.emit('afterUpdate', updatedRecord);
        res.status(200).json(updatedRecord);
      } catch (err) {
        router.emit('error', err);
        next(err);
      }
    });
    

afterUpdate

  • Emitted after an existing record has been updated.

    Example:

    router.put('/:id', async (req, res, next) => {
      try {
        const updatedRecord = await Model.findByIdAndUpdate(req.params.id, req.body, { new: true });
        router.emit('afterUpdate', updatedRecord);
        res.status(200).json(updatedRecord);
      } catch (err) {
        router.emit('error', err);
        next(err);
      }
    });
    

beforeDelete

  • Emitted before a record is deleted.

    Example:

    router.delete('/:id', async (req, res, next) => {
      try {
        router.emit('beforeDelete', req.params.id);
        await Model.findByIdAndDelete(req.params.id);
        router.emit('afterDelete', req.params.id);
        res.status(204).end();
      } catch (err) {
        router.emit('error', err);
        next(err);
      }
    });
    

afterDelete

  • Emitted after a record has been deleted.

    Example:

    router.delete('/:id', async (req, res, next) => {
      try {
        await Model.findByIdAndDelete(req.params.id);
        router.emit('afterDelete', req.params.id);
        res.status(204).end();
      } catch (err) {
        router.emit('error', err);
        next(err);
      }
    });
    

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the ISC License.

Keywords

CRUD

FAQs

Package last updated on 19 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts