Skip to content
On this page

Hooks

Sutando models dispatch several events, allowing you to hook into the following moments in a model's lifecycle: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored, trashed and forceDeleted.

Event names ending with -ing are dispatched before any changes to the model are persisted, while events ending with -ed are dispatched after the changes to the model are persisted.

Available hooks

HookDescription
creating, createdWhen a new model is saved for the first time
updating, updatedWhen an existing model is modified and the save method is called
saving, savedWhen a model is created or updated - even if the model's attributes have not been changed
deleting, deletedWhen a model is deleted, include soft deletes
restoring, restoredWhen a model is restored
trashedWhen a model is soft deleted
forceDeletedWhen a model is hard deleted

When issuing a mass update or delete query via Sutando, the saved, updated, deleting, and deleted model events will not be dispatched for the affected models. This is because the models are never actually retrieved when performing mass updates or deletes.

Declaring Hooks

There are currently two ways to programmatically add hooks:

js
class User extends Model {}

User.creating(user => {
  //
});
js
class User {
  static boot() {
    this.creating(user => {
      //
    });

    this.created(user => {
      //
    });
  }
}

Hooks and Transactions

js
User.deleted(async (user, { client }) => {
  const query = user.related('posts');

  if (client) {
    query.transacting(client);
  }

  await query.delete();
});

const trx = await sutando.beginTransaction();

await user.delete({
  client: trx
});

await trx.commit();

Released under the MIT License. (dev)