creating models dynamically

789 views
Skip to first unread message

jmls

unread,
Mar 17, 2015, 1:06:15 PM3/17/15
to loopb...@googlegroups.com
I want to remove the need for the model.json file - and so I thought that I would dynamically create the models from some database record

I've read up on dataSource.createModel(className, properties, properties) and it seems the perfect way to load the properties in. 

However, if I want to define custom code on the model , like

MyModel.renderView = function( .. ) {}

how can I add that to the dynamic model ? Can I create the .js file in common/models ? 

jmls

unread,
Mar 17, 2015, 2:04:40 PM3/17/15
to loopb...@googlegroups.com
so far, I have got this

module.exports = function buildModels(app) {
 
 
var ds = app.dataSources['myDB']

 app
.models.NewModel.find(function(err,models) {

  models
.forEach(function(model) {
   
var newModel = ds.createModel(model.name,model.properties)
    console
.log(newModel)
    app
.model(newModel,{dataSource: "nodegloo", public: true})
 
})
 
})
};


this runs without error, and a model is logged to the console

however, I can't get the model to appear in the explorer. Which chicken bones, or chant, or both, am I missing ?

Raymond Feng

unread,
Mar 17, 2015, 2:15:55 PM3/17/15
to jmls, loopb...@googlegroups.com
You need to do something similar as https://github.com/strongloop/loopback-example-database/blob/feature/discovery-during-boot/server/boot/discover.js#L23-L24.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
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/d/optout.

jmls

unread,
Mar 17, 2015, 2:51:18 PM3/17/15
to loopb...@googlegroups.com, jul...@dotr.com
thanks ... I think I have a chicken and egg though ..

I want to store the data for the models in a database .. and get those records using loopback. So I defined a single .json / .js file called NewModel and created a boot script file  as below

module.exports = function buildModels(app,cb) {

 
 
var ds = app.dataSources['myDB']

 app
.models.NewModel.find(function(err,models) {

  models
.forEach(function(model) {
   
var newModel = ds.createModel(model.name,model.properties)

    newModel
.setup()
    app
.model(newModel,{dataSource: "myDB", public: true})
    console
.log("loaded",model.name)
 
})

 
return cb()
 
})
};

I get the message "loaded foobar" , but then loopback seems to hang

if I put the return cb() immediately after the "var ds" then the dynamic model is obviously not created, but loopback starts as normal

what am I missing ?

Julian

jmls

unread,
Mar 25, 2015, 4:46:49 PM3/25/15
to loopb...@googlegroups.com, jul...@dotr.com
*bump*

anyone able to give some help here ?

thanks

jmls

unread,
Apr 23, 2015, 8:34:41 AM4/23/15
to loopb...@googlegroups.com, jul...@dotr.com
ok, asking again :(

#1 if you are creating dynamic models, how do you add custom code to the model ? for example, If I wanted to add a foo() function, I would normally modify common/models/mymodel.js and add MyModel.foo = function() {} . How do I do this with a dynamic model
#2 how do you add the model to the rest api and explorer api without restarting loopback ?

thanks

Raymond Feng

unread,
Apr 23, 2015, 11:58:28 AM4/23/15
to loopb...@googlegroups.com, jul...@dotr.com
Do you use the async boot in server.js?

boot(app, __dirname, function(err) {
 // … The rest of the server.js code
});

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

jmls

unread,
Apr 23, 2015, 2:59:45 PM4/23/15
to loopb...@googlegroups.com, jul...@dotr.com
Yes I am doing that for models found during startup. But how do I make the rest and explorer api available on the model if I add a new one during runtime , not startup

The other question was regarding adding functions to a dynamic model - ie how ? ;) 

thanks

jmls

unread,
Apr 24, 2015, 12:56:47 PM4/24/15
to loopb...@googlegroups.com, jul...@dotr.com
I can create the model, and it gets added to the app.models list 

        var newModel = ds.createModel(name,options || {}, properties)
        newModel.setup()
        app.model(newModel,{dataSource: dbName, public: true})
        console.log("created",name)

at this point, the model does not appear in the api or explorer

what do I need to do / what am I missing ? 

I've tried to reload the boot process, still nothing

julian

jmls

unread,
Apr 25, 2015, 7:18:51 AM4/25/15
to loopb...@googlegroups.com, jul...@dotr.com
ok, I've worked out how to load functions into a model

now, as I need to add the explorer api and rest api to the model, at the moment I am restarting the loopback process. Is there a better way ?

Julian

Sergey Nosenko

unread,
Aug 25, 2016, 5:14:32 AM8/25/16
to LoopbackJS, jul...@dotr.com

This is my Model.js file

model.prototype.registerModel = function(cb) {
            var server = model.app;
        var schema = this.getSchema();
        var Model = server.dataSources.db.createModel(schema.name, schema.properties, schema.options);
        server.model(Model, {
            "dataSource": "db"
        });
        server.dataSources.db.autoupdate(schema.name, function(err, res) {
            console.log("CHECK AND CREATE TABLES for " + schema.name, err, res);
            cb();
        });
    };

    model.observe('after save', function(ctx, next) {
        if (ctx.instance) {
                           ctx.instance.registerModel(next);
        } else {
                           next();
        }
    });

and boot file:

module.exports = function(server) {
      server.models.Dimension.find({
            where: {
                isSimple: true
            }
        }, function(err, list) {
            async.eachSeries(list, function(m, fn) {
                m.registerModel(fn);
            }, cb);
        });
    };
    });

}


Everything works like a charm, even if I add new model in runtime from Admin panel. BUT i have an issue with clustering. Only one fork can see newly created model on "after save".

суббота, 25 апреля 2015 г., 14:18:51 UTC+3 пользователь jmls написал:
Reply all
Reply to author
Forward
0 new messages