Loopback Console

Using the API Explorer in the browser is all right but if you're looking for something a bit slicker then the loopback-console can be your goto tool.

As of this writing, it is developed and maintained by Heath Morrison, his GitHub handle is doublemarked and you can reach out to him to express your gratitude if you like, in a gitter.im chat room, if you feel so inclined.

loopback-console has many advanced uses and it is even used in production alongside deployed loopback solutions for some very clever reasons that I do not fully understand ... so we will stick to one of its simpler uses: command line interface (CLI) for exploring the API.

Lets get cracking:

a) Install it in your loopback project:

$ cd ~/workspace/loopback-zero-to-hero
$ npm install --save loopback-console
loopback-console@0.1.0 node_modules/loopback-console
├── repl.history@0.1.3
├── app-root-path@1.0.0
└── lodash@3.10.0

b) Set it up for easy access. Update your package.json file. FROM:

  "scripts": {
    "pretest": "jshint ."
  },

TO:

  "scripts": {
    "console": "$(npm bin)/loopback-console .",
    "pretest": "jshint ."
  },

c) Take it for a spin:

c.1) npm run console

c.2) Let's try to list all users: var allUsers = UserModel.find({include:'roles'},cb)

c.3) Results will be something like:

loopback > [ { username: 'admin',
    password: '$2a$10$R.LFPzD7Ov3KZOyw9unS2.drCAbBI7IEVLCK.qX7H/28oKhVME0vq',
    email: 'admin@admin.com',
    firstName: 'Admin',
    lastName: 'User',
    id: 1,
    roles: [ [Object] ] },
  { username: 'user',
    password: '$2a$10$ZXi51B8JH2Su48XA5P/Y7eUrgqiUbnvfyBiJa1dTFlupeKct9X9ei',
    email: 'user@user.com',
    firstName: 'Guest',
    lastName: 'User',
    id: 2,
    roles: [ [Object] ] } ]

press ctrl+c once to kill this command so that you may enter the next one.

c.4) To unpack the roles: [ [Object] ] piece, let's look at a specific result from the array: result[0]

loopback > result[0]
{ username: 'admin',
  password: '$2a$10$R.LFPzD7Ov3KZOyw9unS2.drCAbBI7IEVLCK.qX7H/28oKhVME0vq',
  email: 'admin@admin.com',
  firstName: 'Admin',
  lastName: 'User',
  id: 1,
  roles:
   [ { name: 'admin',
       created: Sun Jul 05 2015 03:06:07 GMT+0000 (UTC),
       modified: Sun Jul 05 2015 03:06:07 GMT+0000 (UTC),
       id: 1 } ] }

now you can clearly see the roles.

You can let your imagination go wild for all the ways you might use this power, for now we'll just move on.

Last updated