不要怂,就是干,撸起袖子干!

Commit d894f107 by Mick Hansen

add: support single object as Sequelize constructor parameter

1 parent 35e68f67
Showing with 113 additions and 22 deletions
...@@ -15,7 +15,8 @@ var url = require('url') ...@@ -15,7 +15,8 @@ var url = require('url')
, Promise = require('./promise') , Promise = require('./promise')
, Hooks = require('./hooks') , Hooks = require('./hooks')
, Instance = require('./instance') , Instance = require('./instance')
, Association = require('./associations/index'); , Association = require('./associations/index')
, _ = require('lodash');
/** /**
* This is the main class, the entry point to sequelize. To use it, you just need to import sequelize: * This is the main class, the entry point to sequelize. To use it, you just need to import sequelize:
...@@ -58,12 +59,15 @@ var url = require('url') ...@@ -58,12 +59,15 @@ var url = require('url')
* @param {String} [username=null] The username which is used to authenticate against the database. * @param {String} [username=null] The username which is used to authenticate against the database.
* @param {String} [password=null] The password which is used to authenticate against the database. * @param {String} [password=null] The password which is used to authenticate against the database.
* @param {Object} [options={}] An object with options. * @param {Object} [options={}] An object with options.
* @param {String} [options.host='localhost'] The host of the relational database.
* @param {Integer} [options.port=] The port of the relational database.
* @param {String} [options.username=null] The username which is used to authenticate against the database.
* @param {String} [options.password=null] The password which is used to authenticate against the database.
* @param {String} [options.database=null] The name of the database
* @param {String} [options.dialect='mysql'] The dialect of the database you are connecting to. One of mysql, postgres, sqlite, mariadb and mssql. * @param {String} [options.dialect='mysql'] The dialect of the database you are connecting to. One of mysql, postgres, sqlite, mariadb and mssql.
* @param {String} [options.dialectModulePath=null] If specified, load the dialect library from this path. For example, if you want to use pg.js instead of pg when connecting to a pg database, you should specify 'pg.js' here * @param {String} [options.dialectModulePath=null] If specified, load the dialect library from this path. For example, if you want to use pg.js instead of pg when connecting to a pg database, you should specify 'pg.js' here
* @param {Object} [options.dialectOptions] An object of additional options, which are passed directly to the connection library * @param {Object} [options.dialectOptions] An object of additional options, which are passed directly to the connection library
* @param {String} [options.storage] Only used by sqlite. Defaults to ':memory:' * @param {String} [options.storage] Only used by sqlite. Defaults to ':memory:'
* @param {String} [options.host='localhost'] The host of the relational database.
* @param {Integer} [options.port=] The port of the relational database.
* @param {String} [options.protocol='tcp'] The protocol of the relational database. * @param {String} [options.protocol='tcp'] The protocol of the relational database.
* @param {Object} [options.define={}] Default options for model definitions. See sequelize.define for options * @param {Object} [options.define={}] Default options for model definitions. See sequelize.define for options
* @param {Object} [options.query={}] Default options for sequelize.query * @param {Object} [options.query={}] Default options for sequelize.query
...@@ -97,20 +101,32 @@ var url = require('url') ...@@ -97,20 +101,32 @@ var url = require('url')
* @param {String} uri A full database URI * @param {String} uri A full database URI
* @param {object} [options={}] See above for possible options * @param {object} [options={}] See above for possible options
*/ */
/**
* Instantiate sequelize with an options object
* @name Sequelize
* @constructor
*
* @param {object} [options={}] See above for possible options
*/
var Sequelize = function(database, username, password, options) { var Sequelize = function(database, username, password, options) {
var urlParts; var config;
options = options || {};
if (arguments.length === 1 && typeof database === 'object') {
// new Sequelize({ ... options })
options = database;
config = _.pick(options, 'host', 'port', 'database', 'username', 'password');
} else if ((arguments.length === 1 && typeof database === 'string') || (arguments.length === 2 && typeof username === 'object')) {
// new Sequelize(URI, { ... options })
if (arguments.length === 1 || (arguments.length === 2 && typeof username === 'object')) { config = {};
options = username || {}; options = username || {};
urlParts = url.parse(arguments[0]);
// reset username and password to null so we don't pass the options as the username var urlParts = url.parse(arguments[0]);
username = null;
password = null;
// SQLite don't have DB in connection url // SQLite don't have DB in connection url
if (urlParts.pathname) { if (urlParts.pathname) {
database = urlParts.pathname.replace(/^\//, ''); config.database = urlParts.pathname.replace(/^\//, '');
} }
options.dialect = urlParts.protocol.replace(/:$/, ''); options.dialect = urlParts.protocol.replace(/:$/, '');
...@@ -121,16 +137,16 @@ var Sequelize = function(database, username, password, options) { ...@@ -121,16 +137,16 @@ var Sequelize = function(database, username, password, options) {
} }
if (urlParts.auth) { if (urlParts.auth) {
username = urlParts.auth.split(':')[0]; config.username = urlParts.auth.split(':')[0];
password = urlParts.auth.split(':')[1]; config.password = urlParts.auth.split(':')[1];
} }
} else {
// new Sequelize(database, username, password, { ... options })
options = options || {};
config = {database: database, username: username, password: password};
} }
var config = {database: database, username: username, password: password};
Sequelize.runHooks('beforeInit', config, options); Sequelize.runHooks('beforeInit', config, options);
database = config.database;
username = config.username;
password = config.password;
this.options = Utils._.extend({ this.options = Utils._.extend({
dialect: 'mysql', dialect: 'mysql',
...@@ -172,12 +188,16 @@ var Sequelize = function(database, username, password, options) { ...@@ -172,12 +188,16 @@ var Sequelize = function(database, username, password, options) {
this.options.hooks = this.replaceHookAliases(this.options.hooks); this.options.hooks = this.replaceHookAliases(this.options.hooks);
if ((['', null, false].indexOf(config.password) > -1) || (typeof config.password === 'undefined')) {
config.password = null;
}
this.config = { this.config = {
database: database, database: config.database,
username: username, username: config.username,
password: (((['', null, false].indexOf(password) > -1) || (typeof password === 'undefined')) ? null : password), password: config.password,
host: this.options.host, host: config.host || this.options.host,
port: this.options.port, port: config.port || this.options.port,
pool: this.options.pool, pool: this.options.pool,
protocol: this.options.protocol, protocol: this.options.protocol,
native: this.options.native, native: this.options.native,
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect
, _ = require('lodash')
, Sequelize = require(__dirname + '/../../index');
describe('sequelize constructor', function () {
it('should support database, username, password, options', function () {
var database = Math.random().toString()
, username = Math.random().toString()
, password = Math.random().toString()
, host = Math.random().toString()
, port = Math.random().toString();
var sequelize = new Sequelize(database, username, password, {
host: host,
port: port
});
expect(sequelize.config.database).to.equal(database);
expect(sequelize.config.username).to.equal(username);
expect(sequelize.config.password).to.equal(password);
expect(sequelize.config.host).to.equal(host);
expect(sequelize.config.port).to.equal(port);
});
it('should connection uri, options', function () {
var dialect = 'postgres'
, database = Math.ceil(Math.random() * 9999).toString()
, username = Math.ceil(Math.random() * 9999).toString()
, password = Math.ceil(Math.random() * 9999).toString()
, host = Math.ceil(Math.random() * 9999).toString()
, port = Math.ceil(Math.random() * 9999).toString();
var uri = dialect + '://' + username + ':' + password + '@' + host + ':' + port + '/' + database;
var sequelize = new Sequelize(uri);
expect(sequelize.config.database).to.equal(database);
expect(sequelize.config.username).to.equal(username);
expect(sequelize.config.password).to.equal(password);
expect(sequelize.config.host).to.equal(host);
expect(sequelize.config.port).to.equal(port);
});
it('should support options', function () {
var database = Math.random().toString()
, username = Math.random().toString()
, password = Math.random().toString()
, host = Math.random().toString()
, port = Math.random().toString();
var sequelize = new Sequelize({
host: host,
port: port,
database: database,
username: username,
password: password
});
expect(sequelize.config.database).to.equal(database);
expect(sequelize.config.username).to.equal(username);
expect(sequelize.config.password).to.equal(password);
expect(sequelize.config.host).to.equal(host);
expect(sequelize.config.port).to.equal(port);
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!