Thursday, January 9, 2020

Loopback3: Comprehensive AccessType, Property and End-point



Access TypePropertyVerbEnd point
READfindGET/model-name-plural
 findByIdGET/model-name-plural/{id}
 existsHEAD/model-name-plural/{id}
 existsREAD/model-name-plural/{id}/exists
 __get__relationNameGET/model-name-plural/{id}/relationName
 __findById__relationNameGET/model-name-plural/{id}/relationName/{fk}
 __count__relationNameGET/model-name-plural/{id}/relationname
 createChangeStreamGET/model-name-plural/change-stream
 createChangeStreamPOST/model-name-plural/change-stream
 countGET/model-name-plural/count
 findOneGET/model-name-plural/findOne
WRITEupsertPATCH/model-name-plural
 upsertPUT/model-name-plural
 createPOST/model-name-plural
 updateAttributesPATCH/model-name-plural/{id}
 updateAttributesPUT/model-name-plural/{id}
 deleteByIdDELETE/model-name-plural/{id}
 replaceByIdPOST/model-name-plural/{id}/replace
 __create__relationNamePOST/model-name-plural/{id}/relationName
 __delete__relationNameDELETE/model-name-plural/{id}/relationName
 __updateById__relationNamePUT/model-name-plural/{id}/relationName/{fk}
 __destroyById__relationNameDELETE/model-name-plural/{id}/relationName/{fk}
 replaceOrCreatePOST/model-name-plural/replaceOrCreate
 updateAllPOST/model-name-plural/update
 upsertWithWherePOST/model-name-plural/upsertWithWhere

Sunday, January 5, 2020

Loopback & MongoDB

từ loopback muốn truy cập vào mongodb có pass thì phải tạo account từ database muốn truy cập vào
ví dụ
"accountDS": {
    "name": "accountDS",
    "connector": "mongodb",
    "host": "demo.strongloop.com",
    "port": 27017,
    "database": "demoDB",
    "username": "demoUser",
    "password": "L00pBack"
  }
thi tức là loopback muốn truy cập vào database tên là demoDB
vậy thì phải tạo user sau trên mongodb

//_id là demoDB.demoUser
use demoDB;
db.createUser({user:"demoUser",pwd:"L00pBack",roles:["readWrite","dbAdmin"]})

chứ không đuợc tạo user trên database admin như sau

//_id là admin.demoUser
use admin;
db.createUser({user:"demoUser",pwd:"L00pBack",roles:["readWrite","dbAdmin"]})

(tuy 2 user có cùng tên là demoUser nhưng vì đuợc tạo trên 2 database khác nhau nên chúng tồn tại song song đuợc)

-----------
Tuy nhiên, khi sử dụng mongo compass để truy cập vào DB thì lại phải tạo user trên database admin
(ví dụ: với 2 user đã tạo ở trên thì admin.demoUser mới có thể sử dụng trong mongo compass đuợc. demoDB.demoUser không thể dùng trong compass đuợc)_

Wednesday, January 1, 2020

Getting Started (Loopback 3)

Install

Assuming you have already installed Node...
Install the LoopBack CLI tool.
$ npm install -g loopback-cli

Create app

Create a "Hello World" LoopBack application.
$ lb
? What's the name of your application? hello-world
? Enter name of the directory to contain the project: hello-world

? Which version of LoopBack would you like to use? 3.x (Active Long Term Support)
? What kind of application do you have in mind? hello-world (A project containing a controller,
including a single vanilla Message and a single remote method)
...
I'm all done. Running npm install for you to install the required dependencies.
If this fails, try running the command yourself.
... 

Create models

$ lb model

The generator guides you through creating your model. Enter the values highlighted in green. To accept the default, just press Enter.
[?] Enter the model name: person
[?] Select the data-source to attach person to: db (memory)
[?] Select model`s base class (PersistedModel)
[?] Expose person via the REST API? Yes
[?] Custom plural form (used to build REST URL): people
[?] Common model or server only? common
Let's add some person properties now.

Define a firstname property for the person model
Enter an empty property name when done.
[?] Property name: firstname

Hit Enter to accept the default string type:
[?] Property type: (Use arrow keys)
❯ string
  number
  boolean
  object
  array
  date
  buffer
  geopoint
  any
  (other)

Make the property required.
[?] Required? (y/N) y

Repeat these steps for lastname property.
Press Enter when prompted for a property name to finish up and create the model.

Run the application

Run as you would any Node application.
$ node .
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/

When you're ready to think about moving to production, give IBM API Connect a try. It enables you to easily deploy your LoopBack app to the cloud or on-premises. It has a built-in API gateway, and it has a visual editor for LoopBack apps!

Explore your REST API

Load http://0.0.0.0:3000/explorer to see the built-in API Explorer.
The API Explorer enables you to exercise all the generated API endpoints. There are create, read, update, and delete (CRUD) endpoints for the people model you just created.
For more information, see the Using the API Explorer in the documentation. You may also want to follow the Getting Started tutorial.