Skip to content

Latest commit

 

History

History
executable file
·
82 lines (53 loc) · 2.31 KB

loopback.md

File metadata and controls

executable file
·
82 lines (53 loc) · 2.31 KB

Table of Contents generated with DocToc

Install LoopBack

npm install -g loopback-cli

Create an application

  • Create application with lb app
  • Create a datasource with lb datasource
  • Create model with lb model (properties can be added during the creation process)
  • Create model properties (after creation) with lb property
  • Create relations between models with lb relation
  • Create new API methods with lb remote-method
  • Create ACL with lb acl
  • Create a boot script with lb boot-script (generated script example: server/boo/create-access-token.js)

Datasources

To create a test datasources, create server/datasources.test.json and set the environment variable NODE_ENV=test

MongoDB

Add MongoDB connector:

yarn add loopback-connector-mongodb

Ceate datasource:

lb datasource

Remote methods

lb remote-method

This function is used to extend functionality of models by defining new methods.

If we add a buy method on product:

  • A new endpoint /Products/{id}/buy is automatically created on the API
  • We have to declare and implement the buy method on the prototype of the Product model

During the remote method creation, the generator asks for:

  • The request arguments (request fields)
  • The response arguments (response fields)

User creation and authentication

Create user by posting on "/Users":

{
  "email": "<email>",
  "password": "<password>"
}

Get an authentication token by posting on "/Users/login":

{
  "email": "<email>",
  "password": "<password>"
}

Filtering data

See Querying data