antonwilhelm
antonwilhelm

Reputation: 7493

Ordering nested / second level Arrays with Prisma?

I am using Prisma & PostgreSQL. Here I grab some stuff:

await prisma.items.findMany({
        where: { itemId: itemId },
        include: {
            modules: {
                include: {
                    lessons: true
                }
            }
        }
    });

I do not need to order the items themselves, but I would like to order the modules & lessons that I get back. Both have an INT property (called: number) on which I could perform the ordering, but I do not know how to do this with prisma / postgresql, or even if it's possible.

Any ideas?

Upvotes: 5

Views: 9206

Answers (1)

Tasin Ishmam
Tasin Ishmam

Reputation: 7218

You can use the orderBy operator for this.

Here's what the query would look like for your use-case:

const data =  await prisma.items.findMany({
    where: {itemId: itemId},
    include: {
        modules: {
            orderBy: {
                number: 'asc'
            },
            include: {
                lessons: {
                    orderBy: {
                        number: 'asc'
                    }
                }
            }
        }
    }
})

The article on filtering and sorting contains more information on this.

Upvotes: 9

Related Questions