1- 'use strict' ; < % if ( filters . mongoose ) { % >
1+ /**
2+ * Using Rails-like standard naming convention for endpoints.
3+ * GET <%= route %> -> index<% if (filters.models) { %>
4+ * POST <%= route %> -> create
5+ * GET <%= route %>/:id -> show
6+ * PUT <%= route %>/:id -> update
7+ * DELETE <%= route %>/:id -> destroy<% } %>
8+ */
29
3- var _ = require ( 'lodash' ) ;
4- var < %= classedName % > = require ( './<%= name %>.model' ) ;
10+ 'use strict' ; < % if ( filters . models ) { % >
11+
12+ var _ = require ( 'lodash' ) ; < % if ( filters . mongooseModels ) { % >
13+ var < %= classedName % > = require ( './<%= name %>.model' ) ; < % } if ( filters . sequelizeModels ) { % >
14+ var sqldb = require ( '../../sqldb' ) ;
15+ var < %= classedName % > = sqldb . < %= classedName % > ; < % } % >
516
617function handleError ( res , statusCode ) {
718 statusCode = statusCode || 500 ;
@@ -14,7 +25,7 @@ function responseWithResult(res, statusCode) {
1425 statusCode = statusCode || 200 ;
1526 return function ( entity ) {
1627 if ( entity ) {
17- return res . status ( statusCode ) . json ( entity ) ;
28+ res . status ( statusCode ) . json ( entity ) ;
1829 }
1930 } ;
2031}
@@ -31,9 +42,11 @@ function handleEntityNotFound(res) {
3142
3243function saveUpdates ( updates ) {
3344 return function ( entity ) {
34- var updated = _ . merge ( entity , updates ) ;
45+ < % if ( filters . mongooseModels ) { % > var updated = _ . merge ( entity , updates ) ;
3546 return updated . saveAsync ( )
36- . spread ( function ( updated ) {
47+ . spread ( function ( updated ) { < % }
48+ if ( filters . sequelizeModels ) { % > return entity . updateAttributes ( updates )
49+ . then ( function ( updated ) { < % } % >
3750 return updated ;
3851 } ) ;
3952 } ;
@@ -42,52 +55,70 @@ function saveUpdates(updates) {
4255function removeEntity ( res ) {
4356 return function ( entity ) {
4457 if ( entity ) {
45- return entity . removeAsync ( )
58+ < % if ( filters . mongooseModels ) { % > return entity . removeAsync ( ) < % }
59+ if ( filters . sequelizeModels ) { % > return entity . destroy ( ) < % } % >
4660 . then ( function ( ) {
4761 res . status ( 204 ) . end ( ) ;
4862 } ) ;
4963 }
5064 } ;
5165} < % } % >
5266
53- // Get list of <%= name %>s
54- exports . index = function ( req , res ) { < % if ( ! filters . mongoose ) { % >
55- res . json ( [ ] ) ; < % } % > < % if ( filters . mongoose ) { % >
56- < %= classedName % > .findAsync()
67+ // Gets a list of <%= name %>s
68+ exports . index = function ( req , res ) { < % if ( ! filters . models ) { % >
69+ res . json ( [ ] ) ; < % } else { % >
70+ < % if ( filters . mongooseModels ) { % > < %= classedName % > .findAsync()< % }
71+ if ( filters . sequelizeModels ) { % > < %= classedName % > .findAll()< % } % >
5772 . then ( responseWithResult ( res ) )
5873 . catch ( handleError ( res ) ) ; < % } % >
59- } ; < % if ( filters . mongoose ) { % >
74+ } ; < % if ( filters . models ) { % >
6075
61- // Gets a single <%= name %> from the DB.
76+ // Gets a single <%= name %> from the DB
6277exports . show = function ( req , res ) {
63- < %= classedName % > .findByIdAsync(req.params.id)
78+ < % if ( filters . mongooseModels ) { % > < %= classedName % > .findByIdAsync(req.params.id)< % }
79+ if ( filters . sequelizeModels ) { % > < %= classedName % > .find({
80+ where : {
81+ _id : req . params . id
82+ }
83+ } )< % } % >
6484 . then ( handleEntityNotFound ( res ) )
6585 . then ( responseWithResult ( res ) )
6686 . catch ( handleError ( res ) ) ;
6787} ;
6888
69- // Creates a new <%= name %> in the DB.
89+ // Creates a new <%= name %> in the DB
7090exports . create = function ( req , res ) {
71- < %= classedName % > .createAsync(req.body)
91+ < % if ( filters . mongooseModels ) { % > < %= classedName % > .createAsync(req.body)< % }
92+ if ( filters . sequelizeModels ) { % > < %= classedName % > .create(req.body)< % } % >
7293 . then ( responseWithResult ( res , 201 ) )
7394 . catch ( handleError ( res ) ) ;
7495} ;
7596
76- // Updates an existing <%= name %> in the DB.
97+ // Updates an existing <%= name %> in the DB
7798exports . update = function ( req , res ) {
7899 if ( req . body . _id ) {
79100 delete req . body . _id ;
80101 }
81- < %= classedName % > .findByIdAsync(req.params.id)
102+ < % if ( filters . mongooseModels ) { % > < %= classedName % > .findByIdAsync(req.params.id)< % }
103+ if ( filters . sequelizeModels ) { % > < %= classedName % > .find({
104+ where : {
105+ _id : req . params . id
106+ }
107+ } )< % } % >
82108 . then ( handleEntityNotFound ( res ) )
83109 . then ( saveUpdates ( req . body ) )
84110 . then ( responseWithResult ( res ) )
85111 . catch ( handleError ( res ) ) ;
86112} ;
87113
88- // Deletes a <%= name %> from the DB.
114+ // Deletes a <%= name %> from the DB
89115exports . destroy = function ( req , res ) {
90- < %= classedName % > .findByIdAsync(req.params.id)
116+ < % if ( filters . mongooseModels ) { % > < %= classedName % > .findByIdAsync(req.params.id)< % }
117+ if ( filters . sequelizeModels ) { % > < %= classedName % > .find({
118+ where : {
119+ _id : req . params . id
120+ }
121+ } )< % } % >
91122 . then ( handleEntityNotFound ( res ) )
92123 . then ( removeEntity ( res ) )
93124 . catch ( handleError ( res ) ) ;
0 commit comments