@@ -22,6 +22,18 @@ function respondWithResult(res, statusCode) {
2222 } ;
2323}
2424
25+ function saveUpsert ( new ) {
26+ return function ( entity ) {
27+ < % _ if ( filters . mongooseModels ) { - % >
28+ var updated = _ . assignIn ( entity , updates ) ;
29+ return updated . save ( ) ;
30+ < % _ } - % >
31+ < % _ if ( filters . sequelizeModels ) { - % >
32+ return entity . updateAttributes ( updates ) ;
33+ < % _ } - % >
34+ } ;
35+ }
36+
2537function saveUpdates ( updates ) {
2638 return function ( entity ) {
2739 < % _ if ( filters . mongooseModels ) { - % >
@@ -93,6 +105,23 @@ export function create(req, res) {
93105 . catch ( handleError ( res ) ) ;
94106}
95107
108+ // Upserts the given <%= classedName %> in the DB at the specified ID
109+ export function upsert ( req , res ) {
110+ if ( req . body . _id ) {
111+ delete req . body . _id ;
112+ }
113+ < % _ if ( filters . mongooseModels ) { - % >
114+ return < %= classedName % > . findOneAndUpdate ( req . params . id , req . body , { upsert : true , setDefaultsOnInsert : true , runValidators : true } ) . exec ( ) < % } % >
115+ < % _ if ( filters . sequelizeModels ) { - % >
116+ return < %= classedName % > . upsert ( req . body , {
117+ where : {
118+ _id : req . params . id
119+ }
120+ } ) < % } % >
121+ . then ( respondWithResult ( res ) )
122+ . catch ( handleError ( res ) ) ;
123+ }
124+
96125// Updates an existing <%= classedName %> in the DB
97126export function update ( req , res ) {
98127 if ( req . body . _id ) {
0 commit comments