diff --git a/lib/scope.js b/lib/scope.js index e2d121539..8d79f3ae2 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -416,6 +416,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { options = {}; } options = options || {}; + cb = cb || utils.createPromiseCallback(); const targetModel = definition.targetModel(this._receiver); // If there is a through model // run another query to apply filter on relatedModel(targetModel) @@ -446,15 +447,18 @@ function defineScope(cls, targetClass, name, params, methods, options) { const scoped = (this._scope && this._scope.where) || {}; const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated}); if (!scopeOnRelatedModel) { - return targetModel.destroyAll(filter.where, options, cb); + targetModel.destroyAll(filter.where, options, cb); + } else { + targetModel.find(filter, options, function(err, findData) { + const smartMergeSuccessful = + smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); + if (!smartMergeSuccessful) { + return cb(null, {count: 0}); + } + return relatedModel.destroyAll(queryRelated.scope.where, options, cb); + }); } - return targetModel.find(filter, options, function(err, findData) { - const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); - if (!smartMergeSuccessful) { - return cb(null, {count: 0}); - } - return relatedModel.destroyAll(queryRelated.scope.where, options, cb); - }); + return cb.promise; } function updateAll(where, data, options, cb) { @@ -471,6 +475,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { options = {}; } options = options || {}; + cb = cb || utils.createPromiseCallback(); const targetModel = definition.targetModel(this._receiver); // If there is a through model // run another query to apply filter on relatedModel(targetModel) @@ -501,15 +506,18 @@ function defineScope(cls, targetClass, name, params, methods, options) { const scoped = (this._scope && this._scope.where) || {}; const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated}); if (!scopeOnRelatedModel) { - return targetModel.updateAll(filter.where, data, options, cb); + targetModel.updateAll(filter.where, data, options, cb); + } else { + targetModel.find(filter, options, function(err, findData) { + const smartMergeSuccessful = + smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); + if (!smartMergeSuccessful) { + return cb(null, {count: 0}); + } + return relatedModel.updateAll(queryRelated.scope.where, data, options, cb); + }); } - return targetModel.find(filter, options, function(err, findData) { - const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); - if (!smartMergeSuccessful) { - return cb(null, {count: 0}); - } - return relatedModel.updateAll(queryRelated.scope.where, data, options, cb); - }); + return cb.promise; } function findById(id, filter, options, cb) { @@ -554,6 +562,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { options = {}; } options = options || {}; + cb = cb || utils.createPromiseCallback(); const targetModel = definition.targetModel(this._receiver); // If there is a through model // run another query to apply filter on relatedModel(targetModel) @@ -586,15 +595,18 @@ function defineScope(cls, targetClass, name, params, methods, options) { const scoped = (this._scope && this._scope.where) || {}; filter = mergeQuery({where: scoped}, filter || {}); if (!scopeOnRelatedModel) { - return targetModel.findOne(filter, options, cb); + targetModel.findOne(filter, options, cb); + } else { + targetModel.find(filter, options, function(err, findData) { + const smartMergeSuccessful = + smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); + if (!smartMergeSuccessful) { + return cb(null, null); + } + return relatedModel.findOne(queryRelated.scope, options, cb); + }); } - return targetModel.find(filter, options, function(err, findData) { - const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); - if (!smartMergeSuccessful) { - return cb(null, null); - } - return relatedModel.findOne(queryRelated.scope, options, cb); - }); + return cb.promise; } function count(where, options, cb) { @@ -608,6 +620,7 @@ function defineScope(cls, targetClass, name, params, methods, options) { options = {}; } options = options || {}; + cb = cb || utils.createPromiseCallback(); const targetModel = definition.targetModel(this._receiver); // If there is a through model // run another query to apply filter on relatedModel(targetModel) @@ -638,15 +651,18 @@ function defineScope(cls, targetClass, name, params, methods, options) { const scoped = (this._scope && this._scope.where) || {}; const filter = mergeQuery({where: scoped}, {where: where || {}, fields: fieldsRelated}); if (!scopeOnRelatedModel) { - return targetModel.count(filter.where, options, cb); + targetModel.count(filter.where, options, cb); + } else { + targetModel.find(filter, options, function(err, findData) { + const smartMergeSuccessful = + smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); + if (!smartMergeSuccessful) { + return cb(null, 0); + } + return relatedModel.count(queryRelated.scope.where, options, cb); + }); } - return targetModel.find(filter, options, function(err, findData) { - const smartMergeSuccessful = smartMergeRelatedModelQuery(findData, queryRelated.scope, keyFrom, IdKey); - if (!smartMergeSuccessful) { - return cb(null, 0); - } - return relatedModel.count(queryRelated.scope.where, options, cb); - }); + return cb.promise; } return definition; diff --git a/test/relations.test.js b/test/relations.test.js index 635eb4eac..20e9e3225 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -607,858 +607,418 @@ describe('relations', function() { describe('scoped queries', function() { describe('find', function() { - it('should allow to use fields on related model', function(done) { - Physician.create(function(err, physician) { - physician.patients.create({name: 'a', age: 5}, function(err, patient) { - Address.create({name: 'z'}, function(err, address) { - if (err) return done(err); - patient.address(address); - patient.save(function() { - verify(physician, address.id); - }); - }); - }); - }); - function verify(physician, addressId) { - physician.patients({fields: 'age'}, function(err, ch) { - if (err) return done(err); - should.exist(ch); - ch.should.have.lengthOf(1); - ch[0].age.should.eql(5); - should.not.exist(ch[0].name); - done(); - }); - } + it('should allow to use fields on related model', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a', age: 5}); + const address = await Address.create({name: 'z'}); + patient.address(address); + await patient.save(); + const ch = await physician.patients({fields: 'age'}); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].age.should.eql(5); + should.not.exist(ch[0].name); }); - it('should find related model using model id', function(done) { - Physician.create(function(err, physician) { - if (err) return done(err); - physician.patients.create({name: 'a', age: 5}, function(err, patient) { - if (err) return done(err); - physician.patients.create({name: 'b', age: 5}, function(err) { - if (err) return done(err); - physician.patients.create({name: 'c', age: 5}, function(err) { - if (err) return done(err); - verify(physician, patient.id); - }); - }); - }); - }); - function verify(physician, patientId) { - physician.patients({where: {id: patientId}}, function(err, ch) { - if (err) return done(err); - should.exist(ch); - ch.should.have.lengthOf(1); - ch[0].age.should.eql(5); - done(); - }); - } + it('should find related model using model id', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a', age: 5}); + await physician.patients.create({name: 'b', age: 5}); + await physician.patients.create({name: 'c', age: 5}); + const ch = await physician.patients({where: {id: patient.id}}); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].age.should.eql(5); }); - it('should find related model using model id list', function(done) { - Physician.create(function(err, physician) { - if (err) return done(err); - physician.patients.create({name: 'a', age: 5}, function(err, patient) { - if (err) return done(err); - physician.patients.create({name: 'b', age: 5}, function(err) { - if (err) return done(err); - physician.patients.create({name: 'c', age: 5}, function(err) { - if (err) return done(err); - verify(physician, patient.id); - }); - }); - }); - }); - function verify(physician, patientId) { - physician.patients({where: {id: {inq: [patientId]}}}, function(err, ch) { - if (err) return done(err); - should.exist(ch); - ch.should.have.lengthOf(1); - ch[0].age.should.eql(5); - done(); - }); - } + it('should find related model using model id list', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a', age: 5}); + await physician.patients.create({name: 'b', age: 5}); + await physician.patients.create({name: 'c', age: 5}); + const ch = await physician.patients({where: {id: {inq: [patient.id]}}}); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].age.should.eql(5); }); - it('should find scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients({ - where: { - name: 'a', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } + it('should find scoped record with promises based on related model properties', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + name: 'a', + }, + }); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); }); it('should find scoped record with promises based on related model' + - ' properties with empty results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients({ - where: { - id: 'bar', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(0); - done(); - }); - } + ' properties with empty results', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + id: 'bar', + }, + }); + should.exist(patients); + patients.should.have.lengthOf(0); }); - it('should find scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients({ - where: { - id, - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } + it('should find scoped record with promises based on related model id', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + id: patient.id, + }, + }); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); }); - it('should find scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients({ - where: { - id: { - inq: [id], - }, + it('should find scoped record with promises based on related model id list', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + id: { + inq: [patient.id], }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } + }, + }); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); }); }); describe('find explicit', function() { it('should find (explicit) scoped record with promises based on related' + - ' model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.find({ - where: { - name: 'a', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } + ' model properties', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + name: 'a', + }, + }); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); }); it('should find (explicit) scoped record with promises based on related model' + - ' properties with empty results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.find({ - where: { - id: 'bar', - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(0); - done(); - }); - } + ' properties with empty results', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + id: 'bar', + }, + }); + should.exist(patients); + patients.should.have.lengthOf(0); }); - it('should find (explicit) scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.find({ - where: { - id, - }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } + it('should find (explicit) scoped record with promises based on related model id', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + id: patient.id, + }, + }); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); }); it('should find (explicit) scoped record with promises based on ' + - 'related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.find({ - where: { - id: { - inq: [id], - }, + 'related model id list', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patients = await physician.patients({ + where: { + id: { + inq: [patient.id], }, - }, function(err, patients) { - if (err) return done(err); - should.exist(patients); - patients.should.have.lengthOf(1); - patients[0].name.should.equal('a'); - done(); - }); - } + }, + }); + should.exist(patients); + patients.should.have.lengthOf(1); + patients[0].name.should.equal('a'); }); }); describe('count', function() { - it('should count scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.count({ - name: 'a', - }, function(err, count) { - if (err) return done(err); - count.should.equal(1); - done(); - }); - } + it('should count scoped record with promises based on related model properties', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const count = await physician.patients.count({ + name: 'a', + }); + should.exist(count); + count.should.equal(1); }); it('should count scoped record with promises based on related model ' + - 'properties with no results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.count({ - id: 'bar', - }, function(err, count) { - if (err) return done(err); - count.should.equal(0); - done(); - }); - } + 'properties with no results', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const count = await physician.patients.count({ + id: 'bar', + }); + should.exist(count); + count.should.equal(0); }); - it('should count scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.count({ - id: id, - }, function(err, count) { - if (err) return done(err); - count.should.equal(1); - done(); - }); - } + it('should count scoped record with promises based on related model id', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const count = await physician.patients.count({ + id: patient.id, + }); + should.exist(count); + count.should.equal(1); }); - it('should count scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.count({ - id: { - inq: [id], - }, - }, function(err, count) { - if (err) return done(err); - count.should.equal(1); - done(); - }); - } + it('should count scoped record with promises based on related model id list', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const count = await physician.patients.count({ + id: { + inq: [patient.id], + }, + }); + should.exist(count); + count.should.equal(1); }); }); describe('find one', function() { - it('should find one scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.name.should.equal('a'); - done(); - }); - } + it('should find one scoped record with promises based on related model properties', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patient = await physician.patients.findOne({ + where: { + name: 'a', + }, + }); + should.exist(patient); + patient.name.should.equal('a'); }); it('should find one scoped record with promises based on related model' + - ' properties with empty results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.findOne({ - where: { - id: 'bar', - }, - }, function(err, patient) { - if (err) return done(err); - should.not.exist(patient); - done(); - }); - } + ' properties with empty results', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patient = await physician.patients.findOne({ + where: { + id: 'bar', + }, + }); + should.not.exist(patient); }); - it('should find one scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.findOne({ - where: { - id, - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.name.should.equal('a'); - done(); - }); - } + it('should find one scoped record with promises based on related model id', async function() { + const physician = await Physician.create(); + const createdPatient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patient = await physician.patients.findOne({ + where: { + id: createdPatient.id, + }, + }); + should.exist(patient); + patient.name.should.equal('a'); }); - it('should find one scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.findOne({ - where: { - id: { - inq: [id], - }, + it('should find one scoped record with promises based on related model id list', async function() { + const physician = await Physician.create(); + const createdPatient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const patient = await physician.patients.findOne({ + where: { + id: { + inq: [createdPatient.id], }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.name.should.equal('a'); - done(); - }); - } + }, + }); + should.exist(patient); + patient.name.should.equal('a'); }); }); describe('update all', function() { - it('should update all scoped record with promises based on related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.updateAll({ + it('should update all scoped record with promises based on related model properties', + async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.updateAll({ name: 'a', - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.age.should.equal(5); - done(); - }); + }, { + age: 5, }); - } - }); - - it('should update all scoped record with promises based on related ' + - 'model properties no results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.updateAll({ - id: 'bar', - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(0); - done(); + should.exist(result); + result.count.should.equal(1); + const patient = await physician.patients.findOne({ + where: { + name: 'a', + }, }); - } - }); - - it('should update all scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); + should.exist(patient); + patient.age.should.equal(5); + }); - function verify(physician) { - return physician.patients.updateAll({ - id, - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.age.should.equal(5); - done(); - }); - }); - } + it('should update all scoped record with promises based on related ' + + 'model properties no results', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.updateAll({ + id: 'bar', + }, { + age: 5, + }); + should.exist(result); + result.count.should.equal(0); + }); + + it('should update all scoped record with promises based on related model id', async function() { + const physician = await Physician.create(); + const createdPatient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.updateAll({ + id: createdPatient.id, + }, { + age: 5, + }); + should.exist(result); + result.count.should.equal(1); + const patient = await physician.patients.findOne({ + where: { + name: 'a', + }, + }); + should.exist(patient); + patient.age.should.equal(5); }); - it('should update all scoped record with promises based on related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.updateAll({ - id: { - inq: [id], - }, - }, {age: 5}, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - physician.patients.findOne({ - where: { - name: 'a', - }, - }, function(err, patient) { - if (err) return done(err); - should.exist(patient); - patient.age.should.equal(5); - done(); - }); - }); - } + it('should update all scoped record with promises based on related model id list', async function() { + const physician = await Physician.create(); + const createdPatient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.updateAll({ + id: { + inq: [createdPatient.id], + }, + }, { + age: 5, + }); + should.exist(result); + result.count.should.equal(1); + const patient = await physician.patients.findOne({ + where: { + name: 'a', + }, + }); + should.exist(patient); + patient.age.should.equal(5); }); }); describe('destroy all', function() { it('should destroyAll all scoped record with promises based on ' + - 'related model properties', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.destroyAll({ - name: 'a', - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - done(); - }); - } + 'related model properties', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.destroyAll({ + name: 'a', + }); + should.exist(result); + result.count.should.equal(1); }); it('should destroyAll all scoped record with promises based on related ' + - 'model properties no results', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.destroyAll({ - id: 'foo', - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(0); - done(); - }); - } + 'model properties no results', async function() { + const physician = await Physician.create(); + await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.destroyAll({ + id: 'foo', + }); + should.exist(result); + result.count.should.equal(0); }); - it('should destroyAll all scoped record with promises based on related model id', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.destroyAll({ - id, - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - done(); - }); - } + it('should destroyAll all scoped record with promises based on related model id', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.destroyAll({ + id: patient.id, + }); + should.exist(result); + result.count.should.equal(1); }); it('should destroyAll all scoped record with promises based on ' + - 'related model id list', function(done) { - let id; - Physician.create() - .then(function(physician) { - return physician.patients.create({name: 'a'}) - .then(function(ch) { - id = ch.id; - return physician.patients.create({name: 'z'}); - }) - .then(function() { - return physician.patients.create({name: 'c'}); - }) - .then(function() { - return verify(physician); - }); - }).catch(done); - - function verify(physician) { - return physician.patients.destroyAll({ - id: { - inq: [id], - }, - }, function(err, result) { - if (err) return done(err); - should.exist(result); - result.count.should.equal(1); - done(); - }); - } + 'related model id list', async function() { + const physician = await Physician.create(); + const patient = await physician.patients.create({name: 'a'}); + await physician.patients.create({name: 'z'}); + await physician.patients.create({name: 'c'}); + const result = await physician.patients.destroyAll({ + id: { + inq: [patient.id], + }, + }); + should.exist(result); + result.count.should.equal(1); }); }); });