Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adapter reloading when using different instances of sails #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = function initialize(hook, sails, done){
try {
// Otherwise, we'll try and load it as a dependency from the app's `node_modules/` folder,
// and also validate and normalize it.
hook.adapters[datastoreConfig.adapter] = loadAdapterFromAppDependencies(datastoreConfig.adapter, datastoreName, sails);
hook.adapters[datastoreConfig.adapter] = loadAdapterFromAppDependencies(datastoreConfig.adapter, datastoreName, sails, !(datastoreConfig.adapter in hook.adapters));
} catch (e) {
// Special case -- the default adapter will load the sails-disk bundled with sails-hook-orm if it's not installed locally.
if (datastoreName === 'default' && datastoreConfig.adapter === 'sails-disk' && e.code === 'E_ADAPTER_NOT_INSTALLED') {
Expand Down
12 changes: 11 additions & 1 deletion lib/load-adapter-from-app-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var couldNotLoadAdapterError = require('../constants/could-not-load-adapter.erro
* @throws {Error} E_ADAPTER_NOT_COMPATIBLE
*/

module.exports = function loadAdapterFromAppDependencies(adapterPackageName, datastoreIdentity, sails) {
module.exports = function loadAdapterFromAppDependencies(adapterPackageName, datastoreIdentity, sails, reloadAdapter) {

// ╦ ╔═╗╔═╗╔╦╗ ╔═╗╔╦╗╔═╗╔═╗╔╦╗╔═╗╦═╗
// ║ ║ ║╠═╣ ║║ ╠═╣ ║║╠═╣╠═╝ ║ ║╣ ╠╦╝
Expand All @@ -48,6 +48,16 @@ module.exports = function loadAdapterFromAppDependencies(adapterPackageName, dat
// Now try to require the adapter from userland dependencies (node_modules of the sails app).
var adapter;
try {
// This is a work around so we don't have to modify each adapter interface. By default, NodeJS
// module system requires files with a cache. Because of that, each adapter will be a Singleton
// so we can't have two instances of Sails using the same adapter, making Sails unuseful for
// multi-same-technology-databases.
// The ideal solution would be that adapters export a function instead of an object, so each
// adapter can choose what to cache and what not. But that will imply a breaking change in all
// the adapters interfaces.
if (reloadAdapter) {
delete require.cache[require.resolve(adapterPackagePath)];
}
adapter = require(adapterPackagePath);
} catch (e) {
// If there was a problem loading the adapter,
Expand Down