@@ -1236,6 +1236,9 @@ ESConnector.prototype.updateAll = function updateAll(model, where, data, options
12361236      // default language for inline scripts is painless if ES 5, so this needs the extra params. 
12371237      body . script . inline  +=  'ctx._source.'  +  key  +  '=params.'  +  key  +  ';' ; 
12381238      body . script . params [ key ]  =  value ; 
1239+       if  ( key  ===  'docType' )  { 
1240+         body . script . params [ key ]  =  model ; 
1241+       } 
12391242    } 
12401243  } ) ; 
12411244
@@ -1247,7 +1250,7 @@ ESConnector.prototype.updateAll = function updateAll(model, where, data, options
12471250  self . db . updateByQuery ( document ) 
12481251    . then ( function  ( response )  { 
12491252      log ( 'ESConnector.prototype.updateAll' ,  'response' ,  response ) ; 
1250-       cb ( null ,  { 
1253+       return   cb ( null ,  { 
12511254        updated : response . updated , 
12521255        total : response . total 
12531256      } ) ; 
@@ -1370,38 +1373,57 @@ ESConnector.prototype.destroy = function destroy(modelName, id, done) {
13701373/** 
13711374 * Update a model instance by id 
13721375 * 
1373-  * NOTES: 
1374-  * > The _source field need to be enabled for this feature to work. 
13751376 */ 
1377+ 
13761378ESConnector . prototype . updateAttributes  =  function  updateAttrs ( modelName ,  id ,  data ,  callback )  { 
13771379  var  self  =  this ; 
1378-   log ( 'ESConnector.prototype.updateAttributes' ,  'modelName' ,  modelName ,  'id' ,  id ,  'data' ,  data ) ; 
1379- 
1380-   if  ( id  ===  undefined  ||  id  ===  null )  { 
1381-     throw  new  Error ( 'id not set!' ) ; 
1380+   if  ( self . debug )  { 
1381+     log ( 'ESConnector.prototype.updateAttributes' ,  'modelName' ,  modelName ,  'id' ,  id ,  'data' ,  data ) ; 
13821382  } 
1383+   var  idName  =  self . idName ( modelName ) ; 
1384+   log ( 'ESConnector.prototype.updateAttributes' ,  'idName' ,  idName ) ; 
13831385
1384-   var  defaults  =  self . addDefaults ( modelName ,  'updateAttributes' ) ; 
1385-   self . db . update ( _ . defaults ( { 
1386-     id : id , 
1387-     body : { 
1388-       doc : data , 
1389-       'doc_as_upsert' : false 
1386+   var  defaults  =  self . addDefaults ( modelName ,  'updateAll' ) ; 
1387+ 
1388+   var  body  =  { 
1389+     query : self . buildWhere ( modelName ,  idName ,  { 
1390+       _id : id 
1391+     } ) . query 
1392+   } ; 
1393+ 
1394+   body . script  =  { 
1395+     inline : '' , 
1396+     params : { } 
1397+   } ; 
1398+   _ . forEach ( data ,  function  ( value ,  key )  { 
1399+     if  ( key  !==  '_id'  ||  key  !==  idName )  { 
1400+       // default language for inline scripts is painless if ES 5, so this needs the extra params. 
1401+       body . script . inline  +=  'ctx._source.'  +  key  +  '=params.'  +  key  +  ';' ; 
1402+       body . script . params [ key ]  =  value ; 
1403+       if  ( key  ===  'docType' )  { 
1404+         body . script . params [ key ]  =  modelName ; 
1405+       } 
13901406    } 
1391-   } ,  defaults ) ) . then ( 
1392-     function  ( response )  { 
1393-       // TODO: what does the framework want us to return as arguments w/ callback? 
1394-       callback ( null ,  response ) ; 
1395-       //callback(null, response._id); 
1396-       //callback(null, data); 
1397-     } , 
1398-     function  ( err )  { 
1407+   } ) ; 
1408+ 
1409+   var  document  =  _ . defaults ( { 
1410+     body : body 
1411+   } ,  defaults ) ; 
1412+   log ( 'ESConnector.prototype.updateAttributes' ,  'document to update' ,  document ) ; 
1413+ 
1414+   self . db . updateByQuery ( document ) 
1415+     . then ( function  ( response )  { 
1416+       log ( 'ESConnector.prototype.updateAttributes' ,  'response' ,  response ) ; 
1417+       return  callback ( null ,  { 
1418+         updated : response . updated , 
1419+         total : response . total 
1420+       } ) ; 
1421+     } ,  function  ( err )  { 
13991422      log ( 'ESConnector.prototype.updateAttributes' ,  err . message ) ; 
14001423      if  ( err )  { 
14011424        return  callback ( err ,  null ) ; 
14021425      } 
1403-     } 
1404-   ) ; 
1426+     } ) ; 
14051427} ; 
14061428
14071429/** 
@@ -1465,7 +1487,7 @@ ESConnector.prototype.save = function (model, data, done) {
14651487  if  ( id  ===  undefined  ||  id  ===  null )  { 
14661488    return  done ( 'Document id not setted!' ,  null ) ; 
14671489  } 
1468- 
1490+    data . docType   =   model ; 
14691491  self . db . update ( _ . defaults ( { 
14701492    id : id , 
14711493    body : { 
@@ -1509,6 +1531,7 @@ ESConnector.prototype.updateOrCreate = function updateOrCreate(modelName, data,
15091531  } 
15101532
15111533  var  defaults  =  self . addDefaults ( modelName ,  'updateOrCreate' ) ; 
1534+   data . docType  =  modelName ; 
15121535  self . db . update ( _ . defaults ( { 
15131536    id : id , 
15141537    body : { 
@@ -1583,6 +1606,7 @@ ESConnector.prototype.replaceOrCreate = function (modelName, data, callback) {
15831606  document [ self . idField ]  =  id ; 
15841607  document . body  =  { } ; 
15851608  _ . assign ( document . body ,  data ) ; 
1609+   document . body . docType  =  modelName ; 
15861610  log ( 'ESConnector.prototype.replaceOrCreate' ,  'document' ,  document ) ; 
15871611  self . db . index ( 
15881612    document 
@@ -1615,6 +1639,7 @@ ESConnector.prototype.replaceById = function replace(modelName, id, data, option
16151639  document [ self . idField ]  =  self . getDocumentId ( id ) ; 
16161640  document . body  =  { } ; 
16171641  _ . assign ( document . body ,  data ) ; 
1642+   document . body . docType  =  modelName ; 
16181643  if  ( modelProperties . hasOwnProperty ( idName ) )  { 
16191644    document . body [ idName ]  =  id ; 
16201645  } 
0 commit comments