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
  • Remember
  • Info
  1. 3. The Backend

Extending built-in models

The built-in User model is quite all right but many developers quickly find a need to extend it. The most common cause is the need to fetch all the entities related to a given user. For example: a) find all the roles for the current user, or b) find all stores that belong to the current user, or c) find all suppliers that belong to the current user, or d) find all reports that belong to the current user, etc.

There isn't a concrete naming convention for extended models but there are some practical considerations around what not to do.

You have the option of naming a user (lowercase) model which builds upon the built-in User model as its base. But personally that makes me far too uneasy as I feel that the possibility of mistakes around which model actually gets used, both in the loopback core code and developer code, skyrockets.

Instead let's name it UserModel instead, no room for confusion!

Create a new model: common/models/user-model.json

$ slc loopback:model

Just found a `.yo-rc.json` in a parent directory.
Setting the project root at: /home/codio/workspace/loopback-zero-to-hero

? Enter the model name: UserModel
? Select the data-source to attach UserModel to: db (memory)
? Select model's base class: User

|||warning

Remember

Don't forget to set the model's base class as user |||

Carry on:

? Custom plural form (used to build REST URL):
Let's add some UserModel properties now.

Enter an empty property name when done.
? Property name:

Leave the last prompt (? Property name:) empty and hit enter to finish model creation.

|||info

Info

Hit the refresh symbol at the bottom right of the nav tree panel to see the new common/models directory and its contents listed. |||

PreviousBuilt-in modelsNextUnderstanding UserModel

Last updated 7 years ago