Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.41.x.
Deprecation of lifecycle events created before 5.34.0
In the 5.34.0 we changed the property names which define the lifecycle events in the Headless CMS. Old ones are still available, but are marked as deprecated. They will be removed in 5.35.0.
What you'll learn
  • what are lifecycle events
  • how lifecycle events work
  • how to subscribe to a lifecycle event

Lifecycle events using publish/subscribe pattern replace the hook plugins starting from version 5.18.0.

Overview
anchor

In our Headless CMS we provide lifecycle events available for you to hook into.

With the lifecycle events you can hook into a number of different operations. For example, when using DynamoDB + Elasticsearchexternal link as storage layer, we use the onSystemBeforeInstall to insert the template for Elasticsearch index.

System
anchor

onSystemBeforeInstall
anchor

This event is triggered before the installation of the Headless CMS and insertion of initial “Ungrouped” group.

Event Arguments
anchor

PropertyDescription
tenantID of the current tenant
localecurrent locale

How to Subscribe to This Event?
anchor

onSystemAfterInstall
anchor

This event is triggered after the installation of the Headless CMS.

Event Arguments
anchor

PropertyDescription
tenantID of the current tenant
localecurrent locale

How to Subscribe to This Event?
anchor

onSystemInstallError
anchor

This event is triggered on Headless CMS installation error.

Event Arguments
anchor

PropertyDescription
tenantID of the current tenant
localecurrent locale

How to Subscribe to This Event?
anchor

Please note that in between onSystemBeforeInstall and onSystemAfterInstall we create a default CmsGroup named “Ungrouped” and, because of that, there are events onGroupBeforeCreate and onGroupAfterCreate being run.

Groups
anchor

onGroupBeforeCreate
anchor

This event is triggered before a new group is stored into the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which is going to be stored

How to Subscribe to This Event?
anchor

onGroupAfterCreate
anchor

This event is triggered after a new group is stored into the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which was stored

How to Subscribe to This Event?
anchor

onGroupCreateError
anchor

This event is triggered on group create error.

Event Arguments
anchor

PropertyDescription
groupGroup object which was going to be created
errorError object

How to Subscribe to This Event?
anchor

onGroupBeforeUpdate
anchor

This event is triggered before a group is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalGroup object which is received from the database
groupGroup object which is going to be stored

How to Subscribe to This Event?
anchor

onGroupAfterUpdate
anchor

This event is triggered before a group is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalGroup object which is received from the database
groupGroup object which was stored

How to Subscribe to This Event?
anchor

onGroupUpdateError
anchor

This event is triggered on group update error.

Event Arguments
anchor

PropertyDescription
originalGroup object received from the database
groupGroup object which was going to be updated
errorError object

How to Subscribe to This Event?
anchor

onGroupBeforeDelete
anchor

This event is triggered before a group is deleted from the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which is going to be deleted

How to Subscribe to This Event?
anchor

onGroupAfterDelete
anchor

This event is triggered after a group is deleted from the database.

Event Arguments
anchor

PropertyDescription
groupGroup object which was deleted

How to Subscribe to This Event?
anchor

onGroupDeleteError
anchor

This event is triggered on group delete error.

Event Arguments
anchor

PropertyDescription
groupGroup object which was going to be deleted
errorError object

How to Subscribe to This Event?
anchor

Model
anchor

onModelBeforeCreate
anchor

This event is triggered before a new model is stored into the database.

Event Arguments
anchor

PropertyDescription
inputUsers raw input data
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onModelAfterCreate
anchor

This event is triggered before a new model is stored into the database.

Event Arguments
anchor

PropertyDescription
inputUsers raw input data
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onModelCreateError
anchor

This event is triggered on model create error.

Event Arguments
anchor

PropertyDescription
inputUsers raw input data
modelModel object which was going to be created
errorthrown error

How to Subscribe to This Event?
anchor

onModelBeforeCreateFrom
anchor

This event is triggered before a newly cloned model is stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we are creating a new model
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onModelAfterCreateFrom
anchor

This event is triggered after a newly cloned model is stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we are creating a new model
modelModel object which was stored

How to Subscribe to This Event?
anchor

onModelCreateFromError
anchor

This event is triggered on model create from (clone) error.

Event Arguments
anchor

PropertyDescription
inputUsers raw input data
originalModel which is being cloned
modelModel object which was going to be created
errorthrown error

How to Subscribe to This Event?
anchor

onModelBeforeUpdate
anchor

This event is triggered before a model is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we received from the database
modelModel object which is going to be stored

How to Subscribe to This Event?
anchor

onModelAfterUpdate
anchor

This event is triggered after a model is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
originalModel object from which we received from the database
modelModel object which was stored

How to Subscribe to This Event?
anchor

onModelUpdateError
anchor

This event is triggered on model update error.

Event Arguments
anchor

PropertyDescription
inputUsers raw input data
originalModel which is being updated
modelModel object which was going to be stored
errorthrown error

How to Subscribe to This Event?
anchor

onModelBeforeDelete
anchor

This event is triggered before a model is deleted from the database.

Event Arguments
anchor

PropertyDescription
modelModel object which is going to be deleted

How to Subscribe to This Event?
anchor

onModelAfterDelete
anchor

This event is triggered after a model is deleted from the database.

Event Arguments
anchor

PropertyDescription
modelModel object which was deleted

How to Subscribe to This Event?
anchor

onModelDeleteError
anchor

This event is triggered on model delete error.

Event Arguments
anchor

PropertyDescription
ModelModel object which was going to be deleted
errorthrown error

How to Subscribe to This Event?
anchor

Entry
anchor

Note that storageEntry, originalStorageEntry, latestStorageEntry and publishedStorageEntry are objects derived from the entry object which we want to store into the database. Those new objects have StorageTransformPlugin run on them to prepare them to be stored.

onEntryBeforeCreate
anchor

This event is triggered before an entry is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
entryEntry object which is going to be stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryAfterCreate
anchor

This event is triggered after an entry is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
entryEntry object which was stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryCreateError
anchor

This event is triggered on entry create error.

Event Arguments
anchor

PropertyDescription
inputRaw user input
entryEntry object which was going to be stored
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

onEntryRevisionBeforeCreate
anchor

This event is triggered before a new entry is created from originating entry and is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
entryEntry object which is going to be stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryRevisionAfterCreate
anchor

This event is triggered after a new entry is created from originating entry and is stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
storageEntryEntry object prepared to be stored into the database
entryEntry object which was stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryCreateFromError
anchor

This event is triggered on entry create from error.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry
entryEntry object which was going to be stored
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

onEntryBeforeUpdate
anchor

This event is triggered before an entry is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
entryEntry object which is going to be stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryAfterUpdate
anchor

This event is triggered after an entry is changed and stored into the database.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry which we received from the database
storageEntryEntry object prepared to be stored into the database
entryEntry object which was stored
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryUpdateError
anchor

This event is triggered on entry update error.

Event Arguments
anchor

PropertyDescription
inputRaw user input
originalOriginal entry
entryEntry object which was going to be stored
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

onEntryBeforeDelete
anchor

This event is triggered before all entry revisions are deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryAfterDelete
anchor

This event is triggered after all entry revisions are deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryDeleteError
anchor

This event is triggered on entry delete error.

Event Arguments
anchor

PropertyDescription
entryEntry object which was going to be deleted
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

onEntryRevisionBeforeDelete
anchor

This event is triggered before an entry revision is deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryRevisionAfterDelete
anchor

This event is triggered after an entry revision is deleted from the database.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be deleted
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryRevisionDeleteError
anchor

This event is triggered on entry delete error.

Event Arguments
anchor

PropertyDescription
entryEntry revision which was going to be deleted
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

onEntryBeforePublish
anchor

This event is triggered before an entry is published.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be published
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryAfterPublish
anchor

This event is triggered after an entry is published.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be published
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryPublishError
anchor

This event is triggered on entry publishing error.

Event Arguments
anchor

PropertyDescription
entryEntry revision which was going to be published
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

onEntryBeforeUnpublish
anchor

This event is triggered before an entry is unpublished.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be unpublished
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryAfterUnpublish
anchor

This event is triggered after an entry is unpublished.

Event Arguments
anchor

PropertyDescription
entryEntry object which is going to be unpublished
modelModel this entry belongs to

How to Subscribe to This Event?
anchor

onEntryUnpublishError
anchor

This event is triggered on entry unpublishing error.

Event Arguments
anchor

PropertyDescription
entryEntry revision which was going to be unpublished
modelModel this entry belongs to
errorthrown error

How to Subscribe to This Event?
anchor

Please, be aware that you can change what ever you want on the object before it is stored into the database, so be careful with changing the data.

Registering Lifecycle Event Subscriptions
anchor

System Lifecycle Events
anchor

For the subscriptions (your code) to be run, you must register it in the createHandler in the api/code/graphql/src/index.ts file.

api/code/graphql/src/index.ts

Group, Model and Entries Lifecycle Events
anchor

For the subscriptions (your code) to be run, you must register it in the createHandler in the api/code/headlessCMS/src/index.ts file.

api/code/headlessCMS/src/index.ts

Please be aware that the order of subscribing does matter: if you want some event subscription to be executed before some other one, add it first.