lawjolla
06/14/2022, 7:27 PMfindMany
preserve id ordering? E.g. …
const getUser = await prisma.user.findMany({
where: {
id: { in: [22, 91, 14, 2, 5] },
},
})
and guarantee the order of the users is always 22, 91, etc?
I can only come up with a manual solution, but I’m not sure how performant it is against Prisma’s findMany, e.g.
const getUsers = (await Promise.all(ids.map(id => prisma.user.findUnique({ where: { id }})))
Austin
06/14/2022, 9:33 PMid
by default, so the order would not be preserved in this case.
Could you sort the array of ids before passing it to the query? That way it would be synced with the query results. Or do you have another use case?Jacob Martin
06/14/2022, 11:17 PMJacob Martin
06/14/2022, 11:18 PMconst ids = [22, 91, 14, 2, 5];
const users = prisma.user.findMany({
where: {
id: { in: ids },
},
});
const sorted = ids.map(id => users.find(user => user.id === id));
Jacob Martin
06/14/2022, 11:20 PMsorted
contains the users in the same order as ids
lawjolla
06/14/2022, 11:40 PM