0. Quick Start

Learn System Runtime in 5 minutes

In this little tutorial we will:

  • install System Runtime server-side,
  • create a sytem,
  • create a model,
  • add behaviors to the model,
  • create components,
  • start the system,
  • bundle the system,
  • install the bundle server-side,
  • install the bundle in a client app,
  • use System Runtime NoSQL Database API.

Install System Runtime

To install System Runtime server-side, you juste need to use npm:

npm install system-runtime --save

Learn more about Node.js

System Runtime is available as a node package. To learn mode about node, go to its webpage.

Create a system

Create a file in which you will write the code (example: system.js).
Then we can require System Runtime and use its APIs:

let runtime = require('system-runtime');

The first thing you have to do is to create a system that represents your application:

let system = runtime.system('mySystem');

Create a model

We will create a Jedi class:

UML representation of the class we will create

UML representation of the class we will create

To do so, get metamodel component to create your model:

let metamodel = runtime.require('metamodel');

Define your model:

metamodel.schema({
  '_name': 'Jedi',
  'firstName': 'property',
  'lastName': 'property',
  'nickname': 'property',
  'shout': 'method',
  'father': 'link'
});

Now we have define the model, we can create it:

metamodel.create();

Add behaviors to the model

Once the model is created, you can require the classes of your model:

let Jedi = runtime.require('Jedi');

In System Runtime, implementing a method is called adding a behavior. So now we will add a behavior to the shout method of Jedi class:

Jedi.on('shout', message => message + '!!');

Create components

Once the model and the behaviors are defined, you can create your components.

system.on('start', () => {
  let Jedi = this.require('Jedi');

  let anakin = new Jedi({
    'firstName': 'Anakin',
    'lastName': 'Skywalker',
    'nickname': 'Darth Vader'
  });

  let luke = new Jedi({
    'firstName': 'Luke',
    'lastName': 'Skywalker',
    'father': anakin
  });

  if (luke.father().nickname() === 'Darth Vader') {
    console.log(luke.shout('NOOOOOO'));
  }
});

As you see, the code is located in a behavior of the start method of your system. You have to do this to be able to export the system.

To create a component, just require the class and create the instance. The parameter of the instance will be an object that contains all the properties of the class you have defined.

In this example, we can add anakin as value of father property because we have defined the link in the model.

Start the system

To start the system, just call the start method of your system:

system.start();

Now execute your file and see the result:

node system.js

Bundle the system

To bundle the system, just call the bundle method:

console.log( runtime.bundle() );

Now execute your file:

{"_id":"d132781aaab10724","name":"mySystem","description":"","version":"0.0.1","master":true,"schemas":{"o186481992613bb7":{"_name":"Jedi","firstName":"property","lastName":"property","nickname":"property","father":"link","shout":"method","_id":"o186481992613bb7","_inherit":["_Component"]}},"models":{},"types":{},"behaviors":{"a1f78212f7d1d48f":{"_id":"a1f78212f7d1d48f","component":"Jedi","state":"shout","action":"message => message + '!!'","useCoreAPI":false,"core":false},"u126d2146a417495":{"_id":"u126d2146a417495","component":"d132781aaab10724","state":"start","action":"() => {\n  let Jedi = this.require('Jedi');\n\n  let anakin = new Jedi({\n    'firstName': 'Anakin',\n    'lastName': 'Skywalker',\n    'nickname': 'Darth Vader'\n  });\n\n  let luke = new Jedi({\n    'firstName': 'Luke',\n    'lastName': 'Skywalker',\n    'father': anakin\n  });\n\n  if (luke.father().nickname() === 'Darth Vader') {\n    console.log(luke.shout('NOOOOOO'));\n  }\n}","useCoreAPI":false,"core":false}},"components":{"Jedi":{"l1478a12c181b9c2":{"firstName":"Anakin","lastName":"Skywalker","nickname":"Darth Vader","father":"","_id":"l1478a12c181b9c2"},"y113e717d3f17f4a":{"firstName":"Luke","lastName":"Skywalker","father":"l1478a12c181b9c2","nickname":"","_id":"y113e717d3f17f4a"}}}}

As you see, all your code has been bundled in a JSON object.

See the full script

Full script can be found here.

Install a bundle server-side

Copy the JSON you get from the bundle and copy it in a file (system.json for example).
Create a new file (import.js for example) and start the bundle like this:

let runtime = require('system-runtime');

// install and start the bundle
runtime.install('system.json');

Now execute your file:

node import.js

System Runtime will install the bundle. The start method of the system will be called when the bundle will be installed.

Install a bundle client-side

Copy the JSON you get from the bundle and copy it in a file (system.json for example).
Then add this link tag in your HTML to install the bundle:

<link rel="system" type="application/json" href="system.json">

Then add this script tag in your HTML to install System Runtime:

<script src="https://cdn.jsdelivr.net/npm/system-runtime@2.0.0/dist/system-runtime.min.js"></script>

System Runtime will install the bundle on the load of the page. The start method of the system will be called when the bundle will be installed.

Run the page and look at the console of your browser:

Use System Runtime NoSQL APIs

Now type in the console:

runtime.require('db').collections().Jedi.count();

System Runtime stores all the component you create in its database. So the result of this script will be 2 because we have created 2 Jedi components.

About System Runtime NoSQL Database APIs

The APIs to query the database are mongoDB-like, so it is very easy to learn them.

Conclusion

Now that you have finished the quick start, you can go on learning System Runtime: let's design your model.


0. Quick Start

Learn System Runtime in 5 minutes