Create a model to persist data in a Node.js LoopBack API

Bram Borggreve
InstructorBram Borggreve

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

In this lesson you will learn what a LoopBack model is, you will create a Product model using the LoopbBack CLI. The product model will be based off the built-in PersistedModel which gives it basic functionality like Create, Ready, Update, Delete and some more. Using the API Explorer you can interact with the new model, store, retrieve, edit and delete the product data.

[00:01] Inside the loopback project, we run LB model. This will prompt us for the model name. A model name tends to be singular so we enter "product." The data source we use is the default in memory DB. We pick persistent model as our base class.

[00:15] This will give our model a set of basic operations like create, read, updates, delete and a few more. Hit "enter" to expose the products via a REST. We accept the default plural form which in our case will be "products."

[00:29] Then we hit "enter" to add the model definitions to the common folder. Our product model has been created and we can now add the model properties. The first property we add is "name." It is of type string and we want it to be required. We don't want to add a default value.

[00:46] The second property is "price." Price is of type number. It's also required and again no default value. We're done adding properties so we hit "enter" one more time to quit the dialog. We now start our server by running notes dots.

[01:02] When we open the API Explorer in the browser, we see our new product model. Here we can see all the operations that got inherited from the base model. Let's take a look at some of these operations in more detail. To retrieve a list of the products, we'll make a get request to the /products endpoints.

[01:20] The result is an empty array because we did not add any products yet. To add a product, we make a post request to the /products endpoints. In the data field we define our product using JSON and submit the data to the API.

[01:34] When we look at the response, we see we have a validation error. This is because we didn't submit price property. When we add the price property and submit again, we will see that the product got created. To verify this, we can look at the products endpoints in our browser, and we see that we have an array with one item.

[01:52] We can add a second product, submit it to the API and we see it got created. To change an existing product, we can make a patch request to the /products /ID endpoints. As a parameter, we take in the ID of the item we want to edit and the new payloads with the change to values.

[02:11] Let's change the price of coffee to $2.99. We execute the commands and we see that the price got updated. To remove a product from our API, we make a delete request to /products /ID. We take in a parameter of ID and make the request.

[02:28] In response, we can see the number of items that got deleted. When we look at our list, we see we only have one product left. Let's open our editor and see what got added. We open model config. of JSON and see that the products got defined with some basic parameters.

[02:44] In common/models we see two new files. Product of JSON is the model definition. Here we see the base model that we inherited from and the properties that we added. Product of JS is the model class. This file can be used to customize the model behavior.