Fix nested relations (#7158)

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Weiko
2024-09-20 05:16:13 +02:00
committed by GitHub
parent 6a5f9492d3
commit b1889e4569
17 changed files with 753 additions and 531 deletions

View File

@ -1,16 +0,0 @@
import { FindOptionsWhere, LessThan, MoreThan, ObjectLiteral } from 'typeorm';
export const applyRangeFilter = (
where: FindOptionsWhere<ObjectLiteral>,
cursor: Record<string, any>,
isForwardPagination = true,
): FindOptionsWhere<ObjectLiteral> => {
Object.entries(cursor ?? {}).forEach(([key, value]) => {
if (key === 'id') {
return;
}
where[key] = isForwardPagination ? MoreThan(value) : LessThan(value);
});
return where;
};

View File

@ -0,0 +1,66 @@
import {
OrderByDirection,
RecordFilter,
RecordOrderBy,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
import {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
export const computeCursorArgFilter = (
cursor: Record<string, any>,
orderBy: RecordOrderBy,
isForwardPagination = true,
): RecordFilter[] => {
const cursorKeys = Object.keys(cursor ?? {});
const cursorValues = Object.values(cursor ?? {});
if (cursorKeys.length === 0) {
return [];
}
return Object.entries(cursor ?? {}).map(([key, value], index) => {
let whereCondition = {};
for (
let subConditionIndex = 0;
subConditionIndex < index;
subConditionIndex++
) {
whereCondition = {
...whereCondition,
[cursorKeys[subConditionIndex]]: {
eq: cursorValues[subConditionIndex],
},
};
}
const keyOrderBy = orderBy.find((order) => key in order);
if (!keyOrderBy) {
throw new GraphqlQueryRunnerException(
'Invalid cursor',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
const isAscending =
keyOrderBy[key] === OrderByDirection.AscNullsFirst ||
keyOrderBy[key] === OrderByDirection.AscNullsLast;
const operator = isAscending
? isForwardPagination
? 'gt'
: 'lt'
: isForwardPagination
? 'lt'
: 'gt';
return {
...whereCondition,
...{ [key]: { [operator]: value } },
} as RecordFilter;
});
};

View File

@ -1,6 +1,7 @@
import { FindOptionsOrderValue } from 'typeorm';
import { Record as IRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
import {
Record as IRecord,
RecordOrderBy,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/record.interface';
import {
GraphqlQueryRunnerException,
@ -24,11 +25,15 @@ export const decodeCursor = (cursor: string): CursorData => {
export const encodeCursor = <ObjectRecord extends IRecord = IRecord>(
objectRecord: ObjectRecord,
order: Record<string, FindOptionsOrderValue> | undefined,
order: RecordOrderBy | undefined,
): string => {
const orderByValues: Record<string, any> = {};
Object.keys(order ?? {}).forEach((key) => {
const orderBy = order?.reduce((acc, orderBy) => ({ ...acc, ...orderBy }), {});
const orderByKeys = Object.keys(orderBy ?? {});
orderByKeys?.forEach((key) => {
orderByValues[key] = objectRecord[key];
});