Create a Persisted Entity ​
Saving an Entity ​
Let's start with creating a simple 'Person' entity.
Creating the Person Entity ​
- Create a new Ruleset
- Check the 'Library' and 'Allow persistency' boxes under Version Settings
- Create a new Entity named 'Person'
- Add a 'FirstName' property of type
string - Add a 'LastName' property of type
string - Add a 'Birthdate' property of type
date - Now, add a new Calculated Property named 'Age' with the following expression:
AGE(Birthdate) - Finally, check the 'Allow persistency' box and save the Ruleset
Saving an instance of the Person entity ​
- Create a Rule named 'Test' with the following expression:
let person1 = new Person({ FirstName: 'John', LastName: 'Doe', Birthdate: '1980-01-01' })
person1.save()
return Person.getAll().itemsLet's analyze what this code does.
The first line creates a new instance of the Person Entity. This only creates an Entity as usual and does not yet save it to the database.
The second line actually stores the Person entity in the database.
The final line returns all Person entities currently in the database.
If we were to save the Ruleset and open it in the sandbox ("Try it out"), each time we will execute the Ruleset, it will create a new Person and the 'Test' rule's output will keep growing in size.
Before we do that, let's make it so the user can enter Person's name and birthdate via the Ruleset's input.
- Create an Input named 'person1' of type 'Person'
- Change the expression of the 'Test' Rule to:
person1.save()
return Person.getAll().items- Save the Ruleset and click on "Try it out" at the top.
- Enter some data into the input on the left
- Execute the Ruleset
If you repeat steps 4-5, you will will notice how the output of the Test rule will increase until a size of 20 is reached.
This is because the .getAll() method is paginated and will only return 20 items each time it is called. To get to the other items we can either:
- Use a different page number (page 1, page 2, etc.)
- Use a different page size (return more items per page)
See the #static-methods section for more details on how to use
getAll().
Also note how the Age property is properly calculated for each entry in Test.
Language reference ​
These are the methods available when using Persisted Entities programmatically. These are divided into: Instance Methods and Static Methods.
Instance Methods ​
Instance methods are called on an instance of a Persisted Entity. These methods allow you to interact with the data and perform actions specific to a particular instance, such as saving changes, deleting the entity, or retrieving specific attributes.
| Method | Description |
|---|---|
| save() | Saves the instances. Can be used on new instances or to update an existing instance. For instance, calling it twice in a row on a new instance will first create a new entry in the database, followed by updating it. |
| delete() | Deletes the instance from the database. Only works on entities that already exist in the database. |
| setId(guid) | Sets the ID of the entity. Only useful in certain circumstances. Saving or fetching entities will automatically set the ID. |
| getId() | Returns the ID of the entity. |
Static Methods ​
Static methods are called on the Entity class itself, not on a specific instance. These methods are useful for actions that do not pertain to a particular entity instance, such as creating a new entity, listing all entities, finding entities by certain criteria, or deleting multiple entities.
| Method | Description |
|---|---|
| getAll(meta, entityFilter) meta: { pageIndex: number, pageSize: number, sortBy: string } entityFilter: { type: 'Equals' | 'And' |
| get(id) | Gets a single instance by ID |
| save(entities) | Saves an array of entities and returns the saved instances. |
| delete(meta, entityFilter) meta: { createdFrom: date, createdTo: date, modifiedFrom: date, modifiedTo: date } entityFilter: { type: 'Equals' | 'And' |