Skip to content
v2.6

Create a Persisted Entity ​

Saving an Entity ​

Let's start with creating a simple 'Person' entity.

Creating the Person Entity ​

  1. Create a new Ruleset
    1. Check the 'Library' and 'Allow persistency' boxes under Version Settings
  2. Create a new Entity named 'Person'
  3. Add a 'FirstName' property of type string
  4. Add a 'LastName' property of type string
  5. Add a 'Birthdate' property of type date
  6. Now, add a new Calculated Property named 'Age' with the following expression:
    AGE(Birthdate)
  7. Finally, check the 'Allow persistency' box and save the Ruleset

Saving an instance of the Person entity ​

  1. Create a Rule named 'Test' with the following expression:
csharp
let person1 = new Person({ FirstName: 'John', LastName: 'Doe', Birthdate: '1980-01-01' })
person1.save()
return Person.getAll().items

Let'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.

  1. Create an Input named 'person1' of type 'Person'
  2. Change the expression of the 'Test' Rule to:
javascript
person1.save()
return Person.getAll().items
  1. Save the Ruleset and click on "Try it out" at the top.
  2. Enter some data into the input on the left
  3. 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.

MethodDescription
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.

MethodDescription
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'