Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback - Easiest way to extend a built-in model

I've been using Loopback to create an API. The documentation is generally really good but doesn't really answer my question about the following: how do I extend (not replace) a built in model?

The most promising piece of information came from this page - it specifies the way of basing a class from another class, via inheritance. This is useful but not ideal - I'd like to create relationships to custom models from the stock models, for example - "Role" should have many "Permission".

The page I mention also shows a Javascript file, located at common/models/<modelName>.js, where it states you can "extend" a model based on the properties and options you give it. The server never seems to hit the file... For example - I put a file in common/models/role.js with the following content:

var properties = {
  exampleProperty: {type: String, required: true}
};


var user = loopback.Model.extend('Role', properties);
console.log('test');

First off, it doesn't seem to hit the file at all (no console.log output given). Second, obviously because of the first point, it doesn't extend the model with the properties I created.

Am I missing something obvious or is the documentation just plain wrong?

like image 817
Tom Hallam Avatar asked Apr 08 '15 12:04

Tom Hallam


1 Answers

You should generate a new model via slc loopback:model named user. By default, the built in user is named User, which is why you can use lowercase user or even UserModel if you prefer. Then when you are prompted by the model generator for a base model, choose User. See https://github.com/strongloop/loopback-faq-user-management/blob/master/common/models/user.json#L3

like image 144
superkhau Avatar answered Oct 05 '22 06:10

superkhau