lce

loopback-component-extender

Loopback Component for extending built-in Loopback Model without defining new model.

Showing:

Popularity

Downloads/wk

1

GitHub Stars

1

Maintenance

Last Commit

4yrs ago

Contributors

0

Package

Dependencies

2

Size (min+gzip)

5.5KB

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Loopback Component for Extending Built-in Model

A component to extend Loopback built-in Model (User, AccessToken, etc.) without creating new Model. loopback-component-extender will loads Model extension synchronously.

Features

  1. Extend built-in model definition:
    • acl
    • hidden
    • mixins
    • options
    • properties
    • relations (Polymorphic and HasAndBelongsToMany is not supported yet)
    • Datasource-related options (e.g. mysql)

Usage

Steps below is an example to extend User model:

  1. Create model file user-x.js and user-x.json in common/models folder.

  2. Write only attributes to be extended in user-x.json such as mapping model to MySQL table, add properties, add relations, etc. No need to rewrite what already defined in User model, unless you need it:

    {
      "mysql": {
        "table": "user"
      },
      "properties": {
        "emailVerified": {
          "type": "boolean",
          "mysql": {
            "columnName": "email_verified",
            "dataType": "smallint",
            "dataLength": null,
            "dataPrecision": 1,
            "dataScale": 0,
            "nullable": "N"
          }
        },
        "verificationToken": {
          "type": "string",
          "mysql": {
            "columnName": "verification_token",
            "dataType": "smallint",
            "dataLength": null,
            "dataPrecision": 1,
            "dataScale": 0,
            "nullable": "Y"
          }
        },
        "createdAt": {
          "type": "Date",
          "required": false,
          "mysql": {
            "columnName": "created_at",
            "dataType": "datetime",
            "nullable": "N"
          }
        },
        "updatedAt": {
          "type": "Date",
          "required": false,
          "mysqlz": {
            "columnName": "updated_at",
            "dataType": "datetime",
            "nullable": "N"
          }
        }
      },
      "mixin": {
        "Controller": true
      },
      "relations": {
        "customer": {
          "type": "hasOne",
          "model": "Customer",
          "foreignKey": "customerId"
        },
      }
    }
    
  3. Write extended methods in user-x.js file.

    module.exports = User => {
      const say = User.say = (userId, message) => {
        console.log('User %d said %s', userId, message)
      }
    
      User.prototype.say = function(message) => {
        let userId = this.id
        say(userId, message)
      }
    }
    
  4. Enable component in component-config.json

    {
      "loopback-component-extender": {
        "models": "User"
      }
    }
    

TODOs

  • Add support for extending Polymorphic and HasAndBelongsToMany relation
  • API documentation
  • Write test

Contributors

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial