StrongLoop Loopback Cheatsheet

Loopback cheatsheetThe following is an (incomplete) cheatsheet / reference from the Loopback Node.js API framework.

In many ways the Loopback framework reminds me of Symfony. It feels very enterprisey and professional. It also has its own terminology and command line syntax which I am constantly forgetting (much like when I learned Symfony!), so this is my work-in-progress reference guide / Loopback cheatsheet.

This cheat sheet is currently for Loopback v2.14.0.

New to Loopback and want to know what it is, and what it does, at a high level, but from a technical perspective? The Core Concepts document was the first document I read that I thought… aha, this is awesome.

Start a new project

slc loopback
cd your-new-project
npm install

Create a new model

slc loopback:model
// or
slc loopback:model ModelName

Define a new data source

slc loopback:datasource
npm install loopback-connector-mysql --save
// and / or
npm install loopback-connector-mongodb --save

Configure the new connection inside

server/datasources.json

Example mysql config:

{
  "db": {
    "name": "db",
    "connector": "memory"
  },
  "mysqlDs": {
    "name": "mysqlDs",
    "connector": "mysql",
    "host": "demo.strongloop.com",
    "port": 3306,
    "database": "demo",
    "username": "demo",
    "password": "L00pBack"
  }
}

Remember to update the server/model-config.json file also, changing the dataSource property for your particular model.

Changing a model’s data source

For example, to move from db (memory) to mysql, firstly ensure you have defined the new data source (see above).

Then, update server/model-config.json, changing the dataSource property to the name of the right data source.

Manually defining db table name

Inside your model json file, e.g. common/models/your-model.json:

  "options": {
    "validateUpsert": true,
    "mysql": {
      "table": "your_table_name_here"
    }
  },

Manually defining model ID

Inside your model json file, e.g. common/models/your-model.json:

  "properties": {
    "id": {
      "type": "number",
      "id": true
    },

Auto-migration

Coming from Sails JS where this just works on a sails lift, I found this quite confusing.

To migrate, so long as your db supports it (which MySQL seems to), first do:

slc arc

This should auto-open your browser.

Then, browse to ‘compose’ section, highlight your model on the left menu, and click the ‘Migrate Model’ button.

Your table should now have been created.

Run your app in dev mode

node .

Run your app in prod mode

I have not yet fully tested this, aside from completing the tutorial where this is described.

slc start

Helpful URLs

In development, your App will run on 127.0.0.1:3000.

API Explorer: 127.0.0.1:3000/explorer

Remote Method Example

module.exports = function(CoffeeShop) {
...
  CoffeeShop.getName = function(shopId, cb) {
    CoffeeShop.findById( shopId, function (err, instance) {
        response = "Name of coffee shop is " + instance.name;
        cb(null, response);
        console.log(response);
    });
  }
...
  CoffeeShop.remoteMethod (
        'getName',
        {
          http: {path: '/getname', verb: 'get'},
          accepts: {arg: 'id', type: 'number', http: { source: 'query' } },
          returns: {arg: 'name', type: 'string'}
        }
    );
}

Model definitions (your-model.json) JSON file

When using the slc loopback:model command, you end up with two files: common/models/your-model.js and common/models/your-model.json

This page describes what you can do with the common/models/your-model.json file.

Access Another Model From Current Model JS

CurrentModel.getApp(function (err, App) {
      if (err) { throw err; }
      App.models.DifferentModel.findOne(

Change findOne to call whatever method you need.

Example routes

  • /api/customers
  • /api/customers?filter[fields][name]=true
  • /api/customers/1
  • /api/customers/youngFolks
  • /api/customers/1/reviews
  • /api/customers/1/orders
  • /api/customers?filter[include]=reviews
  • /api/customers?filter[include][reviews]=author
  • /api/customers?filter[include][reviews]=author&filter[where][age]=21
  • /api/customers?filter[include][reviews]=author&filter[limit]=2
  • /api/customers?filter[include]=reviews&filter[include]=orders

Official Strongloop Loopback API Examples

This is a filtered representation of all the official Strongloop Loopback example / reference implementations.

By filtered I mean a simple Github search of the Strongloop GitHub repo, for anything starting with ‘loopback-example*’.

Link

Loopback Cheatsheet Work In Progress

This guide is a work in progress. If you want to contribute, please leave a comment.

As far as I am aware, development is moving quickly on this project and what appears above may be out of date for your version in the future.

Published by

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.