Fix destroy many resolver broken on custom objects (#13858)
Fixes https://github.com/twentyhq/twenty/issues/13804
This commit is contained in:
+132
@@ -0,0 +1,132 @@
|
||||
import { type WhereClauseCondition } from 'typeorm/query-builder/WhereClause';
|
||||
|
||||
import { applyTableAliasOnWhereCondition } from 'src/engine/twenty-orm/utils/apply-table-alias-on-where-condition';
|
||||
|
||||
describe('applyTableAliasOnWhereCondition', () => {
|
||||
const tableName = '_listing';
|
||||
const aliasName = 'listing';
|
||||
|
||||
describe('when processing simple where conditions with string conditions', () => {
|
||||
it('should replace alias name with table name in string condition', () => {
|
||||
const condition: WhereClauseCondition = [
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `${aliasName}.id = :id`,
|
||||
},
|
||||
];
|
||||
|
||||
const result = applyTableAliasOnWhereCondition({
|
||||
condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `${tableName}.id = :id`,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should replace alias name with table name in array condition and wrapping operator condition', () => {
|
||||
const condition: WhereClauseCondition = [
|
||||
{
|
||||
type: 'simple',
|
||||
condition: {
|
||||
operator: 'brackets',
|
||||
condition: [
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `"${aliasName}"."id" IN (:...id8oi6y)`,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = applyTableAliasOnWhereCondition({
|
||||
condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
type: 'simple',
|
||||
condition: {
|
||||
operator: 'brackets',
|
||||
condition: [
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `"${tableName}"."id" IN (:...id8oi6y)`,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should replace alias name with table name in object condition', () => {
|
||||
const condition: WhereClauseCondition = {
|
||||
parameters: ['id'],
|
||||
operator: 'equal',
|
||||
};
|
||||
|
||||
const result = applyTableAliasOnWhereCondition({
|
||||
condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
operator: 'equal',
|
||||
parameters: ['id'],
|
||||
});
|
||||
});
|
||||
|
||||
it('should replace alias name with table name in string condition without alias name', () => {
|
||||
const condition: WhereClauseCondition = [
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `id = :id`,
|
||||
},
|
||||
];
|
||||
|
||||
const result = applyTableAliasOnWhereCondition({
|
||||
condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `id = :id`,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should replace alias name with table name in string condition without quotes', () => {
|
||||
const condition: WhereClauseCondition = [
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `${aliasName}."id" = :id`,
|
||||
},
|
||||
];
|
||||
|
||||
const result = applyTableAliasOnWhereCondition({
|
||||
condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
});
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
type: 'simple',
|
||||
condition: `${tableName}."id" = :id`,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
import { isArray, isObject, isString } from '@sniptt/guards';
|
||||
import {
|
||||
type WhereClause,
|
||||
type WhereClauseCondition,
|
||||
} from 'typeorm/query-builder/WhereClause';
|
||||
|
||||
type ApplyTableAliasOnWhereConditionParams = {
|
||||
condition: WhereClauseCondition;
|
||||
tableName: string;
|
||||
aliasName: string;
|
||||
};
|
||||
|
||||
export const applyTableAliasOnWhereCondition = ({
|
||||
condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
}: ApplyTableAliasOnWhereConditionParams): WhereClauseCondition => {
|
||||
if (isString(condition)) {
|
||||
const conditionParts = condition.split('.');
|
||||
|
||||
if (conditionParts.length === 1) {
|
||||
return condition;
|
||||
}
|
||||
|
||||
const [tableNamePart, ...rest] = conditionParts;
|
||||
|
||||
return `${tableNamePart.replace(aliasName, tableName)}.${rest.join('.')}`;
|
||||
}
|
||||
|
||||
if (isArray(condition)) {
|
||||
return condition.map((where: WhereClause) => {
|
||||
return {
|
||||
...where,
|
||||
condition: applyTableAliasOnWhereCondition({
|
||||
condition: where.condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
if (isObject(condition)) {
|
||||
if ('condition' in condition) {
|
||||
return {
|
||||
...condition,
|
||||
condition: applyTableAliasOnWhereCondition({
|
||||
condition: condition.condition,
|
||||
tableName,
|
||||
aliasName,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
if ('operator' in condition) {
|
||||
return condition;
|
||||
}
|
||||
}
|
||||
|
||||
return condition;
|
||||
};
|
||||
Reference in New Issue
Block a user