Hi guys, when updating an instance, how I should c...
# orm-help
y
Hi guys, when updating an instance, how I should check it for existence? For ex:
Copy code
await this.client.movieList.update({
      where: { id: parseInt(listId) },
      data: {
        movies: {
          delete: { id: parseInt(movieId) },
        },
      },
    });
In this case, if movieList doesn't contain movie with id=movieId, It's throwing error
r
Currently you need to query it beforehand. I think there is an open issue for this.
s
I encountered this too.
update()
threw for me in prod, but I couldn't find any indication from the docs that this method might throw. If this is intentional behaviour, it certainly seems like something that should be documented. Are there any other methods I should know about that will throw in undocumented ways?
r
Hey @Simon 👋 The best way currently here would be to perform two queries, one for checking and the other for updating. Also if you do want to create as well, there is
upsert
for that.
👍 1
s
OK, this is what I'm doing now, using two queries. Obv there's a race condition there, but that's a far edge case. The larger problem is that this update behavior is not obvious and not well documented, so it's likely another engineer on my team will make this mistake in the future, without any hint that it's a problem until it blows up in prod.
Alternatively, (and I know this is a big API change) if the
update()
method leveraged TypeScript instead of runtime exceptions, it could return some kind of well-typed result object with
{ success: boolean, result: T }
or something similar to avoid throwing and enforce via TS that all edge cases are handled by the programmer. Does that make sense?