Is there a way to set a field before insert/update...
# orm-help
j
Is there a way to set a field before insert/update op using a hook or something ?
c
If you refer to something like feathersjs no, but since you're writing everything, before/after doing the operation you can do whatever you'd like. Example:
Copy code
myCreateOperation = async (...) => {
    // Before code
    const result = await prisma.createOperation(...)
    //After code
    return result;
}
j
That’s what I was going to do, but with prisma, there is a lot of places where a node can be updated / created, e.g. in nested queries... I’m thinking of using a middleware but not sure if it’s the right way
c
Queries aren't updating/creating nodes, only Mutations are capable of doing it. I don't think that there are any side-effects in Mutations. PS: Maybe I don't get your point do...
j
oops I meant nested mutations
c
Ohhh.. Got it know, something like this
Copy code
createUser = async (..) => {
    // Before
    await prisma.createUser({
        ...
        posts: create({
            ....
        })
    })
    // After
}
c
If you want you can create posts async, with
//Before and //After
hooks, return the ids, and on create user, you can connect the returned ids.
j
it would be a nothmare to cover all the cases one by one, so I guess middlewares are my best options...
Ah
c
🙂 Yes
You're right, but at least you have an working piece 🙂
j
Thank you for helping anyway !
c
No problem!