Skip to content

The open alternative to Salesforce, designed for AI.

Fix date-sorted pagination skipping or duplicating records (#27977)

Weikoce52ef667f4a
diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts
index 0a76a16e6f8..0101b5ba5e4 100644
--- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts
+++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/__tests__/compute-where-condition-parts.spec.ts
@@ -126,4 +126,50 @@ describe('computeWhereConditionParts', () => {
},
);
});
+
+ describe('DATE_TIME precision', () => {
+ it.each([
+ '2020-01-01 00:00:00.1239+00',
+ '2020-01-01T00:00:00.123900Z',
+ '2020-01-01T00:00:00.123Z',
+ ])('uses exact cursor comparisons for %s', (value) => {
+ const condition = {
+ objectNameSingular: 'person',
+ key: 'createdAt',
+ value,
+ fieldMetadataType: FieldMetadataType.DATE_TIME,
+ };
+
+ expect(
+ computeWhereConditionParts({
+ ...condition,
+ operator: 'gtStrict',
+ }),
+ ).toEqual({
+ sql: `"person"."createdAt" > :createdAt0000000000`,
+ params: { createdAt0000000000: value },
+ });
+ expect(
+ computeWhereConditionParts({
+ ...condition,
+ operator: 'eqStrict',
+ }),
+ ).toEqual({
+ sql: `"person"."createdAt" = :createdAt0000000000`,
+ params: { createdAt0000000000: value },
+ });
+ expect(
+ computeWhereConditionParts({ ...condition, operator: 'gt' }),
+ ).toEqual({
+ sql: `"person"."createdAt" >= :createdAt0000000000::timestamptz + interval '1 millisecond'`,
+ params: { createdAt0000000000: value },
+ });
+ expect(
+ computeWhereConditionParts({ ...condition, operator: 'eq' }),
+ ).toEqual({
+ sql: `("person"."createdAt" >= :createdAt0000000000 AND "person"."createdAt" < :createdAt0000000000::timestamptz + interval '1 millisecond')`,
+ params: { createdAt0000000000: value },
+ });
+ });
+ });
});
diff --git a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts
index c5bb9209f82..71f3035184a 100644
--- a/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts
+++ b/packages/twenty-server/src/engine/api/graphql/graphql-query-runner/utils/compute-where-condition-parts.ts
@@ -158,14 +158,12 @@ export const computeWhereConditionParts = ({
sql: `${fieldReference} IS ${value === 'NULL' ? 'NULL' : 'NOT NULL'}`,
params: {},
};
+ case 'gtStrict':
+ return {
+ sql: `${fieldReference} > :${key}${paramSuffix}`,
+ params: { [`${key}${paramSuffix}`]: value },
+ };
case 'eqStrict':
- if (isDateTimeField) {
- return {
- sql: `(${fieldReference} >= :${key}${paramSuffix} AND ${fieldReference} < :${key}${paramSuffix}::timestamptz + interval '1 millisecond')`,
- params: { [`${key}${paramSuffix}`]: value },
- };
- }
-
return {
sql: `${fieldReference} = :${key}${paramSuffix}`,
params: { [`${key}${paramSuffix}`]: value },
diff --git a/packages/twenty-server/src/engine/api/utils/__tests__/compute-cursor-arg-filter.utils.spec.ts b/packages/twenty-server/src/engine/api/utils/__tests__/compute-cursor-arg-filter.utils.spec.ts
index 69e02573fa9..0239c124daf 100644
--- a/packages/twenty-server/src/engine/api/utils/__tests__/compute-cursor-arg-filter.utils.spec.ts
+++ b/packages/twenty-server/src/engine/api/utils/__tests__/compute-cursor-arg-filter.utils.spec.ts
@@ -224,7 +224,12 @@ describe('computeCursorArgFilter', () => {
// Empty TEXT values are stored as SQL NULL, so the trailing NULL block continues the scan
expect(result).toEqual([
- { or: [{ name: { gt: 'John' } }, { name: { isStrictly: 'NULL' } }] },
+ {
+ or: [
+ { name: { gtStrict: 'John' } },
+ { name: { isStrictly: 'NULL' } },
+ ],
+ },
]);
});
@@ -263,7 +268,12 @@ describe('computeCursorArgFilter', () => {
});
expect(result).toEqual([
- { or: [{ name: { gt: 'John' } }, { name: { isStrictly: 'NULL' } }] },
+ {
+ or: [
+ { name: { gtStrict: 'John' } },
+ { name: { isStrictly: 'NULL' } },
+ ],
+ },
{
and: [
{ name: { eqStrict: 'John' } },
@@ -300,7 +310,7 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{
or: [
- { fullName: { firstName: { gt: 'John' } } },
+ { fullName: { firstName: { gtStrict: 'John' } } },
{ fullName: { firstName: { isStrictly: 'NULL' } } },
],
},
@@ -313,7 +323,7 @@ describe('computeCursorArgFilter', () => {
},
{
or: [
- { fullName: { lastName: { gt: 'Doe' } } },
+ { fullName: { lastName: { gtStrict: 'Doe' } } },
{ fullName: { lastName: { isStrictly: 'NULL' } } },
],
},
@@ -346,7 +356,7 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{
or: [
- { fullName: { firstName: { gt: 'John' } } },
+ { fullName: { firstName: { gtStrict: 'John' } } },
{ fullName: { firstName: { isStrictly: 'NULL' } } },
],
},
@@ -379,7 +389,7 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{
or: [
- { fullName: { firstName: { gt: 'John' } } },
+ { fullName: { firstName: { gtStrict: 'John' } } },
{ fullName: { firstName: { isStrictly: 'NULL' } } },
],
},
@@ -392,7 +402,7 @@ describe('computeCursorArgFilter', () => {
},
{
or: [
- { fullName: { lastName: { gt: 'Doe' } } },
+ { fullName: { lastName: { gtStrict: 'Doe' } } },
{ fullName: { lastName: { isStrictly: 'NULL' } } },
],
},
@@ -498,7 +508,7 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{
or: [
- { closeDate: { gt: '2026-01-01T00:00:00Z' } },
+ { closeDate: { gtStrict: '2026-01-01T00:00:00Z' } },
{ closeDate: { isStrictly: 'NULL' } },
],
},
@@ -518,7 +528,9 @@ describe('computeCursorArgFilter', () => {
isForwardPagination: true,
});
- expect(result).toEqual([{ closeDate: { gt: '2026-01-01T00:00:00Z' } }]);
+ expect(result).toEqual([
+ { closeDate: { gtStrict: '2026-01-01T00:00:00Z' } },
+ ]);
});
it('should continue on tie-breaking keys when the cursor sits in a trailing NULL block', () => {
@@ -541,7 +553,7 @@ describe('computeCursorArgFilter', () => {
{
and: [
{ closeDate: { isStrictly: 'NULL' } },
- { id: { gt: 'uuid-1' } },
+ { id: { gtStrict: 'uuid-1' } },
],
},
]);
@@ -568,7 +580,7 @@ describe('computeCursorArgFilter', () => {
{
and: [
{ closeDate: { isStrictly: 'NULL' } },
- { id: { gt: 'uuid-1' } },
+ { id: { gtStrict: 'uuid-1' } },
],
},
]);
@@ -610,7 +622,7 @@ describe('computeCursorArgFilter', () => {
isForwardPagination: true,
});
- expect(result).toEqual([{ id: { gt: 'uuid-1' } }]);
+ expect(result).toEqual([{ id: { gtStrict: 'uuid-1' } }]);
});
});
@@ -696,14 +708,14 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{
or: [
- { company: { name: { gt: 'Acme' } } },
+ { company: { name: { gtStrict: 'Acme' } } },
{ company: { name: { isStrictly: 'NULL' } } },
],
},
{
and: [
{ company: { name: { eqStrict: 'Acme' } } },
- { id: { gt: 'uuid-1' } },
+ { id: { gtStrict: 'uuid-1' } },
],
},
]);
@@ -725,7 +737,7 @@ describe('computeCursorArgFilter', () => {
{
and: [
{ company: { name: { isStrictly: 'NULL' } } },
- { id: { gt: 'uuid-1' } },
+ { id: { gtStrict: 'uuid-1' } },
],
},
]);
@@ -775,7 +787,7 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual([
{
or: [
- { company: { contactName: { firstName: { gt: 'Ada' } } } },
+ { company: { contactName: { firstName: { gtStrict: 'Ada' } } } },
{ company: { contactName: { firstName: { isStrictly: 'NULL' } } } },
],
},
@@ -784,7 +796,11 @@ describe('computeCursorArgFilter', () => {
{ company: { contactName: { firstName: { eqStrict: 'Ada' } } } },
{
or: [
- { company: { contactName: { lastName: { gt: 'Lovelace' } } } },
+ {
+ company: {
+ contactName: { lastName: { gtStrict: 'Lovelace' } },
+ },
+ },
{
company: {
contactName: { lastName: { isStrictly: 'NULL' } },
@@ -800,7 +816,7 @@ describe('computeCursorArgFilter', () => {
{
company: { contactName: { lastName: { eqStrict: 'Lovelace' } } },
},
- { id: { gt: 'uuid-1' } },
+ { id: { gtStrict: 'uuid-1' } },
],
},
]);
diff --git a/packages/twenty-server/src/engine/api/utils/build-cursor-keyset-condition.utils.ts b/packages/twenty-server/src/engine/api/utils/build-cursor-keyset-condition.utils.ts
index 4d97298a239..34e078b0064 100644
--- a/packages/twenty-server/src/engine/api/utils/build-cursor-keyset-condition.utils.ts
+++ b/packages/twenty-server/src/engine/api/utils/build-cursor-keyset-condition.utils.ts
@@ -48,7 +48,7 @@ export function buildCursorKeysetCondition({
}
const mainCondition = buildLeafCondition({
- [isAscending ? 'gt' : 'lt']: cursorValue,
+ [isAscending ? 'gtStrict' : 'lt']: cursorValue,
});
if (areNullsScannedLast && canFieldHoldNullValue) {
diff --git a/packages/twenty-server/src/engine/twenty-orm/executor/client-query-executor.ts b/packages/twenty-server/src/engine/twenty-orm/executor/client-query-executor.ts
index 62cd8df53ad..e034108cd8b 100644
--- a/packages/twenty-server/src/engine/twenty-orm/executor/client-query-executor.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/executor/client-query-executor.ts
@@ -15,10 +15,7 @@ export class ClientQueryExecutor implements QueryExecutor {
statement: CompiledStatement,
): Promise<Record<string, unknown>[]> {
try {
- const result = await this.client.query({
- text: statement.text,
- values: statement.values,
- });
+ const result = await this.client.query(statement);
return result.rows as Record<string, unknown>[];
} catch (error) {
diff --git a/packages/twenty-server/src/engine/twenty-orm/executor/pool-query-executor.ts b/packages/twenty-server/src/engine/twenty-orm/executor/pool-query-executor.ts
index 90abc78d13e..d9022ef5d75 100644
--- a/packages/twenty-server/src/engine/twenty-orm/executor/pool-query-executor.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/executor/pool-query-executor.ts
@@ -15,10 +15,7 @@ export class PoolQueryExecutor implements QueryExecutor {
statement: CompiledStatement,
): Promise<Record<string, unknown>[]> {
try {
- const result = await this.pool.query({
- text: statement.text,
- values: statement.values,
- });
+ const result = await this.pool.query(statement);
return result.rows as Record<string, unknown>[];
} catch (error) {
diff --git a/packages/twenty-server/src/engine/twenty-orm/query-builder/__tests__/workspace-select-query-builder-selection.spec.ts b/packages/twenty-server/src/engine/twenty-orm/query-builder/__tests__/workspace-select-query-builder-selection.spec.ts
index e7921407f33..f9f1a7cd5fc 100644
--- a/packages/twenty-server/src/engine/twenty-orm/query-builder/__tests__/workspace-select-query-builder-selection.spec.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/query-builder/__tests__/workspace-select-query-builder-selection.spec.ts
@@ -1,3 +1,5 @@
+import { types } from 'pg';
+
import { WorkspaceSelectQueryBuilder } from 'src/engine/twenty-orm/query-builder/workspace-select-query-builder';
import {
SCHEMA_NAME,
@@ -10,6 +12,32 @@ import { buildColumnResultAlias } from 'src/engine/twenty-orm/sql/utils/build-co
import { TwentyOrmException } from 'src/engine/twenty-orm/exceptions/twenty-orm.exception';
describe('WorkspaceSelectQueryBuilder selection', () => {
+ it('should preserve raw dates only for pagination without global parser setup', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder();
+ const timestamp = '2026-01-01 00:00:00.1239+00';
+
+ await queryBuilder.getRawAndEntities();
+
+ const parsers = executedStatements[0].types;
+
+ expect(parsers?.getTypeParser(types.builtins.TIMESTAMPTZ)(timestamp)).toBe(
+ timestamp,
+ );
+ expect(parsers?.getTypeParser(types.builtins.DATE)('2026-01-01')).toBe(
+ '2026-01-01',
+ );
+ expect(
+ types.getTypeParser(types.builtins.TIMESTAMPTZ)(timestamp),
+ ).toBeInstanceOf(Date);
+ expect(
+ types.getTypeParser(types.builtins.DATE)('2026-01-01'),
+ ).toBeInstanceOf(Date);
+
+ await queryBuilder.getMany();
+
+ expect(executedStatements[1].types).toBeUndefined();
+ });
+
it('should select the requested columns with alias-prefixed output names', () => {
const { queryBuilder } = buildQueryBuilder();
diff --git a/packages/twenty-server/src/engine/twenty-orm/query-builder/workspace-select-query-builder.ts b/packages/twenty-server/src/engine/twenty-orm/query-builder/workspace-select-query-builder.ts
index 4824de394f5..09cfddf116a 100644
--- a/packages/twenty-server/src/engine/twenty-orm/query-builder/workspace-select-query-builder.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/query-builder/workspace-select-query-builder.ts
@@ -1,3 +1,4 @@
+import { type QueryConfig, types } from 'pg';
import {
type FeatureFlagKey,
type ObjectsPermissions,
@@ -606,7 +607,17 @@ export class WorkspaceSelectQueryBuilder implements WhereExpressionLike {
entities: T[];
raw: Record<string, unknown>[];
}> {
- const rows = await this.executeSelect();
+ const rows = await this.executeSelect({
+ types: {
+ getTypeParser: (oid, format) =>
+ types.getTypeParser(
+ oid === types.builtins.TIMESTAMPTZ || oid === types.builtins.DATE
+ ? types.builtins.TEXT
+ : oid,
+ format,
+ ),
+ },
+ });
return {
raw: rows,
@@ -1216,13 +1227,17 @@ export class WorkspaceSelectQueryBuilder implements WhereExpressionLike {
private async executeSelect(options?: {
allowPlainToManyJoins?: boolean;
+ types?: QueryConfig['types'];
}): Promise<Record<string, unknown>[]> {
this.context.onBeforeExecute(this);
const { sql, parameters } = this.buildSelectStatement(options);
const compiled = compileNamedParameters(sql, parameters);
- return this.context.executor.execute(compiled);
+ return this.context.executor.execute({
+ ...compiled,
+ types: options?.types,
+ });
}
private buildSelectStatement(options?: { allowPlainToManyJoins?: boolean }): {
diff --git a/packages/twenty-server/src/engine/twenty-orm/sql/utils/compile-named-parameters.util.ts b/packages/twenty-server/src/engine/twenty-orm/sql/utils/compile-named-parameters.util.ts
index 85ec8e182eb..b5f648cc66d 100644
--- a/packages/twenty-server/src/engine/twenty-orm/sql/utils/compile-named-parameters.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/sql/utils/compile-named-parameters.util.ts
@@ -1,3 +1,5 @@
+import { type QueryConfig } from 'pg';
+
import {
TwentyOrmException,
TwentyOrmExceptionCode,
@@ -8,6 +10,7 @@ export type NamedParameters = Record<string, unknown>;
export type CompiledStatement = {
text: string;
values: unknown[];
+ types?: QueryConfig['types'];
};
const PARAMETER_NAME_CHARACTER = /[A-Za-z0-9_]/;
diff --git a/packages/twenty-server/src/engine/twenty-orm/utils/__tests__/format-result.util.spec.ts b/packages/twenty-server/src/engine/twenty-orm/utils/__tests__/format-result.util.spec.ts
index fae4f9aed72..bc48c895cfb 100644
--- a/packages/twenty-server/src/engine/twenty-orm/utils/__tests__/format-result.util.spec.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/utils/__tests__/format-result.util.spec.ts
@@ -45,6 +45,38 @@ describe('formatResult', () => {
jest.clearAllMocks();
});
+ it.each([
+ '2026-01-01T00:00:00.123900Z',
+ '2026-01-01 00:00:00.1239+00',
+ '2026-01-01 05:30:00.1239+05:30',
+ ])('formats %s as a Date without changing the raw value', (createdAt) => {
+ const dateField = getFlatFieldMetadataMock({
+ universalIdentifier: '20202020-3c25-4d02-bf25-6aeccf7ea419',
+ objectMetadataId: '20202020-4c25-4d02-bf25-6aeccf7ea419',
+ name: 'createdAt',
+ type: FieldMetadataType.DATE_TIME,
+ });
+ const objectMetadata = getFlatObjectMetadataMock({
+ id: dateField.objectMetadataId,
+ universalIdentifier: dateField.objectMetadataUniversalIdentifier,
+ fieldIds: [dateField.id],
+ });
+ const rawRecords = [{ createdAt }, { createdAt: null }];
+
+ const records = formatResult(
+ rawRecords,
+ objectMetadata,
+ buildFlatEntityMaps([objectMetadata]),
+ buildFlatEntityMaps([dateField]),
+ );
+
+ expect(records).toEqual([
+ { createdAt: new Date('2026-01-01T00:00:00.123Z') },
+ { createdAt: null },
+ ]);
+ expect(rawRecords[0].createdAt).toBe(createdAt);
+ });
+
it('reuses derived field metadata per object type within one invocation', () => {
const companyObjectMetadataId = '20202020-3c25-4d02-bf25-6aeccf7ea419';
const personObjectMetadataId = '20202020-4c25-4d02-bf25-6aeccf7ea419';
diff --git a/packages/twenty-server/src/engine/twenty-orm/utils/format-result.util.ts b/packages/twenty-server/src/engine/twenty-orm/utils/format-result.util.ts
index 33657237374..792a046a7f5 100644
--- a/packages/twenty-server/src/engine/twenty-orm/utils/format-result.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/utils/format-result.util.ts
@@ -1,6 +1,6 @@
import { isPlainObject } from '@nestjs/common/utils/shared.utils';
-import { isNull } from '@sniptt/guards';
+import { isNull, isString } from '@sniptt/guards';
import {
FieldActorSource,
FieldMetadataType,
@@ -209,12 +209,14 @@ function formatResultRecursively<T>(
}
if (
- typeof rawUpdatedDateTime === 'string' ||
+ isString(rawUpdatedDateTime) ||
rawUpdatedDateTime instanceof Date ||
isPlainObject(rawUpdatedDateTime)
) {
// @ts-expect-error legacy noImplicitAny
- newData[dateTimeField.name] = rawUpdatedDateTime;
+ newData[dateTimeField.name] = isString(rawUpdatedDateTime)
+ ? new Date(rawUpdatedDateTime)
+ : rawUpdatedDateTime;
} else {
const stringifiedUnknownValue = stringifySafely(rawUpdatedDateTime);
diff --git a/packages/twenty-server/test/integration/graphql/suites/cursor-pagination-order-by.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/cursor-pagination-order-by.integration-spec.ts
index 22b391faca8..5f80e378deb 100644
--- a/packages/twenty-server/test/integration/graphql/suites/cursor-pagination-order-by.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/cursor-pagination-order-by.integration-spec.ts
@@ -3,6 +3,7 @@ import { randomUUID } from 'crypto';
import { createManyOperationFactory } from 'test/integration/graphql/utils/create-many-operation-factory.util';
import { findManyOperationFactory } from 'test/integration/graphql/utils/find-many-operation-factory.util';
import { makeGraphqlApiRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';
+import { makeRestApiRequest } from 'test/integration/rest/utils/make-rest-api-request.util';
import { deleteAllRecords } from 'test/integration/utils/delete-all-records';
const PAGE_SIZE = 3;
@@ -49,6 +50,7 @@ type PaginateAllParams = {
filter?: object;
gqlFields?: string;
first?: number;
+ last?: number;
};
const paginateForward = async ({
@@ -98,6 +100,8 @@ const paginateBackwardFrom = async ({
orderBy,
startingBefore,
gqlFields = 'id',
+ filter,
+ last = PAGE_SIZE,
}: PaginateAllParams & { startingBefore: string }) => {
const ids: string[] = [];
let before: string | undefined = startingBefore;
@@ -109,7 +113,8 @@ const paginateBackwardFrom = async ({
objectMetadataPluralName: 'opportunities',
gqlFields,
orderBy,
- last: PAGE_SIZE,
+ filter,
+ last,
before,
}),
).expect(200);
@@ -580,3 +585,268 @@ describe('Cursor pagination ordered by a TEXT field with empty values', () => {
]);
});
});
+
+describe('Cursor pagination ordered by a DATE_TIME field with rows sharing a millisecond', () => {
+ const opportunities = [
+ {
+ id: '55555555-5555-4555-8555-555555555555',
+ createdAt: '2020-01-01T00:00:00.123100Z',
+ },
+ {
+ id: 'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee',
+ createdAt: '2020-01-01T00:00:00.123500Z',
+ },
+ {
+ id: '11111111-1111-4111-8111-111111111111',
+ createdAt: '2020-01-01T00:00:00.123900Z',
+ },
+ {
+ id: 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
+ createdAt: '2020-01-01T00:00:00.124000Z',
+ },
+ {
+ id: '99999999-9999-4999-8999-999999999999',
+ createdAt: '2020-01-01T00:00:00.124500Z',
+ },
+ ];
+ const ascendingIds = opportunities.map(({ id }) => id);
+ const descendingIds = [...ascendingIds].reverse();
+ const filter = { id: { in: ascendingIds } };
+
+ beforeAll(async () => {
+ await deleteAllRecords('opportunity');
+
+ await makeGraphqlApiRequest(
+ createManyOperationFactory({
+ objectMetadataSingularName: 'opportunity',
+ objectMetadataPluralName: 'opportunities',
+ gqlFields: 'id',
+ data: opportunities.map(({ id, createdAt }, index) => ({
+ id,
+ name: `Same millisecond opportunity ${index + 1}`,
+ createdAt,
+ })),
+ }),
+ ).expect(200);
+ });
+
+ it('should keep returning millisecond values', async () => {
+ const response = await makeGraphqlApiRequest(
+ findManyOperationFactory({
+ objectMetadataSingularName: 'opportunity',
+ objectMetadataPluralName: 'opportunities',
+ gqlFields: 'createdAt',
+ filter,
+ orderBy: { createdAt: 'AscNullsLast' },
+ }),
+ ).expect(200);
+
+ expect(
+ response.body.data.opportunities.edges.map(
+ (edge: { node: { createdAt: string } }) => edge.node.createdAt,
+ ),
+ ).toEqual([
+ '2020-01-01T00:00:00.123Z',
+ '2020-01-01T00:00:00.123Z',
+ '2020-01-01T00:00:00.123Z',
+ '2020-01-01T00:00:00.124Z',
+ '2020-01-01T00:00:00.124Z',
+ ]);
+ });
+
+ it.each([
+ { operator: 'eq', expectedIds: ascendingIds.slice(0, 3) },
+ { operator: 'gt', expectedIds: ascendingIds.slice(3) },
+ ])(
+ 'should preserve millisecond $operator filters while paginating through GraphQL',
+ async ({ operator, expectedIds }) => {
+ const { ids } = await paginateForward({
+ orderBy: { createdAt: 'AscNullsLast' },
+ filter: {
+ and: [
+ filter,
+ { createdAt: { [operator]: '2020-01-01T00:00:00.123Z' } },
+ ],
+ },
+ first: 1,
+ });
+
+ expect(ids).toEqual(expectedIds);
+ },
+ );
+
+ it.each([
+ { operator: 'eq', expectedIds: ascendingIds.slice(0, 3) },
+ { operator: 'gt', expectedIds: ascendingIds.slice(3) },
+ ])(
+ 'should preserve millisecond $operator filters while paginating through REST',
+ async ({ operator, expectedIds }) => {
+ const ids: string[] = [];
+ let startingAfter: string | undefined = undefined;
+
+ for (let iteration = 0; iteration < opportunities.length; iteration++) {
+ const path: string =
+ `/opportunities?filter=createdAt[${operator}]:2020-01-01T00:00:00.123Z&order_by=createdAt[AscNullsLast]&limit=1` +
+ (startingAfter ? `&starting_after=${startingAfter}` : '');
+ const response = await makeRestApiRequest({
+ method: 'get',
+ path,
+ }).expect(200);
+
+ ids.push(
+ ...response.body.data.opportunities.map(
+ (opportunity: { id: string }) => opportunity.id,
+ ),
+ );
+
+ if (!response.body.pageInfo.hasNextPage) {
+ break;
+ }
+
+ startingAfter = response.body.pageInfo.endCursor;
+ }
+
+ expect(ids).toEqual(expectedIds);
+ },
+ );
+
+ it.each([
+ { direction: 'AscNullsLast', first: 1, expectedIds: ascendingIds },
+ { direction: 'AscNullsLast', first: 2, expectedIds: ascendingIds },
+ { direction: 'DescNullsFirst', first: 1, expectedIds: descendingIds },
+ ])(
+ 'should walk forward with $direction in pages of $first without skipping or repeating',
+ async ({ direction, first, expectedIds }) => {
+ const { ids } = await paginateForward({
+ orderBy: { createdAt: direction },
+ filter,
+ first,
+ });
+
+ expect(ids).toEqual(expectedIds);
+ },
+ );
+
+ it.each([
+ { direction: 'AscNullsLast', expectedIds: ascendingIds },
+ { direction: 'DescNullsFirst', expectedIds: descendingIds },
+ ])(
+ 'should walk backward with $direction without skipping or repeating',
+ async ({ direction, expectedIds }) => {
+ const forwardResponse = await makeGraphqlApiRequest(
+ findManyOperationFactory({
+ objectMetadataSingularName: 'opportunity',
+ objectMetadataPluralName: 'opportunities',
+ gqlFields: 'id',
+ filter,
+ orderBy: { createdAt: direction },
+ first: 3,
+ }),
+ ).expect(200);
+
+ const { ids } = await paginateBackwardFrom({
+ orderBy: { createdAt: direction },
+ filter,
+ last: 1,
+ startingBefore:
+ forwardResponse.body.data.opportunities.pageInfo.endCursor,
+ });
+
+ expect(ids).toEqual(expectedIds.slice(0, 2));
+ },
+ );
+});
+
+describe('Cursor pagination ordered by a relation DATE_TIME field with rows sharing a millisecond', () => {
+ const companies = [
+ { id: randomUUID(), createdAt: '2020-01-01T00:00:00.123100Z' },
+ { id: randomUUID(), createdAt: '2020-01-01T00:00:00.123500Z' },
+ { id: randomUUID(), createdAt: '2020-01-01T00:00:00.123900Z' },
+ ];
+ const relationOpportunityIds = [
+ '55555555-5555-4555-8555-000000000001',
+ 'eeeeeeee-eeee-4eee-8eee-000000000002',
+ '11111111-1111-4111-8111-000000000003',
+ ];
+
+ beforeAll(async () => {
+ await deleteAllRecords('opportunity');
+ await deleteAllRecords('company');
+
+ await makeGraphqlApiRequest(
+ createManyOperationFactory({
+ objectMetadataSingularName: 'company',
+ objectMetadataPluralName: 'companies',
+ gqlFields: 'id',
+ data: companies.map(({ id, createdAt }, index) => ({
+ id,
+ name: `Same millisecond company ${index + 1}`,
+ createdAt,
+ })),
+ }),
+ ).expect(200);
+
+ await makeGraphqlApiRequest(
+ createManyOperationFactory({
+ objectMetadataSingularName: 'opportunity',
+ objectMetadataPluralName: 'opportunities',
+ gqlFields: 'id',
+ data: relationOpportunityIds.map((id, index) => ({
+ id,
+ name: `Same millisecond company opportunity ${index + 1}`,
+ companyId: companies[index].id,
+ })),
+ }),
+ ).expect(200);
+ });
+
+ it.each(['AscNullsLast', 'DescNullsFirst'])(
+ 'should walk forward with %s without skipping or repeating',
+ async (direction) => {
+ const { ids } = await paginateForward({
+ orderBy: { company: { createdAt: direction } },
+ filter: { id: { in: relationOpportunityIds } },
+ first: 1,
+ });
+
+ expect(ids).toEqual(
+ direction === 'AscNullsLast'
+ ? relationOpportunityIds
+ : [...relationOpportunityIds].reverse(),
+ );
+ },
+ );
+
+ it.each(['AscNullsLast', 'DescNullsFirst'])(
+ 'should walk backward with %s without skipping or repeating',
+ async (direction) => {
+ const orderBy = { company: { createdAt: direction } };
+ const filter = { id: { in: relationOpportunityIds } };
+ const response = await makeGraphqlApiRequest(
+ findManyOperationFactory({
+ objectMetadataSingularName: 'opportunity',
+ objectMetadataPluralName: 'opportunities',
+ gqlFields: 'id',
+ orderBy,
+ filter,
+ first: 3,
+ }),
+ ).expect(200);
+
+ expect(response.body.errors).toBeUndefined();
+
+ const { ids } = await paginateBackwardFrom({
+ orderBy,
+ filter,
+ last: 1,
+ startingBefore: response.body.data.opportunities.pageInfo.endCursor,
+ });
+ const expectedIds =
+ direction === 'AscNullsLast'
+ ? relationOpportunityIds
+ : [...relationOpportunityIds].reverse();
+
+ expect(ids).toEqual(expectedIds.slice(0, -1));
+ },
+ );
+});