-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix cursor-based pagination #16558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix cursor-based pagination #16558
Changes from 2 commits
09ad3a4
a92c9a6
de0f776
c7dc5cb
5018b3e
0359179
d9adc00
2a570b3
f286f26
b0a294e
006fa43
3e3455e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| import { isNull } from '@sniptt/guards'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||
| FieldMetadataType, | ||||||||||||||||||||||||||||||||||||||||||||||
| type ObjectRecord, | ||||||||||||||||||||||||||||||||||||||||||||||
| OrderByDirection, | ||||||||||||||||||||||||||||||||||||||||||||||
| compositeTypeDefinitions, | ||||||||||||||||||||||||||||||||||||||||||||||
| } from 'twenty-shared/types'; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { isDefined } from 'twenty-shared/utils'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,6 +31,87 @@ type BuildCursorCompositeFieldWhereConditionParams = { | |||||||||||||||||||||||||||||||||||||||||||||
| isEqualityCondition?: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const buildNullCompositeFieldFilter = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldKey: keyof ObjectRecord, | ||||||||||||||||||||||||||||||||||||||||||||||
| compositeFieldProperties: Array<{ name: string }>, | ||||||||||||||||||||||||||||||||||||||||||||||
| ): ObjectRecordFilter => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const nullFilters = compositeFieldProperties.reduce< | ||||||||||||||||||||||||||||||||||||||||||||||
| Record<string, ObjectRecordFilter> | ||||||||||||||||||||||||||||||||||||||||||||||
| >( | ||||||||||||||||||||||||||||||||||||||||||||||
| (acc, property) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||
| ...acc, | ||||||||||||||||||||||||||||||||||||||||||||||
| [property.name]: { is: 'NULL' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||
| {}, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { [fieldKey]: nullFilters }; | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const buildAllNullComparisonFilter = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldKey: keyof ObjectRecord, | ||||||||||||||||||||||||||||||||||||||||||||||
| firstProperty: { name: string }, | ||||||||||||||||||||||||||||||||||||||||||||||
| computedOperator: string, | ||||||||||||||||||||||||||||||||||||||||||||||
| ): ObjectRecordFilter | Record<string, never> => { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (computedOperator === 'gt') { | ||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| [fieldKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| [firstProperty.name]: { is: 'NOT NULL' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+57
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: The Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (computedOperator === 'lt') { | ||||||||||||||||||||||||||||||||||||||||||||||
abdulrahmancodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| [fieldKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| [firstProperty.name]: { is: 'NULL' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const shouldIncludeNullsInComparison = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| computedOperator: string, | ||||||||||||||||||||||||||||||||||||||||||||||
| orderByDirection: OrderByDirection, | ||||||||||||||||||||||||||||||||||||||||||||||
| ): boolean => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const isNullsFirst = | ||||||||||||||||||||||||||||||||||||||||||||||
| orderByDirection === OrderByDirection.AscNullsFirst || | ||||||||||||||||||||||||||||||||||||||||||||||
| orderByDirection === OrderByDirection.DescNullsFirst; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| (computedOperator === 'lt' && isNullsFirst) || | ||||||||||||||||||||||||||||||||||||||||||||||
| (computedOperator === 'gt' && !isNullsFirst) | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const buildComparisonFilterWithNulls = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldKey: keyof ObjectRecord, | ||||||||||||||||||||||||||||||||||||||||||||||
| cursorKey: string, | ||||||||||||||||||||||||||||||||||||||||||||||
| computedOperator: string, | ||||||||||||||||||||||||||||||||||||||||||||||
| cursorValue: unknown, | ||||||||||||||||||||||||||||||||||||||||||||||
| ): ObjectRecordFilter => { | ||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| or: [ | ||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||
| [fieldKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| [cursorKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| [computedOperator]: cursorValue, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||
| [fieldKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| [cursorKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| is: 'NULL', | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export const buildCursorCompositeFieldWhereCondition = ({ | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldType, | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldKey, | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -64,36 +147,61 @@ export const buildCursorCompositeFieldWhereCondition = ({ | |||||||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const allSubFieldsAreNull = compositeFieldProperties.every((property) => | ||||||||||||||||||||||||||||||||||||||||||||||
| isNull(cursorValue[property.name]), | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (allSubFieldsAreNull) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (isEqualityCondition) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return buildNullCompositeFieldFilter(fieldKey, compositeFieldProperties); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const firstProperty = compositeFieldProperties[0]; | ||||||||||||||||||||||||||||||||||||||||||||||
| const firstOrderByDirection = fieldOrderBy[fieldKey]?.[firstProperty.name]; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (!isDefined(firstOrderByDirection)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const isAscending = isAscendingOrder(firstOrderByDirection); | ||||||||||||||||||||||||||||||||||||||||||||||
| const computedOperator = computeOperator(isAscending, isForwardPagination); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return buildAllNullComparisonFilter( | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldKey, | ||||||||||||||||||||||||||||||||||||||||||||||
| firstProperty, | ||||||||||||||||||||||||||||||||||||||||||||||
| computedOperator, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const cursorEntries = compositeFieldProperties | ||||||||||||||||||||||||||||||||||||||||||||||
| .map((property) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (cursorValue[property.name] === undefined) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (isNull(cursorValue[property.name])) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Filtering null values from Prompt for AI agents |
||||||||||||||||||||||||||||||||||||||||||||||
| 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]; | ||||||||||||||||||||||||||||||||||||||||||||||
| const equalityFilters = cursorEntries.reduce< | ||||||||||||||||||||||||||||||||||||||||||||||
| Record<string, ObjectRecordFilter> | ||||||||||||||||||||||||||||||||||||||||||||||
| >((acc, cursorEntry) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const [cursorKey, cursorValue] = Object.entries(cursorEntry)[0]; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| ...acc, | ||||||||||||||||||||||||||||||||||||||||||||||
| [cursorKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| eq: cursorValue, | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||
| {}, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| ...acc, | ||||||||||||||||||||||||||||||||||||||||||||||
| [cursorKey]: { eq: cursorValue }, | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| }, {}); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| [fieldKey]: result, | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| return { [fieldKey]: equalityFilters }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| const orConditions = buildCursorCumulativeWhereCondition({ | ||||||||||||||||||||||||||||||||||||||||||||||
abdulrahmancodes marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -121,6 +229,15 @@ export const buildCursorCompositeFieldWhereCondition = ({ | |||||||||||||||||||||||||||||||||||||||||||||
| isForwardPagination, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if (shouldIncludeNullsInComparison(computedOperator, orderByDirection)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| return buildComparisonFilterWithNulls( | ||||||||||||||||||||||||||||||||||||||||||||||
| fieldKey, | ||||||||||||||||||||||||||||||||||||||||||||||
| cursorKey, | ||||||||||||||||||||||||||||||||||||||||||||||
| computedOperator, | ||||||||||||||||||||||||||||||||||||||||||||||
| cursorValue, | ||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| [fieldKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
| [cursorKey]: { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.