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

mongorestgen

v1.0.2

Published

Create controllers, mongoose models and routes using command line

Downloads

23

Readme

mongorestgen

This is created for making devlopment process faster. As of now, it is only created according what we follow here at Chapter247 Generate controllers, models and routes using the command line

Installation

npm install -g mongorestgen

Validate the installation by running mongorestgen --help

Available scripts

Create a controller

mongorestgen make:controller NewTest

alias: make:c, make:ctrl, gen:controller, gen:c, gen:ctrl

This will create a file inside controllers directory named NewTestController.js with the following content.

"use strict";

/**
 * Get all record
 * @param { req, res }
 * @returns JsonResponse
 */
const index = async (req, res, next) => {
  try {
    // next() or
    return res.status(200).json({
      success: true,
      message: "Data fetched successfully.",
      data: []
    });
  } catch (error) {
    return res.status(500).json({
      success: false,
      message:
        "We are having some error while completing your request. Please try again after some time.",
      error: error
    });
  }
};
/**
 * Create a record
 * @param { req, res }
 * @returns JsonResponse
 */
const store = async (req, res, next) => {
  try {
    // next() or
    return res.status(200).json({
      success: true,
      message: "Data saved successfully.",
      data: []
    });
  } catch (error) {
    return res.status(500).json({
      success: false,
      message:
        "We are having some error while completing your request. Please try again after some time.",
      error: error
    });
  }
};

/**
 * Get only single record
 * @param { req, res }
 * @returns JsonResponse
 */
 const details = async(req, res, next) => {
   try{
    // next() or
    return res.status(200).json({
      success: true,
      message: "Details fatched successfully.",
      data: {}
    });
   }
   catch (error) {
    return res.status(500).json({
      success: false,
      message:
        "We are having some error while completing your request. Please try again after some time.",
      error: error
    });
  }
 }

/**
 * update a record
 * @param { req, res }
 * @returns JsonResponse
 */
const update = async (req, res, next) => {
  try {
    // next() or
    return res.status(200).json({
      success: true,
      message: "Data updated successfully.",
      data: []
    });
  } catch (error) {
    return res.status(500).json({
      success: false,
      message:
        "We are having some error while completing your request. Please try again after some time.",
      error: error
    });
  }
};
/**
 * Destroy a record
 * @param { req, res }
 * @returns JsonResponse
 */
const destroy = async (req, res, next) => {
  try {
    // next() or
    return res.status(200).json({
      success: true,
      message: "Data deleted successfully.",
      data: []
    });
  } catch (error) {
    return res.status(500).json({
      success: false,
      message:
        "We are having some error while completing your request. Please try again after some time.",
      error: error
    });
  }
};



/**
 * Export as a single common js module
 */
module.exports = {
  index,
  store,
  details,
  update,
  destroy
};

Create a model

mongorestgen make:model Test --attr name:type=String:require=true

alias: make:m, make:md, gen:model, gen:m, gen:md

This will create a file inside models directory named TestModel.js with the following content.

"use strict";

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const TestModelSchema = new Schema({

  name: {type: String,require: true,},
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now }
});

TestModelSchema.pre('save', function(next){
  this.updatedAt = Date.now();
  next();
});

TestModelSchema.pre('update', function() {
  this.update({}, { $set: { updatedAt: Date.now() } });
});

TestModelSchema.pre('findOneAndUpdate', function() {
  this.update({}, { $set: { updatedAt: Date.now() } });
});


module.exports = mongoose.model('test', TestModelSchema);

Create a route

mongorestgen make:route Test

alias: make:r, make:rt, gen:route, gen:r, gen:rt

This will create a file inside routes directory named TestRoute.js with the following content.

const express = require('express')
const router = express.Router()
const { TestController } = require('../controllers')

router.get('/' , TestController.index)
router.post('/', TestController.store)
router.put('/:id', TestController.update)
router.delete('/', TestController.destroy)
router.get('/:id', TestController.details)

module.exports = router

Create route and controller in single command

mongorestgen make:route Test --controller

alias: -c, --ctrl This will create a file inside routes directory named TestRoute.js with the following content and controller file inside controllers directory as name TestController with default controller code.