fix(core): fix batch update query logic (#2230)
This commit is contained in:
parent
1fb15de29a
commit
ff7b1aaf71
@ -596,6 +596,37 @@ describe('repository.update', () => {
|
||||
});
|
||||
expect(p1Updated2.userId).toBe(u1.id);
|
||||
});
|
||||
|
||||
it('update in batch filtered by belongsTo field as deep association field', async () => {
|
||||
const u1 = await User.repository.create({ values: { name: 'u1' } });
|
||||
const u2 = await User.repository.create({ values: { name: 'u2' } });
|
||||
const p1 = await Post.repository.create({ values: { name: 'p1', userId: u1.id } });
|
||||
const p2 = await Post.repository.create({ values: { name: 'p2', userId: u1.id } });
|
||||
const p3 = await Post.repository.create({ values: { name: 'p3', userId: u2.id } });
|
||||
|
||||
const r1 = await Post.repository.update({
|
||||
filter: {
|
||||
user: {
|
||||
id: {
|
||||
$eq: u1.id
|
||||
},
|
||||
},
|
||||
},
|
||||
values: {
|
||||
name: 'p1_1',
|
||||
},
|
||||
individualHooks: false,
|
||||
});
|
||||
|
||||
expect(r1).toEqual(2);
|
||||
|
||||
const updated = await Post.repository.find({
|
||||
filter: {
|
||||
id: [p1.id, p2.id],
|
||||
}
|
||||
});
|
||||
expect(updated.map(item => item.name)).toEqual(['p1_1', 'p1_1']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('repository.destroy', () => {
|
||||
|
@ -607,8 +607,6 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
|
||||
const values = guard.sanitize(options.values);
|
||||
|
||||
const queryOptions = this.buildQueryOptions(options);
|
||||
|
||||
// NOTE:
|
||||
// 1. better to be moved to separated API like bulkUpdate/updateMany
|
||||
// 2. strictly `false` comparing for compatibility of legacy api invoking
|
||||
@ -620,16 +618,12 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
// 1. find ids first for reusing `queryOptions` logic
|
||||
// 2. estimation memory usage will be N * M bytes (N = rows, M = model object memory)
|
||||
// 3. would be more efficient up to 100000 ~ 1000000 rows
|
||||
const rows = await Model.findAll({
|
||||
const queryOptions = this.buildQueryOptions({
|
||||
...options,
|
||||
fields: [primaryKeyField],
|
||||
});
|
||||
const rows = await this.find({
|
||||
...queryOptions,
|
||||
attributes: [primaryKeyField],
|
||||
group: `${Model.name}.${primaryKeyField}`,
|
||||
include: queryOptions.include.filter((include) => {
|
||||
return (
|
||||
Object.keys(include.where || {}).length > 0 ||
|
||||
JSON.stringify(queryOptions?.filter)?.includes(include.association)
|
||||
);
|
||||
}),
|
||||
transaction,
|
||||
});
|
||||
const [result] = await Model.update(values, {
|
||||
@ -648,6 +642,8 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
|
||||
return result;
|
||||
}
|
||||
|
||||
const queryOptions = this.buildQueryOptions(options);
|
||||
|
||||
const instances = await this.find({
|
||||
...queryOptions,
|
||||
transaction,
|
||||
|
Loading…
Reference in New Issue
Block a user