balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.83k stars 1.95k forks source link

session aware waterline adapters #1389

Closed mattaylor closed 10 years ago

mattaylor commented 10 years ago

I am trying to use sails to implement some api's on a database with strong user access controls, and would like to be able to pass session tokens from the http request object through to each database query.

The existing API's around models, and adapters do not seem to support this kind of use case. I was thinking of extending some of the controllers blueprints to pass on the req.session to the model lifecycle methods, which would in turn need to be extended to pass this back to the adpater.

Is this the best way to do this, or is there some otherway for the adapter to access session data for the current request it is fullfilling?

Thanks, mat

particlebanana commented 10 years ago

The adapters we maintain all use connection pooling. The connection is opened when Waterline is initialized and each request grabs a connection out of the pool to use for the query. It sounds like what you need is to make the connection params dynamic so that each query can pass in it's own config to use for the one time connection.

I think this would probably require a custom database adapter to be written that accepts a config object on each query method.

mattaylor commented 10 years ago

thats what I was thinking - and then also persist the connection in the connection pool so it can be reused within the session.

I presume this would also mean writing custom methods in each controller as well as each model to accept the config. Is this something you would consider adding to the core for restfull controller blueprints? (ie passing the session object through to the adapter for each controller action)?

mattaylor commented 10 years ago

My suggestion is that the connectionName could be stored in the session on some custom 'login' method. If this was defined in subsequent requests, this connectionName would override the defaults from the connection pool, and would be passed on to the adapter as per the existing waterline 1.0 api's, so as to maintain compatibility with existing waterline adapters. I am working on some changes to waterlines default request handlers to support this.

mikermcneil commented 10 years ago

@mattaylor we've definitely considered doing more to help adapter developers in Waterline core, as far as helping with management of connection configurations. Check out the way we did the mandrill adapter for some ideas: https://github.com/mikermcneil/sails-mandrill/blob/master/index.js#L75

Basically, the solution is to set up static configuration in config/connections.js (assuming you're using the v0.10.x beta), and then send anything dynamic down as an option. In general, the idea is to keep requests to the adapter stateless (a lot like HTTP). The adapter itself might have all sorts of stuff going on, but each request to the adapter would need to pass, at the very least, an identifier. The point of all that is to simplify the usage pattern and guarantee consistency (i.e. we developers always seem to start running into trouble when we bind events in app-level code-- inevitably, the state gets messed up, and in node, that can lead to memory leaks, or worse, a breakdown of app logic and crashes)

Anyways, thanks for the ideas! Would you mind reopening this issue in the waterline repo as a feature request?