W
Wasp3mo ago
cmilazzo

Is there a CRUD entity update example?

I don't see an example in the documentation. I'm wondering if we have to first do a find, then a delete and then a new create?!? How do we actually update the entity without removing the old one?
5 Replies
kapa.ai
kapa.ai3mo ago
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, you don't need to perform a find, delete, and then create to update an entity. Wasp provides a straightforward way to update an entity using the update operation in the CRUD declaration. Here is an example of how you can declare an update operation for a Task entity in Wasp:
crud Tasks {
entity: Task,
operations: {
update: {},
},
}
crud Tasks {
entity: Task,
operations: {
update: {},
},
}
In the update operation, Wasp uses the field marked with @id in the Prisma schema as the id field. Here is how the update operation is implemented:
// Wasp uses the field marked with `@id` in Prisma schema as the id field.
return Task.update({ where: { id: args.id }, data: args.data })
// Wasp uses the field marked with `@id` in Prisma schema as the id field.
return Task.update({ where: { id: args.id }, data: args.data })
This operation updates an existing entity based on the provided id and data. The data argument contains the new values for the entity. Please note that in the default create and update implementations, all of the data that the client sends to the server is saved. This might not be desirable in some cases. Wasp is planning to add validation of action input in the future, where only the data that the user is allowed to change will be saved. For now, you can provide an override function to implement the validation logic yourself. You can find more details in the Wasp documentation (https://wasp-lang.dev/docs/data-model/crud#declaring-a-crud-with-default-options). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @cmilazzo, 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?
cmilazzo
cmilazzoOP3mo ago
This doesn't answer my question. I'm implementing a custom update function. In this function, I'm only updating one field. I'm not sure how to actually call any backend/default functions that may exist to update the file. Or do I have to just update directly in the Database?
MEE6
MEE63mo ago
Wohooo @cmilazzo, you just became a Waspeteer level 1!
miho
miho3mo ago
Are you using the crud or action? In any case, you use the entity by name and call the update Prisma function. I'd advise going through the Wasp tutorial to get the hang of the basics first: https://wasp-lang.dev/docs/tutorial/actions
6. Modifying Data | Wasp
In the previous section, you learned about using Queries to fetch data.
cmilazzo
cmilazzoOP3mo ago
Hi @miho Thanks for the reply. I figured it out with some trial and error and reading docs over and over. :). Thanks!
Want results from more Discord servers?
Add your server