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

Commit 3cd38910 by Simon Schick Committed by Sushant

refactor(data-types): move to classes (#10495)

1 parent 1b4a7bf8
......@@ -105,7 +105,8 @@
"camelcase": "warn",
"prefer-template": "error",
"no-else-return": ["error", { "allowElseIf": false }],
"no-lonely-if": "error"
"no-lonely-if": "error",
"no-this-before-super": "error"
},
"parserOptions": {
"ecmaVersion": 6,
......
......@@ -196,51 +196,49 @@ modules.exports = function sequelizeAdditions(Sequelize) {
/*
* Create new types
*/
class NEWTYPE extends DataTypes.ABSTRACT {
// Mandatory, complete definition of the new type in the database
toSql() {
return 'INTEGER(11) UNSIGNED ZEROFILL'
}
// Create new type
DataTypes.NEWTYPE = function NEWTYPE() {
if (!(this instanceof DataTypes.NEWTYPE)) return new DataTypes.NEWTYPE()
}
inherits(DataTypes.NEWTYPE, DataTypes.ABSTRACT)
// Optional, validator function
validate(value, options) {
return (typeof value === 'number') && (! Number.isNaN(value))
}
// Mandatory, set key
DataTypes.NEWTYPE.prototype.key = DataTypes.NEWTYPE.key = 'NEWTYPE'
// Optional, sanitizer
_sanitize(value) {
// Force all numbers to be positive
if (value < 0) {
value = 0
}
// Mandatory, complete definition of the new type in the database
DataTypes.NEWTYPE.prototype.toSql = function toSql() {
return 'INTEGER(11) UNSIGNED ZEROFILL'
}
// Optional, validator function
DataTypes.NEWTYPE.prototype.validate = function validate(value, options) {
return (typeof value === 'number') && (! Number.isNaN(value))
}
return Math.round(value)
}
// Optional, sanitizer
DataTypes.NEWTYPE.prototype._sanitize = function _sanitize(value) {
// Force all numbers to be positive
if (value < 0) {
value = 0
// Optional, value stringifier before sending to database
_stringify(value) {
return value.toString()
}
return Math.round(value)
// Optional, parser for values received from the database
static parse(value) {
return Number.parseInt(value)
}
}
// Optional, value stringifier before sending to database
DataTypes.NEWTYPE.prototype._stringify = function _stringify(value) {
return value.toString()
}
// Mandatory, set key
DataTypes.NEWTYPE.prototype.key = DataTypes.NEWTYPE.key = 'NEWTYPE'
// Optional, disable escaping after stringifier. Not recommended.
// Warning: disables Sequelize protection against SQL injections
//DataTypes.NEWTYPE.escape = false
// Optional, parser for values received from the database
DataTypes.NEWTYPE.parse = function parse(value) {
return Number.parseInt(value)
}
// For convenience
Sequelize.NEWTYPE = DataTypes.NEWTYPE
// `inferNew` allows you to use the datatype without `new`
Sequelize.NEWTYPE = Sequelize.Utils.inferNew(DataTypes.NEWTYPE)
}
```
......
......@@ -2,7 +2,6 @@
const _ = require('lodash');
const moment = require('moment-timezone');
const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://mariadb.com/kb/en/library/resultset/#field-types';
......@@ -35,138 +34,83 @@ module.exports = BaseTypes => {
BaseTypes.GEOMETRY.types.mariadb = ['GEOMETRY'];
BaseTypes.JSON.types.mariadb = ['JSON'];
function DECIMAL(precision, scale) {
if (!(this instanceof DECIMAL)) {
return new DECIMAL(precision, scale);
class DECIMAL extends BaseTypes.DECIMAL {
toSql() {
let definition = super.toSql();
if (this._unsigned) {
definition += ' UNSIGNED';
}
if (this._zerofill) {
definition += ' ZEROFILL';
}
return definition;
}
BaseTypes.DECIMAL.apply(this, arguments);
}
inherits(DECIMAL, BaseTypes.DECIMAL);
DECIMAL.prototype.toSql = function toSql() {
let definition = BaseTypes.DECIMAL.prototype.toSql.apply(this);
if (this._unsigned) {
definition += ' UNSIGNED';
class DATE extends BaseTypes.DATE {
toSql() {
return `DATETIME${this._length ? `(${this._length})` : ''}`;
}
if (this._zerofill) {
definition += ' ZEROFILL';
_stringify(date, options) {
date = this._applyTimezone(date, options);
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
}
return definition;
};
function DATE(length) {
if (!(this instanceof DATE)) {
return new DATE(length);
}
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function toSql() {
return `DATETIME${this._length ? `(${this._length})` : ''}`;
};
DATE.prototype._stringify = function _stringify(date, options) {
date = BaseTypes.DATE.prototype._applyTimezone(date, options);
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
};
DATE.parse = function parse(value, options) {
value = value.string();
if (value === null) {
static parse(value, options) {
value = value.string();
if (value === null) {
return value;
}
if (moment.tz.zone(options.timezone)) {
value = moment.tz(value, options.timezone).toDate();
}
else {
value = new Date(`${value} ${options.timezone}`);
}
return value;
}
if (moment.tz.zone(options.timezone)) {
value = moment.tz(value, options.timezone).toDate();
} else {
value = new Date(`${value} ${options.timezone}`);
}
return value;
};
function DATEONLY() {
if (!(this instanceof DATEONLY)) {
return new DATEONLY();
}
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
DATEONLY.parse = function parse(value) {
return value.string();
};
class DATEONLY extends BaseTypes.DATEONLY {
static parse(value) {
return value.string();
};
}
function UUID() {
if (!(this instanceof UUID)) {
return new UUID();
class UUID extends BaseTypes.UUID {
toSql() {
return 'CHAR(36) BINARY';
}
BaseTypes.UUID.call(this);
}
inherits(UUID, BaseTypes.UUID);
UUID.prototype.toSql = function toSql() {
return 'CHAR(36) BINARY';
};
function GEOMETRY(type, srid) {
if (!(this instanceof GEOMETRY)) {
return new GEOMETRY(type, srid);
class GEOMETRY extends BaseTypes.GEOMETRY {
constructor(type, srid) {
super(type, srid);
if (_.isEmpty(this.type)) {
this.sqlType = this.key;
}
else {
this.sqlType = this.type;
}
}
BaseTypes.GEOMETRY.apply(this, arguments);
if (_.isEmpty(this.type)) {
this.sqlType = this.key;
} else {
this.sqlType = this.type;
toSql() {
return this.sqlType;
}
}
inherits(GEOMETRY, BaseTypes.GEOMETRY);
GEOMETRY.prototype.toSql = function toSql() {
return this.sqlType;
};
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
class ENUM extends BaseTypes.ENUM {
toSql(options) {
return `ENUM(${this.values.map(value => options.escape(value)).join(', ')})`;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function toSql(options) {
return `ENUM(${this.values.map(value => options.escape(value)).join(', ')})`;
};
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) {
return new JSONTYPE();
class JSONTYPE extends BaseTypes.JSON {
_stringify(value, options) {
return options.operation === 'where' && typeof value === 'string' ? value
: JSON.stringify(value);
}
BaseTypes.JSON.call(this);
}
inherits(JSONTYPE, BaseTypes.JSON);
JSONTYPE.prototype._stringify = function _stringify(value, options) {
return options.operation === 'where' && typeof value === 'string' ? value
: JSON.stringify(value);
};
const exports = {
return {
ENUM,
DATE,
DATEONLY,
......@@ -175,17 +119,4 @@ module.exports = BaseTypes => {
DECIMAL,
JSON: JSONTYPE
};
_.forIn(exports, (DataType, key) => {
if (!DataType.key) {
DataType.key = key;
}
if (!DataType.extend) {
DataType.extend = function extend(oldType) {
return new DataType(oldType.options);
};
}
});
return exports;
};
......@@ -3,8 +3,6 @@
const wkx = require('wkx');
const _ = require('lodash');
const moment = require('moment-timezone');
const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
......@@ -36,143 +34,101 @@ module.exports = BaseTypes => {
BaseTypes.GEOMETRY.types.mysql = ['GEOMETRY'];
BaseTypes.JSON.types.mysql = ['JSON'];
function DECIMAL(precision, scale) {
if (!(this instanceof DECIMAL)) return new DECIMAL(precision, scale);
BaseTypes.DECIMAL.apply(this, arguments);
}
inherits(DECIMAL, BaseTypes.DECIMAL);
DECIMAL.prototype.toSql = function toSql() {
let definition = BaseTypes.DECIMAL.prototype.toSql.apply(this);
if (this._unsigned) {
definition += ' UNSIGNED';
}
if (this._zerofill) {
definition += ' ZEROFILL';
class DECIMAL extends BaseTypes.DECIMAL {
toSql() {
let definition = super.toSql();
if (this._unsigned) {
definition += ' UNSIGNED';
}
if (this._zerofill) {
definition += ' ZEROFILL';
}
return definition;
}
return definition;
};
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function toSql() {
return `DATETIME${this._length ? `(${this._length})` : ''}`;
};
DATE.prototype._stringify = function _stringify(date, options) {
date = BaseTypes.DATE.prototype._applyTimezone(date, options);
// Fractional DATETIMEs only supported on MySQL 5.6.4+
if (this._length) {
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
class DATE extends BaseTypes.DATE {
toSql() {
return `DATETIME${this._length ? `(${this._length})` : ''}`;
}
return date.format('YYYY-MM-DD HH:mm:ss');
};
DATE.parse = function parse(value, options) {
value = value.string();
if (value === null) {
_stringify(date, options) {
date = this._applyTimezone(date, options);
// Fractional DATETIMEs only supported on MySQL 5.6.4+
if (this._length) {
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
}
return date.format('YYYY-MM-DD HH:mm:ss');
}
static parse(value, options) {
value = value.string();
if (value === null) {
return value;
}
if (moment.tz.zone(options.timezone)) {
value = moment.tz(value, options.timezone).toDate();
}
else {
value = new Date(`${value} ${options.timezone}`);
}
return value;
}
}
if (moment.tz.zone(options.timezone)) {
value = moment.tz(value, options.timezone).toDate();
} else {
value = new Date(`${value} ${options.timezone}`);
class DATEONLY extends BaseTypes.DATEONLY {
static parse(value) {
return value.string();
}
return value;
};
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
DATEONLY.parse = function parse(value) {
return value.string();
};
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.call(this);
class UUID extends BaseTypes.UUID {
toSql() {
return 'CHAR(36) BINARY';
}
}
inherits(UUID, BaseTypes.UUID);
UUID.prototype.toSql = function toSql() {
return 'CHAR(36) BINARY';
};
const SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
function GEOMETRY(type, srid) {
if (!(this instanceof GEOMETRY)) return new GEOMETRY(type, srid);
BaseTypes.GEOMETRY.apply(this, arguments);
if (_.isEmpty(this.type)) {
this.sqlType = this.key;
} else if (SUPPORTED_GEOMETRY_TYPES.includes(this.type)) {
this.sqlType = this.type;
} else {
class GEOMETRY extends BaseTypes.GEOMETRY {
constructor(type, srid) {
super(type, srid);
if (_.isEmpty(this.type)) {
this.sqlType = this.key;
return;
}
if (SUPPORTED_GEOMETRY_TYPES.includes(this.type)) {
this.sqlType = this.type;
return;
}
throw new Error(`Supported geometry types are: ${SUPPORTED_GEOMETRY_TYPES.join(', ')}`);
}
}
inherits(GEOMETRY, BaseTypes.GEOMETRY);
GEOMETRY.parse = GEOMETRY.prototype.parse = function parse(value) {
value = value.buffer();
// Empty buffer, MySQL doesn't support POINT EMPTY
// check, https://dev.mysql.com/worklog/task/?id=2381
if (!value || value.length === 0) {
return null;
static parse(value) {
value = value.buffer();
// Empty buffer, MySQL doesn't support POINT EMPTY
// check, https://dev.mysql.com/worklog/task/?id=2381
if (!value || value.length === 0) {
return null;
}
// For some reason, discard the first 4 bytes
value = value.slice(4);
return wkx.Geometry.parse(value).toGeoJSON();
}
// For some reason, discard the first 4 bytes
value = value.slice(4);
return wkx.Geometry.parse(value).toGeoJSON();
};
GEOMETRY.prototype.toSql = function toSql() {
return this.sqlType;
};
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
toSql() {
return this.sqlType;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function toSql(options) {
return `ENUM(${this.values.map(value => options.escape(value)).join(', ')})`;
};
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) return new JSONTYPE();
BaseTypes.JSON.call(this);
class ENUM extends BaseTypes.ENUM {
toSql(options) {
return `ENUM(${this.values.map(value => options.escape(value)).join(', ')})`;
}
}
inherits(JSONTYPE, BaseTypes.JSON);
JSONTYPE.prototype._stringify = function _stringify(value, options) {
return options.operation === 'where' && typeof value === 'string' ? value : JSON.stringify(value);
};
class JSONTYPE extends BaseTypes.JSON {
_stringify(value, options) {
return options.operation === 'where' && typeof value === 'string' ? value : JSON.stringify(value);
}
}
const exports = {
return {
ENUM,
DATE,
DATEONLY,
......@@ -181,15 +137,4 @@ module.exports = BaseTypes => {
DECIMAL,
JSON: JSONTYPE
};
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function extend(oldType) {
return new DataType(oldType.options);
};
}
});
return exports;
};
'use strict';
const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
......@@ -45,235 +42,155 @@ module.exports = BaseTypes => {
BaseTypes.GEOMETRY.types.sqlite = false;
BaseTypes.JSON.types.sqlite = ['JSON', 'JSONB'];
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) return new JSONTYPE();
BaseTypes.JSON.call(this);
class JSONTYPE extends BaseTypes.JSON {
static parse(data) {
return JSON.parse(data);
}
}
inherits(JSONTYPE, BaseTypes.JSON);
JSONTYPE.parse = function parse(data) {
return JSON.parse(data);
};
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
class DATE extends BaseTypes.DATE {
static parse(date, options) {
if (!date.includes('+')) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
return new Date(date + options.timezone);
}
return new Date(date); // We already have a timezone stored in the string
}
}
inherits(DATE, BaseTypes.DATE);
DATE.parse = function parse(date, options) {
if (!date.includes('+')) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
return new Date(date + options.timezone);
class DATEONLY extends BaseTypes.DATEONLY {
static parse(date) {
return date;
}
return new Date(date); // We already have a timezone stored in the string
};
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
BaseTypes.DATEONLY.call(this);
}
inherits(DATEONLY, BaseTypes.DATEONLY);
DATEONLY.parse = function parse(date) {
return date;
};
function STRING(length, binary) {
if (!(this instanceof STRING)) return new STRING(length, binary);
BaseTypes.STRING.apply(this, arguments);
class STRING extends BaseTypes.STRING {
toSql() {
if (this._binary) {
return `VARCHAR BINARY(${this._length})`;
}
return super.toSql(this);
}
}
inherits(STRING, BaseTypes.STRING);
STRING.prototype.toSql = function toSql() {
if (this._binary) {
return `VARCHAR BINARY(${this._length})`;
class TEXT extends BaseTypes.TEXT {
toSql() {
if (this._length) {
warn('SQLite does not support TEXT with options. Plain `TEXT` will be used instead.');
this._length = undefined;
}
return 'TEXT';
}
return BaseTypes.STRING.prototype.toSql.call(this);
};
function TEXT(length) {
if (!(this instanceof TEXT)) return new TEXT(length);
BaseTypes.TEXT.apply(this, arguments);
}
inherits(TEXT, BaseTypes.TEXT);
TEXT.prototype.toSql = function toSql() {
if (this._length) {
warn('SQLite does not support TEXT with options. Plain `TEXT` will be used instead.');
this._length = undefined;
class CITEXT extends BaseTypes.CITEXT {
toSql() {
return 'TEXT COLLATE NOCASE';
}
return 'TEXT';
};
function CITEXT() {
if (!(this instanceof CITEXT)) return new CITEXT();
BaseTypes.CITEXT.call(this);
}
inherits(CITEXT, BaseTypes.CITEXT);
CITEXT.prototype.toSql = function toSql() {
return 'TEXT COLLATE NOCASE';
};
function CHAR(length, binary) {
if (!(this instanceof CHAR)) return new CHAR(length, binary);
BaseTypes.CHAR.apply(this, arguments);
class CHAR extends BaseTypes.CHAR {
toSql() {
if (this._binary) {
return `CHAR BINARY(${this._length})`;
}
return super.toSql();
}
}
inherits(CHAR, BaseTypes.CHAR);
CHAR.prototype.toSql = function toSql() {
if (this._binary) {
return `CHAR BINARY(${this._length})`;
class NUMBER extends BaseTypes.NUMBER {
toSql() {
let result = this.key;
if (this._unsigned) {
result += ' UNSIGNED';
}
if (this._zerofill) {
result += ' ZEROFILL';
}
if (this._length) {
result += `(${this._length}`;
if (typeof this._decimals === 'number') {
result += `,${this._decimals}`;
}
result += ')';
}
return result;
}
return BaseTypes.CHAR.prototype.toSql.call(this);
};
function NUMBER(options) {
if (!(this instanceof NUMBER)) return new NUMBER(options);
BaseTypes.NUMBER.apply(this, arguments);
}
inherits(NUMBER, BaseTypes.NUMBER);
NUMBER.prototype.toSql = function toSql() {
let result = this.key;
if (this._unsigned) {
result += ' UNSIGNED';
}
if (this._zerofill) {
result += ' ZEROFILL';
class TINYINT extends BaseTypes.TINYINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
if (this._length) {
result += `(${this._length}`;
if (typeof this._decimals === 'number') {
result += `,${this._decimals}`;
}
result += ')';
class SMALLINT extends BaseTypes.SMALLINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
return result;
};
function TINYINT(length) {
if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments);
removeUnsupportedIntegerOptions(this);
}
inherits(TINYINT, BaseTypes.TINYINT);
TINYINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
removeUnsupportedIntegerOptions(this);
class MEDIUMINT extends BaseTypes.MEDIUMINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
inherits(SMALLINT, BaseTypes.SMALLINT);
SMALLINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function MEDIUMINT(length) {
if (!(this instanceof MEDIUMINT)) return new MEDIUMINT(length);
BaseTypes.MEDIUMINT.apply(this, arguments);
removeUnsupportedIntegerOptions(this);
class INTEGER extends BaseTypes.INTEGER {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
inherits(MEDIUMINT, BaseTypes.MEDIUMINT);
MEDIUMINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
removeUnsupportedIntegerOptions(this);
class BIGINT extends BaseTypes.BIGINT {
constructor(length) {
super(length);
removeUnsupportedIntegerOptions(this);
}
}
inherits(INTEGER, BaseTypes.INTEGER);
INTEGER.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
class FLOAT extends BaseTypes.FLOAT {
}
removeUnsupportedIntegerOptions(this);
class DOUBLE extends BaseTypes.DOUBLE {
}
inherits(BIGINT, BaseTypes.BIGINT);
BIGINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
class REAL extends BaseTypes.REAL { }
function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments);
function parseFloating(value) {
if (typeof value !== 'string') {
return value;
}
if (value === 'NaN') {
return NaN;
}
if (value === 'Infinity') {
return Infinity;
}
if (value === '-Infinity') {
return -Infinity;
}
}
inherits(FLOAT, BaseTypes.FLOAT);
FLOAT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
for (const floating of [FLOAT, DOUBLE, REAL]) {
floating.parse = parseFloating;
};
function DOUBLE(length, decimals) {
if (!(this instanceof DOUBLE)) return new DOUBLE(length, decimals);
BaseTypes.DOUBLE.apply(this, arguments);
}
inherits(DOUBLE, BaseTypes.DOUBLE);
DOUBLE.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function REAL(length, decimals) {
if (!(this instanceof REAL)) return new REAL(length, decimals);
BaseTypes.REAL.apply(this, arguments);
}
inherits(REAL, BaseTypes.REAL);
REAL.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
for (const num of [FLOAT, DOUBLE, REAL, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT]) {
num.prototype.toSql = NUMBER.prototype.toSql;
};
[FLOAT, DOUBLE, REAL].forEach(floating => {
floating.parse = function parse(value) {
if (typeof value === 'string') {
if (value === 'NaN') {
return NaN;
}
if (value === 'Infinity') {
return Infinity;
}
if (value === '-Infinity') {
return -Infinity;
}
}
return value;
};
});
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
class ENUM extends BaseTypes.ENUM {
toSql() {
return 'TEXT';
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function toSql() {
return 'TEXT';
};
const exports = {
return {
DATE,
DATEONLY,
STRING,
......@@ -292,16 +209,4 @@ module.exports = BaseTypes => {
JSON: JSONTYPE,
CITEXT
};
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = oldType => {
return new DataType(oldType.options);
};
}
});
return exports;
};
......@@ -11,6 +11,8 @@ const operatorsArray = _.values(operators);
let inflection = require('inflection');
exports.classToInvokable = require('./utils/classToInvokable').classToInvokable;
exports.Promise = Promise;
function useInflection(_inflection) {
......@@ -595,15 +597,6 @@ function defaults(object) {
}
exports.defaults = defaults;
function classToInvokable(Class) {
return new Proxy(Class, {
apply(Target, thisArg, args) {
return new Target(...args);
}
});
}
exports.classToInvokable = classToInvokable;
/**
*
* @param {Object} index
......
'use strict';
/**
* Wraps a constructor to not need the `new` keyword using a proxy.
* Only used for data types.
* @param {Function} ctor
* @return {Proxy}
*/
function classToInvokable(Class) {
return new Proxy(Class, {
apply(Target, thisArg, args) {
return new Target(...args);
}
});
}
exports.classToInvokable = classToInvokable;
'use strict';
const util = require('util');
/**
* like util.inherits, but also copies over static properties. Inherit child constructor
* to have properties from super constructor
*
* @param {Function} constructor the child constructor
* @param {Function} superConstructor the super constructor
*
* @private
*/
function inherits(constructor, superConstructor) {
util.inherits(constructor, superConstructor); // Instance (prototype) methods
Object.assign(constructor, superConstructor); // Static methods
}
module.exports = inherits;
module.exports.inherits = inherits;
module.exports.default = inherits;
......@@ -890,7 +890,6 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
dataTypes.forEach(dataType => {
const tableName = `TaskXYZ_${dataType.key}`;
Tasks[dataType] = this.sequelize.define(tableName, { title: DataTypes.STRING });
Tasks[dataType].belongsTo(User, { foreignKey: 'userId', keyType: dataType, constraints: false });
});
......
......@@ -64,6 +64,14 @@ export const TICK_CHAR: string;
export function addTicks(s: string, tickChar?: string): string;
export function removeTicks(s: string, tickChar?: string): string;
/**
* Wraps a constructor to not need the `new` keyword using a proxy.
* Only used for data types.
*/
export function classToInvokable<T extends new (...args: any[]) => any>(ctor: T): T & {
(...args: ConstructorParameters<T>): T;
}
export class SequelizeMethod {
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!