Create a Model with Properties

While developing an Application we need to link the Business Objects to Application Models. This Guide demonstrates the process to create models and associate multiple properties with it. This guide also demonstrates the process to add the Model instances and persist them.

What you’ll build

Having followed this guide, you will have started an oeCloud based NodeJS application and -

  • created a new Model called Product with the following properties: code, name, category, price, offeredSince, active and description within the running application
  • use the automatically created CRUD REST API of the Product Model to add, read, modify and delete data into the corresponding table/collection of the created Model.

The Model would be created dynamically and made available to the application, meaning the model would be created while the application is running, without the need to restart the application (server).

What you’ll need

To complete this guide, you will need the following -

  • a running NodeJS application built using the oeCloud.
  • a working REST client. You can use the Linux cURL command as a REST client if you have access to a Linux machine or have Git Bash installed on your Windows machine. REST addons for browsers, like Postman for Firefox or Google Chrome also can be used to complete this guide.

How to complete this guide

Download and install sample application from here.

Once you have started the server, open a browser link: http://localhost:3000/explorer/.

Creating the Product Model

To create model, you will need to POST into ModelDefinition. We will be posting following data into ModelDefinition

{
  "name": "Product",
  "base": "BaseEntity",
  "strict": false,
  "plural": "Products",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "code": {
      "type": "string",
      "source": "code",
      "required": true
    },
    "name": {
      "type": "string",
      "source": "name",
      "required": true
    },
    "category": {
      "type": "string",
      "source": "category"
    },
    "price": {
      "type": "number",
      "source": "price"
    },
    "offeredSince": {
      "type": "date",
      "source": "offeredSince"
    },
    "active": {
      "type": "boolean",
      "source": "active"
    },
    "flavourable": {
      "type": "boolean",
      "source": "flavourable"
    },
    "flavours": {
      "type": [
        "string"
      ]
    },
    "description": {
      "type": "string",
      "source": "description",
      "required": true
    }
  },
  "relations": {},
  "acls": []
}

Below is description of each fields.

Model Definition fields Description

Field Description
name Unique Name of the model. eg Customer, Employee, Notebook, Account etc are valid names. Usually model name should begin with upperCase.
base Name of the base model. If Base model is defined, all properties of the base model are inherited. By default base model is set to BaseEntity. Refer to BaseEntity Guide
strict If strict is set to true, when you are posting data to model, you will not able to add additional properties which are not defined in Model. By default it is set to false
plural This is by which you can access Model’s REST API end point. Usually it is plural of model like Customers, Accounts
idInjection If id needs to be generated automatically
properties Properties contains all the properties or fields of the model like name, age, birth date etc. Each property will have it’s own data type like string, number, date. More will be available in examples
relations Use to defined relations with other model. Please follow Guide
acls Access Control List(ACL) can be used to allow/restrict API to either role or user.

Few of Supported properies

type Description example
string field is regular string type “abcd”
number field is number. 10
boolean field is boolean true or false
array type array of known types like number, string etc array[“string”] or array[“number”]
object JSON object type {“a” : “a”, “b” : false}
date Javascript date object Date.now()

Creating model

  • Open http://localhost:3000/explorer and Locate ModelDefintion model
  • Post following data into ModelDefinition
{
  "name": "Product",
  "base": "BaseEntity",
  "strict": false,
  "plural": "Products",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "code": {
      "type": "string",
      "source": "code",
      "required": true
    },
    "name": {
      "type": "string",
      "source": "name",
      "required": true
    },
    "category": {
      "type": "string",
      "source": "category"
    },
    "price": {
      "type": "number",
      "source": "price"
    },
    "offeredSince": {
      "type": "date",
      "source": "offeredSince"
    },
    "active": {
      "type": "boolean",
      "source": "active"
    },
    "flavourable": {
      "type": "boolean",
      "source": "flavourable"
    },
    "flavours": {
      "type": [
        "string"
      ]
    },
    "description": {
      "type": "string",
      "source": "description",
      "required": true
    }
  },
  "relations": {},
  "acls": []
}
  • You will receive response similar to below
{
  "properties": {
    "code": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "category": {
      "type": "string"
    },
    "price": {
      "type": "number"
    },
    "offeredSince": {
      "type": "date"
    },
    "active": {
      "type": "boolean"
    },
    "description": {
      "type": "string"
    }
  },
  "readonly": false,
  "name": "Product",
  "plural": "Products",
  "base": "BaseEntity",
  "strict": false,
  "public": true,
  "idInjection": true,
  "validateUpsert": false,
  "CompositeTransaction": false,
  "dataSourceName": "db",
  "mongodb": {
    "collection": "Product"
  },
  "upward": false,
  "options": {
    "validateUpsert": true
  },
  "enableDefaultUI": false,
  "enableRemoteProxy": true,
  "id": "59255c5d500f730c0b4fd48c",
  "_type": "ModelDefinition",
  "_createdBy": "admin",
  "_modifiedBy": "admin",
  "_createdOn": "2017-05-24T10:11:41.692Z",
  "_modifiedOn": "2017-05-24T10:11:41.692Z",
  "_version": "5f75b995-1415-4991-9716-dbb87e1b7b33",
  "_isDeleted": false
}
  • If you refresh explorer page (http://localhost:3000/explorer), you should able to see Product model. This shows that model has been created and you can start using it right away.
  • You can now post following data in that model

POST http://localhost:3000/api/Products

{
	"code": "M001",
	"name": "Samsung Galaxy S7",
	"category": "Mobiles",
	"price": 46000,
	"offeredSince": "2016-05-01",
	"active": true,
	"description": "Samsung Galaxy S7 Android mobile phone"
}

Response body:

{
   "code":"M001",
   "name":"Samsung Galaxy S7",
   "category":"Mobiles",
   "price":46000,
   "offeredSince":"2016-05-01T00:00:00.000Z",
   "active":true,
   "description":"Samsung Galaxy S7 Android mobile phone",
   "id":"57568bbec5ceaf88260eca9d",
   "_isDeleted":false,
   "_type":"Product",
   "_createdBy":"demouser",
   "_modifiedBy":"demouser",
   "_createdOn":"2016-06-07T08:54:22.169Z",
   "_modifiedOn":"2016-06-07T08:54:22.169Z",
   "_version":"e0b58ac6-d1e5-4de2-a3ce-d7f5c75005fd"
}
  • You can perform different operations on this model like GET (find), PUT (update), POST(create) and DELETE
  • You can go to mongodb database and check the data exists in Product collection

Modify existing Model

in oeCloud.io framework, you should able to modify existing model runtime. In this example, we will continue and try to modify the Product model by adding website field to model.

POST following data to /api/ModelDefinitions API.

{
  "name": "Product",
  "base": "BaseEntity",
  "strict": false,
  "plural": "Products",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "code": {
      "type": "string",
      "source": "code",
      "required": true
    },
    "name": {
      "type": "string",
      "source": "name",
      "required": true
    },
    "category": {
      "type": "string",
      "source": "category"
    },
    "price": {
      "type": "number",
      "source": "price"
    },
    "offeredSince": {
      "type": "date",
      "source": "offeredSince"
    },
    "active": {
      "type": "boolean",
      "source": "active"
    },
    "flavourable": {
      "type": "boolean",
      "source": "flavourable"
    },
    "flavours": {
      "type": [
        "string"
      ]
    },
    "description": {
      "type": "string",
      "source": "description",
      "required": true
    },
    "website": {
      "type": "string"
    }
  },
  "relations": {},
  "acls": [],
  "id": "59255c5d500f730c0b4fd48c",
  "_version": "5f75b995-1415-4991-9716-dbb87e1b7b33"
}

Once model is modified, you can start posting website property in Product model.

{
	"code": "M005",
	"name": "Samsung Galaxy S5",
	"category": "Mobiles",
	"price": 48000,
	"offeredSince": "2017-05-01",
	"active": true,
	"description": "Samsung Galaxy S5 Android mobile phone",
	"website" : "www.samsung.com"
}

Summary

We have seen how we can create a Model dynamically within a oeCloud.io based NodeJS application using the ModelDefinition API provided by the framework.
We have also seen how we can invoke some of the automatically generated CRUD REST API.