Direct access to prisma

Hello guys. I am facing this issue. I have a api in KY wasp project that receives a list of bids and I use this to send them to my users via email. The trick part is how to fetch my users in this case since this won't be managed by wasp , I tried to use query I call it inside the api logic I fail to provide de context or args there. Maybe I am missing something here and if you can see it please let me know
9 Replies
kapa.ai
kapa.ai3mo ago
Hi @Stefan Vitória. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Sure, I can help with that. If you need to fetch your users directly in your server-side code, you can use the Prisma Client directly. Wasp allows you to import the Prisma Client in your server code and use it to interact with your entities. Here's how you can do it:
import { prisma } from 'wasp/server'

const users = await prisma.user.findMany({})
import { prisma } from 'wasp/server'

const users = await prisma.user.findMany({})
In this example, user is the name of your entity. Replace it with the actual name of your entity. Please note that this is a lower-level way of interacting with your entities and it's recommended to use it only if you need a feature that Wasp doesn't provide. Normally, you would work with entities within the context of Operations (Queries & Actions). If you want to use a query, you should import it from wasp/server/operations and not from wasp/client/operations when you're on the server. Also, you need to provide the context as the first argument and then the arguments as the second argument. Here's an example:
import { getFilteredTasks } from 'wasp/server/operations'

const doneTasks = await getFilteredTasks(context, { isDone: true })
import { getFilteredTasks } from 'wasp/server/operations'

const doneTasks = await getFilteredTasks(context, { isDone: true })
In this example, getFilteredTasks is the name of your query. Replace it with the actual name of your query. For more information, you can check the following resources: - Working with Entities (https://wasp-lang.dev/docs/data-model/entities#working-with-entities) - Using Queries (https://wasp-lang.dev/docs/data-model/operations/queries#using-queries) - Github Issue about calling queries and actions on the server (https://github.com/wasp-lang/wasp/issues/1909) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Stefan Vitória, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Stefan Vitória
Stefan VitóriaOP3mo ago
@kapa.ai where the context that you passed to the query example you gave came from?
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The context object is automatically provided by Wasp when you're inside an operation (like a Query or Action). It contains useful information such as the current user session and entities. However, if you're calling a query directly from your server-side code (outside of an operation), you'll need to construct the context object yourself. Here's an example of how you might do this:
const context = {
user: { id: 1 }, // Replace with the actual user ID
entities: { /* Your entities here */ },
// ... any other context properties you need
}
const context = {
user: { id: 1 }, // Replace with the actual user ID
entities: { /* Your entities here */ },
// ... any other context properties you need
}
In this example, you would replace { id: 1 } with the actual user ID, and { /* Your entities here */ } with your actual entities. Please note that this is a lower-level way of interacting with your entities and it's recommended to use it only if you need a feature that Wasp doesn't provide. For more information, you can check the following resources: - Implementing Queries (https://wasp-lang.dev/docs/data-model/operations/queries#implementing-queries) - Github Issue about calling queries and actions on the server (https://github.com/wasp-lang/wasp/issues/1909) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Stefan Vitória, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho3mo ago
@sodic one for you for Monday
martinsos
martinsos3mo ago
@sodic I got this one. @Stefan Vitória I didn't quite get the situation you are having, could you please descvribe it in more details?
I have a api in KY wasp project
- What is KY? Did you mean to say you have an api route in your Wasp app, like api doSomething { ... }? Aaah I just realized that is probably "my Wasp project", KY -> my, right? Ok, so your api receives a list of bids. Now you want to send those, at that moment, to all users or maybe some of them via email? In that case, I think this should answer your question: https://wasp-lang.dev/docs/advanced/apis#using-entities-in-apis -> you can speicfy User entity and then do what you want with it -> get all users, just some users, ... -> and then send them emails or whatever. Let me know if this helps!
Custom HTTP API Endpoints | Wasp
In Wasp, the default client-server interaction mechanism is through Operations. However, if you need a specific URL method/path, or a specific response, Operations may not be suitable for you. For these cases, you can use an api. Best of all, they should look and feel very familiar.
Stefan Vitória
Stefan VitóriaOP3mo ago
Hello. The KY was a type, I meant *MY api. I have solved it. The problem wasn't in creating the api but in the logic I wanted to perform on it. Which is sending emails to the users like you said. The issue I was facing was in using the entities there, I was fixed in the operations logic and way of interacting with the db. But I read the docs and found out a solution, not what you mention but I will refactor it to match what you ve shared. Thanks @martinsos It was a very simple fix, but somehow I wasn't figuring it out, kkkk
martinsos
martinsos3mo ago
No worries @Stefan Vitória and glad to hear you fixed it! How did you do it though, I am interested? Maybe that was also ok solution and no need to change it to mine?
Stefan Vitória
Stefan VitóriaOP3mo ago
My problem actually was a way to reach de database, so i took the answer provided by kapa and used the prisma client, a more low level approach has kapa described it, because I needed to perform this find users logic and send email in a separated function in order to keep the single responsibility of the api logic. But I really missed the fact that I could access the context in the api as well and would simply pass it as an argument to the function. Now I am feeling ashamed kkkkkk. But in my defense I was coding for so long that nothing seemed obvious at that point I am migrating to yours because I was basically reinventing the wheel, and it's a more cleaner way as well
martinsos
martinsos3mo ago
Awesome! Hah no reason to be ashamed, it is probably on us that API is not easier to figure out! But it is something we are always improvement, so thanks for the feedback and I am glad you got it working :).
Want results from more Discord servers?
Add your server