Skip to content

Commit

Permalink
format and postSaveHook methods were added outside the process in ord…
Browse files Browse the repository at this point in the history
…er to be able to define methods for it
  • Loading branch information
gcipriani committed Feb 14, 2022
1 parent 167c401 commit b46ca97
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions lib/api-save-data.js
Expand Up @@ -22,6 +22,8 @@ const EndpointParser = require('./helpers/endpoint-parser');
module.exports = class ApiSaveData extends API {

/**
* Use this in case you're saving relationships with other models (mostly for relational databases)
* If don't have any relationship, there's no need to implement it.
* @static
* @return {Object}
*/
Expand All @@ -30,6 +32,8 @@ module.exports = class ApiSaveData extends API {
}

/**
* This is used to validate the ID received as path parameter.
* Defaults to an optional string or number.
* @static
* @return {Function}
*/
Expand All @@ -38,6 +42,8 @@ module.exports = class ApiSaveData extends API {
}

/**
* Used to validate the data received in the request, checking the data to be saved in the main entity.
* Defaults to an object with any property.
* @static
* @return {string}
*/
Expand All @@ -46,6 +52,8 @@ module.exports = class ApiSaveData extends API {
}

/**
* Used to validate the data received in the request, checking the data to be passed to the relationships.
* Defaults to an object partial with no properties.
* @static
* @return {Function}
*/
Expand Down Expand Up @@ -90,8 +98,7 @@ module.exports = class ApiSaveData extends API {

try {

if(this.format)
this.dataToSave.main = await this.format(this.dataToSave.main);
this.dataToSave.main = await this.format(this.dataToSave.main);

if(!(await this.shouldSave(this.dataToSave.main))) {

Expand All @@ -114,8 +121,7 @@ module.exports = class ApiSaveData extends API {
await apiSaveRelationships.process();
}

if(this.postSaveHook)
await this.postSaveHook(savedId, this.dataToSave.main);
await this.postSaveHook(savedId, this.dataToSave.main);

this.setBody({
id: savedId
Expand Down Expand Up @@ -147,6 +153,8 @@ module.exports = class ApiSaveData extends API {
/**
* Optional method allows you to validate if saving the item is really necessary. Is called after formatting.
*
* @async
* @param {Object} data - The data to save.
* @return {boolean} - True if should save, false otherwise.
*/
shouldSave() {
Expand Down Expand Up @@ -175,4 +183,27 @@ module.exports = class ApiSaveData extends API {
this.recordId = recordId;
this.parents = parents;
}

/**
* Use this to format your main record before it's saved.
*
* @async
* @param {Object} data - The data to format.
* @return {Object} - The formatted data.
*/
format(data) {
return data;
}

/**
* Use this to perform a task after saving your main record.
*
* @async
* @param {String} id - The id of the saved item
* @param {Object} data
* @return {Object}
*/
postSaveHook(id, data) {
return (id, data);
}
};

0 comments on commit b46ca97

Please sign in to comment.