Tutorial: Creating a model and a data source in the API Designer

This tutorial shows you how to add a new model and data source to a LoopBack® project by using the API Designer in IBM® API Connect Version 5.0.7 and later.

Before you begin

Note: For tutorials about working with the developer toolkit in IBM API Connect Version 5.0.6 and earlier, see Developer toolkit tutorials for V5.0.6 and earlier.

Before you begin, you must install the developer toolkit on your local machine. For details, see Installing the toolkit.

You must also do the following:

  1. Create a LoopBack project. For more information, see Creating new projects with API Designer.
  2. Change directories to your LoopBack project and enter the following command:
    apic edit
    After a brief pause, the console displays this message:
    Express server listening on http://127.0.0.1:9000

    API Designer opens in your web browser, initially displaying the login page if you haven't logged in recently.

    Note: The login page prompts you to Sign in with IBM Cloud. Enter your IBM Cloud credentials, which authenticates you on IBM Cloud and provides access to the API Manager features such as Publish, Explore, and Analytics. You will continue to work in API Designer locally to create APIs, models and data sources.
    Note: If you need to run the editor on a different port, use the following command:
    [Icon indicating that this applies on Linux platforms][Icon indicating that this applies on Mac OS X platforms]
    PORT=port_number apic edit
    [Icon indicating that this applies on Windows platforms]
    set PORT=port_number && apic edit
    where port_number is the port number to use.
The following diagram shows the sequential flow through the IBM API Connect Developer toolkit tutorials for working with LoopBack projects. Before beginning a tutorial, ensure that you have completed the previous tutorials in the sequence. You can click a tutorial in the diagram to open the instructions for that tutorial.
Tutorial flow diagram for working with LoopBack projects Installing the toolkit Tutorial: Creating a LoopBack project from the command line Tutorial: Creating a model and a data source from the command line Tutorial: Creating a model and a data source in the API Designer Tutorial: Testing a LoopBack project Tutorial: Publishing a project from the command line Tutorial: Staging and publishing a project from the API Designer

About this tutorial

This tutorial builds on Tutorial: Creating a LoopBack project from the command line. In this tutorial, you will complete the following activities:
  • Create a model with the API Designer.
  • Create a data source with the API Designer.

Adding a data source

A LoopBack project requires at least one data source. To access the data source, the appropriate LoopBack connector must be installed. By default, an empty LoopBack project does not have any data sources defined. For now, you will add the in-memory data source, which is suitable for development and testing. Complete the following steps to add the data source to the project:

Complete the following steps:
  1. Click Data Sources.
  2. Click Add. The New LoopBack Data Source window opens.
  3. Enter bankDS in the Name text field.
    Note: You can use any alphanumeric characters, dashes, and underscores in a data source name.
  4. Click New.
  5. By default, the Connector setting shows In-memory db and the other settings are blank. Keep the default settings for now, and API Designer automatically saves the new data source.
    Note: The In-memory data source is built in to LoopBack and is suitable only for development and initial testing. When you are ready to connect your models to a real data source such as database server, change the Connector setting accordingly then install the data source connector by following the instructions in Tutorial: Installing LoopBack connectors. Then enter the connector settings (host name, port, database name, user name, password) as appropriate for your Connector type, and click the Save icon Save. Then, API Designer automatically tests the connection to the data source. If the test is successful, it displays the message Success - Data source connection test succeeded.
  6. You can test the data source connection by clicking Test Connection. The message Success - Data source connection test succeeded is displayed.
  7. Click All Data Sources. The data source will appear in the list of data sources. API Designer updates the server/datasources.json file with settings for the new data source.

Adding a model

The next step is to add a model to the project. Models represent back-end data sources such as databases or other back-end services (for example, REST or SOAP). Every LoopBack application has a set of default models, which you can extend to suit your application's requirements. You can also define custom models. Models are stored in JSON format and determine properties and other characteristics of the API. For more information on models, see Defining models. Follow these steps to add a model to your LoopBack project:

  1. Click Models.
  2. Click Add. The New LoopBack Model window opens.
  3. Enter branch in the Name text field, then click New.
    Note: You can use any alphanumeric characters, dashes, and underscores in a model name.
  4. In the Data Source field, select bankDS.
  5. In the Properties section, click the Add property icon Add .
  6. In the Property Name text field, enter type.
  7. For Type, select string.
  8. Keep the default values for the other settings:
    • Required: Whether the property must have a value.
    • Is Array: Whether the property is a JavaScript array with elements of the specified type.
    • ID: Whether the property is a unique identifier. For more information on LoopBack ID properties, see LoopBack documentation.
    • Index: Whether the property represents a column (field) that is a database index.
    • Description: Text description of the property.
    For more information, see Model definition JSON file reference.
  9. Click the Add property icon Add again to add another property.
  10. In the Property Name text field, enter phone.
  11. For Type, select string.
  12. Select Required to make the property required. This means that it must have a value when you add or update a model instance.
  13. Select ID to indicate that the property must be unique. Leave the other settings with their default values.
  14. Click the Save icon Save to save your changes.
  15. Click All Models to finish editing the model.

What you did in this tutorial

In this tutorial, you completed the following activities:
  • Created a new LoopBack model.
  • Created a new LoopBack data source.

What to do next

Test your LoopBack project.