Events

At this point, you already know how to define the entity, and you probably have an understanding of all the "stuff around" like internal and external relations and so on. So you know how to define entities. Before we show how to store, retrieve, and query them, let's talk about events.

And no, don't expect an "ultra-sophisticated" event system (Note: Jet had that once too, but it was more for spite than for utility).

The events (and especially their handlers) within Jet DataModel are nothing more than a few methods that you may (or may not) implement within your entities and that Jet DataModel calls at the right moment.

afterLoad(): void

The name of the method shows that it is called after the object is loaded. That is, after Jet DataModel creates an instance of the corresponding class and sets its parameters with the values read from the database.

The method can be used to additionally check the integrity of an entity instance. Example: you have stored some localized content (it doesn't matter if it is articles, e-shop products, etc.) and a new localization has been added to the system. For example, your client is looking to expand into another market. In such a situation, it is necessary to check if the entity has all the necessary localized versions ready and if not, to add the missing localization.

I have no doubt you can think of other uses. But it must be remembered that this is a potentially problematic application site. Loading must always be as fast as possible.

beforeSave(): void

The method is called before the process of saving data to the database begins.

afterAdd(): void

This method is called after the saving process has been completed - specifically, the addition of a brand new record. When the method is called, the process is completely finished, the identifiers are generated and the entity is in a new state.

afterUpdate(): void

The name of the method shows that it is called after modifying an existing record - i.e. the method is called after finishing the process of modifying existing records in the database.

afterDelete(): void

And of course, the method that is called after the entity record(s) in the database has been deleted must not be missing. In this method, it is possible, for example, to delete related files, or to delete entities that are related to the deleted entity, but are linked only by an external session. It's simply a matter of cleaning up after a deletion ...

Previous chapter
Composite keys
Next chapter
Saving