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

Commit 3107396d by Felix Becker Committed by Jan Aagaard Meier

ES6 refactor: dialects / PostgreSQL (#6048)

* ES6 refactor of hstore.js

const, export

* ES6 refactor of PostgresDialect

const, classes, property shorthands, export default

* ES6 refactor of postgres ConnectionManager

classes, let, const, arrow functions, for of, export default

* Make postgres Query an ES6 class

* ES6 refactor of postgres Query

let, const, arrow functions, property shorthands, export default

* ES6 refactor of postgres QueryGenerator

let, const, arrow functions, template strings where it was easy to implement

* ES6 refactor of postgres range.js

let, const, arrow functions, export
1 parent 3e911332
'use strict'; 'use strict';
var hstore = require('pg-hstore')({sanitize : true}); const hstore = require('pg-hstore')({sanitize : true});
function stringify (data) { function stringify (data) {
if (data === null) return null; if (data === null) return null;
return hstore.stringify(data); return hstore.stringify(data);
} }
exports.stringify = stringify;
function parse (value) { function parse (value) {
if (value === null) return null; if (value === null) return null;
return hstore.parse(value); return hstore.parse(value);
} }
exports.parse = parse;
module.exports = {
stringify: stringify,
parse: parse
};
'use strict'; 'use strict';
var _ = require('lodash') const _ = require('lodash');
, Abstract = require('../abstract') const AbstractDialect = require('../abstract');
, ConnectionManager = require('./connection-manager') const ConnectionManager = require('./connection-manager');
, Query = require('./query') const Query = require('./query');
, QueryGenerator = require('./query-generator') const QueryGenerator = require('./query-generator');
, DataTypes = require('../../data-types').postgres; const DataTypes = require('../../data-types').postgres;
var PostgresDialect = function(sequelize) { class PostgresDialect extends AbstractDialect {
this.sequelize = sequelize; constructor(sequelize) {
this.connectionManager = new ConnectionManager(this, sequelize); super();
this.connectionManager.initPools(); this.sequelize = sequelize;
this.connectionManager = new ConnectionManager(this, sequelize);
this.connectionManager.initPools();
this.QueryGenerator = _.extend({}, QueryGenerator, { this.QueryGenerator = _.extend({}, QueryGenerator, {
options: sequelize.options, options: sequelize.options,
_dialect: this, _dialect: this,
sequelize: sequelize sequelize
}); });
}; }
}
PostgresDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supports), { PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.supports), {
'DEFAULT VALUES': true, 'DEFAULT VALUES': true,
'EXCEPTION': true, 'EXCEPTION': true,
'ON DUPLICATE KEY': false, 'ON DUPLICATE KEY': false,
...@@ -59,3 +62,5 @@ PostgresDialect.prototype.TICK_CHAR_LEFT = PostgresDialect.prototype.TICK_CHAR; ...@@ -59,3 +62,5 @@ PostgresDialect.prototype.TICK_CHAR_LEFT = PostgresDialect.prototype.TICK_CHAR;
PostgresDialect.prototype.TICK_CHAR_RIGHT = PostgresDialect.prototype.TICK_CHAR; PostgresDialect.prototype.TICK_CHAR_RIGHT = PostgresDialect.prototype.TICK_CHAR;
module.exports = PostgresDialect; module.exports = PostgresDialect;
module.exports.default = PostgresDialect;
module.exports.PostgresDialect = PostgresDialect;
'use strict'; 'use strict';
var _ = require('lodash'); const _ = require('lodash');
function stringifyRangeBound (bound) { function stringifyRangeBound (bound) {
if (bound === null) { if (bound === null) {
...@@ -39,44 +39,38 @@ function stringify (data) { ...@@ -39,44 +39,38 @@ function stringify (data) {
data.inclusive = [true, false]; data.inclusive = [true, false];
} }
_.each(data, function (value, index) { _.each(data, (value, index) => {
if (_.isObject(value)) { if (_.isObject(value)) {
if (value.hasOwnProperty('inclusive')) data.inclusive[index] = !!value.inclusive; if (value.hasOwnProperty('inclusive')) data.inclusive[index] = !!value.inclusive;
if (value.hasOwnProperty('value')) data[index] = value.value; if (value.hasOwnProperty('value')) data[index] = value.value;
} }
}); });
var lowerBound = stringifyRangeBound(data[0]); const lowerBound = stringifyRangeBound(data[0]);
var upperBound = stringifyRangeBound(data[1]); const upperBound = stringifyRangeBound(data[1]);
return (data.inclusive[0] ? '[' : '(') + lowerBound + ',' + upperBound + (data.inclusive[1] ? ']' : ')'); return (data.inclusive[0] ? '[' : '(') + lowerBound + ',' + upperBound + (data.inclusive[1] ? ']' : ')');
} }
exports.stringify = stringify;
function parse (value, parser) { function parse (value, parser) {
if (value === null) return null; if (value === null) return null;
if (value === 'empty') { if (value === 'empty') {
var empty = []; const empty = [];
empty.inclusive = []; empty.inclusive = [];
return empty; return empty;
} }
var result = value let result = value
.substring(1, value.length - 1) .substring(1, value.length - 1)
.split(',', 2); .split(',', 2);
if (result.length !== 2) return value; if (result.length !== 2) return value;
result = result result = result.map(value => parseRangeBound(value, parser));
.map(function (value) {
return parseRangeBound(value, parser);
});
result.inclusive = [(value[0] === '['), (value[value.length - 1] === ']')]; result.inclusive = [(value[0] === '['), (value[value.length - 1] === ']')];
return result; return result;
} }
exports.parse = parse;
module.exports = {
stringify: stringify,
parse: parse
};
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!