express-mongoose-generator
It’s a mongoose model, REST controller and Express router code generator for Express.js 4 application.
Installation
$ npm install -g express-mongoose-generator
Usage
Non-Interactive mode
Generates a Mongoose model, a REST controller and Express router :
$ mongoose-gen -m car -f carDoor:number,color -r create: ./models/cardModel.js create: ./routes/cards.js create: ./controllers/cardController.js
Options
-m, --model <modelName>
- the model name.-f, --fields <fields>
- the fields (name1:type,name2:type).-r, --rest
- enable generation REST.
Available types
- string
- number
- date
- boolean
- array
- objectId
Interactive mode
Generates a Mongoose model, a REST controller and Express router :
$ mongoose-genModel Name : carAvailable types : string, number, date, boolean, arrayField Name : doorField Type [string] : numberField Name : colorField Type [string] : Field Name : ownerField Type [string] : objectIdReference : UserField Name : Generate Rest ? [yes] : create: ./models/carModel.js create: ./routes/cars.js create: ./controllers/carController.js
Rendering
Model
models/carModel.js :
var mongoose = ;var Schema = mongooseSchema; var carSchema = "color" : String "door" : Number "owner" : type: SchemaTypesObjectId ref: 'User' ; moduleexports = mongoose;
Router
routes/cars.js :
var express = ;var router = express;var carController = ; /* * GET */router; /* * GET */router; /* * POST */router; /* * PUT */router; /* * DELETE */router; moduleexports = router;
Controller
controllers/carController.js :
var carModel = ; /** * carController.js * * @description :: Server-side logic for managing cars. */moduleexports = /** * carController.list() */ { carModel; } /** * carController.show() */ { var id = reqparamsid; carModel; } /** * carController.create() */ { var car = color : reqbodycolor door : reqbodydoor ; car; } /** * carController.update() */ { var id = reqparamsid; carModel; } /** * carController.remove() */ { var id = reqparamsid; carModel; };
You then only have to add router in app.js file and MongoDB connection whit Mongoose. app.js :
var routes = ;var cars = ; ... app;app; ...
Licence
Copyright (c) 2014 Damien Perrier Licensed under the MIT license.