Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ E.g.
{
"edges": [
{
"cursor": "SuWlzjWxdtSVDzgPp_mVdQH7S4pbbIpNqmJJFWwXOws.9732ntsWW_36ePfEiU1_85i4VHGpnlt60LcekRaV6hZiIfLaDEJyTxv4_mT0gVYjC05he25PbktssRXIQMdfnwfl2PkS47CW75s-XwbfYuhuaZJZfUpZLW5O7xVWf5R2YD7FEdd53lDlXJDbEE5TvRvM8TNkhCSh8LTnJEFNqADHkbWz-H7pDPOaOBsLE3n_EUYcf498pgjPJCRN",
"cursor": "l1X624m67Z5aYShVOLrThEcP7c-ezmCc4C48Dvxtt98.x7zYjxX9VEWDA1KAnJii8zyw5DP_OdIRnSkXATGhwTy6Wf0SSkjdjq6pTl9qxhp87EI-85pUJW9Thz_A6F_8BzlgccgDV-hXWjEj3CsGl96tSaA-X0_qNWBu425Mt6t5j3wNSdk8sSArBQ",
"node": {
"id": 1,
"first_name": "Joe",
Expand Down
808 changes: 248 additions & 560 deletions src/__snapshots__/sql-cursor-pagination.test.ts.snap

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/arrays.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { MaybePromise } from './maybe-promise';
import { Cursor } from './zod-models/cursor';
import { FieldValue } from './zod-models/field-value';
import { FieldWithOrder } from './zod-models/field-with-order';
import { FieldWithValue } from './zod-models/field-with-value';

const ivCipherTextRegex = /^([^.]+)\.([^.]+)$/;

Expand Down Expand Up @@ -149,14 +148,14 @@ export function buildCursor<TNode extends Record<string, unknown>>({
node: TNode;
sortFields: readonly FieldWithOrder[];
}): Cursor {
const fields: FieldWithValue[] = [];
const fields: Cursor['fields'] = {};

for (const { field } of sortFields) {
const { alias } = parseFieldName(field);
if (!(alias in node)) {
throw new ErrUnexpected(`"${alias}" field is missing`);
}
fields.push({ field: alias, value: FieldValue.parse(node[alias]) });
fields[alias] = FieldValue.parse(node[alias]);
}

return { fields, queryName };
Expand Down
5 changes: 5 additions & 0 deletions src/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function areSetsEqual<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): boolean {
if (a.size !== b.size) return false;
for (const _a of a) if (!b.has(_a)) return false;
return true;
}
6 changes: 5 additions & 1 deletion src/sql-cursor-pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,11 @@ describe('SqlCursorPagination', () => {
query: {
beforeCursor: all.edges[0].cursor,
last: 1,
sortFields: [{ field: 'email', order: Asc }],
sortFields: [
{ field: 'email', order: Asc },
{ field: 'last_name', order: Asc },
{ field: 'id', order: Asc },
],
},
}),
).rejects.toThrowError(ErrBeforeCursorWrongSortConfig);
Expand Down
13 changes: 7 additions & 6 deletions src/sql-cursor-pagination.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pMap from 'p-map';
import { areArraysEqual } from './arrays';
import { notNull } from './assert';
import { RawCursor, buildCursor, encryptCursor, resolveCursor } from './cursor';
import { CursorSecret } from './cursor-secret';
Expand All @@ -22,6 +21,7 @@ import {
import { parseFieldName } from './field-name';
import { MaybePromise } from './maybe-promise';
import { FragmentBuilder, QueryBuilder } from './query-builder';
import { areSetsEqual } from './set';
import { Cursor } from './zod-models/cursor';
import { FieldWithOrder } from './zod-models/field-with-order';
import { GenericQueryResult } from './zod-models/generic-query-result';
Expand Down Expand Up @@ -309,9 +309,9 @@ export async function withPagination<

// fields in the cursor must match the ones being requested
if (
!areArraysEqual(
sortFields.map(({ field }) => parseFieldName(field).name),
cursor.fields.map(({ field }) => field),
!areSetsEqual(
new Set(sortFields.map(({ field }) => parseFieldName(field).name)),
new Set(Object.keys(cursor.fields)),
)
) {
if (type === 'before') {
Expand All @@ -331,7 +331,8 @@ export async function withPagination<
whereQuery.appendText(`(`);
for (let j = 0; j <= i; j++) {
const { field, order } = sortFields[j];
const { value } = c.fields[j];
const fieldName = parseFieldName(field).name;
const value = c.fields[fieldName];
const sign =
i === j
? maybeFlip(order, type === 'before') === Asc
Expand All @@ -341,7 +342,7 @@ export async function withPagination<

if (j > 0) whereQuery.appendText(` AND `);
whereQuery
.appendText(`${quoteField(parseFieldName(field).name)}${sign}`)
.appendText(`${quoteField(fieldName)}${sign}`)
.appendValue(value);
}
whereQuery.appendText(`)`);
Expand Down
5 changes: 3 additions & 2 deletions src/zod-models/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { z } from 'zod';
import { FieldWithValue } from './field-with-value';
import { FieldName } from './field-name';
import { FieldValue } from './field-value';
import { QueryName } from './query-name';

export const Cursor = z
.object({
fields: z.array(FieldWithValue).min(1),
fields: z.record(FieldName, FieldValue),
queryName: QueryName,
})
.strict()
Expand Down
9 changes: 0 additions & 9 deletions src/zod-models/field-with-value.ts

This file was deleted.