Lifecycle Event Behavior

Lifecycle events are used to perform heuristic data manipulations and/or business logic that applies to your data model. This tutorial contains an example of how it can be used to interact with your data model.

Steps to Use Lifecycle Events (Onyx Cloud Database)

1

Define the BeverageEffects Entity

Use the provided JSON schema to declare your entity in the Onyx Cloud Database.
1{
2  "tables": [
3    {
4      "name": "BeverageEffects",
5      "identifier": {
6        "name": "behaviorId",
7        "type": "Long",
8        "generator": "Sequence"
9      },
10      "attributes": [
11        {
12          "name": "behaviorId",
13          "type": "Long",
14          "isNullable": false
15        },
16        {
17          "name": "description",
18          "type": "String",
19          "isNullable": true
20        },
21        {
22          "name": "beverage",
23          "type": "String",
24          "isNullable": true
25        }
26      ],
27      "relationships": [],
28      "indexes": []
29    }
30  ],
31  "revisionDescription": "Schema for BeverageEffects"
32}
33
2

Create the Beverage Enum

Define the Beverage enum in your Kotlin code to represent beverage effects.
1const BEVERAGES = {
2  "BEER": {
3    preConsumption: "I'm feeling dangerous",
4    duringConsumption: "Let's make out on a plane and annoy the people around me!",
5    afterConsumption: "Hold my hair back"
6  },
7  "WATER": {
8    preConsumption: "Soooo thirsty",
9    duringConsumption: "Satisfied",
10    afterConsumption: "I gotta pee"
11  },
12  "COFFEE": {
13    preConsumption: "Very Sleepy",
14    duringConsumption: "Bouncing off the walls",
15    afterConsumption: "Zzzzzzzzzz"
16  }
17};
18
3

Apply Lifecycle Events in the BeverageEffects Class

Use lifecycle event annotations to perform actions at specific points in the entity"s lifecycle.
1// Event lifecycle simulation in JavaScript
2
3// These helper functions simulate lifecycle event handlers.
4// You would call these before or after your db operations as needed.
5
6async function beforeInsert(effects) {
7  effects.description = BEVERAGES[effects.beverage].preConsumption;
8}
9
10async function postUpdate(effects) {
11  effects.description = BEVERAGES[effects.beverage].duringConsumption;
12}
13
14async function postRemove(effects) {
15  effects.description = BEVERAGES[effects.beverage].afterConsumption;
16}
17
4

Observe the Behavior

Watch the behavior of the lifecycle events as you save, update, and delete the BeverageEffects entity.
1// Define a beverage effect
2const effectsOfWater = {
3  beverage: "WATER",
4  description: null
5};
6
7// Observe the behavior of the pre-insert listener
8await beforeInsert(effectsOfWater);
9await db.save("BeverageEffects", effectsOfWater);
10console.assert(effectsOfWater.description === BEVERAGES["WATER"].preConsumption, "After saving the behavior entity, the effects should be thirsty");
11
12// Observe the behavior of the pre-update listener
13await postUpdate(effectsOfWater);
14await db.save("BeverageEffects", effectsOfWater);
15console.assert(effectsOfWater.description === BEVERAGES["WATER"].duringConsumption, "After updating the behavior entity, the effects should be satisfied");
16
17// Observe the behavior of the pre-delete listener
18await postRemove(effectsOfWater);
19// Assuming we have a behaviorId after initial insert, for demonstration we pass it directly.
20// In a real scenario, you would retrieve the primary key from the saved entity.
21await db.delete("BeverageEffects", effectsOfWater.behaviorId);
22console.assert(effectsOfWater.description === BEVERAGES["WATER"].afterConsumption, "Before deleting the behavior entity, the effects should be 'I gotta pee'");
23
Notes:
  • After inserting the entity, the description is set to Beverage.WATER.preConsumption.
  • Updating the entity changes the description to Beverage.WATER.duringConsumption.
  • Deleting the entity updates the description to Beverage.WATER.afterConsumption.

Important Notes

  • Lifecycle events allow you to hook into the persistence lifecycle of an entity and perform custom logic.
  • Onyx open source supports various lifecycle annotations such as @PostInsert, @PostUpdate, @PostRemove, and more however; the cloud version does not support lifecycle events on the server yet.
  • You can define multiple annotations on a single method if you want the same logic to be executed at different lifecycle events.

Next Steps

Now that you have learned how to use lifecycle events, you can explore more advanced topics: