allow connecting the same id multiple times

This commit is contained in:
Marc-Roig 2022-11-25 16:19:04 +01:00
parent 15460a60fb
commit c73971f9fd
2 changed files with 27 additions and 10 deletions

View File

@ -19,6 +19,7 @@ const {
isNumber,
map,
difference,
uniqBy,
} = require('lodash/fp');
const types = require('../types');
const { createField } = require('../fields');
@ -569,7 +570,7 @@ const createEntityManager = (db) => {
}
// prepare new relations to insert
const insert = relsToAdd.map((data) => {
const insert = uniqBy('id', relsToAdd).map((data) => {
return {
[joinColumn.name]: id,
[inverseJoinColumn.name]: data.id,
@ -623,13 +624,7 @@ const createEntityManager = (db) => {
}
// insert new relations
// ignore duplicates, as connect syntax can contain duplicated ids to add
await this.createQueryBuilder(joinTable.name)
.insert(insert)
.onConflict(joinTable.pivotColumns)
.ignore()
.transacting(trx)
.execute();
await this.createQueryBuilder(joinTable.name).insert(insert).transacting(trx).execute();
}
}
},
@ -841,7 +836,7 @@ const createEntityManager = (db) => {
}
// prepare relations to insert
const insert = cleanRelationData.connect.map((relToAdd) => ({
const insert = uniqBy('id', cleanRelationData.connect).map((relToAdd) => ({
[joinColumn.name]: id,
[inverseJoinColumn.name]: relToAdd.id,
...(joinTable.on || {}),
@ -953,7 +948,7 @@ const createEntityManager = (db) => {
continue;
}
const insert = cleanRelationData.set.map((relToAdd) => ({
const insert = uniqBy('id', cleanRelationData.set).map((relToAdd) => ({
[joinColumn.name]: id,
[inverseJoinColumn.name]: relToAdd.id,
...(joinTable.on || {}),

View File

@ -832,4 +832,26 @@ describe('Relations', () => {
expect(updatedShop).toMatchObject(expectedShop);
});
});
test.only('Update relations using the same id multiple times', async () => {
const shop = await createShop({
anyToManyRel: [
{ id: id1, position: { end: true } },
{ id: id2, position: { end: true } },
],
});
const updatedShop = await updateShop(shop, {
anyToManyRel: [
{ id: id1, position: { end: true } },
{ id: id1, position: { start: true } },
{ id: id1, position: { after: id2 } },
],
});
const expectedShop = shopFactory({
anyToManyRel: [{ id: id2 }, { id: id1 }],
});
expect(updatedShop).toMatchObject(expectedShop);
});
});