Skip to content

Commit e3567db

Browse files
jaumardjaumard
jaumard
authored and
jaumard
committed
Clean code
Add possibility to disable automatic orm reload
1 parent 56329cd commit e3567db

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# sails-util-mcvsloader
2-
Load the models, controllers, services, policies and config from your Sails hooks into the main app
1+
# sails-util-mvcsloader
2+
Load the models, controllers, services, policies and config from your Sails hooks into the main app and extend it on your main sails server if you need
33

44
### USAGE
55
Look the example folder.
@@ -14,7 +14,7 @@ npm install --save sails-util-mvcsloader
1414

1515
```
1616
module.exports = function(sails) {
17-
var hookLoader = require('sails-hook-hookloader')(sails);
17+
var hookLoader = require('sails-util-mvcsloader')(sails);
1818
hookLoader.injectAll({
1919
policies : __dirname + '/policies',// Path to your hook's policies
2020
config : __dirname + '/config'// Path to your hook's config
@@ -24,8 +24,7 @@ module.exports = function(sails) {
2424
hookLoader.injectAll({
2525
controllers: __dirname+'/controllers', // Path to your hook's controllers
2626
models: __dirname+'/models', // Path to your hook's models
27-
services: __dirname+'/services', // Path to your hook's services
28-
config: __dirname+'/config' // Path to your hook's config
27+
services: __dirname+'/services' // Path to your hook's services
2928
}, function(err) {
3029
return cb();
3130
});
@@ -34,6 +33,24 @@ module.exports = function(sails) {
3433
}
3534
```
3635

36+
### USED BY
37+
[sails-hook-passport](https://github.com/jaumard/sails-hook-passport)
38+
39+
### TROUBLES
40+
If you use multiple hooks how use this module you will have this error :
41+
42+
error: Failed to reinitialize ORM.
43+
error: AdapterError: Connection is already registered
44+
45+
To fix this just create a config/mvcsloarder.js with :
46+
47+
module.exports.mvcsloader = {
48+
reloadORM : false
49+
};
50+
51+
And add this on your config/bootstrap.js
52+
53+
sails.hooks.orm.reload();
3754

3855
### TODO
3956
- Add support for loading :

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module.exports = function (sails) {
8181
toLoad.push(loadServices);
8282
}
8383

84-
async.waterfall(toLoad, function (err) {
84+
async.parallel(toLoad, function (err) {
8585
if (err) {
8686
sails.log.error(err);
8787
}

libs/models.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
var async = require('async');
55
var _ = require('lodash');
66
var buildDictionary = require('sails-build-dictionary');
7-
var utils = require(__dirname + '/utils.js');
87

98
module.exports = function (sails, dir, cb) {
109
async.waterfall([function loadModelsFromDirectory(next) {
@@ -22,13 +21,6 @@ module.exports = function (sails, dir, cb) {
2221
replaceExpr: /^.*\//,
2322
flattenDirectories: true
2423
}, function (err, supplements) {
25-
if (err) {
26-
return cb(err);
27-
}
28-
return next(null, models, supplements);
29-
});
30-
}, function bindSupplementsToSails(models, supplements, next) {
31-
utils._bindToSails(sails, supplements, function (err, supplements) {
3224
if (err) {
3325
return cb(err);
3426
}
@@ -39,7 +31,16 @@ module.exports = function (sails, dir, cb) {
3931

4032
return next(null);
4133
}, function reloadSailsORM(next) {
42-
sails.hooks.orm.reload();
34+
if (!sails.config.mvcsloader || (sails.config.mvcsloader && sails.config.mvcsloader.reloadORM)) {
35+
var d = require('domain').create();
36+
d.on('error', function (err) {
37+
// handle the error safely
38+
sails.log.error("mvcs-loader error, you have multiple hooks that try to reload orm, look here (https://github.com/jaumard/sails-util-mvcsloader/#troubles) to disable orm reload and do it manually on your bootstrap.js");
39+
});
40+
d.run(function () {
41+
sails.hooks.orm.reload();
42+
});
43+
}
4344

4445
return next(null);
4546
}], function (err) {

libs/services.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
var async = require('async');
55
var _ = require('lodash');
66
var buildDictionary = require('sails-build-dictionary');
7-
var utils = require(__dirname + '/utils.js');
87
module.exports = function (sails, dir, cb) {
98
async.waterfall([function loadServicesFromDirectory(next) {
109
buildDictionary.optional({
@@ -13,8 +12,6 @@ module.exports = function (sails, dir, cb) {
1312
replaceExpr: /^.*\//,
1413
flattenDirectories: true
1514
}, next);
16-
}, function bindServicesToSails(modules, next) {
17-
utils._bindToSails(sails, modules, next);
1815
}, function injectServicesIntoSails(modules, next) {
1916
sails.services = _.merge(modules || {}, sails.services || {});
2017
if (sails.config.globals.services) {

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name" : "sails-util-mvcsloader",
3-
"version" : "0.1.5",
4-
"description" : "Load models, controllers, services, policies and config from your Sails hooks into the main app",
3+
"version": "0.1.6",
4+
"description": "Load the models, controllers, services, policies and config from your Sails hooks into the main app and extend it on your main sails server if you need",
55
"main" : "index.js",
66
"scripts" : {
77
"test": "echo \"Error: no test specified\" && exit 1"
@@ -14,7 +14,17 @@
1414
"sails",
1515
"sails-hook",
1616
"loader",
17-
"mvc"
17+
"mvc",
18+
"hook-models",
19+
"hook-controllers",
20+
"hook-services",
21+
"hook-config",
22+
"hook-policies",
23+
"models",
24+
"controllers",
25+
"services",
26+
"config",
27+
"policies"
1828
],
1929
"author" : "Leeroy Brun, Aumard Jimmy",
2030
"license" : "MIT",

0 commit comments

Comments
 (0)