How to create a model programmatically

565 views
Skip to first unread message

Antonio Calanducci

unread,
Feb 6, 2014, 5:49:53 PM2/6/14
to loopb...@googlegroups.com
Hi,

looking at the documentation is seems there are 3 ways to create model programmatically:

1) using the Model.extend() method, like in this example:

var Model = require('loopback').Model;
var Product = Model.extend('product');

2) using the app.model() method, like in the example:

var Widget = app.model('Widget', {dataSource: 'db'});

3) using the loopback.createModel() method.


In which cases we should use the three different methods?

thank you


Raymond Feng

unread,
Feb 6, 2014, 7:35:54 PM2/6/14
to Antonio Calanducci, loopb...@googlegroups.com
This is the beauty that JavaScript doesn’t have private/protected methods :-)

3 is the recommended way to create model programatically. It invokes 1 internally and try to attach it to a datasource if necessary.
2 is typically used to expose the model to REST APIs, for example, app.model(MyModel). But it also support creating and exposing the model from config. The scaffolded application uses it to load models.json.
1 is the model inheritance. You can SuperModel.extend(…) to create a SubModel.

Thanks,
Raymond 

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Andrew B

unread,
Aug 28, 2014, 3:47:57 PM8/28/14
to loopb...@googlegroups.com, in...@etnatraining.it
With LoopBack 2.0 where is the best place to programmatically create models.
In 1.x it would run js in the models dir. In 2 it won't run the js unless the model is defined in model-config.json with a datasource and a mymodel.json is provided.

Adding a model in model-config.json to get the js to run seems like it would be duplicating the creation of a model and attaching it to a datasource, especially if the datasouce is going to be changed programatically but is required to be hard coded in the model-config.json

Where have people been programatically creating their models in 2.x?
Thanks!
Reply all
Reply to author
Forward
0 new messages