@@ -1236,6 +1236,9 @@ ESConnector.prototype.updateAll = function updateAll(model, where, data, options
1236
1236
// default language for inline scripts is painless if ES 5, so this needs the extra params.
1237
1237
body . script . inline += 'ctx._source.' + key + '=params.' + key + ';' ;
1238
1238
body . script . params [ key ] = value ;
1239
+ if ( key === 'docType' ) {
1240
+ body . script . params [ key ] = model ;
1241
+ }
1239
1242
}
1240
1243
} ) ;
1241
1244
@@ -1247,7 +1250,7 @@ ESConnector.prototype.updateAll = function updateAll(model, where, data, options
1247
1250
self . db . updateByQuery ( document )
1248
1251
. then ( function ( response ) {
1249
1252
log ( 'ESConnector.prototype.updateAll' , 'response' , response ) ;
1250
- cb ( null , {
1253
+ return cb ( null , {
1251
1254
updated : response . updated ,
1252
1255
total : response . total
1253
1256
} ) ;
@@ -1370,38 +1373,57 @@ ESConnector.prototype.destroy = function destroy(modelName, id, done) {
1370
1373
/**
1371
1374
* Update a model instance by id
1372
1375
*
1373
- * NOTES:
1374
- * > The _source field need to be enabled for this feature to work.
1375
1376
*/
1377
+
1376
1378
ESConnector . prototype . updateAttributes = function updateAttrs ( modelName , id , data , callback ) {
1377
1379
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 ) ;
1382
1382
}
1383
+ var idName = self . idName ( modelName ) ;
1384
+ log ( 'ESConnector.prototype.updateAttributes' , 'idName' , idName ) ;
1383
1385
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
+ }
1390
1406
}
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 ) {
1399
1422
log ( 'ESConnector.prototype.updateAttributes' , err . message ) ;
1400
1423
if ( err ) {
1401
1424
return callback ( err , null ) ;
1402
1425
}
1403
- }
1404
- ) ;
1426
+ } ) ;
1405
1427
} ;
1406
1428
1407
1429
/**
@@ -1465,7 +1487,7 @@ ESConnector.prototype.save = function (model, data, done) {
1465
1487
if ( id === undefined || id === null ) {
1466
1488
return done ( 'Document id not setted!' , null ) ;
1467
1489
}
1468
-
1490
+ data . docType = model ;
1469
1491
self . db . update ( _ . defaults ( {
1470
1492
id : id ,
1471
1493
body : {
@@ -1509,6 +1531,7 @@ ESConnector.prototype.updateOrCreate = function updateOrCreate(modelName, data,
1509
1531
}
1510
1532
1511
1533
var defaults = self . addDefaults ( modelName , 'updateOrCreate' ) ;
1534
+ data . docType = modelName ;
1512
1535
self . db . update ( _ . defaults ( {
1513
1536
id : id ,
1514
1537
body : {
@@ -1583,6 +1606,7 @@ ESConnector.prototype.replaceOrCreate = function (modelName, data, callback) {
1583
1606
document [ self . idField ] = id ;
1584
1607
document . body = { } ;
1585
1608
_ . assign ( document . body , data ) ;
1609
+ document . body . docType = modelName ;
1586
1610
log ( 'ESConnector.prototype.replaceOrCreate' , 'document' , document ) ;
1587
1611
self . db . index (
1588
1612
document
@@ -1615,6 +1639,7 @@ ESConnector.prototype.replaceById = function replace(modelName, id, data, option
1615
1639
document [ self . idField ] = self . getDocumentId ( id ) ;
1616
1640
document . body = { } ;
1617
1641
_ . assign ( document . body , data ) ;
1642
+ document . body . docType = modelName ;
1618
1643
if ( modelProperties . hasOwnProperty ( idName ) ) {
1619
1644
document . body [ idName ] = id ;
1620
1645
}
0 commit comments