(Model, id, current, data, change, conflicts, options, cb)
| 1495 | } |
| 1496 | |
| 1497 | function applyUpdate(Model, id, current, data, change, conflicts, options, cb) { |
| 1498 | const Change = Model.getChangeModel(); |
| 1499 | const rev = current ? Change.revisionForInst(current) : null; |
| 1500 | |
| 1501 | if (rev !== change.prev) { |
| 1502 | debug('Detected non-rectified change of %s %j', |
| 1503 | Model.modelName, id); |
| 1504 | debug('\tExpected revision: %s', change.rev); |
| 1505 | debug('\tActual revision: %s', rev); |
| 1506 | conflicts.push(change); |
| 1507 | return Change.rectifyModelChanges(Model.modelName, [id], cb); |
| 1508 | } |
| 1509 | |
| 1510 | // TODO(bajtos) modify `data` so that it instructs |
| 1511 | // the connector to remove any properties included in "inst" |
| 1512 | // but not included in `data` |
| 1513 | // See https://github.com/strongloop/loopback/issues/1215 |
| 1514 | |
| 1515 | Model.updateAll(current.toObject(), data, options, function(err, result) { |
| 1516 | if (err) return cb(err); |
| 1517 | |
| 1518 | const count = result && result.count; |
| 1519 | switch (count) { |
| 1520 | case 1: |
| 1521 | // The happy path, exactly one record was updated |
| 1522 | return cb(); |
| 1523 | |
| 1524 | case 0: |
| 1525 | debug('UpdateAll detected non-rectified change of %s %j', |
| 1526 | Model.modelName, id); |
| 1527 | conflicts.push(change); |
| 1528 | // NOTE(bajtos) updateAll triggers change rectification |
| 1529 | // for all model instances, even when no records were updated, |
| 1530 | // thus we don't need to rectify explicitly ourselves |
| 1531 | return cb(); |
| 1532 | |
| 1533 | case undefined: |
| 1534 | case null: |
| 1535 | return cb(new Error( |
| 1536 | g.f('Cannot apply bulk updates, ' + |
| 1537 | 'the connector does not correctly report ' + |
| 1538 | 'the number of updated records.'), |
| 1539 | )); |
| 1540 | |
| 1541 | default: |
| 1542 | debug('%s.updateAll modified unexpected number of instances: %j', |
| 1543 | Model.modelName, count); |
| 1544 | return cb(new Error( |
| 1545 | g.f('Bulk update failed, the connector has modified unexpected ' + |
| 1546 | 'number of records: %s', JSON.stringify(count)), |
| 1547 | )); |
| 1548 | } |
| 1549 | }); |
| 1550 | } |
| 1551 | |
| 1552 | function applyCreate(Model, id, current, data, change, conflicts, options, cb) { |
| 1553 | Model.create(data, options, function(createErr) { |
no outgoing calls
no test coverage detected
searching dependent graphs…