Editing
Persist
The AbstractRepository#persist method is used to create/update an entity.
The persist method will assert that the provided entity will not break validations rules. You can use AbstractRepository#isValid, AbstractRepository#validate and AbstractRepository#validateProperty to check validation.
Be aware of the cascade operations defined in Adsum REST API documentation
Create
The creation occurred when there is no id specified on the entity given to the repository. The repository will set it's id to a new Symbol to ensure unity as the changes are not send to the API.
import { Category } from '@adactive/adsum-client-api';
// Create a new Category
const category = new Category();
category.name = "A Category";
// Create the category
em.getRepository("Category").persist(category);
// category.id is now a Symbol
Update
The update occurred when the id is specified on the entity given to the persist method.
// Update the Category
const clonedEntity = category.clone();
clonedEntity.name = "THE Category";
// Update the category
em.getRepository("Category").persist(clonedEntity);
You should ALWAYS clone the entity otherwise it will affect the data integrity !
Remove
The remove method basically accept the entity itself, a Reference or it's identifier.
// Remove the category
em.getRepository("Category").remove(category);
Manipulating associations
Reference
You can set the new reference by using Reference#set
method which accept null|int|symbol|Reference|AbstractEntity
category.logo.set(null);
category.logo.set(42);
category.logo.set(entityManager.getRepository('File').get(42));
Collection
You can set the new reference by using Collection#set
method which accept null|int|symbol|Reference|AbstractEntity
category.pois.set([42, entityManager.getRepository('Poi').get(42)]);
You can clear it using Collection#clear
category.pois.clear();
You can add element using Collection#add
category.pois.add(42);
You can remove element using Collection#remove
category.pois.remove(42);
OrderedCollection
You can set the new reference by using OrderedCollection#set
method which accept null|int|symbol|Reference|AbstractEntity
category.children.set([42, entityManager.getRepository('Category').get(42)]);
You can clear it using OrderedCollection#clear
category.children.clear();
You can add element using OrderedCollection#add
// Will happen at last position
category.children.add(42);
// Will insert at third position
category.children.add(44, 3);
You can remove element using OrderedCollection#remove
category.pois.remove(42);
You can remove element using OrderedCollection#removeAt
// Will remove the third element
category.pois.removeAt(3);
Flush
Modifications are only available in your EntityManager. If you want to save modification through the API you need to call EntityManager#flush method.
em.flush().then(() => {
// Changes are persists to the server and we retrieve also external changes
});
Events
There is default listener that are doing all the business logic of Adsum Data Model, so when persist / remove an entity the listener may end up by updating / removing other entities (see cascade operations for each entities in Adsum API Doc). If you want to make sure to keep your application up-to-date with the current state of the adsum-client-api, you must register repository events to help you to handle default event listeners.
For example, a simple persist may result in severals CREATE, UPDATE and DELETE events and implied all repositories.
Hence when you use write feature, you must subscribe to events to update your UI instead of trying to do it by hand.
To learn more about events, please go in the next page.