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
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:
- Create a LoopBack project. For more information, see
Creating new projects with API Designer.
- 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.
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.
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:
- Click .
- Click Add. The New LoopBack Data Source window
opens.
- Enter bankDS in the Name text field.
Note: You can use any alphanumeric characters, dashes, and underscores in a data source name.
- Click New.
- 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
. 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.
- You can test the data source connection by clicking . The message Success - Data source connection test succeeded is
displayed.
- 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:
- Click .
- Click Add. The New LoopBack Model window opens.
- Enter branch in the Name text field, then click
New.
Note: You can use any alphanumeric characters, dashes, and underscores
in a model name.
- In the Data Source field, select bankDS.
- In the Properties section, click the Add property
icon .
- In the Property Name text field, enter type.
- For Type, select string.
- 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.
- Click the Add property icon again to add another property.
- In the Property Name text field, enter phone.
- For Type, select string.
- Select Required to make the property required. This means that it must
have a value when you add or update a model instance.
- Select ID to indicate that the property must be unique.
Leave the other settings with their default values.
- Click the Save icon to save your changes.
- 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.