design-first / system-runtime

A JavaScript library that runs systems
https://designfirst.io/systemruntime/
Apache License 2.0
121 stars 35 forks source link

Simplify the way we create schemas, models and types #62

Closed ecarriou closed 6 years ago

ecarriou commented 6 years ago

Important note: these modifications will not break the current APIs behaviors.

We simplify the way we create schemas, models and types. It works well for defining simple classes and structure. Example:

const mm = runtime.require('metamodel');

// defining a schema
mm.schema('Person', {
  firstName: 'property',
  address: 'property'
});

// overriding the generated model
mm.model('Person', {
  firstName: 'string',
  address: 'address'
});

// defining a new type
mm.type('address', {
  street: 'string',
  city: 'string'
});

// defining an enumeration type
mm.type('city', [
  'Paris',
  'Rennes'
]);

// model creation
mm.create();

// get the class
const Person = runtime.require('Person');

// create an instance
const person = new Person({
  firstName: 'a name',
  address: {
    street: 'a street',
    city: 'Rennes'
  }
});

// use instance
person.address().street();