Search code examples
node.jssails.jswaterline

sails.js - I want to add DB connection dynamically after sails lift


During sails lift I don't yet have all the connection information for my DB.

Is there a way to either have config values dependent on promises or dynamically create a connection after sails lift has completed?

I would obviously have to add a policy or hook to handle requests to routes needing the model if it wasn't available yet, but at this point I don't see how to even let the sails lift until I already know the connection info (it must be in the configs).

I'm hoping I'm missing a way to dynamically create connections and wire models to them.


Solution

  • Yes; two things in sails.js allow you to do this. One currently exists, and one is upcoming.

    1. https://github.com/sgress454/sails-hook-autoreload. This module watches for config file changes on disk and will re-load your ORM models when a file changes.

    2. I am working on this exact feature right now, and my plan is to publish my work at the end of next week. I agree that it will be very useful.

    The API will allow you to define new Models and Connections in the database on the fly. sails.js lifecycle callbacks handle updating the ORM and adapters and so forth. It is event-based and will allow you to manually fire events to update the ORM/Connections, like so:

    • sails.emit('hook:dynamic-orm:reload')

    Is this what you need?