-
Hello guys, I was asking if this is possible to retrieve random rows from Postgresql DB with this ORM. This is what I tried to do using raw SQL query fragments but I get this error : getSingle: publicProcedure
.input(getSingleProductSchema)
.query(async ({ ctx: { entities, lang, raw }, input: { slug } }) => {
const product = await entities.product.findOneOrFail(
{ slug },
{ populate: ["images", "materials", "categories"] },
)
const similarProducts = await entities.product.find(
{
id: { $ne: product.id },
categories: product.categories.map((category) => category.id),
stock: { $gt: 0 },
},
{
limit: 6,
orderBy: raw("RANDOM()"),
},
)
return {
result: {
...formatProduct(product, lang, "product"),
similarProducts: similarProducts.map(
(similarProduct) => similarProduct.name[lang],
),
},
}
}) For your knowledge, I am using tRPC for my backend development. |
Beta Was this translation helpful? Give feedback.
Answered by
B4nan
Apr 29, 2024
Replies: 1 comment 1 reply
-
This needs to be an object, keys map to property names and values to the order (asc/desc). Try |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
SeRiice
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This needs to be an object, keys map to property names and values to the order (asc/desc). Try
orderBy: { [raw("RANDOM()")]: 'asc' }
.