drizzle-pagination icon indicating copy to clipboard operation
drizzle-pagination copied to clipboard

how to with select used ?

Open productdevbook opened this issue 1 year ago • 3 comments

await db.select().from(users);

example code

productdevbook avatar Nov 04 '23 15:11 productdevbook

withCursorPagination returns an object of this type:

{
    orderBy: SQL[]
    limit: number
    where?: SQL
}

So, try something like this:

const { orderBy, limit, where } = withCursorPagination({
    where: eq(schema.user.name, 'tom'), // 'where' is optional
    limit: 32,
    cursors: [
        [
            schema.user.id, // Column to use for cursor 
            'asc', // Sort order ('asc' or 'desc')
            '94b5a795-5af4-40c3-8db8-a1c33906f5af' // Cursor value
        ]
    ]
})

// Then, use them with a .select query
const users = await db.select().from(users).where(where).orderBy(...orderBy).limit(limit)

miketromba avatar Nov 05 '23 16:11 miketromba

image

Why can't it sort the data correctly in such a data? It's correct when going forward, it's nonsense when coming back.

Is this happening because the registration date times are the same?

productdevbook avatar Nov 05 '23 16:11 productdevbook

Hmm, could you provide the code for the query you are making?

miketromba avatar Nov 24 '23 16:11 miketromba

This is really helpful. I have a similar challenge where i want to use use this great code though with a more complex query.

I have a query that needs to bring in data from a many to many relationship. I’m doing this with joins. I want to use your code though am struggling to find a way to integrate with the complex query, paginate and restructure the data.

I’m hoping you may have a tip of two for me to explore

mteichtahl avatar Sep 02 '24 11:09 mteichtahl