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

express-mongoose-gen

v2.0.3

Published

It’s a mongoose model, REST controller and Express router code generator for Express.js 4.0.17 application

Downloads

42

Readme

express-mongoose-gen

This package of models receives your database and structures your project with architecture MVC as REST controller and Express router code generator for Express.js 4.0.17 application. Full support for type script and defining files as base class

Note that the package must be installed globally on your system

Installation

$ npm install -g express-mongoose-gen

Usage

$ mongoose-gen -m user -f firstName:number,lastName:string -r
        create: ./models/userdModel.js
        create: ./routes/userdRoutes.js
        create: ./controllers/userdController.js
Options
  • -m, --model <modelName> - the model name.
  • -f, --fields <fields> - the fields (name1:type,name2:type).
  • -r, --rest - enable generation REST.
  • -t, --tree <tree> files tree generation grouped by (t)ype or by (m)odule
Available types
  • string
  • number
  • date
  • boolean
  • array
  • objectId

Interactive mode

$ mongoose-gen
Model Name : user
Available types : string, number, date, boolean, array
Field Name (press <return> to stop adding fields) : door
Field Type [string] : number
Field Name (press <return> to stop adding fields) : color
Field Type [string] : 
Field Name (press <return> to stop adding fields) : owner
Field Type [string] : objectId
Reference (model name referred by the objectId field) : User
Field Name (press <return> to stop adding fields) : 
Generate Rest (yes/no) ? [yes] : 
Files tree generation grouped by Type or by Module (t/m) ? [t] : 
        create: ./models/userModel.js
        create: ./routes/usersRoutes.js
        create: ./controllers/userController.js

Package output in your project

Model

models/userModel.js :

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

const userSchema = new Schema({
	firstName : { type : String },
	lastName : { type : Number },
    owner : {
        type: Schema.Types.ObjectId,
        ref: 'User'
    }
}, {timestamps : true});

module.exports = mongoose.model('user', userSchema);

Router

routes/userRoutes.js :

const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController.js');


router.get('/', userController.list.bind(userController));

router.get('/:id', userController.show.bind(userController));

router.post('/', userController.create.bind(userController));

router.put('/:id', userController.update.bind(userController));

router.delete('/:id', userController.remove.bind(userController));

module.exports = router;

Controller

controllers/userController.js :

const userModel = require({modelPath});
 
 
module.exports = new class user {
   list(req , res) {
       userModel.find({} , (err, user) => {
           if (err) {
               return res.status(500).json({
                   message: 'Error when getting user.',
                   error: err
               });
           }
           return res.status(200).json(user);
       });
   };
   show(req , res) {
       userModel.findById(req.params.id,  (err, user) => {
           if (err) {
               return res.status(500).json({
                   message: 'Error when getting user.',
                   error: err
               });
           }
           if (!user) {
               return res.status(404).json({
                   message: 'No such user'
               });
           }
           return res.status(200).json(user);
       });
   };
   create(req , res) {
       let user = new userModel({{createFields}
       });
 
       user.save((err, user) =>  {
           if (err) {
               return res.status(500).json({
                   message: 'Error when creating user',
                   error: err
               });
           }
           return res.status(201).json(user);
       });
   };
   update(req , res) {
       userModel.findById(req.params.id ,  (err, user) =>  {
           if (err) {
               return res.status(500).json({
                   message: 'Error when getting user',
                   error: err
               });
           }
           if (!user) {
               return res.status(404).json({
                   message: 'No such user'
               });
           }
           {updateFields}
           user.save((err, user) => {
               if (err) {
                   return res.status(500).json({
                       message: 'Error when updating user.',
                       error: err
                   });
               }
               return res.json(user);
           });
       });
   };
   remove(req , res) {
       userModel.findByIdAndRemove(req.params.id,  (err, user) =>  {
           if (err) {
               return res.status(500).json({
                   message: 'Error when deleting the user.',
                   error: err
               });
           }
           return res.status(204).json();
       });
   };
};

With files tree generation by module

Files tree generation grouped by Type or by Module (t/m) ? [t] : m
        create: ./user
        create: ./user/userModel.js
        create: ./user/userController.js
        create: ./user/userRoutes.js

You then only have to add router in app.js file and MongoDB connection whit Mongoose. app.js :

const routes = require('./routes/index');
const users = require('./routes/userRoutes');
 ...

app.use('/', routes);
app.use('/users', users);
 ...
 

Licence

Copyright (c) 2020 Navidrezadoost Licensed under the MIT license.