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

Commit 7a9b26a

Browse files
committedMay 19, 2016
refactor(Connect, Discoverable, gulp): Initial es2015 refactor, babelify, add gulp
1 parent fd9df97 commit 7a9b26a

15 files changed

+526
-143
lines changed
 

‎.babelrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "presets": ["es2015"] }

‎.eslintrc

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"parser": "babel-eslint",
3+
"env": {
4+
"node": true,
5+
"es6": true,
6+
"browser": true,
7+
"mocha": true
8+
},
9+
"rules": {
10+
"eqeqeq": 2,
11+
"no-caller": 2,
12+
"no-else-return": 2,
13+
"no-eq-null": 2,
14+
"quotes": "single",
15+
"no-new-func": 2,
16+
"camelcase": [2, "always"],
17+
"brace-style": [2, "stroustrup", { "allowSingleLine": true }],
18+
"strict": [2, "global"],
19+
"comma-spacing": [2, {"before": false, "after": true}],
20+
"indent": [2, 4],
21+
"key-spacing": [2, { "beforeColon": false, "afterColon": true, "align": "value" }],
22+
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
23+
"space-before-blocks": [2, "always"],
24+
"space-before-function-paren": [2, "always"],
25+
"space-in-brackets": [2, "always", {
26+
"singleValue": false,
27+
"objectsInArrays": true,
28+
"arraysInArrays": true,
29+
"arraysInObjects": true,
30+
"objectsInObjects": true,
31+
"propertyName": false
32+
}],
33+
"space-in-parens": [2, "never"],
34+
"space-infix-ops": 2,
35+
"space-return-throw-case": 2,
36+
"dot-notation": 0
37+
}
38+
}

‎dist/index.js

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

‎dist/index.js.map

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

‎dist/lib/Connect.js

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

‎dist/lib/Connect.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

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

‎dist/lib/Discoverable.js.map

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

‎gulpfile.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
var gulp = require('gulp');
4+
var clean = require('gulp-clean');
5+
var babel = require('gulp-babel');
6+
var sourcemaps = require('gulp-sourcemaps');
7+
8+
var entry = 'index.js';
9+
var src = [entry, 'lib/**/*.js'];
10+
var srcOption = {
11+
base: './'
12+
};
13+
var dest = './dist';
14+
15+
16+
gulp.task('clean', function() {
17+
return gulp.src(dest, {
18+
read: false
19+
})
20+
.pipe(clean());
21+
});
22+
23+
24+
gulp.task('node', ['clean'], function() {
25+
return gulp.src(src, srcOption)
26+
.pipe(sourcemaps.init())
27+
.pipe(babel())
28+
.pipe(sourcemaps.write('.', {
29+
includeContent: false,
30+
sourceRoot: '..'
31+
}))
32+
.pipe(gulp.dest(dest));
33+
});
34+
35+
gulp.task('default', ['node']);

‎index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
module.exports = require('./lib/connect');
1+
// Import Connect
2+
import Connect from './lib/Connect';
3+
4+
// Export Connect
5+
export default Connect;

‎lib/Connect.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"use strict";
2+
3+
import Promise from "bluebird";
4+
import path from "path";
5+
import Sequelize from "sequelize";
6+
import Discoverable from "./Discoverable";
7+
8+
// Setup Logger
9+
// logger.level = "info"; // Default log level to debug
10+
11+
let instance = null;
12+
13+
class Connection {
14+
15+
constructor(database, username, password, options={}, discover=["/model"], matcher=null, logger=console) {
16+
17+
if(instance) return instance;
18+
19+
this.database = database;
20+
this.username = username;
21+
this.password = password;
22+
this.options = options;
23+
this.discover = discover; // Set the default discovery paths to ["/model"]
24+
this.matcher = matcher; // Set the default matcher to null
25+
this.logger = logger;
26+
27+
// Expose db
28+
this.models = {};
29+
this.Sequelize = Sequelize; // Expose Sequelize
30+
31+
this._connect()
32+
.then((connection) => {
33+
return instance = connection;
34+
});
35+
}
36+
37+
/**
38+
* Connect to the db
39+
* @return {Object} A database object containing sequelize and models
40+
*/
41+
_connect(){
42+
43+
// return the instance, although this shouldn't be being called externally
44+
if(instance) return instance;
45+
46+
this.logger.log("info", "Connecting to: " + this.database + " as: " + this.username);
47+
48+
// Instantiate a new sequelize instance
49+
let sequelize = new db.Sequelize(this.database, this.username, this.password, this.options);
50+
let models = {};
51+
52+
53+
let dir = typeof this.discover === "string" ? [this.discover] : this.discover;
54+
let discoverable = new Discoverable(dir, this.matcher, this.logger);
55+
56+
return discoverable.discover()
57+
.each((path) => {
58+
// Import each discovered path
59+
let model = sequelize["import"](path);
60+
61+
if(model) {
62+
this.logger.log("debug", "Import for path succeeded: " + path);
63+
models[model.name] = model;
64+
} else {
65+
this.logger.log("debug", "Import for path failed: " + path);
66+
}
67+
68+
})
69+
.then((path) => {
70+
// Execute the associate methods for each Model
71+
this.logger.log("info","Import completed");
72+
73+
return Promise.each(Object.keys(models), (modelName) => {
74+
75+
if ("associate" in models[modelName]) {
76+
this.logger.log("debug", "Associating Model: "+ modelName);
77+
models[modelName].associate(models);
78+
} else {
79+
this.logger.log("debug", "Nothing to associate for Model: "+ modelName);
80+
}
81+
});
82+
83+
})
84+
.then(() => {
85+
// Syncronize the DB
86+
this.logger.log("info", "Finished connecting to: " + database + " as: " + username);
87+
return sequelize.sync();
88+
89+
})
90+
.then(() => {
91+
this.logger.log("info", "Finished synchronizing " + database);
92+
// Expose objects
93+
this.sequelize = sequelize;
94+
this.models = models;
95+
96+
return this;
97+
});
98+
}
99+
}
100+
101+
export default Connection;

‎lib/Discoverable.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Promise from "bluebird";
2+
import _ from "lodash";
3+
import f from "fs";
4+
5+
let fs = Promise.promisifyAll(f);
6+
7+
class Discoverable {
8+
9+
constructor(paths, matcher, logger=console){
10+
this.paths = paths;
11+
this.matcher = matcher;
12+
this.logger = logger;
13+
}
14+
15+
/**
16+
* Discover the specified path for files
17+
* matching the given convention
18+
*
19+
* @return {Array} The array of matching files for the discoverable directories
20+
*/
21+
discover() {
22+
23+
let discovered = [];
24+
25+
return Promise.each(this.paths, function(location){
26+
27+
// Recurse through the api directory and collect the models
28+
return this._dive(location)
29+
.then((results) => {
30+
this.logger.log("debug", "Flattening results");
31+
return _.flatten(results, true);
32+
})
33+
.filter((value) => {
34+
return value !== false
35+
})
36+
.then((results) => {
37+
this.logger.log("debug", "Assigning filtered results to discover: " + results);
38+
return discovered = results;
39+
});
40+
41+
}).then(() => {
42+
return discovered;
43+
});
44+
}
45+
46+
/**
47+
* [dive description]
48+
* @param {String} dir The directory to recurse through
49+
* @return {Array} An array of matching files at the given location
50+
*/
51+
_dive(dir) {
52+
53+
// Read the directory
54+
return fs.readdirAsync(dir).map((file) => {
55+
let path = dir + "/" + file; // Full path of that file
56+
let stat = fs.statSync(path); // Get the file's stats
57+
58+
// If the file is a directory
59+
if (stat && stat.isDirectory()) {
60+
return this._dive(path); // Dive into the directory
61+
} else {
62+
// Allow user to define a custom matcher function
63+
if(typeof this.matcher === 'function' && this.matcher(file) === true) {
64+
this.logger.log("debug", "Discovered path: " + path);
65+
return path;
66+
} else if((file.indexOf(".") !== 0) && (file.indexOf(".model.js") > 0)) {
67+
this.logger.log("debug", "Discovered path: " + path);
68+
return path;
69+
}
70+
71+
return false;
72+
}
73+
});
74+
75+
};
76+
}
77+
78+
export default Discoverable;

‎lib/connect.js

-74
This file was deleted.

‎lib/discover.js

-62
This file was deleted.

‎package.json

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "sequelize-connect",
3-
"version": "1.3.0",
3+
"version": "2.0.0-alpha.1",
44
"author": "Jacob Spizziri <jacob.spizziri@gmail.com> (https://github.com/jspizziri)",
55
"license": "BSD-2-Clause",
66
"description": "A simple connection wrapper for the sequelize ORM, making it easier to configure and build models & connections.",
77
"homepage": "https://github.com/jspizziri/sequelize-singleton",
8-
"main": "index.js",
8+
"main": "dist/index.js",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/jspizziri/sequelize-connect.git"
@@ -24,15 +24,23 @@
2424
],
2525
"dependencies": {
2626
"bluebird": "^2.10.2",
27-
"commitizen": "^2.7.6",
28-
"lodash": "^3.10.1",
29-
"winston": "^1.1.1"
27+
"lodash": "^3.10.1"
3028
},
3129
"peerDependencies": {
3230
"sequelize": ">=2.0"
3331
},
3432
"devDependencies": {
35-
"cz-conventional-changelog": "^1.1.5"
33+
"babel-core": "^6.9.0",
34+
"babel-eslint": "^6.0.4",
35+
"babel-preset-es2015": "^6.9.0",
36+
"babelify": "^7.3.0",
37+
"commitizen": "^2.7.6",
38+
"cz-conventional-changelog": "^1.1.5",
39+
"eslint": "^2.10.2",
40+
"gulp": "^3.9.1",
41+
"gulp-babel": "^6.1.2",
42+
"gulp-clean": "^0.3.2",
43+
"gulp-sourcemaps": "^1.6.0"
3644
},
3745
"config": {
3846
"commitizen": {

0 commit comments

Comments
 (0)
This repository has been archived.