EnergisaOne / loopback-example-relations

Basic model relations concepts.

Home Page:http://docs.strongloop.com/display/LB/Creating+model+relations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

loopback-example-relations

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

Prerequisites

Tutorials

Knowledge

Procedure

Create the application

Application information

  • 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

Create the models

Model information

  • 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
  • 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
  • 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
$ slc loopback:model Customer
... # follow the prompts, repeat for `Review` and `Order` models

Configure server-side views

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.

Add sample data

Create three boot scripts:

Create model relations

Model relation information

  • Customer
    • has many
      • Review
        • Property name for the relation: reviews
        • Custom foreign key: authorId
      • Order
        • Property name for the relation: orders
        • Custom foreign key: customerId
        • Require a through model: No
  • Review
    • belongs to
      • Customer
        • Property name for the relation: author
        • Custom foreign key: authorId
  • Order
    • belongs to
      • Customer
        • Property name for the relation: Leave blank - defaults to customer
        • Custom foreign key: Leave blank
$ 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.

Try the API

Start the application with node . and browse to localhost:3000. You should see various links. Each endpoint is defined as follows:


About

Basic model relations concepts.

http://docs.strongloop.com/display/LB/Creating+model+relations

License:MIT License


Languages

Language:JavaScript 100.0%