Skip to content

Commit

Permalink
✨ Add Model#beforeCommit(document, options) support
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Oct 19, 2023
1 parent a6ff810 commit 2ccd68f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
34 changes: 22 additions & 12 deletions lib/app/helper_model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1771,28 +1771,33 @@ Model.setMethod(function compose(data, options) {
/**
* Create a record in the database
*
* @author Jelle De Loecker <jelle@develry.be>
* @author Jelle De Loecker <jelle@elevenways.be>
* @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) {

if (options.validate === false || !that.schema) {
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) {
Expand All @@ -1819,29 +1824,34 @@ Model.setMethod(function createRecord(data, options, callback) {
/**
* Update a record in the database
*
* @author Jelle De Loecker <jelle@develry.be>
* @author Jelle De Loecker <jelle@elevenways.be>
* @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) {

if (options.validate === false || !that.schema) {
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) {
Expand Down
15 changes: 1 addition & 14 deletions lib/class/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <jelle@elevenways.be>",
"keywords": [
"alchemy",
Expand Down

0 comments on commit 2ccd68f

Please sign in to comment.