不要怂,就是干,撸起袖子干!

Commit 39c35012 by Andy Edwards Committed by GitHub

refactor(associations): asyncify methods (#12123)

1 parent 5a1472ba
......@@ -608,13 +608,12 @@ class BelongsToMany extends Association {
}
if (obsoleteAssociations.length > 0) {
const where = Object.assign({
[identifier]: sourceInstance.get(sourceKey),
[foreignIdentifier]: obsoleteAssociations.map(obsoleteAssociation => obsoleteAssociation[foreignIdentifier])
}, this.through.scope);
promises.push(
this.through.model.destroy(_.defaults({
where
where: Object.assign({
[identifier]: sourceInstance.get(sourceKey),
[foreignIdentifier]: obsoleteAssociations.map(obsoleteAssociation => obsoleteAssociation[foreignIdentifier])
}, this.through.scope)
}, options))
);
}
......@@ -724,13 +723,11 @@ class BelongsToMany extends Association {
if (throughAttributes instanceof association.through.model) {
throughAttributes = {};
}
const where = {
promises.push(association.through.model.update(attributes, Object.assign(options, { where: {
[identifier]: sourceInstance.get(sourceKey),
[foreignIdentifier]: assoc.get(targetKey)
};
promises.push(association.through.model.update(attributes, Object.assign(options, { where })));
} })));
}
return Utils.Promise.all(promises);
......
......@@ -123,7 +123,7 @@ class BelongsTo extends Association {
*
* @returns {Promise<Model>}
*/
get(instances, options) {
async get(instances, options) {
const where = {};
let Target = this.target;
let instance;
......@@ -149,7 +149,7 @@ class BelongsTo extends Association {
if (instances) {
where[this.targetKey] = {
[Op.in]: instances.map(instance => instance.get(this.foreignKey))
[Op.in]: instances.map(_instance => _instance.get(this.foreignKey))
};
} else {
if (this.targetKeyIsPrimary && !options.where) {
......@@ -164,18 +164,17 @@ class BelongsTo extends Association {
where;
if (instances) {
return Target.findAll(options).then(results => {
const result = {};
for (const instance of instances) {
result[instance.get(this.foreignKey, { raw: true })] = null;
}
for (const instance of results) {
result[instance.get(this.targetKey, { raw: true })] = instance;
}
return result;
});
const results = await Target.findAll(options);
const result = {};
for (const _instance of instances) {
result[_instance.get(this.foreignKey, { raw: true })] = null;
}
for (const _instance of results) {
result[_instance.get(this.targetKey, { raw: true })] = _instance;
}
return result;
}
return Target.findOne(options);
......@@ -191,7 +190,7 @@ class BelongsTo extends Association {
*
* @returns {Promise}
*/
set(sourceInstance, associatedInstance, options = {}) {
async set(sourceInstance, associatedInstance, options = {}) {
let value = associatedInstance;
if (associatedInstance instanceof this.target) {
......@@ -209,7 +208,7 @@ class BelongsTo extends Association {
}, options);
// passes the changed field to save, so only that field get updated.
return sourceInstance.save(options);
return await sourceInstance.save(options);
}
/**
......@@ -224,14 +223,14 @@ class BelongsTo extends Association {
*
* @returns {Promise<Model>} The created target model
*/
create(sourceInstance, values, options) {
async create(sourceInstance, values, options) {
values = values || {};
options = options || {};
return this.target.create(values, options)
.then(newAssociatedObject => sourceInstance[this.accessors.set](newAssociatedObject, options)
.then(() => newAssociatedObject)
);
const newAssociatedObject = await this.target.create(values, options);
await sourceInstance[this.accessors.set](newAssociatedObject, options);
return newAssociatedObject;
}
verifyAssociationAlias(alias) {
......
......@@ -168,7 +168,7 @@ class HasMany extends Association {
*
* @returns {Promise<Array<Model>>}
*/
get(instances, options = {}) {
async get(instances, options = {}) {
const where = {};
let Model = this.target;
......@@ -187,7 +187,7 @@ class HasMany extends Association {
}
if (instances) {
values = instances.map(instance => instance.get(this.sourceKey, { raw: true }));
values = instances.map(_instance => _instance.get(this.sourceKey, { raw: true }));
if (options.limit && instances.length > 1) {
options.groupedLimit = {
......@@ -223,20 +223,19 @@ class HasMany extends Association {
Model = Model.schema(options.schema, options.schemaDelimiter);
}
return Model.findAll(options).then(results => {
if (instance) return results;
const results = await Model.findAll(options);
if (instance) return results;
const result = {};
for (const instance of instances) {
result[instance.get(this.sourceKey, { raw: true })] = [];
}
const result = {};
for (const _instance of instances) {
result[_instance.get(this.sourceKey, { raw: true })] = [];
}
for (const instance of results) {
result[instance.get(this.foreignKey, { raw: true })].push(instance);
}
for (const _instance of results) {
result[_instance.get(this.foreignKey, { raw: true })].push(_instance);
}
return result;
});
return result;
}
/**
......@@ -249,7 +248,7 @@ class HasMany extends Association {
*
* @returns {Promise<number>}
*/
count(instance, options) {
async count(instance, options) {
options = Utils.cloneDeep(options);
options.attributes = [
......@@ -264,7 +263,9 @@ class HasMany extends Association {
options.raw = true;
options.plain = true;
return this.get(instance, options).then(result => parseInt(result.count, 10));
const result = await this.get(instance, options);
return parseInt(result.count, 10);
}
/**
......@@ -276,7 +277,7 @@ class HasMany extends Association {
*
* @returns {Promise}
*/
has(sourceInstance, targetInstances, options) {
async has(sourceInstance, targetInstances, options) {
const where = {};
if (!Array.isArray(targetInstances)) {
......@@ -305,7 +306,9 @@ class HasMany extends Association {
]
};
return this.get(sourceInstance, options).then(associatedObjects => associatedObjects.length === targetInstances.length);
const associatedObjects = await this.get(sourceInstance, options);
return associatedObjects.length === targetInstances.length;
}
/**
......@@ -318,68 +321,69 @@ class HasMany extends Association {
*
* @returns {Promise}
*/
set(sourceInstance, targetInstances, options) {
async set(sourceInstance, targetInstances, options) {
if (targetInstances === null) {
targetInstances = [];
} else {
targetInstances = this.toInstanceArray(targetInstances);
}
return this.get(sourceInstance, _.defaults({ scope: false, raw: true }, options)).then(oldAssociations => {
const promises = [];
const obsoleteAssociations = oldAssociations.filter(old =>
!targetInstances.find(obj =>
obj[this.target.primaryKeyAttribute] === old[this.target.primaryKeyAttribute]
)
);
const unassociatedObjects = targetInstances.filter(obj =>
!oldAssociations.find(old =>
obj[this.target.primaryKeyAttribute] === old[this.target.primaryKeyAttribute]
)
);
let updateWhere;
let update;
const oldAssociations = await this.get(sourceInstance, _.defaults({ scope: false, raw: true }, options));
const promises = [];
const obsoleteAssociations = oldAssociations.filter(old =>
!targetInstances.find(obj =>
obj[this.target.primaryKeyAttribute] === old[this.target.primaryKeyAttribute]
)
);
const unassociatedObjects = targetInstances.filter(obj =>
!oldAssociations.find(old =>
obj[this.target.primaryKeyAttribute] === old[this.target.primaryKeyAttribute]
)
);
let updateWhere;
let update;
if (obsoleteAssociations.length > 0) {
update = {};
update[this.foreignKey] = null;
if (obsoleteAssociations.length > 0) {
update = {};
update[this.foreignKey] = null;
updateWhere = {
[this.target.primaryKeyAttribute]: obsoleteAssociations.map(associatedObject =>
associatedObject[this.target.primaryKeyAttribute]
)
};
updateWhere = {
[this.target.primaryKeyAttribute]: obsoleteAssociations.map(associatedObject =>
associatedObject[this.target.primaryKeyAttribute]
)
};
promises.push(this.target.unscoped().update(
update,
_.defaults({
where: updateWhere
}, options)
));
}
promises.push(this.target.unscoped().update(
update,
_.defaults({
where: updateWhere
}, options)
));
}
if (unassociatedObjects.length > 0) {
updateWhere = {};
if (unassociatedObjects.length > 0) {
updateWhere = {};
update = {};
update[this.foreignKey] = sourceInstance.get(this.sourceKey);
update = {};
update[this.foreignKey] = sourceInstance.get(this.sourceKey);
Object.assign(update, this.scope);
updateWhere[this.target.primaryKeyAttribute] = unassociatedObjects.map(unassociatedObject =>
unassociatedObject[this.target.primaryKeyAttribute]
);
Object.assign(update, this.scope);
updateWhere[this.target.primaryKeyAttribute] = unassociatedObjects.map(unassociatedObject =>
unassociatedObject[this.target.primaryKeyAttribute]
);
promises.push(this.target.unscoped().update(
update,
_.defaults({
where: updateWhere
}, options)
));
}
promises.push(this.target.unscoped().update(
update,
_.defaults({
where: updateWhere
}, options)
));
}
return Utils.Promise.all(promises).then(() => sourceInstance);
});
await Utils.Promise.all(promises);
return sourceInstance;
}
/**
......@@ -392,7 +396,7 @@ class HasMany extends Association {
*
* @returns {Promise}
*/
add(sourceInstance, targetInstances, options = {}) {
async add(sourceInstance, targetInstances, options = {}) {
if (!targetInstances) return Utils.Promise.resolve();
const update = {};
......@@ -408,7 +412,9 @@ class HasMany extends Association {
)
};
return this.target.unscoped().update(update, _.defaults({ where }, options)).then(() => sourceInstance);
await this.target.unscoped().update(update, _.defaults({ where }, options));
return sourceInstance;
}
/**
......@@ -420,7 +426,7 @@ class HasMany extends Association {
*
* @returns {Promise}
*/
remove(sourceInstance, targetInstances, options = {}) {
async remove(sourceInstance, targetInstances, options = {}) {
const update = {
[this.foreignKey]: null
};
......@@ -434,7 +440,9 @@ class HasMany extends Association {
)
};
return this.target.unscoped().update(update, _.defaults({ where }, options)).then(() => this);
await this.target.unscoped().update(update, _.defaults({ where }, options));
return this;
}
/**
......@@ -446,7 +454,7 @@ class HasMany extends Association {
*
* @returns {Promise}
*/
create(sourceInstance, values, options = {}) {
async create(sourceInstance, values, options = {}) {
if (Array.isArray(options)) {
options = {
fields: options
......@@ -466,7 +474,7 @@ class HasMany extends Association {
values[this.foreignKey] = sourceInstance.get(this.sourceKey);
if (options.fields) options.fields.push(this.foreignKey);
return this.target.create(values, options);
return await this.target.create(values, options);
}
verifyAssociationAlias(alias) {
......
......@@ -122,7 +122,7 @@ class HasOne extends Association {
*
* @returns {Promise<Model>}
*/
get(instances, options) {
async get(instances, options) {
const where = {};
let Target = this.target;
......@@ -149,7 +149,7 @@ class HasOne extends Association {
if (instances) {
where[this.foreignKey] = {
[Op.in]: instances.map(instance => instance.get(this.sourceKey))
[Op.in]: instances.map(_instance => _instance.get(this.sourceKey))
};
} else {
where[this.foreignKey] = instance.get(this.sourceKey);
......@@ -164,18 +164,17 @@ class HasOne extends Association {
where;
if (instances) {
return Target.findAll(options).then(results => {
const result = {};
for (const instance of instances) {
result[instance.get(this.sourceKey, { raw: true })] = null;
}
const results = await Target.findAll(options);
const result = {};
for (const _instance of instances) {
result[_instance.get(this.sourceKey, { raw: true })] = null;
}
for (const instance of results) {
result[instance.get(this.foreignKey, { raw: true })] = instance;
}
for (const _instance of results) {
result[_instance.get(this.foreignKey, { raw: true })] = _instance;
}
return result;
});
return result;
}
return Target.findOne(options);
......@@ -190,45 +189,42 @@ class HasOne extends Association {
*
* @returns {Promise}
*/
set(sourceInstance, associatedInstance, options) {
let alreadyAssociated;
async set(sourceInstance, associatedInstance, options) {
options = Object.assign({}, options, {
scope: false
});
return sourceInstance[this.accessors.get](options).then(oldInstance => {
// TODO Use equals method once #5605 is resolved
alreadyAssociated = oldInstance && associatedInstance && this.target.primaryKeyAttributes.every(attribute =>
oldInstance.get(attribute, { raw: true }) === (associatedInstance.get ? associatedInstance.get(attribute, { raw: true }) : associatedInstance)
);
const oldInstance = await sourceInstance[this.accessors.get](options);
// TODO Use equals method once #5605 is resolved
const alreadyAssociated = oldInstance && associatedInstance && this.target.primaryKeyAttributes.every(attribute =>
oldInstance.get(attribute, { raw: true }) === (associatedInstance.get ? associatedInstance.get(attribute, { raw: true }) : associatedInstance)
);
if (oldInstance && !alreadyAssociated) {
oldInstance[this.foreignKey] = null;
return oldInstance.save(Object.assign({}, options, {
fields: [this.foreignKey],
allowNull: [this.foreignKey],
association: true
}));
if (oldInstance && !alreadyAssociated) {
oldInstance[this.foreignKey] = null;
await oldInstance.save(Object.assign({}, options, {
fields: [this.foreignKey],
allowNull: [this.foreignKey],
association: true
}));
}
if (associatedInstance && !alreadyAssociated) {
if (!(associatedInstance instanceof this.target)) {
const tmpInstance = {};
tmpInstance[this.target.primaryKeyAttribute] = associatedInstance;
associatedInstance = this.target.build(tmpInstance, {
isNewRecord: false
});
}
}).then(() => {
if (associatedInstance && !alreadyAssociated) {
if (!(associatedInstance instanceof this.target)) {
const tmpInstance = {};
tmpInstance[this.target.primaryKeyAttribute] = associatedInstance;
associatedInstance = this.target.build(tmpInstance, {
isNewRecord: false
});
}
Object.assign(associatedInstance, this.scope);
associatedInstance.set(this.foreignKey, sourceInstance.get(this.sourceKeyAttribute));
Object.assign(associatedInstance, this.scope);
associatedInstance.set(this.foreignKey, sourceInstance.get(this.sourceKeyAttribute));
return associatedInstance.save(options);
}
return associatedInstance.save(options);
}
return null;
});
return null;
}
/**
......@@ -243,7 +239,7 @@ class HasOne extends Association {
*
* @returns {Promise<Model>} The created target model
*/
create(sourceInstance, values, options) {
async create(sourceInstance, values, options) {
values = values || {};
options = options || {};
......@@ -261,7 +257,7 @@ class HasOne extends Association {
options.fields.push(this.foreignKey);
}
return this.target.create(values, options);
return await this.target.create(values, options);
}
verifyAssociationAlias(alias) {
......
......@@ -369,10 +369,10 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
return Promise.join(
Post.create(),
Comment.create()
).then(([post, comment]) => {
).then(async ([post, comment]) => {
expect(comment.get('post_id')).not.to.be.ok;
const setter = comment.setPost(post, { save: false });
const setter = await comment.setPost(post, { save: false });
expect(setter).to.be.undefined;
expect(comment.get('post_id')).to.equal(post.get('id'));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!