The schema.org define two schemas interesting for our use case, https://schema.org/Person and https://schema.org/BuyAction. The BuyAction schema will be use to store a specific expense (see for example form G103 3.1 "auditory and oral messages").
This schema contains 4 properties that we are going to use to store the expenses linked to the form:
- price
- priceCurrency
- description
- identifier
And the agent property of the BuyAction schema can link a Person to a BuyAction. Therefore we can store an expense by defining theses triples.
- BuyAction agent <person_ref>
- BuyAction identifier <field_id>
- BuyAction description "auditory and oral messages"
- BuyAction price 123
- BuyAction priceCurrrency "EUR"
Let's look at how we can convert these triples into a typescript code:
interface BuyActionData {
identifier: string;
description: string;
price: number;
priceCurrency: string;
}
function createExpense(doc: TripleDocument, person: TripleSubject, buyActionData: BuyActionData) {
const buyAction = doc.addSubject();
// Set the type to BuyAction
buyAction.addRef(rdf.type, schema.BuyAction);
// Add all the triples needed to define the BuyAction
buyAction.addRef(schema.agent, person.asRef());
buyAction.addString(schema.identifier, buyActionData.identifier);
buyAction.addString(schema.description, buyActionData.description);
buyAction.addInteger(schema.price, buyActionData.price);
buyAction.addString(schema.priceCurrency, buyActionData.priceCurrency);
// Don't forget that it is not save yet, doc.save([buyAction]) must be called for that
return buyAction;
}Usage example:
const doc = createAppDocument(appStorage, 'dummyExpense.ttl');
const person = doc.addSubject();
person.addRef(rdf.type, schema.Person);
person.addString(schema.givenName, 'John');
person.addString(schema.familyName, 'Smith');
// The identifier must be unique, and this identifier needs to be used when you want to access the data
// Here I used '3.1' for the identifier however we should define an identifier for each field in the form
// This identifier should be common between the declaration team and the front end team
const buyAction = createExpense(doc, person, {identifier: '3.1', description: 'auditory and oral messages', price: 43, priceCurrency: 'EUR'});
doc.save([person, buyAction]);