From 130aff94e6bf3ed2403253314e5db8ecb14c4d7b Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 2 Aug 2024 13:08:00 -0300 Subject: [PATCH] log orm errors --- packages/models/src/updater.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/models/src/updater.ts b/packages/models/src/updater.ts index 8ee15c484e093..97acfa01699f2 100644 --- a/packages/models/src/updater.ts +++ b/packages/models/src/updater.ts @@ -89,12 +89,24 @@ export class UpdaterImpl implements Updater { throw new Error('Updater is not dirty'); } - this.dirty = true; - await this.model.updateOne(query, { + const update = { ...(this._set && { $set: Object.fromEntries(this._set) }), ...(this._unset && { $unset: Object.fromEntries([...this._unset.values()].map((k) => [k, 1])) }), ...(this._inc && { $inc: Object.fromEntries(this._inc) }), ...(this._addToSet && { $addToSet: { $each: Object.fromEntries(this._addToSet) } }), - } as unknown as UpdateFilter); + } as unknown as UpdateFilter; + + if ((process.env.NODE_ENV === 'development' || process.env.TEST_MODE) && Object.keys(update).length === 0) { + throw new Error('Nothing to update'); + } + + this.dirty = true; + + try { + await this.model.updateOne(query, update); + } catch (error) { + console.error('Failed to update', JSON.stringify(query), JSON.stringify(update, null, 2)); + throw error; + } } }