Skip to content

Commit

Permalink
feat: execute "create" and "update" events before main "save" event
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j authored Jan 30, 2019
1 parent 6716b4b commit 8c35352
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/webiny-entity/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,28 +299,26 @@ class Entity {

this.processing = "save";

events.beforeSave !== false && (await this.emit("beforeSave", { params }));

if (existing) {
events.beforeUpdate !== false && (await this.emit("beforeUpdate", { params }));
} else {
events.beforeCreate !== false && (await this.emit("beforeCreate", { params }));
}

events.beforeSave !== false && (await this.emit("beforeSave", { params }));

const logs = _.get(this, "constructor.crud.logs");

try {
events.save !== false && (await this.emit("__save", { params }));
if (existing) {
events.update !== false && (await this.emit("__update", { params }));
} else {
events.create !== false && (await this.emit("__create", { params }));
}
events.save !== false && (await this.emit("__save", { params }));

params.validation !== false && (await this.validate());

events.__beforeSave !== false && (await this.emit("__beforeSave", { params }));

if (existing) {
events.__beforeUpdate !== false && (await this.emit("__beforeUpdate", { params }));
if (logs && this.isDirty()) {
Expand All @@ -335,16 +333,18 @@ class Entity {
}
}

events.__beforeSave !== false && (await this.emit("__beforeSave", { params }));

if (this.isDirty()) {
await this.getDriver().save(this, params);
}

events.__afterSave !== false && (await this.emit("__afterSave", { params }));
if (existing) {
events.__afterUpdate !== false && (await this.emit("__afterUpdate", { params }));
} else {
events.__afterCreate !== false && (await this.emit("__afterCreate", { params }));
}
events.__afterSave !== false && (await this.emit("__afterSave", { params }));

this.setExisting();
this.getModel().clean();
Expand All @@ -356,12 +356,12 @@ class Entity {
this.processing = null;
}

events.afterSave !== false && (await this.emit("afterSave", { params }));
if (existing) {
events.afterUpdate !== false && (await this.emit("afterUpdate", { params }));
} else {
events.afterCreate !== false && (await this.emit("afterCreate", { params }));
}
events.afterSave !== false && (await this.emit("afterSave", { params }));
}

/**
Expand Down

0 comments on commit 8c35352

Please sign in to comment.