Create new types
To create a new type you need to call the type method of metamodel component:
// create the system
const system = runtime.system('example08');
// create the model
const metamodel = runtime.require('metamodel');
// new adress type
metamodel.type('address', {
'planet': 'string'
});
// new sex type
metamodel.type('sex', ['male', 'female']);
metamodel.schema('Jedi', {
'firstName': 'property',
'lastName': 'property',
'sex': 'property',
'address': 'property'
});
// override the model generated by the schema
metamodel.model('Jedi', {
'sex': 'sex',
'address': 'address'
});
metamodel.create();
const Jedi = runtime.require('Jedi');
new Jedi({
'_id': 'yoda',
'sex': 'male',
'firstName': 'Yoda',
'lastName': 'Master',
'address': {
'planet': 'Dagobah'
}
});
system.on('start', () => {
// get the component
const yoda = this.require('yoda');
// show Yoda's planet
console.log(yoda.address().planet());
});
// run the system
system.start();
Version compatibility
Remember that the example works both on server and browser.