From 2ccd68f95a0b2f85bb4880752bfabd2406483ae2 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Thu, 19 Oct 2023 12:14:54 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`Model#beforeCommit(document,?= =?UTF-8?q?=20options)`=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/app/helper_model/model.js | 34 ++++++++++++++++++++++------------ lib/class/model.js | 15 +-------------- package.json | 2 +- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b6daf16..0044a8df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.3.20 (WIP) * Make `Base#callOrNext(name, args, next)` handle methods without callbacks +* Add `Model#beforeCommit(document, options)` support ## 1.3.19 (2023-10-18) diff --git a/lib/app/helper_model/model.js b/lib/app/helper_model/model.js index f1cbb8e6..2db640ce 100644 --- a/lib/app/helper_model/model.js +++ b/lib/app/helper_model/model.js @@ -1771,20 +1771,23 @@ Model.setMethod(function compose(data, options) { /** * Create a record in the database * - * @author Jelle De Loecker + * @author Jelle De Loecker * @since 0.2.0 - * @version 1.3.1 + * @version 1.3.20 * - * @param {Object} data The record data to check + * @param {Document} document * @param {Object} options * @param {Function} callback */ -Model.setMethod(function createRecord(data, options, callback) { +Model.setMethod(function createRecord(document, options, callback) { const that = this; // Normalize & clone the data, set default values, ... - data = this.compose(data, options); + let data = this.compose(document, options); + + // Turn it into a new document + document = this.createDocument(data); Function.series(function validate(next) { @@ -1792,7 +1795,9 @@ Model.setMethod(function createRecord(data, options, callback) { return next(); } - Pledge.done(that.schema.validate(that.createDocument(data)), next); + Pledge.done(that.schema.validate(document), next); + }, function doBeforeCommit(next) { + that.callOrNext('beforeCommit', [document, options], next); }, function done(err) { if (err) { @@ -1819,20 +1824,23 @@ Model.setMethod(function createRecord(data, options, callback) { /** * Update a record in the database * - * @author Jelle De Loecker + * @author Jelle De Loecker * @since 0.2.0 - * @version 1.3.1 + * @version 1.3.20 * - * @param {Object} data The record data to check + * @param {Document} document * @param {Object} options * @param {Function} callback */ -Model.setMethod(function updateRecord(data, options, callback) { +Model.setMethod(function updateRecord(document, options, callback) { const that = this; // Normalize the data, but no default values should be set (skip non present items) - data = this.compose(data, Object.assign({update: true}, options)); + let data = this.compose(document, Object.assign({update: true}, options)); + + // Turn it into a new document + document = this.createDocument(data); Function.series(function validate(next) { @@ -1840,8 +1848,10 @@ Model.setMethod(function updateRecord(data, options, callback) { return next(); } - Pledge.done(that.schema.validate(that.createDocument(data)), next); + Pledge.done(that.schema.validate(document), next); + }, function doBeforeCommit(next) { + that.callOrNext('beforeCommit', [document, options], next); }, function done(err) { if (err) { diff --git a/lib/class/model.js b/lib/class/model.js index a526edf9..1765f6bb 100644 --- a/lib/class/model.js +++ b/lib/class/model.js @@ -896,20 +896,7 @@ Model.setMethod(function saveRecord(document, options, callback) { next(); }); }, function doBeforeSave(next) { - - if (typeof that.beforeSave == 'function') { - let promise = that.beforeSave(document, options, next); - - if (promise) { - Pledge.done(promise, next); - } else if (that.beforeSave.length < 3) { - // If the method accepts no `next` callback, call it now - next(); - } - } else { - next(); - } - + that.callOrNext('beforeSave', [document, options], next); }, function emitSavingEvent(next) { that.emit('saving', document, options, creating, function afterSavingEvent(err, stopped) { return next(err); diff --git a/package.json b/package.json index ac815063..9d77877d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "alchemymvc", "description": "MVC framework for Node.js", - "version": "1.3.19", + "version": "1.3.20-alpha", "author": "Jelle De Loecker ", "keywords": [ "alchemy",