mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 06:55:50 +00:00
fix: duplicated items in update associations (#4393)
* fix: data template middleware in data source * fix: typo * fix: duplicated items in update associations
This commit is contained in:
parent
299c5a14cb
commit
144338be90
@ -240,6 +240,28 @@ describe('update associations', () => {
|
||||
afterEach(async () => {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('should update association values', async () => {
|
||||
const user1 = await User.repository.create({
|
||||
values: {
|
||||
name: 'u1',
|
||||
posts: [{ name: 'u1t1' }],
|
||||
},
|
||||
});
|
||||
|
||||
// update with associations
|
||||
const updateRes = await User.repository.update({
|
||||
filterByTk: user1.get('id'),
|
||||
values: {
|
||||
name: 'u1',
|
||||
posts: [{ id: user1.get('posts')[0].get('id'), name: 'u1t1' }],
|
||||
},
|
||||
updateAssociationValues: ['comments'],
|
||||
});
|
||||
|
||||
expect(updateRes[0].toJSON()['posts'].length).toBe(1);
|
||||
});
|
||||
|
||||
it('user.posts', async () => {
|
||||
await User.model.create<any>({ name: 'user01' });
|
||||
await User.model.create<any>({ name: 'user02' });
|
||||
|
@ -538,9 +538,20 @@ export async function updateMultipleAssociation(
|
||||
associationContext: association,
|
||||
updateAssociationValues: keys,
|
||||
});
|
||||
|
||||
newItems.push(instance);
|
||||
}
|
||||
}
|
||||
|
||||
model.setDataValue(key, setItems.concat(newItems));
|
||||
for (const newItem of newItems) {
|
||||
const existIndexInSetItems = setItems.findIndex((setItem) => setItem[targetKey] === newItem[targetKey]);
|
||||
|
||||
if (existIndexInSetItems !== -1) {
|
||||
setItems[existIndexInSetItems] = newItem;
|
||||
} else {
|
||||
setItems.push(newItem);
|
||||
}
|
||||
}
|
||||
|
||||
model.setDataValue(key, setItems);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user