[Question] Can I use numeric value for cursor??
JaeWangL opened this issue · 2 comments
JaeWangL commented
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
interface TestObject {
id: number;
}
const result = await findManyCursorConnection<TestObject, TestObject>(
(args) => tests.findMany(args),
() => tests.count(),
{ first: {first}, after: {after} }
)
I want use numeric value for unique value
For that, I set my own TestObject
type when using findManyCursorConnection
But, after is always needed string value
Can I use numeric unique?
queicherius commented
Hey :) Relay uses cursor-based pagination, where the cursor is opaque. This means you can't pass in a number as-is.
However, you can still use your own encoder and decoder for the cursor: https://github.com/devoxa/prisma-relay-cursor-connection/tree/1ad12bb1645350943c850b7b8d78f6254de9f1ad#custom-cursors
So for your example it would be something like this:
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
interface TestObject {
id: number;
}
const result = await findManyCursorConnection<TestObject, TestObject>(
(args) => tests.findMany(args),
() => tests.count(),
{ first: first, after: after },
{
getCursor: (record) => ({ id: record.id }),
encodeCursor: (cursor) => Buffer.from(JSON.stringify(cursor)).toString('base64'),
decodeCursor: (cursor) => JSON.parse(Buffer.from(cursor, 'base64').toString('ascii')),
}
)
first
is the number of records you want (e.g. 5)after
is a base64-encoded JSON of the cursor (e.g.{ id: 12 }
)
Hope this answers your question!
JaeWangL commented
Thank you so much guys~~