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

Commit 5aba3f99 by overlookmotel Committed by Jan Aagaard Meier

Inflection (#6077)

* Updated `inflection` dependency and pinned version

* Expose all used `inflection` methods on `Utils`

* `Sequelize.useInflection` method
1 parent a98be920
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
- [ADDED] Support for range operators [#5990](https://github.com/sequelize/sequelize/issues/5990) - [ADDED] Support for range operators [#5990](https://github.com/sequelize/sequelize/issues/5990)
- [FIXED] Broken transactions in `MySQL` [#3568](https://github.com/sequelize/sequelize/issues/3568) - [FIXED] Broken transactions in `MySQL` [#3568](https://github.com/sequelize/sequelize/issues/3568)
- [FIXED] `Model.count` don't include attributes [#5057](https://github.com/sequelize/sequelize/issues/5057) - [FIXED] `Model.count` don't include attributes [#5057](https://github.com/sequelize/sequelize/issues/5057)
- [INTERNALS] Updated `inflection` dependency and pinned version and expose all used `inflection` methods on `Utils`
- [ADDED] `Sequelize.useInflection` method
## BC breaks: ## BC breaks:
- Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive), previously was `()` (exclusive, exclusive) - Range type bounds now default to [postgres default](https://www.postgresql.org/docs/9.5/static/rangetypes.html#RANGETYPES-CONSTRUCT) `[)` (inclusive, exclusive), previously was `()` (exclusive, exclusive)
......
...@@ -544,7 +544,7 @@ const QueryGenerator = { ...@@ -544,7 +544,7 @@ const QueryGenerator = {
return Utils._.map(indexes, index => { return Utils._.map(indexes, index => {
if (!index.hasOwnProperty('name')) { if (!index.hasOwnProperty('name')) {
const onlyAttributeNames = index.fields.map(field => (typeof field === 'string') ? field : (field.name || field.attribute)); const onlyAttributeNames = index.fields.map(field => (typeof field === 'string') ? field : (field.name || field.attribute));
index.name = Utils.inflection.underscore(rawTablename + '_' + onlyAttributeNames.join('_')); index.name = Utils.underscore(rawTablename + '_' + onlyAttributeNames.join('_'));
} }
return index; return index;
......
...@@ -345,7 +345,7 @@ var QueryGenerator = { ...@@ -345,7 +345,7 @@ var QueryGenerator = {
, indexName = indexNameOrAttributes; , indexName = indexNameOrAttributes;
if (typeof indexName !== 'string') { if (typeof indexName !== 'string') {
indexName = Utils.inflection.underscore(tableName + '_' + indexNameOrAttributes.join('_')); indexName = Utils.underscore(tableName + '_' + indexNameOrAttributes.join('_'));
} }
var values = { var values = {
......
...@@ -192,7 +192,7 @@ const QueryGenerator = { ...@@ -192,7 +192,7 @@ const QueryGenerator = {
let indexName = indexNameOrAttributes; let indexName = indexNameOrAttributes;
if (typeof indexName !== 'string') { if (typeof indexName !== 'string') {
indexName = Utils.inflection.underscore(tableName + '_' + indexNameOrAttributes.join('_')); indexName = Utils.underscore(tableName + '_' + indexNameOrAttributes.join('_'));
} }
return `DROP INDEX ${indexName} ON ${this.quoteTable(tableName)}`; return `DROP INDEX ${indexName} ON ${this.quoteTable(tableName)}`;
......
...@@ -363,7 +363,7 @@ const QueryGenerator = { ...@@ -363,7 +363,7 @@ const QueryGenerator = {
let indexName = indexNameOrAttributes; let indexName = indexNameOrAttributes;
if (typeof indexName !== 'string') { if (typeof indexName !== 'string') {
indexName = Utils.inflection.underscore(tableName + '_' + indexNameOrAttributes.join('_')); indexName = Utils.underscore(tableName + '_' + indexNameOrAttributes.join('_'));
} }
return `DROP INDEX IF EXISTS ${this.quoteIdentifiers(indexName)}`; return `DROP INDEX IF EXISTS ${this.quoteIdentifiers(indexName)}`;
......
...@@ -229,7 +229,7 @@ const QueryGenerator = { ...@@ -229,7 +229,7 @@ const QueryGenerator = {
let indexName = indexNameOrAttributes; let indexName = indexNameOrAttributes;
if (typeof indexName !== 'string') { if (typeof indexName !== 'string') {
indexName = Utils.inflection.underscore(tableName + '_' + indexNameOrAttributes.join('_')); indexName = Utils.underscore(tableName + '_' + indexNameOrAttributes.join('_'));
} }
return 'DROP INDEX IF EXISTS ' + indexName; return 'DROP INDEX IF EXISTS ' + indexName;
......
...@@ -360,8 +360,8 @@ class Sequelize { ...@@ -360,8 +360,8 @@ class Sequelize {
* @param {Boolean} [options.underscoredAll=false] Converts camelCased model names to underscored table names if true * @param {Boolean} [options.underscoredAll=false] Converts camelCased model names to underscored table names if true
* @param {Boolean} [options.freezeTableName=false] If freezeTableName is true, sequelize will not try to alter the DAO name to get the table name. Otherwise, the model name will be pluralized * @param {Boolean} [options.freezeTableName=false] If freezeTableName is true, sequelize will not try to alter the DAO name to get the table name. Otherwise, the model name will be pluralized
* @param {Object} [options.name] An object with two attributes, `singular` and `plural`, which are used when this model is associated to others. * @param {Object} [options.name] An object with two attributes, `singular` and `plural`, which are used when this model is associated to others.
* @param {String} [options.name.singular=inflection.singularize(modelName)] * @param {String} [options.name.singular=Utils.singularize(modelName)]
* @param {String} [options.name.plural=inflection.pluralize(modelName)] * @param {String} [options.name.plural=Utils.pluralize(modelName)]
* @param {Array<Object>} [options.indexes] * @param {Array<Object>} [options.indexes]
* @param {String} [options.indexes[].name] The name of the index. Defaults to model name + _ + fields concatenated * @param {String} [options.indexes[].name] The name of the index. Defaults to model name + _ + fields concatenated
* @param {String} [options.indexes[].type] Index type. Only used by mysql. One of `UNIQUE`, `FULLTEXT` and `SPATIAL` * @param {String} [options.indexes[].type] Index type. Only used by mysql. One of `UNIQUE`, `FULLTEXT` and `SPATIAL`
...@@ -398,8 +398,8 @@ class Sequelize { ...@@ -398,8 +398,8 @@ class Sequelize {
options = Utils.merge({ options = Utils.merge({
name: { name: {
plural: Utils.inflection.pluralize(modelName), plural: Utils.pluralize(modelName),
singular: Utils.inflection.singularize(modelName) singular: Utils.singularize(modelName)
}, },
indexes: [], indexes: [],
omitNul: globalOptions.omitNull omitNul: globalOptions.omitNull
...@@ -1254,6 +1254,12 @@ Sequelize.prototype.Deferrable = Sequelize.Deferrable = Deferrable; ...@@ -1254,6 +1254,12 @@ Sequelize.prototype.Deferrable = Sequelize.Deferrable = Deferrable;
Sequelize.prototype.Association = Sequelize.Association = Association; Sequelize.prototype.Association = Sequelize.Association = Association;
/** /**
* Provide alternative version of `inflection` module to be used by `Utils.pluralize` etc.
* @param {Object} _inflection - `inflection` module
*/
Sequelize.useInflection = Utils.useInflection;
/**
* Allow hooks to be defined on Sequelize + on sequelize instance as universal hooks to run on all models * Allow hooks to be defined on Sequelize + on sequelize instance as universal hooks to run on all models
* and on Sequelize/sequelize methods e.g. Sequelize(), Sequelize#define() * and on Sequelize/sequelize methods e.g. Sequelize(), Sequelize#define()
*/ */
......
...@@ -4,31 +4,35 @@ const DataTypes = require('./data-types'); ...@@ -4,31 +4,35 @@ const DataTypes = require('./data-types');
const SqlString = require('./sql-string'); const SqlString = require('./sql-string');
const _ = require('lodash').runInContext(); // Prevent anyone messing with template settings by creating a fresh copy const _ = require('lodash').runInContext(); // Prevent anyone messing with template settings by creating a fresh copy
const parameterValidator = require('./utils/parameter-validator'); const parameterValidator = require('./utils/parameter-validator');
const inflection = require('inflection');
const uuid = require('node-uuid'); const uuid = require('node-uuid');
const Promise = require('./promise'); const Promise = require('./promise');
const primitives = ['string', 'number', 'boolean']; const primitives = ['string', 'number', 'boolean'];
let inflection = require('inflection');
exports.Promise = Promise; exports.Promise = Promise;
exports._ = _; exports._ = _;
exports.inflection = inflection;
function camelizeIf(string, condition) { function useInflection(_inflection) {
let result = string; inflection = _inflection;
}
exports.useInflection = useInflection;
function camelizeIf(str, condition) {
let result = str;
if (condition) { if (condition) {
result = camelize(string); result = camelize(str);
} }
return result; return result;
} }
exports.camelizeIf = camelizeIf; exports.camelizeIf = camelizeIf;
function underscoredIf(string, condition) { function underscoredIf(str, condition) {
let result = string; let result = str;
if (condition) { if (condition) {
result = inflection.underscore(string); result = underscore(str);
} }
return result; return result;
...@@ -97,6 +101,11 @@ function camelize(str) { ...@@ -97,6 +101,11 @@ function camelize(str) {
} }
exports.camelize = camelize; exports.camelize = camelize;
function underscore(str) {
return inflection.underscore(str);
}
exports.underscore = underscore;
function format(arr, dialect) { function format(arr, dialect) {
const timeZone = null; const timeZone = null;
// Make a clone of the array beacuse format modifies the passed args // Make a clone of the array beacuse format modifies the passed args
...@@ -296,13 +305,13 @@ function combineTableNames(tableName1, tableName2) { ...@@ -296,13 +305,13 @@ function combineTableNames(tableName1, tableName2) {
} }
exports.combineTableNames = combineTableNames; exports.combineTableNames = combineTableNames;
function singularize(s) { function singularize(str) {
return inflection.singularize(s); return inflection.singularize(str);
} }
exports.singularize = singularize; exports.singularize = singularize;
function pluralize(s) { function pluralize(str) {
return inflection.pluralize(s); return inflection.pluralize(str);
} }
exports.pluralize = pluralize; exports.pluralize = pluralize;
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
"depd": "^1.1.0", "depd": "^1.1.0",
"dottie": "^1.0.0", "dottie": "^1.0.0",
"generic-pool": "2.4.2", "generic-pool": "2.4.2",
"inflection": "^1.6.0", "inflection": "1.10.0",
"lodash": "4.13.1", "lodash": "4.13.1",
"moment": "^2.13.0", "moment": "^2.13.0",
"moment-timezone": "^0.5.4", "moment-timezone": "^0.5.4",
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!