training.shoppinpal.com
  • Introduction
  • 1. The Ideal Workspace
    • The Perfect Machine
      • For Biz Team
      • For Developers
      • For Designers
    • Setup a machine in the cloud
      • Solution
      • Setup box on Azure
        • Create a machine on Azure
        • Test drive your remote machine
        • Setup Dropbox On Azure
      • Setup box on DigitalOcean
        • Setup UI
        • Shared FileSystem
          • Dropbox
            • Use locally developed node modules in another project
          • sshfs
        • Long Running Sessions
      • Feedback
  • 2. Learning Git
    • Static Code Analysis
  • 3. The Backend
    • Use Containers
    • Setup a loopback project
    • Lockdown
    • Build a better mousetrap
    • The abyss stares back
    • Built-in models
    • Extending built-in models
    • Understanding UserModel
    • Boot Scripts
    • Promises
    • Find roles for current user
    • Loopback Console
    • Current User
  • 4. Multi-tenancy With Loopback
    • What is Multi-Tenancy
    • Architecting with Loopback
    • Define scope for Roles
    • Role Resolvers
    • Access Control For Tenants
    • Better Programming with multi-tenancy
  • 5. The Frontend
    • The Browser
    • Unit Testing
      • Motivation behind this blog
      • How to write a test
      • Karma and Jasmin
      • Writing Tests
    • End-2-End Testing
    • Angular 1.x
    • Angular 2
      • Testing
  • 6. ElasticSearch
    • Better Search with NGram
    • NGram with Elasticsearch
    • Fun with Path Hierarchy Tokenizer
    • Working with Mappings and Analyzers
  • 7. Promises
    • What are Promises
    • Promise Implementations
    • Nuances
    • What should we use
  • 8. Learning Docker
    • Docker Swarm
  • 9. Queues & Workers
    • PHP workers in AWS EBS
    • NodeJS workers in AWS EBS
      • SQS Daemon by AWS
      • SQS Daemon in NodeJS
      • SQS polling by worker
    • Gearman
  • 10. Docker
    • Capabilities
  • Appendix
    • Bug in WebStorm deployments
    • The Perfect Terminal
    • Scalable App Deployment with AWS
    • Chrome Tips & Tricks
    • Host your own Gitbook
    • Gitbook Tips & Tricks
    • How to handle support incidents
    • Dev Resources
    • Debug e2e Tests on CircleCI
    • Logging
    • Authentication Principles
    • Mac
    • nvm
    • Unify testing with npm
      • Debugging Mocha
    • Sequence Diagrams
    • Project Sync via IDE
      • SFTP with WebStorm
      • SFTP with Visual Studio
    • Soft Linking
    • NodeJS Profiling
      • How to find node.js performance optimization killers
    • Setup Packer on Azure
Powered by GitBook
On this page
  1. 3. The Backend

Understanding UserModel

You should see two new files:

$ cd ~/workspace/loopback-zero-to-hero  
$ tree common -L 3
common
└── models
    ├── user-model.js
    └── user-model.json

1 directory, 2 files

Take note of the following: a) user-model.json has the following lines:

  "name": "UserModel",
  "base": "User",

b) the files are named user-model.js and user-model.json but the internal name used by code is UserModel, this is part of loopback's naming convention.

c) user-model.json serves as a configurable entry point whereas user-model.js lets you add code when something more specialized may be required, something that can't be simply configured.

d) Moving forward we will always use UserModel so we can and should hide the User model from the prying eyes of our REST~ful api.

e) The server/model-config.json file is the gatekeeper for what's exposed over REST and what's not.

cat ~/workspace/loopback-zero-to-hero/server/model-config.json
{
  ...
  "User": {
    "dataSource": "db"
  },
  ...
  "UserModel": {
    "dataSource": "db",
    "public": true
  }
}

f) Go ahead and set "public": false for User in model-config.json file:

{
  ...
  "User": {
    "dataSource": "db",
    "public": false
  },
  ...
}

g) Fire up the server:

$ cd ~/workspace/loopback-zero-to-hero
$ node .
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/

h) Project > Box Info

i) Open a browser window: http://<box-name>.codio.io:3000/explorer/

j) The UserModel api will be ready to explore and User will be no more!

k) Use /POST UserModels from the explorer UI to create a user:

    {
      "username": "test",
      "password": "test",
      "email": "test@test.com"
    }
Use that as the json body and then click the `Try it out!` button.
l) There is a distinct possiblility that your server will crash at this point with `TypeError: Object.keys called on non-object`

Here's the full sample stack trace:

events.js:72 throw er; // Unhandled 'error' event ^ TypeError: Object.keys called on non-object at Function.keys (native) at Memory.all (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/connectors/memory.js:315:22) at /home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/dao.js:1453:19 at doNotify (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at Function.ObserverMixin._notifyBaseObservers (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:116:5) at Function.ObserverMixin.notifyObserversOf (/home/codio/workspace/loopback-zero-to-hero/node_modules/loopback-datasource-juggler/lib/observer.js:91:8)

|||guidance
# Guidance

If this happens, simply delete the previous `db.json` file:
`rm ~/workspace/loopback-zero-to-hero/db.json `
And start the server again, then Follow steps (g) through (k) again.
|||

m) Take a peek at the database, it should have a new UserModel instance: `cat db.json`

$ cat db.json { "ids": { "User": 1, "AccessToken": 1, "ACL": 1, "RoleMapping": 1, "Role": 1, "UserModel": 2 }, "models": { "User": {}, "AccessToken": {}, "ACL": {}, "RoleMapping": {}, "Role": {}, "UserModel": { "1": "{\"username\":\"test\",\"password\":\"$2a$10$DD6l9hEK0YraAvQ9wxXoyujLyted4YUUOUX9opiTUe8RwPOGe8mY2\",\"email\":\"test@test.com\",\"id\":1}" } } }

```

PreviousExtending built-in modelsNextBoot Scripts

Last updated 7 years ago