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
+ */
2
9
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 % > ; < % } % >
5
16
6
17
function handleError ( res , statusCode ) {
7
18
statusCode = statusCode || 500 ;
@@ -14,7 +25,7 @@ function responseWithResult(res, statusCode) {
14
25
statusCode = statusCode || 200 ;
15
26
return function ( entity ) {
16
27
if ( entity ) {
17
- return res . status ( statusCode ) . json ( entity ) ;
28
+ res . status ( statusCode ) . json ( entity ) ;
18
29
}
19
30
} ;
20
31
}
@@ -31,9 +42,11 @@ function handleEntityNotFound(res) {
31
42
32
43
function saveUpdates ( updates ) {
33
44
return function ( entity ) {
34
- var updated = _ . merge ( entity , updates ) ;
45
+ < % if ( filters . mongooseModels ) { % > var updated = _ . merge ( entity , updates ) ;
35
46
return updated . saveAsync ( )
36
- . spread ( function ( updated ) {
47
+ . spread ( function ( updated ) { < % }
48
+ if ( filters . sequelizeModels ) { % > return entity . updateAttributes ( updates )
49
+ . then ( function ( updated ) { < % } % >
37
50
return updated ;
38
51
} ) ;
39
52
} ;
@@ -42,52 +55,70 @@ function saveUpdates(updates) {
42
55
function removeEntity ( res ) {
43
56
return function ( entity ) {
44
57
if ( entity ) {
45
- return entity . removeAsync ( )
58
+ < % if ( filters . mongooseModels ) { % > return entity . removeAsync ( ) < % }
59
+ if ( filters . sequelizeModels ) { % > return entity . destroy ( ) < % } % >
46
60
. then ( function ( ) {
47
61
res . status ( 204 ) . end ( ) ;
48
62
} ) ;
49
63
}
50
64
} ;
51
65
} < % } % >
52
66
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()< % } % >
57
72
. then ( responseWithResult ( res ) )
58
73
. catch ( handleError ( res ) ) ; < % } % >
59
- } ; < % if ( filters . mongoose ) { % >
74
+ } ; < % if ( filters . models ) { % >
60
75
61
- // Gets a single <%= name %> from the DB.
76
+ // Gets a single <%= name %> from the DB
62
77
exports . 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
+ } )< % } % >
64
84
. then ( handleEntityNotFound ( res ) )
65
85
. then ( responseWithResult ( res ) )
66
86
. catch ( handleError ( res ) ) ;
67
87
} ;
68
88
69
- // Creates a new <%= name %> in the DB.
89
+ // Creates a new <%= name %> in the DB
70
90
exports . 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)< % } % >
72
93
. then ( responseWithResult ( res , 201 ) )
73
94
. catch ( handleError ( res ) ) ;
74
95
} ;
75
96
76
- // Updates an existing <%= name %> in the DB.
97
+ // Updates an existing <%= name %> in the DB
77
98
exports . update = function ( req , res ) {
78
99
if ( req . body . _id ) {
79
100
delete req . body . _id ;
80
101
}
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
+ } )< % } % >
82
108
. then ( handleEntityNotFound ( res ) )
83
109
. then ( saveUpdates ( req . body ) )
84
110
. then ( responseWithResult ( res ) )
85
111
. catch ( handleError ( res ) ) ;
86
112
} ;
87
113
88
- // Deletes a <%= name %> from the DB.
114
+ // Deletes a <%= name %> from the DB
89
115
exports . 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
+ } )< % } % >
91
122
. then ( handleEntityNotFound ( res ) )
92
123
. then ( removeEntity ( res ) )
93
124
. catch ( handleError ( res ) ) ;
0 commit comments