Queries require "await"?

I just tried to do a simple
db.update(usersTable)
.set({ [slot]: itemId })
.where(eq(usersTable.id, userId))
db.update(usersTable)
.set({ [slot]: itemId })
.where(eq(usersTable.id, userId))
without an await and the query wasn't sent. When I added the await, it updated the database. Is this normal behavior? If so, why?
2 Replies
Angel
Angel5mo ago
Database actions are asynchronous processes. If you need to use the results of these actions, you must use await to ensure the promises are resolved. If you don't need the results, ensure that all promises are resolved before the execution completes to avoid potential issues, like not executing the update. The most efficient way to do this is by using Promise.all, which waits for all promises to be resolved without blocking the event loop.
A Dapper Raccoon
Some drivers like better-sqlite3 optionally support synchronous queries by ending your query builder chain with .all()/.get()/.values()/.all() - but yeah. Most will be async and should be used as a promise.
Want results from more Discord servers?
Add your server