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

You need to sign in or sign up before continuing.
Commit 107098db by Jan Aagaard Meier

Allow the user to set timezone

1 parent 88e751d3
...@@ -51,7 +51,7 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -51,7 +51,7 @@ ConnectionManager.prototype.connect = function(config) {
}); });
}).tap(function (connection) { }).tap(function (connection) {
connection.query("SET time_zone = '+0:00'"); connection.query("SET time_zone = '" + self.sequelize.options.timezone + "'");
}); });
}; };
ConnectionManager.prototype.disconnect = function(connection) { ConnectionManager.prototype.disconnect = function(connection) {
...@@ -64,4 +64,4 @@ ConnectionManager.prototype.validate = function(connection) { ...@@ -64,4 +64,4 @@ ConnectionManager.prototype.validate = function(connection) {
return connection && connection.state !== 'disconnected'; return connection && connection.state !== 'disconnected';
}; };
module.exports = ConnectionManager; module.exports = ConnectionManager;
\ No newline at end of file
...@@ -66,7 +66,7 @@ module.exports = (function() { ...@@ -66,7 +66,7 @@ module.exports = (function() {
case 'DATE': case 'DATE':
case 'TIMESTAMP': case 'TIMESTAMP':
case 'DATETIME': case 'DATETIME':
row[prop] = new Date(row[prop] + 'Z'); row[prop] = new Date(row[prop] + self.sequelize.options.timezone);
break; break;
case 'BIT': case 'BIT':
case 'BLOB': case 'BLOB':
......
...@@ -27,7 +27,7 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -27,7 +27,7 @@ ConnectionManager.prototype.connect = function(config) {
user: config.username, user: config.username,
password: config.password, password: config.password,
database: config.database, database: config.database,
timezone: 'Z' timezone: self.sequelize.options.timezone
}; };
if (config.dialectOptions) { if (config.dialectOptions) {
...@@ -62,7 +62,7 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -62,7 +62,7 @@ ConnectionManager.prototype.connect = function(config) {
}); });
}).tap(function (connection) { }).tap(function (connection) {
connection.query("SET time_zone = '+0:00'"); connection.query("SET time_zone = '" + self.sequelize.options.timezone + "'");
}); });
}; };
ConnectionManager.prototype.disconnect = function(connection) { ConnectionManager.prototype.disconnect = function(connection) {
...@@ -77,4 +77,4 @@ ConnectionManager.prototype.validate = function(connection) { ...@@ -77,4 +77,4 @@ ConnectionManager.prototype.validate = function(connection) {
return connection && connection.state !== 'disconnected'; return connection && connection.state !== 'disconnected';
}; };
module.exports = ConnectionManager; module.exports = ConnectionManager;
\ No newline at end of file
...@@ -60,7 +60,7 @@ ConnectionManager.prototype.connect = function(config) { ...@@ -60,7 +60,7 @@ ConnectionManager.prototype.connect = function(config) {
}).tap(function (connection) { }).tap(function (connection) {
if (self.sequelize.config.keepDefaultTimezone) return; if (self.sequelize.config.keepDefaultTimezone) return;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
connection.query("SET TIME ZONE 'UTC'").on('error', function (err) { connection.query("SET TIME ZONE INTERVAL '" + self.sequelize.options.timezone + "' HOUR TO MINUTE").on('error', function (err) {
reject(err); reject(err);
}).on('end', function () { }).on('end', function () {
resolve(); resolve();
...@@ -75,4 +75,4 @@ ConnectionManager.prototype.disconnect = function(connection) { ...@@ -75,4 +75,4 @@ ConnectionManager.prototype.disconnect = function(connection) {
}); });
}; };
module.exports = ConnectionManager; module.exports = ConnectionManager;
\ No newline at end of file
...@@ -829,7 +829,7 @@ module.exports = (function() { ...@@ -829,7 +829,7 @@ module.exports = (function() {
value = hstore.stringify(value); value = hstore.stringify(value);
} }
return SqlString.escape(value, false, null, this.dialect, field); return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
}, },
/** /**
......
...@@ -21,6 +21,7 @@ var parseHstoreFields = function(model, row) { ...@@ -21,6 +21,7 @@ var parseHstoreFields = function(model, row) {
}); });
}; };
module.exports = (function() { module.exports = (function() {
var Query = function(client, sequelize, callee, options) { var Query = function(client, sequelize, callee, options) {
this.client = client; this.client = client;
......
...@@ -576,7 +576,8 @@ module.exports = (function() { ...@@ -576,7 +576,8 @@ module.exports = (function() {
QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector, Model) { QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector, Model) {
var sql = this.QueryGenerator.selectQuery(tableName, options, Model) var sql = this.QueryGenerator.selectQuery(tableName, options, Model)
, queryOptions = Utils._.extend({ transaction: options.transaction }, { plain: true, raw: true, type: QueryTypes.SELECT }); , queryOptions = Utils._.extend({ transaction: options.transaction }, { plain: true, raw: true, type: QueryTypes.SELECT })
, self = this;
if (attributeSelector === undefined) { if (attributeSelector === undefined) {
throw new Error('Please pass an attribute selector!'); throw new Error('Please pass an attribute selector!');
...@@ -593,7 +594,7 @@ module.exports = (function() { ...@@ -593,7 +594,7 @@ module.exports = (function() {
} else if (dataType === DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) { } else if (dataType === DataTypes.INTEGER || dataType instanceof DataTypes.BIGINT) {
result = parseInt(result, 10); result = parseInt(result, 10);
} else if (dataType === DataTypes.DATE) { } else if (dataType === DataTypes.DATE) {
result = new Date(result + 'Z'); result = new Date(result + self.sequelize.options.timezone);
} else if (dataType === DataTypes.STRING) { } else if (dataType === DataTypes.STRING) {
// Nothing to do, result is already a string. // Nothing to do, result is already a string.
} }
......
...@@ -63,17 +63,18 @@ module.exports = (function() { ...@@ -63,17 +63,18 @@ module.exports = (function() {
* @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
* @param {Object} [options.sync={}] Default options for sequelize.sync * @param {Object} [options.sync={}] Default options for sequelize.sync
* @param {String} [options.timezone='+00:00'] The timezone used when converting a date from the database into a javascript date. The timezone is also used to SET TIMEZONE when connecting to the server, to ensure that the result of NOW, CURRENT_TIMESTAMP and other time related functions have in the right timezone. For best cross platform performance use the format +/-HH:MM.
* @param {Function} [options.logging=console.log] A function that gets executed everytime Sequelize would log something. * @param {Function} [options.logging=console.log] A function that gets executed everytime Sequelize would log something.
* @param {Boolean} [options.omitNull=false] A flag that defines if null values should be passed to SQL queries or not. * @param {Boolean} [options.omitNull=false] A flag that defines if null values should be passed to SQL queries or not.
* @param {Boolean} [options.queue=true] Queue queries, so that only maxConcurrentQueries number of queries are executing at once. If false, all queries will be executed immediately. * @param {Boolean} [options.queue=true] Queue queries, so that only maxConcurrentQueries number of queries are executing at once. If false, all queries will be executed immediately.
* @param {int} [options.maxConcurrentQueries=50] The maximum number of queries that should be executed at once if queue is true. * @param {Integer} [options.maxConcurrentQueries=50] The maximum number of queries that should be executed at once if queue is true.
* @param {Boolean} [options.native=false] A flag that defines if native library shall be used or not. Currently only has an effect for postgres * @param {Boolean} [options.native=false] A flag that defines if native library shall be used or not. Currently only has an effect for postgres
* @param {Boolean} [options.replication=false] Use read / write replication. To enable replication, pass an object, with two properties, read and write. Write should be an object (a single server for handling writes), and read an array of object (several servers to handle reads). Each read/write server can have the following properties: `host`, `port`, `username`, `password`, `database` * @param {Boolean} [options.replication=false] Use read / write replication. To enable replication, pass an object, with two properties, read and write. Write should be an object (a single server for handling writes), and read an array of object (several servers to handle reads). Each read/write server can have the following properties: `host`, `port`, `username`, `password`, `database`
* @param {Object} [options.pool={}] Should sequelize use a connection pool. Default is true * @param {Object} [options.pool={}] Should sequelize use a connection pool. Default is true
* @param {int} [options.pool.maxConnections] * @param {Integer} [options.pool.maxConnections]
* @param {int} [options.pool.minConnections] * @param {Integer} [options.pool.minConnections]
* @param {int} [options.pool.maxIdleTime] The maximum time, in milliseconds, that a connection can be idle before being released * @param {Integer} [options.pool.maxIdleTime] The maximum time, in milliseconds, that a connection can be idle before being released
* @param {function} [options.pool.validateConnection] A function that validates a connection. Called with client. The default function checks that client is an object, and that its state is not disconnected * @param {Function} [options.pool.validateConnection] A function that validates a connection. Called with client. The default function checks that client is an object, and that its state is not disconnected
* @param {Boolean} [options.quoteIdentifiers=true] Set to `false` to make table names and attributes case-insensitive on Postgres and skip double quoting of them. * @param {Boolean} [options.quoteIdentifiers=true] Set to `false` to make table names and attributes case-insensitive on Postgres and skip double quoting of them.
*/ */
...@@ -120,6 +121,7 @@ module.exports = (function() { ...@@ -120,6 +121,7 @@ module.exports = (function() {
define: {}, define: {},
query: {}, query: {},
sync: {}, sync: {},
timezone:'+00:00',
logging: console.log, logging: console.log,
omitNull: false, omitNull: false,
native: false, native: false,
...@@ -134,6 +136,10 @@ module.exports = (function() { ...@@ -134,6 +136,10 @@ module.exports = (function() {
this.options.dialect = 'postgres'; this.options.dialect = 'postgres';
} }
if (this.options.dialect === 'sqlite' && this.options.timezone !== '+00:00') {
throw new Error('Setting a custom timezone is not supported by SQLite, dates are always returned as UTC. Please remove the custom timezone parameter.');
}
if (this.options.logging === true) { if (this.options.logging === true) {
console.log('DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log'); console.log('DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log');
this.options.logging = console.log; this.options.logging = console.log;
......
...@@ -163,7 +163,7 @@ SqlString.dateToString = function(date, timeZone, dialect) { ...@@ -163,7 +163,7 @@ SqlString.dateToString = function(date, timeZone, dialect) {
// TODO: Ideally all dialects would work a bit more like this // TODO: Ideally all dialects would work a bit more like this
if (dialect === 'postgres' || dialect === 'sqlite') { if (dialect === 'postgres' || dialect === 'sqlite') {
return moment(dt).zone('+00:00').format('YYYY-MM-DD HH:mm:ss.SSS Z'); return moment(dt).format('YYYY-MM-DD HH:mm:ss.SSS' + timeZone);
} }
if (timeZone !== 'local') { if (timeZone !== 'local') {
......
...@@ -63,7 +63,7 @@ var Support = { ...@@ -63,7 +63,7 @@ var Support = {
var sequelizeOptions = _.defaults(options, { var sequelizeOptions = _.defaults(options, {
host: options.host || config.host, host: options.host || config.host,
logging: false, // logging: false,
dialect: options.dialect, dialect: options.dialect,
port: options.port || process.env.SEQ_PORT || config.port, port: options.port || process.env.SEQ_PORT || config.port,
pool: config.pool, pool: config.pool,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!