space-lb2

messaging

space-node

This documentation is deprecated
Please see the new LoopBack documentation site.
Skip to end of metadata
Go to start of metadata

Redirecting to http://loopback.io/doc/en/lb2/Create-new-models.html ...

Icon

Prerequisites:

Recommended: Read LoopBack core concepts.

Creating models with slc loopback is quick and easy.

Recall in Create a simple API step of Getting started you created a CoffeeShop model.

Now you're going to create two new models, Review and Reviewer, with the slc loopback model generator.

Icon

If you followed the previous step in the tutorial, go to Define the Review model.

If you're just jumping in, follow the steps below to catch up...

Get the app (in the state following the last article) from GitHub and install all its dependencies:

Define the Review model

Enter:

When prompted, enter or select the following:

  • Model name: Review
  • Data source: mongoDs (mongodb)
  • Base class: Use the down-arrow key to select PersistedModel.
  • Expose Reviewer via the REST API? Press RETURN to accept the default, Yes.
  • Custom plural form (used to build REST URL):  Press RETURN to accept the default, Yes.

  • Common model or server only: Press RETURN to accept the default, common model.

Then, follow the prompts to add these properties:

Property nameProperty typeRequired?
datedatey
ratingnumbern
commentsstringy

To exit the model generator, press RETURN when prompted for property name.

Define the Reviewer model

Enter:

When prompted, enter or select the following:

  • Model name: Reviewer
  • Data source: mongoDs (mongodb)
  • Base class: Use the down-arrow key to select User.
  • Expose Reviewer via the REST API? Press RETURN to accept the default, Yes.
  • Custom plural form (used to build REST URL):  Press RETURN to accept the default, Yes.

Don't add any properties, since they are all inherited from the base User model.

To exit the model generator, press RETURN when prompted for property name.

Update boot script to add data 

Recall back in part I of Getting started, you added a boot script to create a database table from the model (via auto-migration) and add some data to the database.

Now that you have some new models and a new data source, you need to update this script so it will create data structures in MongoDB and insert data via the new models.

Copy and paste the code below into server/boot/create-sample-models.js, replacing the existing code.

Then run

This boot script has several functions:

  • createCoffeeShops() creates a MySQL table for the CoffeeShop model and adds data to the table.  This is what the create-sample-models.js script from Getting started did.
  • createReviewers() creates the Reviewer data structure in MongoDB using auto-migration and adds data to it.  
  • createReviews() creates the Reviews data structure in MongoDB using auto-migration and adds data to it.

See Creating a database schema from models for more information on auto-migration. 

server/boot/create-sample-models.js


  • No labels