Skip to content
Sutando
Esc
↑↓navigate↵open⌘Jpreview
On this page

Hooks

Hook into a model's lifecycle with events such as creating, created, updating, saved, deleting, restoring, and forceDeleted.

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, forceDeleting 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

Hook Description
creating, created When a new model is saved for the first time
updating, updated When an existing model is modified and the save method is called
saving, saved When a model is created or updated - even if the model’s attributes have not been changed
deleting, deleted When a model is deleted, include soft deletes
restoring, restored When a model is restored
trashed When a model is soft deleted
forceDeleteing, forceDeleted When a model is hard deleted

Declaring Hooks

There are currently two ways to programmatically add hooks:

class User extends Model {}

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

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

Hooks and Transactions

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();

Was this page helpful?