Sails.js - I want to add a dynamic DB join after raising the sails

While sailing, I still do not have all the connection information for my database.

Is there a way to either have promises-specific configuration values ​​or dynamically create a connection after the sail is completed?

I would obviously have to add a policy or hook to handle requests for routes that require a model if it was not already available, but at the moment I don’t see how to even let the sail go up until I already know (it should be in configurations).

I hope that I lack the ability to dynamically create connections and wiring models for them.

+6
source share
3 answers

Yes; two things in sails.js let you do this. One of them exists, and one of them is advancing.

  • https://github.com/sgress454/sails-hook-autoreload . This module monitors changes to configuration files on disk and reloads your ORM models when the file changes.

  • I am currently working on this exact function, and my plan is to publish my work at the end of next week. I agree that this will be very helpful.

The API allows you to define new models and connections in the database on the fly. sails.js lifecycle callbacks handle ORMs and adapters, etc. It is event-based and allows you to manually trigger events to update ORM / Connections, for example:

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

That's what you need?

+1
source

Update . In Sails v1.0 / Waterline v0.13 this can be done by accessing the first stateless driver; e.g. sails.getDatastore().driver . This can be used in any database adapter that supports the new stateless driver interface, including MySQL, PostgreSQL and MongoDB.


Prior to Sails v1.0, this was not officially supported directly in Sails or Waterline, but depending on your use case, there are some good solutions for this. If your use case consists of several dynamic connections for development purposes (for example, in a plug-in with automatic reloading) and you are ready to live on the edge, you can use the private API as a short-term workaround: sails.hook.orm.reload() . However, you definitely do not want to use this in production, as it literally clears the entire ORM.

If you intend to deal with a large number (say, 10 unique configurations) of dynamic storage data warehouse configurations throughout the entire life cycle of a running Node process, this is a different story. In this case, I would recommend using the appropriate raw driver (for example, https://github.com/felixge/node-mysql ) to call or release these dynamic connections from the pool directly through a service . You can still use your regular models in your application for static connections - you just better use dynamic database connections separately in your service. For example, if you created a hosted version of phpMyAdmin, you can use the lower-level NPM package to dynamically retrieve information about user tables, but you probably want to have Account and Database models that refer to tables / collections stored in your own database .

A more complete solution for Sails is underway. This ability to leverage the raw connection lifecycle and access it from user space is a prerequisite for the built-in transaction support that we expect to land in Sails / Waterline sometime in the second half of 2016. Meanwhile, if you encapsulate your logic to call / release connections using the service methods, as suggested above, at the moment you will have a working solution, and your business logic should be more or less reliable in the future (when updating, you just you need to change the implementation in your service). Hope this helps!

+2
source

I found a workaround for MySql DB

Important: in my case, I will change the database, but the whole database will have the same scheme, only the difference in their name and the data that they contain, and be sure to add any error handling you need.

In config / connection.js -------- disable union

  mysql_database: { adapter: 'sails-mysql', host: 'localhost', user: 'root', //optional password: '12345', //optional database: 'db1', //optional pool: false }, 

Now go to

node_modules / sails-mysql / lib / connections / spawn.js

Add connectionObject.config = sails.SwitchDbConfig

 connectionObject.config = sails.SwitchDbConfig var conn = mysql.createConnection(connectionObject.config); conn.connect(function (err) { afterwards(err, conn); }); 

Now finally install sails.SwitchDbConfig anywhere (service, controller, etc.). a

 sails.SwitchDbConfig = { pool: false, connectionLimit: 5, waitForConnections: true, adapter: 'sails-mysql', host: 'localhost', user: 'root', password: '12345', database: sails.DB_NAME, identity: 'mysql_database' } 

And finally, if you find something wrong, because you need to update .... please ping

+1
source

Source: https://habr.com/ru/post/984697/


All Articles