Using It
This page will guide you through an example, highlighting the unique aspects of Itergia Core.
Writing JavaScript Mutator Code
The mutators are JavaScript functions running inside a transaction. They are stateless, and interact only through its two arguments:
intent
contains the parameters and authorization information.system
is an interface to manipulate the schema's objects.
Here, we create a helper function called queryListByID
, to reduce repetition.
export async function createList(intent, system) {
const id = intent.generateIDBytes("list");
if (await queryListByID(system, id)) {
throw new Error(`duplicate list ID: ${id}`);
}
await system.create("List", {
...intent.fields,
id,
});
}
export async function addItem(intent, system) {
const list = await queryListByID(
system,
intent.fields.listId);
if (!list) {
throw new Error(
`unknown list: ${intent.fields.listId}`);
}
intent.fields.prototype.id =
intent.generateIDBytes('item');
list.fields.items.push(intent.fields.prototype);
await list.update(list.fields);
}
// Other mutator functions left out.
async function queryListByID(system, id) {
const query = system.query
.from("List")
.filter("fields.id == bytes(_0)",
{
args: [id],
});
return await query.asSingletonOr(undefined);
}