From 74ffdf83254e166e7056dbd35d68344538409be2 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 2 Sep 2020 14:07:40 +0200 Subject: [PATCH] Model: add equals method --- src/db/Model.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/db/Model.ts b/src/db/Model.ts index 70dc4f0..148fbf2 100644 --- a/src/db/Model.ts +++ b/src/db/Model.ts @@ -184,6 +184,12 @@ export default abstract class Model { this._cached_exists = false; } + public async validate(onlyFormat: boolean = false, connection?: Connection): Promise { + return await Promise.all(this._properties.map( + prop => this._validators[prop]?.execute(prop, this[prop], onlyFormat, connection) + )); + } + public async exists(): Promise { if (this._cached_exists === undefined) { const query = this._factory.select('1'); @@ -196,10 +202,11 @@ export default abstract class Model { return this._cached_exists; } - public async validate(onlyFormat: boolean = false, connection?: Connection): Promise { - return await Promise.all(this._properties.map( - prop => this._validators[prop]?.execute(prop, this[prop], onlyFormat, connection) - )); + public equals(model: this): boolean { + for (const field of this._factory.getPrimaryKeyFields()) { + if (this[field] !== model[field]) return false; + } + return true; } public get table(): string {