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

Commit b9bdc1d4 by Felix Becker Committed by Jan Aagaard Meier

ES6 refactor of utils.js (#6059)

- let, const, arrow functions, property shorthands, exports, classes
 - changes classes to be PascalCase
 - Removes inherit() function (not used anymore)
1 parent 49a7e543
...@@ -1098,9 +1098,9 @@ const QueryGenerator = { ...@@ -1098,9 +1098,9 @@ const QueryGenerator = {
if (Array.isArray(attr) && attr.length === 2) { if (Array.isArray(attr) && attr.length === 2) {
if (attr[0]._isSequelizeMethod) { if (attr[0]._isSequelizeMethod) {
if (attr[0] instanceof Utils.literal || if (attr[0] instanceof Utils.Literal ||
attr[0] instanceof Utils.cast || attr[0] instanceof Utils.Cast ||
attr[0] instanceof Utils.fn attr[0] instanceof Utils.Fn
) { ) {
verbatim = true; verbatim = true;
} }
...@@ -1110,9 +1110,9 @@ const QueryGenerator = { ...@@ -1110,9 +1110,9 @@ const QueryGenerator = {
attrAs = attr[1]; attrAs = attr[1];
attr = attr[0]; attr = attr[0];
} else if (attr instanceof Utils.literal) { } else if (attr instanceof Utils.Literal) {
return attr.val; // We trust the user to rename the field correctly return attr.val; // We trust the user to rename the field correctly
} else if (attr instanceof Utils.cast || attr instanceof Utils.fn) { } else if (attr instanceof Utils.Cast || attr instanceof Utils.Fn) {
throw new Error( throw new Error(
'Tried to select attributes using Sequelize.cast or Sequelize.fn without specifying an alias for the result, during eager loading. ' + 'Tried to select attributes using Sequelize.cast or Sequelize.fn without specifying an alias for the result, during eager loading. ' +
'This means the attribute will not be added to the returned instance' 'This means the attribute will not be added to the returned instance'
...@@ -1503,7 +1503,7 @@ const QueryGenerator = { ...@@ -1503,7 +1503,7 @@ const QueryGenerator = {
const subQueryOrder = []; const subQueryOrder = [];
const validateOrder = order => { const validateOrder = order => {
if (order instanceof Utils.literal) return; if (order instanceof Utils.Literal) return;
if (!_.includes([ if (!_.includes([
'ASC', 'ASC',
...@@ -1538,7 +1538,7 @@ const QueryGenerator = { ...@@ -1538,7 +1538,7 @@ const QueryGenerator = {
mainQueryOrder.push(this.quote(t, model)); mainQueryOrder.push(this.quote(t, model));
} }
} else { } else {
mainQueryOrder.push(this.quote(typeof options.order === 'string' ? new Utils.literal(options.order) : options.order, model)); mainQueryOrder.push(this.quote(typeof options.order === 'string' ? new Utils.Literal(options.order) : options.order, model));
} }
return {mainQueryOrder, subQueryOrder}; return {mainQueryOrder, subQueryOrder};
...@@ -1757,7 +1757,7 @@ const QueryGenerator = { ...@@ -1757,7 +1757,7 @@ const QueryGenerator = {
handleSequelizeMethod(smth, tableName, factory, options, prepend) { handleSequelizeMethod(smth, tableName, factory, options, prepend) {
let result; let result;
if (smth instanceof Utils.where) { if (smth instanceof Utils.Where) {
let value = smth.logic; let value = smth.logic;
let key; let key;
...@@ -1784,9 +1784,9 @@ const QueryGenerator = { ...@@ -1784,9 +1784,9 @@ const QueryGenerator = {
result = (value === 'NULL') ? key + ' IS NULL' : [key, value].join(' ' + smth.comparator + ' '); result = (value === 'NULL') ? key + ' IS NULL' : [key, value].join(' ' + smth.comparator + ' ');
} }
} else if (smth instanceof Utils.literal) { } else if (smth instanceof Utils.Literal) {
result = smth.val; result = smth.val;
} else if (smth instanceof Utils.cast) { } else if (smth instanceof Utils.Cast) {
if (smth.val._isSequelizeMethod) { if (smth.val._isSequelizeMethod) {
result = this.handleSequelizeMethod(smth.val, tableName, factory, options, prepend); result = this.handleSequelizeMethod(smth.val, tableName, factory, options, prepend);
} else { } else {
...@@ -1794,7 +1794,7 @@ const QueryGenerator = { ...@@ -1794,7 +1794,7 @@ const QueryGenerator = {
} }
result = 'CAST(' + result + ' AS ' + smth.type.toUpperCase() + ')'; result = 'CAST(' + result + ' AS ' + smth.type.toUpperCase() + ')';
} else if (smth instanceof Utils.fn) { } else if (smth instanceof Utils.Fn) {
result = smth.fn + '(' + smth.args.map(arg => { result = smth.fn + '(' + smth.args.map(arg => {
if (arg._isSequelizeMethod) { if (arg._isSequelizeMethod) {
return this.handleSequelizeMethod(arg, tableName, factory, options, prepend); return this.handleSequelizeMethod(arg, tableName, factory, options, prepend);
...@@ -1802,7 +1802,7 @@ const QueryGenerator = { ...@@ -1802,7 +1802,7 @@ const QueryGenerator = {
return this.escape(arg); return this.escape(arg);
} }
}).join(', ') + ')'; }).join(', ') + ')';
} else if (smth instanceof Utils.col) { } else if (smth instanceof Utils.Col) {
if (Array.isArray(smth.col)) { if (Array.isArray(smth.col)) {
if (!factory) { if (!factory) {
throw new Error('Cannot call Sequelize.col() with array outside of order / group clause'); throw new Error('Cannot call Sequelize.col() with array outside of order / group clause');
...@@ -1949,7 +1949,7 @@ const QueryGenerator = { ...@@ -1949,7 +1949,7 @@ const QueryGenerator = {
} }
} }
if (value && value._isSequelizeMethod && !(key !== undefined && value instanceof Utils.fn)) { if (value && value._isSequelizeMethod && !(key !== undefined && value instanceof Utils.Fn)) {
return this.handleSequelizeMethod(value); return this.handleSequelizeMethod(value);
} }
...@@ -2025,7 +2025,7 @@ const QueryGenerator = { ...@@ -2025,7 +2025,7 @@ const QueryGenerator = {
let $baseKey = this.quoteIdentifier(key)+'#>>\'{'+path.join(', ')+'}\''; let $baseKey = this.quoteIdentifier(key)+'#>>\'{'+path.join(', ')+'}\'';
if (options.prefix) { if (options.prefix) {
if (options.prefix instanceof Utils.literal) { if (options.prefix instanceof Utils.Literal) {
$baseKey = this.handleSequelizeMethod(options.prefix)+'.'+$baseKey; $baseKey = this.handleSequelizeMethod(options.prefix)+'.'+$baseKey;
} else { } else {
$baseKey = this.quoteTable(options.prefix)+'.'+$baseKey; $baseKey = this.quoteTable(options.prefix)+'.'+$baseKey;
...@@ -2060,7 +2060,7 @@ const QueryGenerator = { ...@@ -2060,7 +2060,7 @@ const QueryGenerator = {
$where[$prop] = $item; $where[$prop] = $item;
const $key = castKey($item); const $key = castKey($item);
$items.push(this.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/)); $items.push(this.whereItemQuery(new Utils.Literal($key), $where/*, _.pick(options, 'prefix')*/));
} else { } else {
traverse($prop, $item, path.concat([$prop])); traverse($prop, $item, path.concat([$prop]));
} }
...@@ -2069,7 +2069,7 @@ const QueryGenerator = { ...@@ -2069,7 +2069,7 @@ const QueryGenerator = {
$where.$eq = item; $where.$eq = item;
const $key = castKey(item); const $key = castKey(item);
$items.push(this.whereItemQuery(new Utils.literal($key), $where/*, _.pick(options, 'prefix')*/)); $items.push(this.whereItemQuery(new Utils.Literal($key), $where/*, _.pick(options, 'prefix')*/));
} }
}; };
...@@ -2125,7 +2125,7 @@ const QueryGenerator = { ...@@ -2125,7 +2125,7 @@ const QueryGenerator = {
comparator = 'IN'; comparator = 'IN';
if (value.$notIn) comparator = 'NOT IN'; if (value.$notIn) comparator = 'NOT IN';
if ((value.$in || value.$notIn) instanceof Utils.literal) { if ((value.$in || value.$notIn) instanceof Utils.Literal) {
value = (value.$in || value.$notIn).val; value = (value.$in || value.$notIn).val;
} else if ((value.$in || value.$notIn).length) { } else if ((value.$in || value.$notIn).length) {
value = '('+(value.$in || value.$notIn).map(item => this.escape(item)).join(', ')+')'; value = '('+(value.$in || value.$notIn).map(item => this.escape(item)).join(', ')+')';
...@@ -2226,7 +2226,7 @@ const QueryGenerator = { ...@@ -2226,7 +2226,7 @@ const QueryGenerator = {
} }
if (options.prefix && prefix) { if (options.prefix && prefix) {
if (options.prefix instanceof Utils.literal) { if (options.prefix instanceof Utils.Literal) {
key = [this.handleSequelizeMethod(options.prefix), key].join('.'); key = [this.handleSequelizeMethod(options.prefix), key].join('.');
} else { } else {
key = [this.quoteTable(options.prefix), key].join('.'); key = [this.quoteTable(options.prefix), key].join('.');
......
...@@ -124,7 +124,7 @@ const QueryGenerator = { ...@@ -124,7 +124,7 @@ const QueryGenerator = {
}, },
handleSequelizeMethod(smth, tableName, factory, options, prepend) { handleSequelizeMethod(smth, tableName, factory, options, prepend) {
if (smth instanceof Utils.json) { if (smth instanceof Utils.Json) {
// Parse nested object // Parse nested object
if (smth.conditions) { if (smth.conditions) {
const conditions = _.map(this.parseConditionObject(smth.conditions), condition => const conditions = _.map(this.parseConditionObject(smth.conditions), condition =>
......
...@@ -890,7 +890,7 @@ class Sequelize { ...@@ -890,7 +890,7 @@ class Sequelize {
* @return {Sequelize.fn} * @return {Sequelize.fn}
*/ */
static fn(fn) { static fn(fn) {
return new Utils.fn(fn, Utils.sliceArgs(arguments, 1)); return new Utils.Fn(fn, Utils.sliceArgs(arguments, 1));
} }
/** /**
...@@ -903,7 +903,7 @@ class Sequelize { ...@@ -903,7 +903,7 @@ class Sequelize {
* @return {Sequelize.col} * @return {Sequelize.col}
*/ */
static col(col) { static col(col) {
return new Utils.col(col); return new Utils.Col(col);
} }
/** /**
...@@ -916,7 +916,7 @@ class Sequelize { ...@@ -916,7 +916,7 @@ class Sequelize {
* @return {Sequelize.cast} * @return {Sequelize.cast}
*/ */
static cast(val, type) { static cast(val, type) {
return new Utils.cast(val, type); return new Utils.Cast(val, type);
} }
/** /**
...@@ -929,7 +929,7 @@ class Sequelize { ...@@ -929,7 +929,7 @@ class Sequelize {
* @return {Sequelize.literal} * @return {Sequelize.literal}
*/ */
static literal(val) { static literal(val) {
return new Utils.literal(val); return new Utils.Literal(val);
} }
/** /**
...@@ -968,7 +968,7 @@ class Sequelize { ...@@ -968,7 +968,7 @@ class Sequelize {
* @return {Sequelize.json} * @return {Sequelize.json}
*/ */
static json(conditionsOrPath, value) { static json(conditionsOrPath, value) {
return new Utils.json(conditionsOrPath, value); return new Utils.Json(conditionsOrPath, value);
} }
/** /**
...@@ -990,7 +990,7 @@ class Sequelize { ...@@ -990,7 +990,7 @@ class Sequelize {
* @return {Sequelize.where} * @return {Sequelize.where}
*/ */
static where(attr, comparator, logic) { static where(attr, comparator, logic) {
return new Utils.where(attr, comparator, logic); return new Utils.Where(attr, comparator, logic);
} }
/** /**
......
...@@ -130,8 +130,8 @@ describe(Support.getTestDialectTeaser('DAO'), function() { ...@@ -130,8 +130,8 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
b: self.sequelize.col('always_false') b: self.sequelize.col('always_false')
}); });
expect(user.get('d')).to.be.instanceof(self.sequelize.Utils.fn); expect(user.get('d')).to.be.instanceof(self.sequelize.Utils.Fn);
expect(user.get('b')).to.be.instanceof(self.sequelize.Utils.col); expect(user.get('b')).to.be.instanceof(self.sequelize.Utils.Col);
return user.save().then(function() { return user.save().then(function() {
return user.reload().then(function() { return user.reload().then(function() {
......
...@@ -195,23 +195,23 @@ describe(Support.getTestDialectTeaser('Utils'), function() { ...@@ -195,23 +195,23 @@ describe(Support.getTestDialectTeaser('Utils'), function() {
another_json_field: { x: 1 } another_json_field: { x: 1 }
}; };
var expected = "metadata#>>'{language}' = 'icelandic' and metadata#>>'{pg_rating,dk}' = 'G' and another_json_field#>>'{x}' = '1'"; var expected = "metadata#>>'{language}' = 'icelandic' and metadata#>>'{pg_rating,dk}' = 'G' and another_json_field#>>'{x}' = '1'";
expect(queryGenerator.handleSequelizeMethod(new Utils.json(conditions))).to.deep.equal(expected); expect(queryGenerator.handleSequelizeMethod(new Utils.Json(conditions))).to.deep.equal(expected);
}); });
it('successfully parses a string using dot notation', function() { it('successfully parses a string using dot notation', function() {
var path = 'metadata.pg_rating.dk'; var path = 'metadata.pg_rating.dk';
expect(queryGenerator.handleSequelizeMethod(new Utils.json(path))).to.equal("metadata#>>'{pg_rating,dk}'"); expect(queryGenerator.handleSequelizeMethod(new Utils.Json(path))).to.equal("metadata#>>'{pg_rating,dk}'");
}); });
it('allows postgres json syntax', function() { it('allows postgres json syntax', function() {
var path = 'metadata->pg_rating->>dk'; var path = 'metadata->pg_rating->>dk';
expect(queryGenerator.handleSequelizeMethod(new Utils.json(path))).to.equal(path); expect(queryGenerator.handleSequelizeMethod(new Utils.Json(path))).to.equal(path);
}); });
it('can take a value to compare against', function() { it('can take a value to compare against', function() {
var path = 'metadata.pg_rating.is'; var path = 'metadata.pg_rating.is';
var value = 'U'; var value = 'U';
expect(queryGenerator.handleSequelizeMethod(new Utils.json(path, value))).to.equal("metadata#>>'{pg_rating,is}' = 'U'"); expect(queryGenerator.handleSequelizeMethod(new Utils.Json(path, value))).to.equal("metadata#>>'{pg_rating,is}' = 'U'");
}); });
}); });
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!