So,
I'm in the middle of implementing a plugin api for my application, and the plugins can have their own models, imagine this.
SimplePlugin = {
pluginName: 'simple',
pluginConfig: {},
SimpleModel: {
attributes: {
name: 'string'
}
}
}
So I need to be able to create the "one-time" model with a function whenever it's needed, it needs to have exactly the same functionality as other models so you automatically get the urls like /simplePlugin/:id
for find
..etc
Thanks
surprised sails doesn't support this in 2018: I have continued the above package with a fork ( @eyn answer) with updates that work for sails v1.x.x.
https://github.com/emahuni/sails-util-micro-apps
I changed it to that coz I am changing a lot of thing in that package. Instead of loading just models and controllers i want to it to load whole apps, mini-apps for a micro service architecture in sails. This is such that you can make mini-apps that can be joined together to form one large app out of reusable apis code.
In v0.12
sails.hooks.orm.normalizeModelDef
doesn't exists anymore. Alsosails/lib/hooks/orm/loadUserModules
went to thesails-hook-orm
npm module and is not longer part of sails.Try this: "Load models, controllers, services, policies and config from specified directories and inject them into the main Sails app."
https://github.com/leeroybrun/sails-util-mvcsloader
What are you trying to do is not easy and a bit messy with Sails in the current state of the project. I'm referring to the v0.10 version. What you'll have to do is
SimplePlugin.SimpleModel
into sails.models_config: { rest: true }
Please note that the code examples I posted are taken from a custom Sails hook I am working on and assume access to
sails
and the code examples to be executed during theloadHooks
phase of Sails initialization / before theMiddlewareRegistry
phase (compare: lib/app/load.js).1. Inject model definition
Following the hints in the
orm
hook in Sails v0.10 you have to:api
, merge your new model into the dictionarysails.hooks.orm.normalizeModelDef
teardown
sails.hooks.orm.prepareModels
(previously:sails.hooks.orm.exposeModels
, changed with: 8d96895662)Because you have to reinitialize Waterline and reload all model definitions I'd recommend to collect all model definitions to inject and pass them to the inject function once. The example code below reflects this.
Would allow you to:
2. Inject controller
For each model that should be exposed via blueprint methods you have to:
sails.controllers[controllerId]
sails.hooks.controllers.middleware[controllerId]
The Sails
MiddlewareRegistry
will automatically pick up the controllers found in these objects.3. In action
EDIT: not working completely since collections are assigned to connections at initialization.
Seems that there is a better solution, with 3 lines of code and without disconnecting/reconnecting databases. I just studied the source code of Waterline (see https://github.com/balderdashy/waterline/blob/master/lib/waterline.js#L109). It's possible to do something like: