0

I have two models named : Domain and Group. I'm not able to understand what the following code does

 Domain.app.models.Group.create({
                      "name": groupItem.name,
                      "type": groupItem.type,
                      "refId": groupItem.id
                    }, function(err, group) {
                     // some work here
                    });
Navneet
  • 41
  • 9

1 Answers1

1

Generally, you create models in your common/models directory and then each script receives the model as an argument of the exports function.

One thing to notice is that Loopback app variable is linked to each model with the property app. So, when you call Domain.app you are retrieving the whole Loopback app object, and upon that object, you fetch other models in the application, namely Group in your example.

It's a best practice to declare a var for each model of interest per function in your script and operate with them after that. For example:

var Group = Domain.app.models.Group;
Group.create(...);

Now, the last part is involved with what properties the Group model inherits from its father PersistentModel, in your example the create function (1)(2). In this case, the create property stores the input data in the DataSource attached to your model. This datasource association with the model is declared in server/model-config.json and the boot function in server/server.js is in charge of reading this file and execute the pertinent routines to do the actual model attachment.