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

Create a new model

Define a new data source

Configure the new connection inside

Example mysql config:

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:

Manually defining model ID

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

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:

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

Run your app in prod mode

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

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

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

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.

Shares

Published by

Code Review

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 *