pMullot
pMullot
Explore posts from servers
PPrisma
Created by pMullot on 9/23/2024 in #help-and-questions
Is "daisy-chaining Extensions" a good practice?
Hi, I need to execute many dependent processes upon "update" of my "users" table. For that, I understand the best option is to use Prisma Extensions. However, since each of these processes requires a fair amount of code, I'd like to separate them in different files, in order not to put everything inside the query.ysers.update block
query: {
users: {
async update({args, query}) => {
// do stuff here
}
}
}
query: {
users: {
async update({args, query}) => {
// do stuff here
}
}
}
The easiest way to proceed would be to extract each process inside its own function, but I'm having a really hard time to declare those functions with type safety, because of the type the args and query variables, as well as the return type. Another option would be to define several extensions and then daisy chain them like this: new PrismaClient().$extends(userExtension1).$extends(userExtension2) The issue with this technique is that I will probably end up executing the query in each extension, leading to unnecessary overload of the database. What are your recommendations? NB: I'm following those guidelines to write my extensions: https://www.youtube.com/watch?v=j5LU6q38E-c&t=627s
1 replies
DTDrizzle Team
Created by pMullot on 11/26/2023 in #help
How do you write a query to get the relations data together with a JOIN or a SUBQUERY?
I need to run something like:
SELECT * FROM POST p
WHERE p.ID IN (SELECT post_ID FROM ANOTHER_TABLE WHERE post_category = 'whatever')
SELECT * FROM POST p
WHERE p.ID IN (SELECT post_ID FROM ANOTHER_TABLE WHERE post_category = 'whatever')
We could also achieve the same result using a JOIN. However, I want to take advantage of the relations I have created in my schema to retrieve the actual data related to my top table. As far as I can read on the docs, to get the relations data, you have to use 'findMany' or 'findFirst'
const posts = await db.query.posts.findMany({
with: {
comments: true,
},
});
const posts = await db.query.posts.findMany({
with: {
comments: true,
},
});
I don't know how to make a JOIN with this syntax neither how to specify a subquery Can someone help please?
2 replies