-
Notifications
You must be signed in to change notification settings - Fork 9
How to pass a database connection to plugins? #182
Comments
If you use "hapi-rethinkdb" then it exposes the connection (https://github.com/ghostbar/hapi-rethinkdb/blob/master/index.js#L26) that you can use in your plugin: const connection = server.plugins['hapi-rethinkdb'].connection; You will also need to make "hapi-rethinkdb" a dependency of your plugin. |
The right way to do this is via the I usually also set up an onRequest extension which copies |
Why? Also, what's the intended usage of server.expose then, server methods where you don't need caching? Confused because my general rule was "share functions as methods, and other stuff via expose" |
In general, if you find yourself using a lot of dependencies between your own application plugins, you are doing it wrong. |
Thanks for the suggestions :) I will try the @hueniverse Was my assumption correct that objects passed to plugins as options are copies instead of reverences? Would be helpful to mention it in the docs if so. Hard to find out without any information/help. |
@electerious it should not be cloning the options. If it does ,it's a bug. |
@hueniverse The comment says: this.app = {}; // Place for application-specific state without conflicts with hapi, should not be used by plugins So which one is true? Can plugins (external modules, for example hapi-rethinkdb) add properties to |
They should not. They should use the plugins namespace. But using plugins to load a db connection is an odd pattern. |
Finally had time to try Here's my code: // --- Main application ---
console.log(1, data) // 1, {…}
server.app = data
console.log(2, server.app) // 2, {…}
// --- Plugin ---
plugin = function(server, options, next) {
console.log(3, server.app) // 3, {}
}
plugin.attributes = {
pkg: require('../../package')
}
module.exports = plugin The plugin gets registered after setting 1, {…} // Filled object
2, {…} // Filled object
3, {} // Empty object I'm using the newest hapi.js version (11.1.2). Is there anything I'm doing wrong? Should be correct according to docs. |
You need to use |
@johnbrett Thanks a lot! :) Works. RethinkDB still says |
I don't think that last issue is hapi-related. In any case, perhaps the connection hasn't closed, but rather hasn't opened yet. |
Yes, I don't think it's an hapi.js issue/problem anymore. I will try to find a solution and keep this thread updated. Thanks for the help! |
|
Finally found the problem 🎉 The provided error is misleading. It's not the connection, it's the rethinkdb module! I import/require it in the main application and inside plugins. It's the same module, but a different instance of it. The connection itself is the same and still open. Passing the connection AND the rethinkdb instance to plugins works. Passing the connection to plugins and using their rethinkdb does not. The connection must be from the same instance as later used to get data from the database. Passing everything down to plugins is a bad idea and I will try to find a better solution. |
Hi,
I'm trying to pass an open database connection to a Hapi plugin using the
options
property. The given connection is available inside the plugin, but it looks like the connection object has been passed to it as a value and not as a reference.rethinkdb throws the following error:
What's the best way to pass a connection to a plugin?
Hapi Version: 11.0.2
Thanks for the help.
The text was updated successfully, but these errors were encountered: