Skip to content
This repository was archived by the owner on Feb 25, 2022. It is now read-only.

Commit ae90fb6

Browse files
committedMay 21, 2016
refactor(Connection, Discoverable): Finished es2015 refactor
1 parent 7a9b26a commit ae90fb6

11 files changed

+98
-64
lines changed
 

‎dist/index.js

+4-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/lib/Connect.js.map

-1
This file was deleted.

‎dist/lib/Connect.js ‎dist/lib/Connection.js

+27-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/lib/Connection.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/lib/Discoverable.js

+20-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/lib/Discoverable.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Connect
2-
import Connect from './lib/Connect';
2+
import Connection from './lib/Connection';
33

4-
// Export Connect
5-
export default Connect;
4+
// Export Connection
5+
module.exports = Connection;

‎lib/Connect.js ‎lib/Connection.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let instance = null;
1212

1313
class Connection {
1414

15-
constructor(database, username, password, options={}, discover=["/model"], matcher=null, logger=console) {
15+
constructor(database, username, password, options={}, discover=["/model"], matcher=null, logger=false) {
1616

1717
if(instance) return instance;
1818

@@ -28,7 +28,9 @@ class Connection {
2828
this.models = {};
2929
this.Sequelize = Sequelize; // Expose Sequelize
3030

31-
this._connect()
31+
instance = this._connect();
32+
33+
return instance
3234
.then((connection) => {
3335
return instance = connection;
3436
});
@@ -43,10 +45,10 @@ class Connection {
4345
// return the instance, although this shouldn't be being called externally
4446
if(instance) return instance;
4547

46-
this.logger.log("info", "Connecting to: " + this.database + " as: " + this.username);
48+
this._log("info", "Connecting to: " + this.database + " as: " + this.username);
4749

4850
// Instantiate a new sequelize instance
49-
let sequelize = new db.Sequelize(this.database, this.username, this.password, this.options);
51+
let sequelize = new this.Sequelize(this.database, this.username, this.password, this.options);
5052
let models = {};
5153

5254

@@ -59,43 +61,52 @@ class Connection {
5961
let model = sequelize["import"](path);
6062

6163
if(model) {
62-
this.logger.log("debug", "Import for path succeeded: " + path);
64+
this._log("debug", "Import for path succeeded: " + path);
6365
models[model.name] = model;
6466
} else {
65-
this.logger.log("debug", "Import for path failed: " + path);
67+
this._log("debug", "Import for path failed: " + path);
6668
}
6769

6870
})
6971
.then((path) => {
7072
// Execute the associate methods for each Model
71-
this.logger.log("info","Import completed");
73+
this._log("info","Import completed");
7274

7375
return Promise.each(Object.keys(models), (modelName) => {
7476

7577
if ("associate" in models[modelName]) {
76-
this.logger.log("debug", "Associating Model: "+ modelName);
78+
this._log("debug", "Associating Model: "+ modelName);
7779
models[modelName].associate(models);
7880
} else {
79-
this.logger.log("debug", "Nothing to associate for Model: "+ modelName);
81+
this._log("debug", "Nothing to associate for Model: "+ modelName);
8082
}
8183
});
8284

8385
})
8486
.then(() => {
8587
// Syncronize the DB
86-
this.logger.log("info", "Finished connecting to: " + database + " as: " + username);
88+
this._log("info", "Finished connecting to: " + this.database + " as: " + this.username);
8789
return sequelize.sync();
8890

8991
})
9092
.then(() => {
91-
this.logger.log("info", "Finished synchronizing " + database);
93+
this._log("info", "Finished synchronizing " + this.database);
9294
// Expose objects
9395
this.sequelize = sequelize;
9496
this.models = models;
9597

9698
return this;
9799
});
98100
}
101+
102+
/**
103+
* Attempt to log
104+
* @param {String} message Message to log
105+
* @return {null}
106+
*/
107+
_log(level, message){
108+
this.logger ? this.logger.log(level, message) : false;
109+
}
99110
}
100111

101-
export default Connection;
112+
module.exports = Connection;

‎lib/Discoverable.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let fs = Promise.promisifyAll(f);
66

77
class Discoverable {
88

9-
constructor(paths, matcher, logger=console){
9+
constructor(paths, matcher, logger=false){
1010
this.paths = paths;
1111
this.matcher = matcher;
1212
this.logger = logger;
@@ -22,19 +22,19 @@ class Discoverable {
2222

2323
let discovered = [];
2424

25-
return Promise.each(this.paths, function(location){
25+
return Promise.each(this.paths, (location) => {
2626

2727
// Recurse through the api directory and collect the models
2828
return this._dive(location)
2929
.then((results) => {
30-
this.logger.log("debug", "Flattening results");
30+
this._log("debug", "Flattening results");
3131
return _.flatten(results, true);
3232
})
3333
.filter((value) => {
3434
return value !== false
3535
})
3636
.then((results) => {
37-
this.logger.log("debug", "Assigning filtered results to discover: " + results);
37+
this._log("debug", "Assigning filtered results to discover: " + results);
3838
return discovered = results;
3939
});
4040

@@ -61,18 +61,27 @@ class Discoverable {
6161
} else {
6262
// Allow user to define a custom matcher function
6363
if(typeof this.matcher === 'function' && this.matcher(file) === true) {
64-
this.logger.log("debug", "Discovered path: " + path);
64+
this._log("debug", "Discovered path: " + path);
6565
return path;
6666
} else if((file.indexOf(".") !== 0) && (file.indexOf(".model.js") > 0)) {
67-
this.logger.log("debug", "Discovered path: " + path);
67+
this._log("debug", "Discovered path: " + path);
6868
return path;
6969
}
7070

7171
return false;
7272
}
7373
});
7474

75-
};
75+
}
76+
77+
/**
78+
* Attempt to log
79+
* @param {String} message Message to log
80+
* @return {null}
81+
*/
82+
_log(level, message){
83+
this.logger ? this.logger.log(level, message) : false;
84+
}
7685
}
7786

78-
export default Discoverable;
87+
module.exports = Discoverable;

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"babel-eslint": "^6.0.4",
3535
"babel-preset-es2015": "^6.9.0",
3636
"babelify": "^7.3.0",
37-
"commitizen": "^2.7.6",
37+
"commitizen": "^2.8.1",
3838
"cz-conventional-changelog": "^1.1.5",
3939
"eslint": "^2.10.2",
4040
"gulp": "^3.9.1",

0 commit comments

Comments
 (0)
This repository has been archived.