Adding models on-the-fly

29 views
Skip to first unread message

Gary Gendel

unread,
Sep 18, 2015, 3:44:59 PM9/18/15
to sails.js
Is there a way to add models on-the-fly in sails correctly?  I've got a unique situation and I want to work with sails instead of against it.

We've got a graph database (orientdb) that we want to have all nodes derived from a base class node.  For example:

pmObject (base class) - table pmObject
|_ pmStuff1 - table foo
|_ pmStuff2 - table bar
|_ ...

pmObject (and the derived classes) require a property, kind.

This allows us to shard the classes in different tables, but still retrieve them by using their base class.  Starting from a node, collect each object from that to the head of the tree.

i.e we can traverse the pmObject graph to pick up the other classes in the graph.  Traversing to retrieve information is imperative.  Allowing us to shard these off into separate tables allows us to optimize operation performance.  Eventually this will be an issue.

To make matters worse, these sub-classed objects (pmStuff1, etc.) are created on-the-fly, not hardcoded into sails model files.

I tried bootstrapping from pmObject in order create the sub-classed objects.  The problem is that migrate=drop will fail because it can't delete pmObject as it is needed by the other objects.

I tried creating a new base class and use pmObject as a virtual class.  That worked great but it's hard to trap the http GET properly as the sub-classed objects aren't known by sails on startup so they don't have the appropriate hooks on standard sails models.

I tried to add the models into sails via waterline.loadCollection() after startup, but I couldn't figure this out.

Bottom line is that I'd like to create them when bootstrapping pmModel as well as when requests come via REST operations on pmModel.  Thanks in advance for any pointers.

Gary

Matthew Marino

unread,
Sep 24, 2015, 4:44:06 PM9/24/15
to sails.js
have you tried sails-hook-autoreload? This is what I use when my apps create controllers or models on the fly.

~Karnith
Reply all
Reply to author
Forward
0 new messages