Manage a Model

While developing an application there can be instances where we might want to modify the property names or create a new property in an existing model(Manage a model). This Guide demonstrates the process to dynamically manage models with oeCloud.io framework.

What you’ll build

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

  • created a new Model called NewProduct with the following properties: code, name, category, price, offeredSince, active and description within the running application
  • verified the model schema
  • modified the name property of the newly created model to productName and added a new boolean property to it called reviewed
  • verified the changed model schema

The Model would be managed and made available to the application 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 -

  • an understanding of what a Model is in the context of Loopback, which is the basis of the oeCloud Framework. You can go through the loopback documentation for the same by clicking here
  • a running NodeJS application built using the oeCloud Framework.
  • 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

You can start from scratch and complete each step, or you can bypass basic setup steps that are already familiar to you.

To start from scratch go to Getting Started

Once you have started the server, open a browser link: http://localhost:3000/explorer/ “explorer to view REST api’s, and perform CRUD operation”).

Creating and Modifying the NewProduct Model

We will first create the NewProduct model with the code, name, category, price, offeredSince, active and description properties. To do this, you can follow the Create a Model with Properties Guide. But briefly, you can:

  • Open a command window (“cmd” in Windows OS, “bash” Terminal in Linux)
  • copy the following command to the command prompt, change the host and port, if necessary, and hit the ENTER key:

curl -v “http://localhost:3000/api/ModelDefinitions” -H “Content-Type: application/json” -H “Accept: application/json” –data “{"name": "NewProduct","base": "BaseEntity","strict": false,"plural": "NewProducts","idInjection": true,"options": {"validateUpsert": true},"properties": {"code": {"type": "string"},"name": {"type": "string"},"category": {"type": "string"},"price": {"type": "number"},"offeredSince": {"type": "date"},"active": {"type": "boolean"},"description": {"type": "string"}}}”

  • To retrieve the schema of the newly created NewProduct Model, you can perform a GET request on the ModelDefinition REST API, filtering on the name property with value NewProduct. This can be done using cURL as follows:

curl -X GET –header “Accept: application/json” “http://localhost:3000/api/ModelDefinitions?filter=%7B%22where%22%20%3A%20%7B%22name%22%20%3A%20%22NewProduct%22%7D%7D”

In the above URL, the value of “filter” is simply the URLEncoded string of the following JSON:

{"where" : {"name" : "NewProduct"}}

The cURL response should have 200 OK and the schema of the NewProduct Model as shown 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":"NewProduct",
      "plural":"NewProducts",
      "base":"BaseEntity",
      "strict":false,
      "public":true,
      "idInjection":true,
      "validateUpsert":false,
      "dataSourceName":"db",
      "id":"57568966c5ceaf88260eca9c",
      "_type":"ModelDefinition",
      "_createdBy":"demouser",
      "_modifiedBy":"demouser",
      "_createdOn":"2016-06-07T08:44:22.598Z",
      "_modifiedOn":"2016-06-07T08:44:22.598Z",
      "_version":"e5f21dda-a88e-40ac-bb4c-86475df5de16",
      "_isDeleted":false,
      "options":{
         "validateUpsert":true
      },
      "mongodb":{
         "collection":"NewProduct"
      }
   }
]

Modifying the NewProduct Model schema
Modifying the schema of a Model involves deleting the model definition and re-creating the same with modified properties using the ModelDefinition API. For example, to modify the name property to productName and add a new boolean property called reviewed, we first delete the existing NewProduct Model definition as follows:

curl -X DELETE –header “Accept: application/json” “http://localhost:3000/api/ModelDefinitions/57568966c5ceaf88260eca9c/e5f21dda-a88e-40ac-bb4c-86475df5de16”

Next, we re-create the NewProduct Model with the modifications as follows:

curl -v “http://localhost:3000/api/ModelDefinitions” -H “Content-Type: application/json” -H “Accept: application/json” –data “{"name": "NewProduct","base": "BaseEntity","strict": false,"plural": "NewProducts","idInjection": true,"options": {"validateUpsert": true},"properties": {"code": {"type": "string"},"productName": {"type": "string"},"category": {"type": "string"},"price": {"type": "number"},"offeredSince": {"type": "date"},"active": {"type": "boolean"},"description": {"type": "string"},"reviewed": {"type": "boolean"}}}"

This creates the modified NewProduct Model. The modified schema can be verified by re-running the GET ModelDefinition API filtered by name=NewProduct, as follows:

curl -X GET –header “Accept: application/json” “http://localhost:3000/api/ModelDefinitions?filter=%7B%22where%22%20%3A%20%7B%22name%22%20%3A%20%22NewProduct%22%7D%7D”

The response should be as follows:

[
   {
      "properties":{
         "code":{
            "type":"string"
         },
         "productName":{
            "type":"string"
         },
         "category":{
            "type":"string"
         },
         "price":{
            "type":"number"
         },
         "offeredSince":{
            "type":"date"
         },
         "active":{
            "type":"boolean"
         },
         "description":{
            "type":"string"
         },
         "reviewed":{
            "type":"boolean"
         }
      },
      "readonly":false,
      "name":"NewProduct",
      "plural":"NewProducts",
      "base":"BaseEntity",
      "strict":false,
      "public":true,
      "idInjection":true,
      "validateUpsert":false,
      "dataSourceName":"db",
      "id":"5756994ac5ceaf88260ecad1",
      "_type":"ModelDefinition",
      "_createdBy":"demouser",
      "_modifiedBy":"demouser",
      "_createdOn":"2016-06-07T09:52:10.982Z",
      "_modifiedOn":"2016-06-07T09:52:10.982Z",
      "_version":"c39943dd-8888-4a27-975b-c23a14379852",
      "_isDeleted":false,
      "options":{
         "validateUpsert":true
      },
      "mongodb":{
         "collection":"NewProduct"
      }
   }
]

As we can see, the name property is modified to productName and a new boolean property called reviewed is now added to the NewProduct Model.

Summary

We have seen how we can modify the properties of a Loopback Model dynamically within a oeCloud Framework based NodeJS application using the ModelDefinition API provided by the framework.