Merge pull request #1424 from lehni/feature/rename-get-async

Rename getAsync() methods to find() and get()
This commit is contained in:
Miroslav Bajtoš 2017-07-20 15:15:12 +02:00 committed by GitHub
commit 1fb7b0e24d
4 changed files with 56 additions and 45 deletions

View File

@ -20,6 +20,7 @@ var idEquals = utils.idEquals;
var ModelBaseClass = require('./model.js'); var ModelBaseClass = require('./model.js');
var applyFilter = require('./connectors/memory').applyFilter; var applyFilter = require('./connectors/memory').applyFilter;
var ValidationError = require('./validations.js').ValidationError; var ValidationError = require('./validations.js').ValidationError;
var deprecated = require('depd')('loopback-datasource-juggler');
var debug = require('debug')('loopback:relations'); var debug = require('debug')('loopback:relations');
var RelationTypes = { var RelationTypes = {
@ -1338,7 +1339,11 @@ RelationDefinition.belongsTo = function(modelFrom, modelToRef, params) {
get: function() { get: function() {
var relation = new BelongsTo(definition, this); var relation = new BelongsTo(definition, this);
var relationMethod = relation.related.bind(relation); var relationMethod = relation.related.bind(relation);
relationMethod.getAsync = relation.getAsync.bind(relation); relationMethod.get = relation.get.bind(relation);
relationMethod.getAsync = function() {
deprecated(g.f('BelongsTo method "getAsync()" is deprecated, use "get()" instead.'));
return this.get.apply(this, arguments);
};
relationMethod.update = relation.update.bind(relation); relationMethod.update = relation.update.bind(relation);
relationMethod.destroy = relation.destroy.bind(relation); relationMethod.destroy = relation.destroy.bind(relation);
if (!polymorphic) { if (!polymorphic) {
@ -1580,9 +1585,8 @@ BelongsTo.prototype.related = function(condOrRefresh, options, cb) {
* @param {Function} cb Callback of the form function (err, inst) * @param {Function} cb Callback of the form function (err, inst)
* @returns {Promise | Undefined} returns promise if callback is omitted * @returns {Promise | Undefined} returns promise if callback is omitted
*/ */
BelongsTo.prototype.getAsync = function(options, cb) { BelongsTo.prototype.get = function(options, cb) {
if (typeof options === 'function' && cb === undefined) { if (typeof options === 'function' && cb === undefined) {
// order.customer.getAsync(cb)
cb = options; cb = options;
options = {}; options = {};
} }
@ -1712,7 +1716,11 @@ RelationDefinition.hasOne = function(modelFrom, modelToRef, params) {
get: function() { get: function() {
var relation = new HasOne(definition, this); var relation = new HasOne(definition, this);
var relationMethod = relation.related.bind(relation); var relationMethod = relation.related.bind(relation);
relationMethod.getAsync = relation.getAsync.bind(relation); relationMethod.get = relation.get.bind(relation);
relationMethod.getAsync = function() {
deprecated(g.f('HasOne method "getAsync()" is deprecated, use "get()" instead.'));
return this.get.apply(this, arguments);
};
relationMethod.create = relation.create.bind(relation); relationMethod.create = relation.create.bind(relation);
relationMethod.build = relation.build.bind(relation); relationMethod.build = relation.build.bind(relation);
relationMethod.update = relation.update.bind(relation); relationMethod.update = relation.update.bind(relation);
@ -1991,15 +1999,13 @@ HasOne.prototype.related = function(condOrRefresh, options, cb) {
/** /**
* Define a Promise-based method for the hasOne relation itself * Define a Promise-based method for the hasOne relation itself
* - order.customer.getAsync(cb): Load the target model instance asynchronously * - order.customer.get(cb): Load the target model instance asynchronously
* *
* @param {Function} cb Callback of the form function (err, inst) * @param {Function} cb Callback of the form function (err, inst)
* @returns {Promise | Undefined} Returns promise if cb is omitted * @returns {Promise | Undefined} Returns promise if cb is omitted
*/ */
HasOne.prototype.get = function(options, cb) {
HasOne.prototype.getAsync = function(options, cb) {
if (typeof options === 'function' && cb === undefined) { if (typeof options === 'function' && cb === undefined) {
// order.profile.getAsync(cb)
cb = options; cb = options;
options = {}; options = {};
} }

View File

@ -6,6 +6,7 @@
var _ = require('lodash'); var _ = require('lodash');
var i8n = require('inflection'); var i8n = require('inflection');
var g = require('strong-globalize')();
var utils = require('./utils'); var utils = require('./utils');
var defineCachedRelations = utils.defineCachedRelations; var defineCachedRelations = utils.defineCachedRelations;
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere; var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
@ -13,6 +14,7 @@ var mergeQuery = utils.mergeQuery;
var DefaultModelBaseClass = require('./model.js'); var DefaultModelBaseClass = require('./model.js');
var collectTargetIds = utils.collectTargetIds; var collectTargetIds = utils.collectTargetIds;
var idName = utils.idName; var idName = utils.idName;
var deprecated = require('depd')('loopback-datasource-juggler');
/** /**
* Module exports * Module exports
@ -272,15 +274,13 @@ function defineScope(cls, targetClass, name, params, methods, options) {
f._targetClass = rel && rel.modelTo && rel.modelTo.modelName || i8n.camelize(f._scope.collect); f._targetClass = rel && rel.modelTo && rel.modelTo.modelName || i8n.camelize(f._scope.collect);
} }
f.getAsync = function(condOrRefresh, options, cb) { f.find = function(condOrRefresh, options, cb) {
if (typeof condOrRefresh === 'function' && if (typeof condOrRefresh === 'function' &&
options === undefined && cb === undefined) { options === undefined && cb === undefined) {
// customer.orders.getAsync(cb)
cb = condOrRefresh; cb = condOrRefresh;
options = {}; options = {};
condOrRefresh = {}; condOrRefresh = {};
} else if (typeof options === 'function' && cb === undefined) { } else if (typeof options === 'function' && cb === undefined) {
// customer.orders.getAsync(condOrRefresh, cb);
cb = options; cb = options;
options = {}; options = {};
} }
@ -288,6 +288,11 @@ function defineScope(cls, targetClass, name, params, methods, options) {
return definition.related(self, f._scope, condOrRefresh, options, cb); return definition.related(self, f._scope, condOrRefresh, options, cb);
}; };
f.getAsync = function() {
deprecated(g.f('Scope method "getAsync()" is deprecated, use "find()" instead.'));
return this.find.apply(this, arguments);
};
f.build = build; f.build = build;
f.create = create; f.create = create;
f.updateAll = updateAll; f.updateAll = updateAll;

View File

@ -182,7 +182,7 @@ function mergeQuery(base, update, spec) {
} else { } else {
if (spec.nestedInclude === true) { if (spec.nestedInclude === true) {
// specify nestedInclude=true to force nesting of inclusions on scoped // specify nestedInclude=true to force nesting of inclusions on scoped
// queries. e.g. In physician.patients.getAsync({include: 'address'}), // queries. e.g. In physician.patients.find({include: 'address'}),
// inclusion should be on patient model, not on physician model. // inclusion should be on patient model, not on physician model.
var saved = base.include; var saved = base.include;
base.include = {}; base.include = {};

View File

@ -194,13 +194,13 @@ describe('relations', function() {
}).catch(done); }).catch(done);
function verify(book) { function verify(book) {
return book.chapters.getAsync() return book.chapters.find()
.then(function(ch) { .then(function(ch) {
should.exist(ch); should.exist(ch);
ch.should.have.lengthOf(3); ch.should.have.lengthOf(3);
var chapters = book.chapters(); var chapters = book.chapters();
chapters.should.eql(ch); chapters.should.eql(ch);
return book.chapters.getAsync() return book.chapters.find()
.then(function(c) { .then(function(c) {
should.exist(c); should.exist(c);
ch.should.have.lengthOf(3); ch.should.have.lengthOf(3);
@ -214,7 +214,7 @@ describe('relations', function() {
} }
}); });
it('should fetch all scoped instances with getAsync with callback and condition', function(done) { it('should fetch all scoped instances with find() with callback and condition', function(done) {
Book.create(function(err, book) { Book.create(function(err, book) {
book.chapters.create({name: 'a'}, function() { book.chapters.create({name: 'a'}, function() {
book.chapters.create({name: 'z'}, function() { book.chapters.create({name: 'z'}, function() {
@ -232,7 +232,7 @@ describe('relations', function() {
var chapters = book.chapters(); var chapters = book.chapters();
chapters.should.eql(ch); chapters.should.eql(ch);
book.chapters.getAsync(function(e, c) { book.chapters.find(function(e, c) {
should.not.exist(e); should.not.exist(e);
should.exist(c); should.exist(c);
ch.should.have.lengthOf(3); ch.should.have.lengthOf(3);
@ -246,7 +246,7 @@ describe('relations', function() {
} }
}); });
it('should fetch all scoped instances with getAsync with callback and no condition', function(done) { it('should fetch all scoped instances with find() with callback and no condition', function(done) {
Book.create(function(err, book) { Book.create(function(err, book) {
book.chapters.create({name: 'a'}, function() { book.chapters.create({name: 'a'}, function() {
book.chapters.create({name: 'z'}, function() { book.chapters.create({name: 'z'}, function() {
@ -265,7 +265,7 @@ describe('relations', function() {
var chapters = book.chapters(); var chapters = book.chapters();
chapters.should.eql(ch); chapters.should.eql(ch);
book.chapters.getAsync(function(e, c) { book.chapters.find(function(e, c) {
should.not.exist(e); should.not.exist(e);
should.exist(c); should.exist(c);
should.exist(c.length); should.exist(c.length);
@ -711,7 +711,7 @@ describe('relations', function() {
}); });
}).catch(done); }).catch(done);
function verify(physician) { function verify(physician) {
return physician.patients.getAsync() return physician.patients.find()
.then(function(ch) { .then(function(ch) {
var patients = physician.patients(); var patients = physician.patients();
should.equal(patients, ch); should.equal(patients, ch);
@ -1012,7 +1012,7 @@ describe('relations', function() {
}).catch(done); }).catch(done);
function verify(physician, addressId) { function verify(physician, addressId) {
return physician.patients.getAsync({include: 'address'}) return physician.patients.find({include: 'address'})
.then(function(ch) { .then(function(ch) {
should.exist(ch); should.exist(ch);
ch.should.have.lengthOf(1); ch.should.have.lengthOf(1);
@ -1646,7 +1646,7 @@ describe('relations', function() {
it('should find records on scope with promises', function(done) { it('should find records on scope with promises', function(done) {
Category.findOne() Category.findOne()
.then(function(c) { .then(function(c) {
return c.jobs.getAsync(); return c.jobs.find();
}) })
.then(function(jobs) { .then(function(jobs) {
jobs.should.have.length(2); jobs.should.have.length(2);
@ -1670,7 +1670,7 @@ describe('relations', function() {
it('should find record on scope with promises - filtered', function(done) { it('should find record on scope with promises - filtered', function(done) {
Category.findOne() Category.findOne()
.then(function(c) { .then(function(c) {
return c.jobs.getAsync({where: {type: 'book'}}); return c.jobs.find({where: {type: 'book'}});
}) })
.then(function(jobs) { .then(function(jobs) {
jobs.should.have.length(1); jobs.should.have.length(1);
@ -2051,7 +2051,7 @@ describe('relations', function() {
it('should find polymorphic relation with promises - employee', function(done) { it('should find polymorphic relation with promises - employee', function(done) {
Employee.findOne() Employee.findOne()
.then(function(employee) { .then(function(employee) {
return employee.mugshot.getAsync() return employee.mugshot.get()
.then(function(pic) { .then(function(pic) {
pic.name.should.equal('Mugshot'); pic.name.should.equal('Mugshot');
pic.imageableId.toString().should.eql(employee.id.toString()); pic.imageableId.toString().should.eql(employee.id.toString());
@ -3140,7 +3140,7 @@ describe('relations', function() {
}); });
}); });
it('can be used to query data with getAsync with callback', function(done) { it('can be used to query data with get() with callback', function(done) {
List.hasMany('todos', {model: Item}); List.hasMany('todos', {model: Item});
db.automigrate(['List', 'Item', 'Fear', 'Find'], function() { db.automigrate(['List', 'Item', 'Fear', 'Find'], function() {
List.create({name: 'List 1'}, function(e, list) { List.create({name: 'List 1'}, function(e, list) {
@ -3149,7 +3149,7 @@ describe('relations', function() {
should.exist(list); should.exist(list);
list.todos.create({name: 'Item 1'}, function(err, todo) { list.todos.create({name: 'Item 1'}, function(err, todo) {
itemId = todo.id; itemId = todo.id;
todo.list.getAsync(function(e, l) { todo.list.get(function(e, l) {
should.not.exist(e); should.not.exist(e);
should.exist(l); should.exist(l);
l.should.be.an.instanceOf(List); l.should.be.an.instanceOf(List);
@ -3173,7 +3173,7 @@ describe('relations', function() {
}) })
.then(function(todo) { .then(function(todo) {
itemId = todo.id; itemId = todo.id;
return todo.list.getAsync() return todo.list.get()
.then(function(l) { .then(function(l) {
should.exist(l); should.exist(l);
l.should.be.an.instanceOf(List); l.should.be.an.instanceOf(List);
@ -3349,7 +3349,7 @@ describe('relations', function() {
// the first row returned may or may not be the same // the first row returned may or may not be the same
p.personId.should.eql(personCreated.id); p.personId.should.eql(personCreated.id);
} }
return p.person.getAsync(); return p.person.get();
}) })
.then(function(person) { .then(function(person) {
person.name.should.equal('Fred'); person.name.should.equal('Fred');
@ -3467,14 +3467,14 @@ describe('relations', function() {
}); });
}); });
it('can be used to query data with getAsync with callback', function(done) { it('can be used to query data with get() with callback', function(done) {
db.automigrate(['Supplier', 'Account'], function() { db.automigrate(['Supplier', 'Account'], function() {
Supplier.create({name: 'Supplier 1'}, function(e, supplier) { Supplier.create({name: 'Supplier 1'}, function(e, supplier) {
supplierId = supplier.id; supplierId = supplier.id;
should.not.exist(e); should.not.exist(e);
should.exist(supplier); should.exist(supplier);
supplier.account.create({accountNo: 'a01'}, function(err, account) { supplier.account.create({accountNo: 'a01'}, function(err, account) {
supplier.account.getAsync(function(e, act) { supplier.account.get(function(e, act) {
accountId = act.id; accountId = act.id;
should.not.exist(e); should.not.exist(e);
should.exist(act); should.exist(act);
@ -3496,7 +3496,7 @@ describe('relations', function() {
should.exist(supplier); should.exist(supplier);
return supplier.account.create({accountNo: 'a01'}) return supplier.account.create({accountNo: 'a01'})
.then(function(account) { .then(function(account) {
return supplier.account.getAsync(); return supplier.account.get();
}) })
.then(function(act) { .then(function(act) {
accountId = act.id; accountId = act.id;
@ -3575,7 +3575,7 @@ describe('relations', function() {
Supplier.findById(supplierId) Supplier.findById(supplierId)
.then(function(supplier) { .then(function(supplier) {
should.exist(supplier); should.exist(supplier);
return supplier.account.getAsync(); return supplier.account.get();
}) })
.then(function(act) { .then(function(act) {
should.exist(act); should.exist(act);
@ -3627,7 +3627,7 @@ describe('relations', function() {
Supplier.findById(supplierId) Supplier.findById(supplierId)
.then(function(supplier) { .then(function(supplier) {
should.exist(supplier); should.exist(supplier);
return supplier.account.getAsync(); return supplier.account.get();
}) })
.then(function(act) { .then(function(act) {
should.not.exist(act); should.not.exist(act);
@ -3723,7 +3723,7 @@ describe('relations', function() {
should.exist(supplier); should.exist(supplier);
return supplier.account.create({accountNo: 'a01', block: false}) return supplier.account.create({accountNo: 'a01', block: false})
.then(function(account) { .then(function(account) {
return supplier.account.getAsync(); return supplier.account.get();
}) })
.then(function(act) { .then(function(act) {
accountId = act.id; accountId = act.id;
@ -3747,7 +3747,7 @@ describe('relations', function() {
return Supplier.findById(supplierId); return Supplier.findById(supplierId);
}) })
.then(function(supplier) { .then(function(supplier) {
return supplier.account.getAsync(); return supplier.account.get();
}) })
.then(function(account) { .then(function(account) {
should.not.exist(account); should.not.exist(account);
@ -3963,7 +3963,7 @@ describe('relations', function() {
return Employee.create({name: 'a01', companyId: COMPANY_ID}) return Employee.create({name: 'a01', companyId: COMPANY_ID})
.then(function(employee) { .then(function(employee) {
should.exist(employee); should.exist(employee);
return boss.employees.getAsync(); return boss.employees.find();
}).then(function(employees) { }).then(function(employees) {
should.exists(employees); should.exists(employees);
employees.length.should.equal(1); employees.length.should.equal(1);
@ -4000,7 +4000,7 @@ describe('relations', function() {
}) })
.then(function(employee) { .then(function(employee) {
should.exists(employee); should.exists(employee);
return employee.boss.getAsync(); return employee.boss.get();
}) })
.then(function(boss) { .then(function(boss) {
should.exists(boss); should.exists(boss);
@ -4101,7 +4101,7 @@ describe('relations', function() {
it('should allow to fetch scoped instances with promises', function(done) { it('should allow to fetch scoped instances with promises', function(done) {
Article.findOne() Article.findOne()
.then(function(article) { .then(function(article) {
return article.tagNames.getAsync() return article.tagNames.find()
.then(function(tags) { .then(function(tags) {
should.exist(tags); should.exist(tags);
article.tagNames().should.eql(tags); article.tagNames().should.eql(tags);
@ -4132,13 +4132,13 @@ describe('relations', function() {
'should allow to remove connection with instance with promises', function(done) { 'should allow to remove connection with instance with promises', function(done) {
Article.findOne() Article.findOne()
.then(function(article) { .then(function(article) {
return article.tagNames.getAsync() return article.tagNames.find()
.then(function(tags) { .then(function(tags) {
var len = tags.length; var len = tags.length;
tags.should.not.be.empty; tags.should.not.be.empty;
return article.tagNames.remove(tags[0]) return article.tagNames.remove(tags[0])
.then(function() { .then(function() {
return article.tagNames.getAsync(); return article.tagNames.find();
}) })
.then(function(tags) { .then(function(tags) {
tags.should.have.lengthOf(len - 1); tags.should.have.lengthOf(len - 1);
@ -5930,7 +5930,7 @@ describe('relations', function() {
Category.findOne() Category.findOne()
.then(function(cat) { .then(function(cat) {
cat.jobIds.should.eql([job2.id]); cat.jobIds.should.eql([job2.id]);
return cat.jobs.getAsync(); return cat.jobs.find();
}) })
.then(function(jobs) { .then(function(jobs) {
var p = jobs[0]; var p = jobs[0];
@ -5948,7 +5948,7 @@ describe('relations', function() {
Category.findOne() Category.findOne()
.then(function(cat) { .then(function(cat) {
cat.jobIds[0].should.be.oneOf(theExpectedIds); cat.jobIds[0].should.be.oneOf(theExpectedIds);
return cat.jobs.getAsync(); return cat.jobs.find();
}) })
.then(function(jobs) { .then(function(jobs) {
var p = jobs[0]; var p = jobs[0];
@ -6062,7 +6062,7 @@ describe('relations', function() {
Category.findOne() Category.findOne()
.then(function(cat) { .then(function(cat) {
var filter = {where: {name: 'Job 1'}}; var filter = {where: {name: 'Job 1'}};
return cat.jobs.getAsync(filter); return cat.jobs.find(filter);
}) })
.then(function(jobs) { .then(function(jobs) {
jobs.should.have.length(1); jobs.should.have.length(1);
@ -6095,7 +6095,7 @@ describe('relations', function() {
var expected = [job2.id, job3.id]; var expected = [job2.id, job3.id];
cat.jobIds.should.have.lengthOf(expected.length); cat.jobIds.should.have.lengthOf(expected.length);
cat.jobIds.should.containDeep(expected); cat.jobIds.should.containDeep(expected);
return cat.jobs.getAsync(); return cat.jobs.find();
}) })
.then(function(jobs) { .then(function(jobs) {
jobs.should.have.length(2); jobs.should.have.length(2);
@ -6111,7 +6111,7 @@ describe('relations', function() {
Category.find() Category.find()
.then(function(categories) { .then(function(categories) {
categories.should.have.length(1); categories.should.have.length(1);
return categories[0].jobs.getAsync({order: 'name DESC'}); return categories[0].jobs.find({order: 'name DESC'});
}) })
.then(function(jobs) { .then(function(jobs) {
jobs.should.have.length(2); jobs.should.have.length(2);
@ -6180,7 +6180,7 @@ describe('relations', function() {
var expected = [job3.id]; var expected = [job3.id];
cat.jobIds.should.have.lengthOf(expected.length); cat.jobIds.should.have.lengthOf(expected.length);
cat.jobIds.should.containDeep(expected); cat.jobIds.should.containDeep(expected);
return cat.jobs.getAsync(); return cat.jobs.find();
}) })
.then(function(jobs) { .then(function(jobs) {
jobs.should.have.length(1); jobs.should.have.length(1);