Skip to content

Instantly share code, notes, and snippets.

@dearfrankg
Last active September 11, 2016 00:44
Show Gist options
  • Select an option

  • Save dearfrankg/39ed3c6162e716ada81bd14d42c8e632 to your computer and use it in GitHub Desktop.

Select an option

Save dearfrankg/39ed3c6162e716ada81bd14d42c8e632 to your computer and use it in GitHub Desktop.
loopback-getting-started-intermediate process
// loopback-getting-started-intermediate process
// goto project directory
cd CoffeeShops
// generate model (table)
slc loopback:model // prompts...
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
Custom plural form (used to build REST URL): Press RETURN
Common model or server only: Press RETURN
propName propType Required?
--------------------------------
comments string y
date date y
rating number n
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
Custom plural form (used to build REST URL): Press RETURN
NO PROPS because User base
// create sample data (see https://goo.gl/nVlZ6z)
update server/boot/create-sample-models.js
// define relations
slc loopback:relation // use this command for each item below
A coffee shop has many reviews; No through model and no foreign key.
? Select the model to create the relationship from: CoffeeShop
? Relation type: has many
? Choose a model to create a relationship with: Review
? Enter the property name for the relation: reviews
? Optionally enter a custom foreign key:
? Require a through model? No
A coffee shop has many reviewers; No through model and no foreign key.
? Select the model to create the relationship from: CoffeeShop
? Relation type: has many
? Choose a model to create a relationship with: Reviewer
? Enter the property name for the relation: reviewers
? Optionally enter a custom foreign key:
? Require a through model? No
A review belongs to a coffee shop; No foreign key.
? Select the model to create the relationship from: Review
? Relation type: belongs to
? Choose a model to create a relationship with: CoffeeShop
? Enter the property name for the relation: coffeeShop
? Optionally enter a custom foreign key:
A review belongs to a reviewer; foreign key is publisherId.
? Select the model to create the relationship from: Review
? Relation type: belongs to
? Choose a model to create a relationship with: Reviewer
? Enter the property name for the relation: reviewer
? Optionally enter a custom foreign key: publisherId
A reviewer has many reviews; foreign key is publisherId.
? Select the model to create the relationship from: Reviewer
? Relation type: has many
? Choose a model to create a relationship with: Review
? Enter the property name for the relation: reviews
? Optionally enter a custom foreign key: publisherId
? Require a through model? No
// define access controls
slc loopback:acl // use this command for each item below
Deny everyone all Review endpoints.
? Select the model to apply the ACL entry to: Review
? Select the ACL scope: All methods and properties
? Select the access type: All (match all types)
? Select the role: All users
? Select the permission to apply: Explicitly deny access
Allow everyone to read reviews.
? Select the model to apply the ACL entry to: Review
? Select the ACL scope: All methods and properties
? Select the access type: Read
? Select the role: All users
? Select the permission to apply: Explicitly grant access
Allow authenticated users to write a review.
? Select the model to apply the ACL entry to: Review
? Select the ACL scope: A single method
? Enter the method name: create
? Select the role: Any authenticated user
? Select the permission to apply: Explicitly grant access
Allow the author of a review (its "owner") to make any changes to it.
? Select the model to apply the ACL entry to: Review
? Select the ACL scope: All methods and properties
? Select the access type: Write
? Select the role: The user owning the object
? Select the permission to apply: Explicitly grant access
// create the remote hook
edit common/models/review.js
module.exports = function(Review) {
  Review.beforeRemote('create', function(context, user, next) {
    context.args.data.date = Date.now();
    context.args.data.publisherId = context.req.accessToken.userId;
    next();
  });
};
// Running StrongLoop Process Manager
slc pm
slc pm(51470): StrongLoop PM v5.1.0 (API v6.1.0) on port `8701`
slc pm(51470): Base folder `/Users/rand/.strong-pm`
slc pm(51470): Applications on port `3000 + service ID`
Browse your REST API at http://127.0.0.1:8701/explorer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment