loopback-component-extender
v0.1.0
Published
Loopback Component for extending built-in Loopback Model without defining new model.
Downloads
3
Maintainers
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
- 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:
- Create model file
user-x.js
anduser-x.json
incommon/models
folder. - 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 inUser
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" }, } }
- 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) } }
- Enable component in
component-config.json
{ "loopback-component-extender": { "models": "User" } }
TODOs
- [ ] Add support for extending Polymorphic and HasAndBelongsToMany relation
- [ ] API documentation
- [ ] Write test
Related Works
Contributors
- Saggaf Arsyad [email protected]