npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saksh-crud

v1.0.5

Published

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

Readme

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:

  1. Run the script:

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

  3. 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.