2022-09-16 18:38:46 +02:00
|
|
|
'use strict';
|
|
|
|
|
2022-09-26 16:22:22 +02:00
|
|
|
const { map, isEmpty } = require('lodash/fp');
|
2022-09-16 18:38:46 +02:00
|
|
|
const {
|
|
|
|
isBidirectional,
|
|
|
|
isOneToAny,
|
|
|
|
isManyToAny,
|
|
|
|
isAnyToOne,
|
2022-09-27 11:56:46 +02:00
|
|
|
hasOrderColumn,
|
|
|
|
hasInverseOrderColumn,
|
2022-09-16 18:38:46 +02:00
|
|
|
} = require('../metadata/relations');
|
|
|
|
const { createQueryBuilder } = require('../query');
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
/**
|
|
|
|
* If some relations currently exist for this oneToX relation, on the one side, this function removes them and update the inverse order if needed.
|
|
|
|
* @param {Object} params
|
|
|
|
* @param {string} params.id - entity id on which the relations for entities relIdsToadd are created
|
|
|
|
* @param {string} params.attribute - attribute of the relation
|
|
|
|
* @param {string} params.inverseRelIds - entity ids of the inverse side for which the current relations will be deleted
|
|
|
|
* @param {string} params.db - database instance
|
|
|
|
*/
|
|
|
|
const deletePreviousOneToAnyRelations = async ({ id, attribute, relIdsToadd, db }) => {
|
|
|
|
const { joinTable } = attribute;
|
2022-09-26 16:22:22 +02:00
|
|
|
const { joinColumn, inverseJoinColumn } = joinTable;
|
2022-09-16 18:38:46 +02:00
|
|
|
|
|
|
|
// need to delete the previous relations for oneToAny relations
|
|
|
|
if (isBidirectional(attribute) && isOneToAny(attribute)) {
|
|
|
|
// delete previous oneToAny relations
|
|
|
|
await createQueryBuilder(joinTable.name, db)
|
|
|
|
.delete()
|
|
|
|
.where({
|
|
|
|
[inverseJoinColumn.name]: relIdsToadd,
|
|
|
|
[joinColumn.name]: { $ne: id },
|
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.execute();
|
2022-09-26 16:22:22 +02:00
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
await cleanOrderColumns({ attribute, db, inverseRelIds: relIdsToadd });
|
2022-09-16 18:38:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
/**
|
|
|
|
* If a relation currently exists for this xToOne relations, this function removes it and update the inverse order if needed.
|
|
|
|
* @param {Object} params
|
|
|
|
* @param {string} params.id - entity id on which the relation for entity relIdToadd is created
|
|
|
|
* @param {string} params.attribute - attribute of the relation
|
|
|
|
* @param {string} params.relIdToadd - entity id of the new relation
|
|
|
|
* @param {string} params.db - database instance
|
|
|
|
*/
|
|
|
|
const deletePreviousAnyToOneRelations = async ({ id, attribute, relIdToadd, db }) => {
|
|
|
|
const { joinTable } = attribute;
|
2022-09-26 16:22:22 +02:00
|
|
|
const { joinColumn, inverseJoinColumn } = joinTable;
|
2022-09-16 18:38:46 +02:00
|
|
|
|
|
|
|
// Delete the previous relations for anyToOne relations
|
|
|
|
if (isBidirectional(attribute) && isAnyToOne(attribute)) {
|
|
|
|
// update orders for previous anyToOne relations that will be deleted if it has order (manyToOne)
|
2022-09-27 11:56:46 +02:00
|
|
|
|
|
|
|
// handling manyToOne
|
2022-09-16 18:38:46 +02:00
|
|
|
if (isManyToAny(attribute)) {
|
2022-09-26 16:22:22 +02:00
|
|
|
// if the database integrity was not broken relsToDelete is supposed to be of length 1
|
|
|
|
const relsToDelete = await createQueryBuilder(joinTable.name, db)
|
|
|
|
.select(inverseJoinColumn.name)
|
2022-09-16 18:38:46 +02:00
|
|
|
.where({
|
|
|
|
[joinColumn.name]: id,
|
2022-09-26 16:22:22 +02:00
|
|
|
[inverseJoinColumn.name]: { $ne: relIdToadd },
|
2022-09-16 18:38:46 +02:00
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.execute();
|
|
|
|
|
2022-09-26 16:22:22 +02:00
|
|
|
const relIdsToDelete = map(inverseJoinColumn.name, relsToDelete);
|
2022-09-16 18:38:46 +02:00
|
|
|
|
2022-09-26 16:22:22 +02:00
|
|
|
await createQueryBuilder(joinTable.name, db)
|
|
|
|
.delete()
|
|
|
|
.where({
|
|
|
|
[joinColumn.name]: id,
|
|
|
|
[inverseJoinColumn.name]: { $in: relIdsToDelete },
|
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.execute();
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
await cleanOrderColumns({ attribute, db, inverseRelIds: relIdsToDelete });
|
|
|
|
|
|
|
|
// handling oneToOne
|
2022-09-26 16:22:22 +02:00
|
|
|
} else {
|
|
|
|
await createQueryBuilder(joinTable.name, db)
|
|
|
|
.delete()
|
|
|
|
.where({
|
|
|
|
[joinColumn.name]: id,
|
|
|
|
[inverseJoinColumn.name]: { $ne: relIdToadd },
|
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.execute();
|
|
|
|
}
|
2022-09-16 18:38:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
/**
|
|
|
|
* Delete all or some relations of entity field
|
|
|
|
* @param {Object} params
|
|
|
|
* @param {string} params.id - entity id for which the relations will be deleted
|
|
|
|
* @param {string} params.attribute - attribute of the relation
|
|
|
|
* @param {string} params.db - database instance
|
|
|
|
* @param {string} params.relIdsToDelete - ids of entities to remove from the relations. Also accepts 'all'
|
|
|
|
* @param {string} params.relIdsToNotDelete - ids of entities to not remove from the relation when relIdsToDelete equals 'all'
|
|
|
|
*/
|
|
|
|
const deleteRelations = async ({
|
|
|
|
id,
|
|
|
|
attribute,
|
|
|
|
db,
|
|
|
|
relIdsToNotDelete = [],
|
|
|
|
relIdsToDelete = [],
|
|
|
|
}) => {
|
|
|
|
const { joinTable } = attribute;
|
2022-09-26 16:22:22 +02:00
|
|
|
const { joinColumn, inverseJoinColumn } = joinTable;
|
2022-09-20 15:53:17 +02:00
|
|
|
const all = relIdsToDelete === 'all';
|
2022-09-16 18:38:46 +02:00
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
if (hasOrderColumn(attribute) || hasInverseOrderColumn(attribute)) {
|
2022-09-16 18:38:46 +02:00
|
|
|
let lastId = 0;
|
|
|
|
let done = false;
|
|
|
|
const batchSize = 100;
|
|
|
|
while (!done) {
|
2022-09-26 16:22:22 +02:00
|
|
|
const batchToDelete = await createQueryBuilder(joinTable.name, db)
|
|
|
|
.select(inverseJoinColumn.name)
|
2022-09-16 18:38:46 +02:00
|
|
|
.where({
|
|
|
|
[joinColumn.name]: id,
|
|
|
|
id: { $gt: lastId },
|
2022-09-20 15:53:17 +02:00
|
|
|
[inverseJoinColumn.name]: { $notIn: relIdsToNotDelete },
|
|
|
|
...(all ? {} : { [inverseJoinColumn.name]: { $in: relIdsToDelete } }),
|
2022-09-16 18:38:46 +02:00
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.orderBy('id')
|
|
|
|
.limit(batchSize)
|
|
|
|
.execute();
|
2022-09-26 16:22:22 +02:00
|
|
|
done = batchToDelete.length < batchSize;
|
|
|
|
lastId = batchToDelete[batchToDelete.length - 1]?.id;
|
|
|
|
|
|
|
|
const batchIds = map(inverseJoinColumn.name, batchToDelete);
|
|
|
|
|
|
|
|
await createQueryBuilder(joinTable.name, db)
|
|
|
|
.delete()
|
|
|
|
.where({
|
|
|
|
[joinColumn.name]: id,
|
|
|
|
[inverseJoinColumn.name]: { $in: batchIds },
|
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.execute();
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
await cleanOrderColumns({ attribute, db, id, inverseRelIds: batchIds });
|
2022-09-16 18:38:46 +02:00
|
|
|
}
|
2022-09-26 16:22:22 +02:00
|
|
|
} else {
|
|
|
|
await createQueryBuilder(joinTable.name, db)
|
|
|
|
.delete()
|
|
|
|
.where({
|
|
|
|
[joinColumn.name]: id,
|
|
|
|
[inverseJoinColumn.name]: { $notIn: relIdsToNotDelete },
|
|
|
|
...(all ? {} : { [inverseJoinColumn.name]: { $in: relIdsToDelete } }),
|
|
|
|
})
|
|
|
|
.where(joinTable.on || {})
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean the order columns by ensuring the order value are continuous (ex: 1, 2, 3 and not 1, 5, 10)
|
|
|
|
* @param {Object} params
|
2022-09-27 11:56:46 +02:00
|
|
|
* @param {string} params.id - entity id for which the clean will be done
|
|
|
|
* @param {string} params.attribute - attribute of the relation
|
|
|
|
* @param {string} params.db - database instance
|
|
|
|
* @param {string} params.inverseRelIds - entity ids of the inverse side for which the clean will be done
|
2022-09-26 16:22:22 +02:00
|
|
|
*/
|
2022-09-27 11:56:46 +02:00
|
|
|
const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds }) => {
|
2022-09-26 16:22:22 +02:00
|
|
|
if (
|
2022-09-27 11:56:46 +02:00
|
|
|
!(hasOrderColumn(attribute) && id) &&
|
|
|
|
!(hasInverseOrderColumn(attribute) && !isEmpty(inverseRelIds))
|
2022-09-26 16:22:22 +02:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
const { joinTable } = attribute;
|
2022-09-26 16:22:22 +02:00
|
|
|
const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
|
2022-09-27 11:56:46 +02:00
|
|
|
const update = [];
|
|
|
|
const updateBinding = [];
|
|
|
|
const select = ['??'];
|
|
|
|
const selectBinding = ['id'];
|
|
|
|
const where = [];
|
|
|
|
const whereBinding = [];
|
|
|
|
|
|
|
|
if (hasOrderColumn(attribute) && id) {
|
|
|
|
update.push('?? = t.src_order');
|
|
|
|
updateBinding.push(orderColumnName);
|
|
|
|
select.push('ROW_NUMBER() OVER (PARTITION BY ?? ORDER BY ??) AS src_order');
|
|
|
|
selectBinding.push(joinColumn.name, orderColumnName);
|
|
|
|
where.push('?? = ?');
|
|
|
|
whereBinding.push(joinColumn.name, id);
|
2022-09-26 16:22:22 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
if (hasInverseOrderColumn(attribute) && !isEmpty(inverseRelIds)) {
|
|
|
|
update.push('?? = t.inv_order');
|
|
|
|
updateBinding.push(inverseOrderColumnName);
|
|
|
|
select.push('ROW_NUMBER() OVER (PARTITION BY ?? ORDER BY ??) AS inv_order');
|
|
|
|
selectBinding.push(inverseJoinColumn.name, inverseOrderColumnName);
|
|
|
|
where.push(`?? IN (${inverseRelIds.map(() => '?').join(', ')})`);
|
|
|
|
whereBinding.push(inverseJoinColumn.name, ...inverseRelIds);
|
2022-09-16 18:38:46 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 11:56:46 +02:00
|
|
|
// raw query as knex doesn't allow updating from a subquery
|
|
|
|
// https://github.com/knex/knex/issues/2504
|
2022-09-26 16:22:22 +02:00
|
|
|
/*
|
2022-09-27 11:56:46 +02:00
|
|
|
`UPDATE :joinTable:
|
|
|
|
SET :orderColumn: = t.src_order, :inverseOrderColumn: = t.inv_order
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
ROW_NUMBER() OVER ( PARTITION BY :joinColumn: ORDER BY :orderColumn:) AS src_order,
|
|
|
|
ROW_NUMBER() OVER ( PARTITION BY :inverseJoinColumn: ORDER BY :inverseOrderColumn:) AS inv_order
|
|
|
|
FROM :joinTable:
|
|
|
|
WHERE :joinColumn: = :id OR :inverseJoinColumn: IN (:inverseRelIds)
|
|
|
|
) AS t
|
|
|
|
WHERE t.id = :joinTable:.id`,
|
|
|
|
*/
|
|
|
|
await db.getConnection().raw(
|
|
|
|
`UPDATE ??
|
|
|
|
SET ${update.join(', ')}
|
2022-09-26 16:22:22 +02:00
|
|
|
FROM (
|
2022-09-27 11:56:46 +02:00
|
|
|
SELECT ${select.join(', ')}
|
|
|
|
FROM ??
|
|
|
|
WHERE ${where.join(' OR ')}
|
2022-09-26 16:22:22 +02:00
|
|
|
) AS t
|
2022-09-27 11:56:46 +02:00
|
|
|
WHERE t.id = ??.id`,
|
|
|
|
[
|
|
|
|
joinTable.name,
|
|
|
|
...updateBinding,
|
|
|
|
...selectBinding,
|
|
|
|
joinTable.name,
|
|
|
|
...whereBinding,
|
|
|
|
joinTable.name,
|
|
|
|
]
|
|
|
|
);
|
2022-09-16 18:38:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
deletePreviousOneToAnyRelations,
|
|
|
|
deletePreviousAnyToOneRelations,
|
2022-09-20 11:18:32 +02:00
|
|
|
deleteRelations,
|
2022-09-26 16:22:22 +02:00
|
|
|
cleanOrderColumns,
|
2022-09-16 18:38:46 +02:00
|
|
|
};
|