Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


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

    Bram BorggreveBram Borggreve

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.

    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."

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.