Fork me on GitHub

datamodeljs

Data Model Management Library for JavaScript

The main goal of datamodeljs is to ensure a consistent and reliable data structure when communicating with a backend service. In all projects the data structure between backend and frontend can be considered as an interface contract. If either side changes the structure of the data - the counterpart has problems validating and handling the data. In order to avoid long lasting debugging sessions in a JavaScript application we use datamodeljs to read in and validate the delivered data against the defined entity model classes. A secondary goal of datamodeljs is the ability to manage entity objects and keep track of their states (e.g. newly created, marked as changed, marked for deletion) This entity object management allows the JavaScript application to manipulate business objects on block before sending them anywhere. In order to be able to handle complex business objects datamodeljs implements a bi-directional entity model. See the Github source code for more documentation and download options via NPM.

Simple Example (interactive)

This example shows the definition of entity classes using native types. You can use the class definition and the object input for trying some around. The output console will show you immediatly any error (as good as the javascript error messages will be). If everything is working you will see the resulting entity object(s) in the output. Some attributes are marked in gray because they are entity object internal and would by default not be printed or cloned.
 
Class definition (JavaScript):  
var dm = datamodeljs.dm() dm.define("Person", )
Object input (JSON):  
dm.create("Person", )
Output console:  

Relationship Example

This example shows the definition of entity classes defining relationships to other classes. The object input shows two different ways to relate objects together.
The first option is to simply relate to the unique id of the other object. TodoTask with id = 1 is created and referenced in the TodoList "tasks" list.
The second option is to simply create the relationship with inline elements. TodoTask with id = 2 is created on the fly as the TodoList is created since it is embedded in the TodoList.
 
Class definition (JavaScript):  
var dm = datamodeljs.dm() dm.define("TodoList", { "id": "@number", "name": "string", "tasks": "TodoTask*" }) dm.define("TodoTask", { "id": "@number", "assignee": "string", "desc": "string", "state": "string" })
Object creation (JavaScript):  
dm.create("TodoTask", { "id": 1, "assignee": "Max Mustermann", "desc": "Cut the grass", "state": "open" }) dm.create("TodoList", { "id": 1, "name": "Tasks@Home", "tasks": [ 1, { "id": 2, "assignee": "Susan Mustermann", "desc": "Wash the dishes", "state": "done" } ] })
Output console:  

Bi-directional Example

This example shows the definition of entity classes defining bi-directional relationships between classes.
In this example it doesn't matter in which order the objects are created. You can create the Milestone at first or the Task. Datamodeljs recognizes stub objects (referenced, but not yet loaded objects) and handles the bi-directional reference with the creation of a stub object which is replaced with the real object later on. Don't be scared of the output of this example - we handled the cyclomatic reference by breaking it into a ∞cycle∞ warning
 
Class definition (JavaScript):  
var dm = datamodeljs.dm() dm.define("Milestone", { "id": "@string", "name": "string", "tasks": "Task*" }) dm.define("Task", { "id": "@number", "assignee": "string", "desc": "string", "milestone": "Milestone" })
Object creation (JavaScript):  
dm.create("Milestone", { "id": "2016/09/01", "name": "Release 1.0.0", "tasks": [1, 2] }) dm.create("Task", { "id": 1, "assignee": "project leader", "desc": "Setup project", "milestone": "2016/09/01" }) dm.create("Task", { "id": 2, "assignee": "build manager", "desc": "Prepare build environment", "milestone": "2016/09/01" })
Output console:  

Copyright © 2015-2016 msg systems ag
Available under MIT distribution license.