miketromba / drizzle-pagination

Easily paginate your Drizzle ORM queries
MIT License
52 stars 3 forks source link

how to with select used ? #2

Closed productdevbook closed 3 months ago

productdevbook commented 1 year ago
await db.select().from(users);

example code

miketromba commented 1 year ago

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)
productdevbook commented 1 year ago

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?

miketromba commented 11 months ago

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

mteichtahl commented 2 months ago

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