Fix cursor-based pagination with lexicographic ordering for composite fields (#12467)

# Fix cursor-based pagination with lexicographic ordering for composite
fields

## Bug

The existing cursor-based pagination implementation had a bug when
handling composite fields.
When paginating through results sorted by composite fields (like
`fullName` with sub-properties `firstName` and`lastName`), the WHERE
conditions generated for cursor positioning were incorrect, leading to
records being skipped.

The previous implementation was generating wrong WHERE conditions:

For example, when paginating with a cursor like `{ firstName: 'John',
lastName: 'Doe' }`, it would generate:

```sql
WHERE firstName > 'John' AND lastName > 'Doe'
```

This is incorrect because it would miss records like `{ firstName:
'John', lastName: 'Smith' }` which should be included in forward
pagination.

## Fix

Create a new util to use proper lexicographic order when sorting a
composite field.

---------

Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Raphaël Bosi
2025-06-11 16:48:03 +02:00
committed by GitHub
parent 4cea354838
commit d4995ab54e
15 changed files with 1710 additions and 183 deletions

View File

@ -7,10 +7,10 @@ export interface ObjectRecord {
deletedAt: string | null;
}
export type ObjectRecordFilter = {
export type ObjectRecordFilter = Partial<{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[Property in keyof ObjectRecord]: any;
};
}>;
export enum OrderByDirection {
AscNullsFirst = 'AscNullsFirst',
@ -19,11 +19,29 @@ export enum OrderByDirection {
DescNullsLast = 'DescNullsLast',
}
export type ObjectRecordOrderBy = Array<{
export type ObjectRecordOrderBy = Array<
ObjectRecordOrderByForScalarField | ObjectRecordOrderByForCompositeField
>;
export type ObjectRecordOrderByForScalarField = {
[Property in keyof ObjectRecord]?: OrderByDirection;
};
export type ObjectRecordOrderByForCompositeField = {
[Property in keyof ObjectRecord]?: Record<string, OrderByDirection>;
};
export type ObjectRecordCursorLeafScalarValue = string | number | boolean;
export type ObjectRecordCursorLeafCompositeValue = Record<
string,
ObjectRecordCursorLeafScalarValue
>;
export type ObjectRecordCursor = {
[Property in keyof ObjectRecord]?:
| OrderByDirection
| Record<string, OrderByDirection>;
}>;
| ObjectRecordCursorLeafScalarValue
| ObjectRecordCursorLeafCompositeValue;
};
export interface ObjectRecordDuplicateCriteria {
objectName: string;

View File

@ -0,0 +1,343 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`buildCompositeFieldWhereCondition multiple properties cases should match snapshots for address composite field - both ascending, forward pagination: multiple properties - address composite field - both ascending, forward pagination 1`] = `
{
"or": [
{
"address": {
"addressStreet1": {
"gt": "123 Main St",
},
},
},
{
"and": [
{
"address": {
"addressStreet1": {
"eq": "123 Main St",
},
},
},
{
"address": {
"addressStreet2": {
"gt": "Apt 4B",
},
},
},
],
},
{
"and": [
{
"address": {
"addressStreet1": {
"eq": "123 Main St",
},
},
},
{
"address": {
"addressStreet2": {
"eq": "Apt 4B",
},
},
},
{
"address": {
"addressCity": {
"gt": "New York",
},
},
},
],
},
{
"and": [
{
"address": {
"addressStreet1": {
"eq": "123 Main St",
},
},
},
{
"address": {
"addressStreet2": {
"eq": "Apt 4B",
},
},
},
{
"address": {
"addressCity": {
"eq": "New York",
},
},
},
{
"address": {
"addressPostcode": {
"gt": "10001",
},
},
},
],
},
{
"and": [
{
"address": {
"addressStreet1": {
"eq": "123 Main St",
},
},
},
{
"address": {
"addressStreet2": {
"eq": "Apt 4B",
},
},
},
{
"address": {
"addressCity": {
"eq": "New York",
},
},
},
{
"address": {
"addressPostcode": {
"eq": "10001",
},
},
},
{
"address": {
"addressState": {
"gt": "NY",
},
},
},
],
},
{
"and": [
{
"address": {
"addressStreet1": {
"eq": "123 Main St",
},
},
},
{
"address": {
"addressStreet2": {
"eq": "Apt 4B",
},
},
},
{
"address": {
"addressCity": {
"eq": "New York",
},
},
},
{
"address": {
"addressPostcode": {
"eq": "10001",
},
},
},
{
"address": {
"addressState": {
"eq": "NY",
},
},
},
{
"address": {
"addressCountry": {
"gt": "USA",
},
},
},
],
},
],
}
`;
exports[`buildCompositeFieldWhereCondition multiple properties cases should match snapshots for two properties - both ascending, backward pagination: multiple properties - two properties - both ascending, backward pagination 1`] = `
{
"or": [
{
"name": {
"firstName": {
"lt": "John",
},
},
},
{
"and": [
{
"name": {
"firstName": {
"eq": "John",
},
},
},
{
"name": {
"lastName": {
"lt": "Doe",
},
},
},
],
},
],
}
`;
exports[`buildCompositeFieldWhereCondition multiple properties cases should match snapshots for two properties - both ascending, forward pagination: multiple properties - two properties - both ascending, forward pagination 1`] = `
{
"or": [
{
"name": {
"firstName": {
"gt": "John",
},
},
},
{
"and": [
{
"name": {
"firstName": {
"eq": "John",
},
},
},
{
"name": {
"lastName": {
"gt": "Doe",
},
},
},
],
},
],
}
`;
exports[`buildCompositeFieldWhereCondition multiple properties cases should match snapshots for two properties - both descending, forward pagination: multiple properties - two properties - both descending, forward pagination 1`] = `
{
"or": [
{
"name": {
"firstName": {
"lt": "John",
},
},
},
{
"and": [
{
"name": {
"firstName": {
"eq": "John",
},
},
},
{
"name": {
"lastName": {
"lt": "Doe",
},
},
},
],
},
],
}
`;
exports[`buildCompositeFieldWhereCondition multiple properties cases should match snapshots for two properties - mixed ordering, forward pagination: multiple properties - two properties - mixed ordering, forward pagination 1`] = `
{
"or": [
{
"name": {
"firstName": {
"gt": "John",
},
},
},
{
"and": [
{
"name": {
"firstName": {
"eq": "John",
},
},
},
{
"name": {
"lastName": {
"lt": "Doe",
},
},
},
],
},
],
}
`;
exports[`buildCompositeFieldWhereCondition single property cases should match snapshot for ascending order with backward pagination: single property - ascending order with backward pagination 1`] = `
{
"person": {
"firstName": {
"lt": "John",
},
},
}
`;
exports[`buildCompositeFieldWhereCondition single property cases should match snapshot for ascending order with forward pagination: single property - ascending order with forward pagination 1`] = `
{
"person": {
"firstName": {
"gt": "John",
},
},
}
`;
exports[`buildCompositeFieldWhereCondition single property cases should match snapshot for descending order with backward pagination: single property - descending order with backward pagination 1`] = `
{
"person": {
"firstName": {
"gt": "John",
},
},
}
`;
exports[`buildCompositeFieldWhereCondition single property cases should match snapshot for descending order with forward pagination: single property - descending order with forward pagination 1`] = `
{
"person": {
"firstName": {
"lt": "John",
},
},
}
`;

View File

@ -0,0 +1,358 @@
import { EachTestingContext } from 'twenty-shared/testing';
import { FieldMetadataType } from 'twenty-shared/types';
import {
ObjectRecordOrderBy,
OrderByDirection,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import { buildCursorCompositeFieldWhereCondition } from 'src/engine/api/utils/build-cursor-composite-field-where-condition.utils';
describe('buildCompositeFieldWhereCondition', () => {
describe('eq operator cases', () => {
it('should handle eq operator', () => {
const result = buildCursorCompositeFieldWhereCondition({
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'name',
orderBy: [
{
name: {
firstName: OrderByDirection.AscNullsLast,
lastName: OrderByDirection.AscNullsLast,
},
},
],
cursorValue: { firstName: 'John', lastName: 'Doe' },
isForwardPagination: true,
isEqualityCondition: true,
});
expect(result).toEqual({
name: {
firstName: {
eq: 'John',
},
lastName: {
eq: 'Doe',
},
},
});
});
});
describe('single property cases', () => {
const singlePropertyTestCases: EachTestingContext<{
description: string;
fieldType: FieldMetadataType;
fieldKey: string;
orderBy: ObjectRecordOrderBy;
value: { [key: string]: any };
isForwardPagination: boolean;
operator?: string;
expectedOperator: string;
}>[] = [
{
title: 'ascending order with forward pagination',
context: {
description: 'ascending order with forward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'person',
orderBy: [{ person: { firstName: OrderByDirection.AscNullsLast } }],
value: { firstName: 'John' },
isForwardPagination: true,
operator: undefined,
expectedOperator: 'gt',
},
},
{
title: 'ascending order with backward pagination',
context: {
description: 'ascending order with backward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'person',
orderBy: [{ person: { firstName: OrderByDirection.AscNullsLast } }],
value: { firstName: 'John' },
isForwardPagination: false,
operator: undefined,
expectedOperator: 'lt',
},
},
{
title: 'descending order with forward pagination',
context: {
description: 'descending order with forward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'person',
orderBy: [{ person: { firstName: OrderByDirection.DescNullsLast } }],
value: { firstName: 'John' },
isForwardPagination: true,
operator: undefined,
expectedOperator: 'lt',
},
},
{
title: 'descending order with backward pagination',
context: {
description: 'descending order with backward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'person',
orderBy: [{ person: { firstName: OrderByDirection.DescNullsLast } }],
value: { firstName: 'John' },
isForwardPagination: false,
operator: undefined,
expectedOperator: 'gt',
},
},
];
test.each(singlePropertyTestCases)(
'should handle $description',
({ context }) => {
const {
fieldType,
fieldKey,
orderBy,
value,
isForwardPagination,
expectedOperator,
} = context;
const result = buildCursorCompositeFieldWhereCondition({
fieldType,
fieldKey,
orderBy,
cursorValue: value,
isForwardPagination,
});
expect(result).toEqual({
[fieldKey]: {
firstName: {
[expectedOperator]: value.firstName,
},
},
});
},
);
test.each(singlePropertyTestCases)(
'should match snapshot for $title',
({ context }) => {
const {
fieldType,
fieldKey,
orderBy,
value,
isForwardPagination,
description,
} = context;
const result = buildCursorCompositeFieldWhereCondition({
fieldType,
fieldKey,
orderBy,
cursorValue: value,
isForwardPagination,
});
expect(result).toMatchSnapshot(`single property - ${description}`);
},
);
});
describe('multiple properties cases', () => {
const multiplePropertiesTestCases: EachTestingContext<{
description: string;
fieldType: FieldMetadataType;
fieldKey: string;
orderBy: ObjectRecordOrderBy;
value: { [key: string]: any };
isForwardPagination: boolean;
}>[] = [
{
title: 'two properties - both ascending, forward pagination',
context: {
description: 'two properties - both ascending, forward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'name',
orderBy: [
{
name: {
firstName: OrderByDirection.AscNullsLast,
lastName: OrderByDirection.AscNullsLast,
},
},
],
value: { firstName: 'John', lastName: 'Doe' },
isForwardPagination: true,
},
},
{
title: 'two properties - both ascending, backward pagination',
context: {
description: 'two properties - both ascending, backward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'name',
orderBy: [
{
name: {
firstName: OrderByDirection.AscNullsLast,
lastName: OrderByDirection.AscNullsLast,
},
},
],
value: { firstName: 'John', lastName: 'Doe' },
isForwardPagination: false,
},
},
{
title: 'two properties - mixed ordering, forward pagination',
context: {
description: 'two properties - mixed ordering, forward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'name',
orderBy: [
{
name: {
firstName: OrderByDirection.AscNullsLast,
lastName: OrderByDirection.DescNullsLast,
},
},
],
value: { firstName: 'John', lastName: 'Doe' },
isForwardPagination: true,
},
},
{
title: 'two properties - both descending, forward pagination',
context: {
description: 'two properties - both descending, forward pagination',
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'name',
orderBy: [
{
name: {
firstName: OrderByDirection.DescNullsLast,
lastName: OrderByDirection.DescNullsLast,
},
},
],
value: { firstName: 'John', lastName: 'Doe' },
isForwardPagination: true,
},
},
{
title: 'address composite field - both ascending, forward pagination',
context: {
description:
'address composite field - both ascending, forward pagination',
fieldType: FieldMetadataType.ADDRESS,
fieldKey: 'address',
orderBy: [
{
address: {
addressStreet1: OrderByDirection.AscNullsLast,
addressStreet2: OrderByDirection.AscNullsLast,
addressCity: OrderByDirection.AscNullsLast,
addressState: OrderByDirection.AscNullsLast,
addressCountry: OrderByDirection.AscNullsLast,
addressPostcode: OrderByDirection.AscNullsLast,
},
},
],
value: {
addressStreet1: '123 Main St',
addressStreet2: 'Apt 4B',
addressCity: 'New York',
addressState: 'NY',
addressCountry: 'USA',
addressPostcode: '10001',
},
isForwardPagination: true,
},
},
];
test.each(multiplePropertiesTestCases)(
'should handle $title',
({ context }) => {
const { fieldType, fieldKey, orderBy, value, isForwardPagination } =
context;
const result = buildCursorCompositeFieldWhereCondition({
fieldType,
fieldKey,
orderBy,
cursorValue: value,
isForwardPagination,
});
expect(result).toHaveProperty('or');
const orConditions = result.or;
expect(Array.isArray(orConditions)).toBe(true);
const propertiesWithValues = Object.keys(value).length;
expect(orConditions).toHaveLength(propertiesWithValues);
expect(orConditions[0]).toHaveProperty(fieldKey);
for (const [index, orCondition] of orConditions.slice(1).entries()) {
expect(orCondition).toHaveProperty('and');
expect(Array.isArray(orCondition.and)).toBe(true);
expect(orCondition.and).toHaveLength(index + 2);
}
},
);
test.each(multiplePropertiesTestCases)(
'should match snapshots for $title',
({ context }) => {
const {
fieldType,
fieldKey,
orderBy,
value,
isForwardPagination,
description,
} = context;
const result = buildCursorCompositeFieldWhereCondition({
fieldType,
fieldKey,
orderBy,
cursorValue: value,
isForwardPagination,
});
expect(result).toMatchSnapshot(`multiple properties - ${description}`);
},
);
});
describe('error cases', () => {
it('should throw error for invalid composite type', () => {
expect(() =>
buildCursorCompositeFieldWhereCondition({
fieldType: FieldMetadataType.TEXT,
fieldKey: 'person',
orderBy: [{ person: { firstName: OrderByDirection.AscNullsLast } }],
cursorValue: { firstName: 'John' },
isForwardPagination: true,
}),
).toThrow('Composite type definition not found for type: TEXT');
});
it('should throw error for invalid cursor with missing order by', () => {
expect(() =>
buildCursorCompositeFieldWhereCondition({
fieldType: FieldMetadataType.FULL_NAME,
fieldKey: 'person',
orderBy: [],
cursorValue: { firstName: 'John' },
isForwardPagination: true,
}),
).toThrow('Invalid cursor');
});
});
});

View File

@ -83,13 +83,13 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{ name: { gt: 'John' } },
{ name: { eq: 'John' }, age: { lt: 30 } },
{ and: [{ name: { eq: 'John' } }, { age: { lt: 30 } }] },
]);
});
});
describe('composite field handling', () => {
it('should handle fullName composite field', () => {
it('should handle fullName composite field with proper ordering', () => {
const cursor = {
fullName: { firstName: 'John', lastName: 'Doe' },
};
@ -109,15 +109,107 @@ describe('computeCursorArgFilter', () => {
true,
);
expect(result).toEqual([
{
or: [
{
fullName: {
firstName: { gt: 'John' },
},
},
{
and: [
{
fullName: {
firstName: { eq: 'John' },
},
},
{
fullName: {
lastName: { gt: 'Doe' },
},
},
],
},
],
},
]);
});
it('should handle single property composite field', () => {
const cursor = {
fullName: { firstName: 'John' },
};
const orderBy = [
{
fullName: {
firstName: OrderByDirection.AscNullsLast,
},
},
];
const result = computeCursorArgFilter(
cursor,
orderBy,
mockFieldMetadataMap,
true,
);
expect(result).toEqual([
{
fullName: {
firstName: { gt: 'John' },
lastName: { gt: 'Doe' },
},
},
]);
});
it('should handle composite field with backward pagination', () => {
const cursor = {
fullName: { firstName: 'John', lastName: 'Doe' },
};
const orderBy = [
{
fullName: {
firstName: OrderByDirection.AscNullsLast,
lastName: OrderByDirection.AscNullsLast,
},
},
];
const result = computeCursorArgFilter(
cursor,
orderBy,
mockFieldMetadataMap,
false,
);
expect(result).toEqual([
{
or: [
{
fullName: {
firstName: { lt: 'John' },
},
},
{
and: [
{
fullName: {
firstName: { eq: 'John' },
},
},
{
fullName: {
lastName: { lt: 'Doe' },
},
},
],
},
],
},
]);
});
});
describe('error handling', () => {

View File

@ -0,0 +1,139 @@
import { FieldMetadataType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import {
ObjectRecord,
ObjectRecordCursorLeafCompositeValue,
ObjectRecordFilter,
ObjectRecordOrderBy,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
import { buildCursorCumulativeWhereCondition } from 'src/engine/api/utils/build-cursor-cumulative-where-conditions.utils';
import { computeOperator } from 'src/engine/api/utils/compute-operator.utils';
import { isAscendingOrder } from 'src/engine/api/utils/is-ascending-order.utils';
import { validateAndGetOrderByForCompositeField } from 'src/engine/api/utils/validate-and-get-order-by.utils';
import { compositeTypeDefinitions } from 'src/engine/metadata-modules/field-metadata/composite-types';
type BuildCursorCompositeFieldWhereConditionParams = {
fieldType: FieldMetadataType;
fieldKey: keyof ObjectRecord;
orderBy: ObjectRecordOrderBy;
cursorValue: ObjectRecordCursorLeafCompositeValue;
isForwardPagination: boolean;
isEqualityCondition?: boolean;
};
export const buildCursorCompositeFieldWhereCondition = ({
fieldType,
fieldKey,
orderBy,
cursorValue,
isForwardPagination,
isEqualityCondition = false,
}: BuildCursorCompositeFieldWhereConditionParams): Record<
string,
ObjectRecordFilter
> => {
const compositeType = compositeTypeDefinitions.get(fieldType);
if (!compositeType) {
throw new GraphqlQueryRunnerException(
`Composite type definition not found for type: ${fieldType}`,
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
const fieldOrderBy = validateAndGetOrderByForCompositeField(
fieldKey,
orderBy,
);
const compositeFieldProperties = compositeType.properties.filter(
(property) =>
property.type !== FieldMetadataType.RAW_JSON &&
cursorValue[property.name] !== undefined,
);
if (compositeFieldProperties.length === 0) {
return {};
}
const cursorEntries = compositeFieldProperties
.map((property) => {
if (cursorValue[property.name] === undefined) {
return null;
}
return {
[property.name]: cursorValue[property.name],
};
})
.filter(isDefined);
if (isEqualityCondition) {
const result = cursorEntries.reduce<Record<string, ObjectRecordFilter>>(
(acc, cursorEntry) => {
const [cursorKey, cursorValue] = Object.entries(cursorEntry)[0];
return {
...acc,
[cursorKey]: {
eq: cursorValue,
},
};
},
{},
);
return {
[fieldKey]: result,
};
}
const orConditions = buildCursorCumulativeWhereCondition({
cursorEntries,
buildEqualityCondition: ({ cursorKey, cursorValue }) => ({
[fieldKey]: {
[cursorKey]: {
eq: cursorValue,
},
},
}),
buildMainCondition: ({ cursorKey, cursorValue }) => {
const orderByDirection = fieldOrderBy[fieldKey]?.[cursorKey];
if (!isDefined(orderByDirection)) {
throw new GraphqlQueryRunnerException(
'Invalid cursor',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
const isAscending = isAscendingOrder(orderByDirection);
const computedOperator = computeOperator(
isAscending,
isForwardPagination,
);
return {
[fieldKey]: {
[cursorKey]: {
[computedOperator]: cursorValue,
},
},
};
},
});
if (orConditions.length === 1) {
return orConditions[0];
}
return {
or: orConditions,
};
};

View File

@ -0,0 +1,74 @@
import {
ObjectRecordCursorLeafCompositeValue,
ObjectRecordCursorLeafScalarValue,
ObjectRecordFilter,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
type BuildCursorConditionParams<CursorValue> = {
cursorKey: string;
cursorValue: CursorValue;
};
type ReturnType = Array<ObjectRecordFilter | { and: ObjectRecordFilter[] }>;
type CursorEntry<CursorValue> = Record<string, CursorValue>;
type BuildCursorCumulativeWhereConditionsParams<CursorValue> = {
cursorEntries: CursorEntry<CursorValue>[];
buildEqualityCondition: ({
cursorKey,
cursorValue,
}: BuildCursorConditionParams<CursorValue>) => ObjectRecordFilter;
buildMainCondition: ({
cursorKey,
cursorValue,
}: BuildCursorConditionParams<CursorValue>) => ObjectRecordFilter;
};
export const buildCursorCumulativeWhereCondition = <
CursorValue extends
| ObjectRecordCursorLeafCompositeValue
| ObjectRecordCursorLeafScalarValue,
>({
cursorEntries,
buildEqualityCondition,
buildMainCondition,
}: BuildCursorCumulativeWhereConditionsParams<CursorValue>): ReturnType => {
return cursorEntries.map((cursorEntry, index) => {
const [currentCursorKey, currentCursorValue] =
Object.entries(cursorEntry)[0];
const andConditions: ObjectRecordFilter[] = [];
for (
let subConditionIndex = 0;
subConditionIndex < index;
subConditionIndex++
) {
const previousCursorEntry = cursorEntries[subConditionIndex];
const [previousCursorKey, previousCursorValue] =
Object.entries(previousCursorEntry)[0];
andConditions.push(
buildEqualityCondition({
cursorKey: previousCursorKey,
cursorValue: previousCursorValue,
}),
);
}
andConditions.push(
buildMainCondition({
cursorKey: currentCursorKey,
cursorValue: currentCursorValue,
}),
);
if (andConditions.length === 1) {
return andConditions[0];
}
return {
and: andConditions,
};
});
};

View File

@ -0,0 +1,78 @@
import { isDefined } from 'twenty-shared/utils';
import {
ObjectRecord,
ObjectRecordCursorLeafCompositeValue,
ObjectRecordCursorLeafScalarValue,
ObjectRecordOrderBy,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
import { computeOperator } from 'src/engine/api/utils/compute-operator.utils';
import { isAscendingOrder } from 'src/engine/api/utils/is-ascending-order.utils';
import { validateAndGetOrderByForScalarField } from 'src/engine/api/utils/validate-and-get-order-by.utils';
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
import { FieldMetadataMap } from 'src/engine/metadata-modules/types/field-metadata-map';
import { buildCursorCompositeFieldWhereCondition } from 'src/engine/api/utils/build-cursor-composite-field-where-condition.utils';
type BuildCursorWhereConditionParams = {
cursorKey: keyof ObjectRecord;
cursorValue:
| ObjectRecordCursorLeafScalarValue
| ObjectRecordCursorLeafCompositeValue;
fieldMetadataMapByName: FieldMetadataMap;
orderBy: ObjectRecordOrderBy;
isForwardPagination: boolean;
isEqualityCondition?: boolean;
};
export const buildCursorWhereCondition = ({
cursorKey,
cursorValue,
fieldMetadataMapByName,
orderBy,
isForwardPagination,
isEqualityCondition = false,
}: BuildCursorWhereConditionParams): Record<string, unknown> => {
const fieldMetadata = fieldMetadataMapByName[cursorKey];
if (!fieldMetadata) {
throw new GraphqlQueryRunnerException(
`Field metadata not found for key: ${cursorKey}`,
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
if (isCompositeFieldMetadataType(fieldMetadata.type)) {
return buildCursorCompositeFieldWhereCondition({
fieldType: fieldMetadata.type,
fieldKey: cursorKey,
orderBy,
cursorValue: cursorValue as ObjectRecordCursorLeafCompositeValue,
isForwardPagination,
isEqualityCondition,
});
}
if (isEqualityCondition) {
return { [cursorKey]: { eq: cursorValue } };
}
const keyOrderBy = validateAndGetOrderByForScalarField(cursorKey, orderBy);
const orderByDirection = keyOrderBy[cursorKey];
if (!isDefined(orderByDirection)) {
throw new GraphqlQueryRunnerException(
'Invalid cursor',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
const isAscending = isAscendingOrder(orderByDirection);
const computedOperator = computeOperator(isAscending, isForwardPagination);
return { [cursorKey]: { [computedOperator]: cursorValue } };
};

View File

@ -1,190 +1,59 @@
import { FieldMetadataType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import {
ObjectRecordCursor,
ObjectRecordCursorLeafCompositeValue,
ObjectRecordCursorLeafScalarValue,
ObjectRecordFilter,
ObjectRecordOrderBy,
OrderByDirection,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
import { compositeTypeDefinitions } from 'src/engine/metadata-modules/field-metadata/composite-types';
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
import { buildCursorCumulativeWhereCondition } from 'src/engine/api/utils/build-cursor-cumulative-where-conditions.utils';
import { FieldMetadataMap } from 'src/engine/metadata-modules/types/field-metadata-map';
const computeOperator = (
isAscending: boolean,
isForwardPagination: boolean,
defaultOperator?: string,
): string => {
if (defaultOperator) return defaultOperator;
return isAscending
? isForwardPagination
? 'gt'
: 'lt'
: isForwardPagination
? 'lt'
: 'gt';
};
const validateAndGetOrderBy = (
key: string,
orderBy: ObjectRecordOrderBy,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Record<string, any> => {
const keyOrderBy = orderBy.find((order) => key in order);
if (!keyOrderBy) {
throw new GraphqlQueryRunnerException(
'Invalid cursor',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
return keyOrderBy;
};
const isAscendingOrder = (direction: OrderByDirection): boolean =>
direction === OrderByDirection.AscNullsFirst ||
direction === OrderByDirection.AscNullsLast;
import { buildCursorWhereCondition } from 'src/engine/api/utils/build-cursor-where-condition.utils';
export const computeCursorArgFilter = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
cursor: Record<string, any>,
cursor: ObjectRecordCursor,
orderBy: ObjectRecordOrderBy,
fieldMetadataMapByName: FieldMetadataMap,
isForwardPagination = true,
): ObjectRecordFilter[] => {
const cursorKeys = Object.keys(cursor ?? {});
const cursorValues = Object.values(cursor ?? {});
const cursorEntries = Object.entries(cursor)
.map(([key, value]) => {
if (value === undefined) {
return null;
}
if (cursorKeys.length === 0) {
return {
[key]: value,
};
})
.filter(isDefined);
if (cursorEntries.length === 0) {
return [];
}
return Object.entries(cursor ?? {}).map(([key, value], index) => {
let whereCondition = {};
for (
let subConditionIndex = 0;
subConditionIndex < index;
subConditionIndex++
) {
whereCondition = {
...whereCondition,
...buildWhereCondition(
cursorKeys[subConditionIndex],
cursorValues[subConditionIndex],
fieldMetadataMapByName,
orderBy,
isForwardPagination,
'eq',
),
};
}
return {
...whereCondition,
...buildWhereCondition(
key,
value,
return buildCursorCumulativeWhereCondition<
ObjectRecordCursorLeafCompositeValue | ObjectRecordCursorLeafScalarValue
>({
cursorEntries,
buildEqualityCondition: ({ cursorKey, cursorValue }) =>
buildCursorWhereCondition({
cursorKey,
cursorValue,
fieldMetadataMapByName,
orderBy,
isForwardPagination: true,
isEqualityCondition: true,
}),
buildMainCondition: ({ cursorKey, cursorValue }) =>
buildCursorWhereCondition({
cursorKey,
cursorValue,
fieldMetadataMapByName,
orderBy,
isForwardPagination,
),
} as ObjectRecordFilter;
}),
});
};
const buildWhereCondition = (
key: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
fieldMetadataMapByName: FieldMetadataMap,
orderBy: ObjectRecordOrderBy,
isForwardPagination: boolean,
operator?: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Record<string, any> => {
const fieldMetadata = fieldMetadataMapByName[key];
if (!fieldMetadata) {
throw new GraphqlQueryRunnerException(
`Field metadata not found for key: ${key}`,
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
if (isCompositeFieldMetadataType(fieldMetadata.type)) {
return buildCompositeWhereCondition(
key,
value,
fieldMetadata.type,
orderBy,
isForwardPagination,
operator,
);
}
const keyOrderBy = validateAndGetOrderBy(key, orderBy);
const isAscending = isAscendingOrder(keyOrderBy[key]);
const computedOperator = computeOperator(
isAscending,
isForwardPagination,
operator,
);
return { [key]: { [computedOperator]: value } };
};
const buildCompositeWhereCondition = (
key: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any,
fieldType: FieldMetadataType,
orderBy: ObjectRecordOrderBy,
isForwardPagination: boolean,
operator?: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Record<string, any> => {
const compositeType = compositeTypeDefinitions.get(fieldType);
if (!compositeType) {
throw new GraphqlQueryRunnerException(
`Composite type definition not found for type: ${fieldType}`,
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
const keyOrderBy = validateAndGetOrderBy(key, orderBy);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: Record<string, any> = {};
compositeType.properties.forEach((property) => {
if (
property.type === FieldMetadataType.RAW_JSON ||
value[property.name] === undefined
) {
return;
}
const isAscending = isAscendingOrder(keyOrderBy[key][property.name]);
const computedOperator = computeOperator(
isAscending,
isForwardPagination,
operator,
);
result[key] = {
...result[key],
[property.name]: {
[computedOperator]: value[property.name],
},
};
});
return result;
};

View File

@ -0,0 +1,12 @@
export const computeOperator = (
isAscending: boolean,
isForwardPagination: boolean,
): string => {
return isAscending
? isForwardPagination
? 'gt'
: 'lt'
: isForwardPagination
? 'lt'
: 'gt';
};

View File

@ -0,0 +1,5 @@
import { OrderByDirection } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
export const isAscendingOrder = (direction: OrderByDirection): boolean =>
direction === OrderByDirection.AscNullsFirst ||
direction === OrderByDirection.AscNullsLast;

View File

@ -0,0 +1,88 @@
import { isDefined } from 'twenty-shared/utils';
import {
ObjectRecord,
ObjectRecordOrderBy,
ObjectRecordOrderByForCompositeField,
ObjectRecordOrderByForScalarField,
OrderByDirection,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import {
GraphqlQueryRunnerException,
GraphqlQueryRunnerExceptionCode,
} from 'src/engine/api/graphql/graphql-query-runner/errors/graphql-query-runner.exception';
const isOrderByDirection = (value: unknown): value is OrderByDirection => {
return Object.values(OrderByDirection).includes(value as OrderByDirection);
};
const isOrderByForScalarField = (
orderByLeaf: Record<string, unknown>,
key: keyof ObjectRecord,
): orderByLeaf is ObjectRecordOrderByForScalarField => {
const value = orderByLeaf[key as string];
return isDefined(value) && isOrderByDirection(value);
};
const isOrderByForCompositeField = (
orderByLeaf: Record<string, unknown>,
key: keyof ObjectRecord,
): orderByLeaf is ObjectRecordOrderByForCompositeField => {
const value = orderByLeaf[key as string];
return (
isDefined(value) &&
typeof value === 'object' &&
value !== null &&
!isOrderByDirection(value) &&
Object.values(value as Record<string, unknown>).every(isOrderByDirection)
);
};
export const validateAndGetOrderByForScalarField = (
key: keyof ObjectRecord,
orderBy: ObjectRecordOrderBy,
): ObjectRecordOrderByForScalarField => {
const keyOrderBy = orderBy.find((order) => key in order);
if (!isDefined(keyOrderBy)) {
throw new GraphqlQueryRunnerException(
'Invalid cursor',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
if (!isOrderByForScalarField(keyOrderBy, key)) {
throw new GraphqlQueryRunnerException(
'Expected non-composite field order by',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
return keyOrderBy;
};
export const validateAndGetOrderByForCompositeField = (
key: keyof ObjectRecord,
orderBy: ObjectRecordOrderBy,
): ObjectRecordOrderByForCompositeField => {
const keyOrderBy = orderBy.find((order) => key in order);
if (!isDefined(keyOrderBy)) {
throw new GraphqlQueryRunnerException(
'Invalid cursor',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
if (!isOrderByForCompositeField(keyOrderBy, key)) {
throw new GraphqlQueryRunnerException(
'Expected composite field order by',
GraphqlQueryRunnerExceptionCode.INVALID_CURSOR,
);
}
return keyOrderBy;
};