Save and Update Entities ​
This guide explains how to save new Persisted Entity instances and update existing ones.
Prerequisites ​
- You have a Ruleset with persistency enabled.
- You have a Persisted Entity configured. See Create a Persisted Entity.
Save a new instance ​
To save a new instance of a Persisted Entity, create the instance and call .save() on it.
- Open your Ruleset and create a Rule named
SavePerson. - Enter the following expression:
let person = new Person({ FirstName: 'Jane', LastName: 'Doe', Birthdate: '1990-06-15' })
person.save()
return person.getId()- Save the Ruleset and click Try it out to verify the entity is saved.
The first call to .save() creates a new record in the database and assigns an ID to the instance. Subsequent calls to .save() on the same instance update the existing record.
Update an existing instance ​
To update an existing entity, retrieve it first, modify its properties, then call .save() again.
- Create a Rule named
UpdatePersonwith the following expression:
let results = Person.getAll()
let person = results.items[0]
person.FirstName = 'John'
person.save()
return personThe entity is updated in the database. The ID remains the same.
Save multiple instances at once ​
To save an array of entity instances in a single operation, use the static Person.save() method.
- Create a Rule with the following expression:
let person1 = new Person({ FirstName: 'Alice', LastName: 'Smith', Birthdate: '1985-03-22' })
let person2 = new Person({ FirstName: 'Bob', LastName: 'Jones', Birthdate: '1978-11-05' })
return Person.save([person1, person2])The method returns the saved instances, including their assigned IDs.
Next steps ​
For a full overview of all available methods, see the Persisted Entities Language Reference.