🖥 To see the code for this step, here is commit/diff.
LoopBack takes a model-driven approach to building out APIs. Based on our responses to the command line prompts, it will create a JSON file with our model schema and details. For an introduction to LoopBack models see LoopBack core concepts - Models in the LoopBack documentation.
To invoke the generator, we type lb model
and go through the list of prompts.
Note: cd
into your working directory, if you haven't already.
➜ band-app git:(master) ✗ lb model
? Enter the model name:
We'll call our first model while building our marketplace app event
.
Note: models should be singular. The application handles plural naturally, as we'll see in an upcoming step.
LoopBack generalizes backend services such as databases, REST APIs, SOAP web services, and storage services as data sources. A connector enables LoopBack applications to use a given data source. For more information, see LoopBack core concepts - Data sources and connectors.
? Enter the model name: event
? Select the data-source to attach event to: (Use arrow keys)
❯ db (memory)
(no data-source)
LoopBack has connectors for many, many data sources. StrongLoop and IBM support many of the most popular data sources and there is also a plethora of community-supported connectors for others.
Later in the workshop, we will add a conventional data source, for the time being, let's just use the in-memory data-source.
Note: An issue with using the in-memory data-source is that every time we shut down our application, we will lose all our data. Again, using the in-memory data-source is only for testing and prototyping. Later in this section, we will look at persisting your in-memory data to a file. And in an upcoming step, we will add a conventional data-source.
? Enter the model name: event
? Select the data-source to attach event to: db (memory)
? Select model's base class (Use arrow keys)
Model
❯ PersistedModel
ACL
AccessToken
Application
Change
Checkpoint
(Move up and down to reveal more choices)
The base class for a model is the object that this model will extend from. There are a number of base model classes and each has a specific purpose. We will choose the PersistedModel
because we will eventually want the persistence functionality: we'll get that "for free" with this choice.
? Enter the model name: event
? Select the data-source to attach event to: db (memory)
? Select model's base class PersistedModel
? Expose event via the REST API? (Y/n)
There may be reasons for keeping an endpoint private, but for the sake of this workshop, we will expose our REST API endpoints for public consumption. We will look at securing endpoints later in the workshop.
? Enter the model name: event
? Select the data-source to attach event to: db (memory)
? Select model's base class PersistedModel
? Expose event via the REST API? Yes
? Custom plural form (used to build REST URL):
LoopBack is very smart about handling plural forms of a model. In this case, event
is easily made plural as events
, but it can also handle other common plurals such as child
to children
and mouse
to mice
. If you have an unconventional model name that isn't easily made plural by adding an s
then you may need to take advantage of this feature. Most of the time, you can let LoopBack do its magic.
? Enter the model name: event
? Select the data-source to attach event to: db (memory)
? Select model's base class PersistedModel
? Expose event via the REST API? Yes
? Custom plural form (used to build REST URL):
? Common model or server only? (Use arrow keys)
❯ common
server
We can choose to only have this model available to the server or we can make it available to the client as well. In an effort to be forward thinking, we will choose common
in case we want to use the Angular SDK or any other client options in the future.
Now that we have our model configuration in place, we will move on to specific model properties. The prompts will guide us, and as you'll see, after your last property is captured, you can hit enter and it will exit the prompt.
Let's add some event properties now.
Enter an empty property name when done.
? Property name:
We will begin with the venue
property.
? Property name: venue
invoke loopback:property
? Property type: (Use arrow keys)
❯ string
number
boolean
object
array
date
buffer
(Move up and down to reveal more choices)
The type of our venue
property will be string
in this case, but as we can see, there are many options. Having the type set for our properties is important when doing validation, which we will visit later.
? Property name: venue
invoke loopback:property
? Property type: string
? Required? (y/N)
We will not make our venue
property required. In fact, for now, we won't make any of properties required, but we will look at how to update any of our properties directly in the configuration.
? Property name: title
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
We will leave our default value blank here, but adding something like "TBD" would work as well.
? Property name: title
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name:
As we can see from the terminal output above: when we are done adding properties, we can simply hit Enter
when prompted for another property. In our case though, we have a few more properties to add, which I've outlined below.
All: not required, no default value.
- title: type:
string
- date: type:
date
- cost: type:
number
- lineup: type:
array
ofstring
s - location: type:
string
- poster: type:
string
- url: type:
string
- city: type:
string
- state: type:
string
- description: type:
string
- age-restriction: type:
string
Note: if we aren't so inclined to add so many properties, we can just do 1-4 and leave out 5-11. The first four properties give us a variety of property types and I've included the remaining properties to keep the workshop in sync with the app's codebase.
Here is the output from adding all of our model's properties:
➜ band-app git:(master) lb model
? Enter the model name: event
? Select the data-source to attach event to: db (memory)
? Select model's base class PersistedModel
? Expose event via the REST API? Yes
? Custom plural form (used to build REST URL):
? Common model or server only? common
Let's add some event properties now.
Enter an empty property name when done.
? Property name: venue
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: date
invoke loopback:property
? Property type: date
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: cost
invoke loopback:property
? Property type: number
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: lineup
invoke loopback:property
? Property type: array
? The type of array items: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: poster
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: url
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: city
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: state
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: description
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name: age-restriction
invoke loopback:property
? Property type: string
? Required? No
? Default value[leave blank for none]:
Let's add another event property.
Enter an empty property name when done.
? Property name:
Now that we are done adding our properties to the event
model, let's take a look at what LoopBack generated from our command line interactions.
Next Step: Model files: config and hooks