$ git clone https://github.com/strongloop/loopback-example-relations.git
$ cd loopback-example-model-relations
$ npm install
$ node .
In this example, we create a simple web app to demonstrate basic model relation concepts. The app consists of a single web page with a list of links to help us query and filter sample data via REST.
- Name:
loopback-example-model-relations - Directory to contain the project:
loopback-example-model-relations
$ slc loopback loopback-example-model-relations
... # follow the prompts
$ cd loopback-example-model-relations
- Name:
Customer- Data source:
db (memory) - Base class:
PersistedModel - Expose over REST:
Yes - Custom plural form: Leave blank
- Properties:
name- String
- Not Required
age- String
- Not Required
- Data source:
- Name:
Review- Data source:
db (memory) - Base class:
PersistedModel - Expose over REST:
Yes - Custom plural form: Leave blank
- Properties:
product- String
- Not Required
star- Number
- Not Required
- Data source:
- Name:
Order- Data source:
db (memory) - Base class:
PersistedModel - Expose over REST:
Yes - Custom plural form: Leave blank
- Properties:
description- String
- Not Required
total- Number
- Not Required
- Data source:
$ slc loopback:model Customer
... # follow the prompts, repeat for `Review` and `Order` models
LoopBack comes preconfigured with EJS out-of-box. This means we can use server-side templating by simply setting the proper view engine and a directory to store the views.
Create a views directory to store server-side templates.
$ mkdir server/views
Add server-side templating configurations to server.js.
Create index.ejs in the views directory.
Configure server.js to use server-side
templating. Remember to import the path package.
Create three boot scripts:
Customer- has many
Review- Property name for the relation:
reviews - Custom foreign key:
authorId
- Property name for the relation:
Order- Property name for the relation:
orders - Custom foreign key:
customerId - Require a through model: No
- Property name for the relation:
- has many
Review- belongs to
Customer- Property name for the relation:
author - Custom foreign key:
authorId
- Property name for the relation:
- belongs to
Order- belongs to
Customer- Property name for the relation: Leave blank - defaults to
customer - Custom foreign key: Leave blank
- Property name for the relation: Leave blank - defaults to
- belongs to
$ slc loopback:relation
? Select the model to create the relationship from:
...
> Customer
... # follow the prompts, repeat for `Review` and `Order`
LoopBack automatically derives relation and foreign key names when you leave the values empty.
Start the application with node . and browse to localhost:3000.
You should see various links. Each endpoint is defined as follows:
- /api/customers
- List all customers
- /api/customers?filter[fields][0]=name
- List all customers, but only return the name property for each result
- /api/customers/1
- Look up a customer by ID
- /api/customers/youngFolks
- List a predefined scope named youngFolks
- /api/customers/1/reviews
- List all reviews posted by a given customer
- /api/customers/1/orders
- List all orders placed by a given customer
- /api/customers?filter[include]=reviews
- List all customers including their reviews
- /api/customers?filter[include][reviews]=author
- List all customers including their reviews which also include the author
- /api/customers?filter[include][reviews]=author&filter[where][age]=21
- List all customers whose age is 21, including their reviews which also include the author
- /api/customers?filter[include][reviews]=author&filter[limit]=2
- List first two customers including their reviews which also include the author
- /api/customers?filter[include]=reviews&filter[include]=orders
- List all customers including their reviews and orders