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

Commit cc4ee8b8 by Felix Becker Committed by Mick Hansen

ES6 refactor of data types (#6072)

- Use let, const and property shorthands.
 - The data types are not changed to ES6 classes because it should be possible
   to call them without `new`.
 - Use function declarations so the functions (which are classes) have a name
 - Simplify `inherits` function and move it to utils.js
   The manual style may be a bit more verbose but it comes closer to true ES6
   classes because the functions are named and not dynamically created
 - Name methods
 - Fix bugs in dialect data types where constructor arguments were not applied
   when called without `new`
 - Remove unneeded code
1 parent b9bdc1d4
'use strict';
/*jshint -W110 */
var util = require('util')
, _ = require('lodash')
, Wkt = require('terraformer-wkt-parser')
, sequelizeErrors = require('./errors')
, warnings = {}
, Validator = require('validator')
, momentTz = require('moment-timezone')
, moment = require('moment');
const util = require('util');
const inherits = require('./utils/inherits');
const _ = require('lodash');
const Wkt = require('terraformer-wkt-parser');
const sequelizeErrors = require('./errors');
const warnings = {};
const Validator = require('validator');
const momentTz = require('moment-timezone');
const moment = require('moment');
/**
* A convenience class holding commonly used data types. The datatypes are used when defining a new model using `Sequelize.define`, like this:
......@@ -55,51 +56,29 @@ var util = require('util')
* @class DataTypes
*/
var ABSTRACT = function(options) {
};
function ABSTRACT() {}
ABSTRACT.prototype.dialectTypes = '';
ABSTRACT.prototype.toString = function(options) {
ABSTRACT.prototype.toString = function toString(options) {
return this.toSql(options);
};
ABSTRACT.prototype.toSql = function() {
ABSTRACT.prototype.toSql = function toSql() {
return this.key;
};
ABSTRACT.warn = function(link, text) {
ABSTRACT.warn = function warn(link, text) {
if (!warnings[text]) {
warnings[text] = true;
console.warn('>> WARNING:', text, '\n>> Check:', link);
}
};
ABSTRACT.prototype.stringify = function (value, options) {
ABSTRACT.prototype.stringify = function stringify(value, options) {
if (this.$stringify) {
return this.$stringify(value, options);
}
return value;
};
ABSTRACT.inherits = function (Constructor) {
var baseType = this;
if (!Constructor) {
Constructor = function () {
if (!(this instanceof Constructor)) {
var args = [null].concat(arguments);
var FactoryFunction = Constructor.bind.apply(Constructor, args);
return new FactoryFunction();
}
baseType.apply(this, arguments);
};
}
util.inherits(Constructor, baseType); // Instance (prototype) methods
_.extend(Constructor, this); // Static methods
return Constructor;
};
/**
* A variable length string. Default length 255
*
......@@ -107,24 +86,22 @@ ABSTRACT.inherits = function (Constructor) {
*
* @property STRING
*/
var STRING = ABSTRACT.inherits(function(length, binary) {
var options = typeof length === 'object' && length || {
length: length,
binary: binary
};
function STRING(length, binary) {
const options = typeof length === 'object' && length || {length, binary};
if (!(this instanceof STRING)) return new STRING(options);
this.options = options;
this._binary = options.binary;
this._length = options.length || 255;
});
}
inherits(STRING, ABSTRACT);
STRING.prototype.key = STRING.key = 'STRING';
STRING.prototype.toSql = function() {
STRING.prototype.toSql = function toSql() {
return 'VARCHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : '');
};
STRING.prototype.validate = function(value) {
STRING.prototype.validate = function validate(value) {
if (Object.prototype.toString.call(value) !== '[object String]') {
if ((this.options.binary && Buffer.isBuffer(value)) || _.isNumber(value)) {
return true;
......@@ -135,7 +112,7 @@ STRING.prototype.validate = function(value) {
return true;
};
Object.defineProperty(STRING.prototype, 'BINARY', {
get: function() {
get() {
this._binary = true;
this.options.binary = true;
return this;
......@@ -149,18 +126,16 @@ Object.defineProperty(STRING.prototype, 'BINARY', {
*
* @property CHAR
*/
var CHAR = STRING.inherits(function(length, binary) {
var options = typeof length === 'object' && length || {
length: length,
binary: binary
};
function CHAR(length, binary) {
const options = typeof length === 'object' && length || {length, binary};
if (!(this instanceof CHAR)) return new CHAR(options);
STRING.apply(this, arguments);
});
}
inherits(CHAR, STRING);
CHAR.prototype.key = CHAR.key = 'CHAR';
CHAR.prototype.toSql = function() {
CHAR.prototype.toSql = function toSql() {
return 'CHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : '');
};
......@@ -168,17 +143,16 @@ CHAR.prototype.toSql = function() {
* An (un)limited length text column. Available lengths: `tiny`, `medium`, `long`
* @property TEXT
*/
var TEXT = ABSTRACT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
function TEXT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof TEXT)) return new TEXT(options);
this.options = options;
this._length = options.length || '';
});
}
inherits(TEXT, ABSTRACT);
TEXT.prototype.key = TEXT.key = 'TEXT';
TEXT.prototype.toSql = function() {
TEXT.prototype.toSql = function toSql() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYTEXT';
......@@ -190,7 +164,7 @@ TEXT.prototype.toSql = function() {
return this.key;
}
};
TEXT.prototype.validate = function(value) {
TEXT.prototype.validate = function validate(value) {
if (!_.isString(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid string', value));
}
......@@ -198,7 +172,7 @@ TEXT.prototype.validate = function(value) {
return true;
};
var NUMBER = ABSTRACT.inherits(function(options) {
function NUMBER(options) {
this.options = options;
this._length = options.length;
this._zerofill = options.zerofill;
......@@ -206,11 +180,12 @@ var NUMBER = ABSTRACT.inherits(function(options) {
this._precision = options.precision;
this._scale = options.scale;
this._unsigned = options.unsigned;
});
}
inherits(NUMBER, ABSTRACT);
NUMBER.prototype.key = NUMBER.key = 'NUMBER';
NUMBER.prototype.toSql = function() {
var result = this.key;
NUMBER.prototype.toSql = function toSql() {
let result = this.key;
if (this._length) {
result += '(' + this._length;
if (typeof this._decimals === 'number') {
......@@ -236,14 +211,14 @@ NUMBER.prototype.validate = function(value) {
};
Object.defineProperty(NUMBER.prototype, 'UNSIGNED', {
get: function() {
get() {
this._unsigned = true;
this.options.unsigned = true;
return this;
}
});
Object.defineProperty(NUMBER.prototype, 'ZEROFILL', {
get: function() {
get() {
this._zerofill = true;
this.options.zerofill = true;
return this;
......@@ -257,16 +232,15 @@ Object.defineProperty(NUMBER.prototype, 'ZEROFILL', {
*
* @property INTEGER
*/
var INTEGER = NUMBER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
function INTEGER(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof INTEGER)) return new INTEGER(options);
NUMBER.call(this, options);
});
}
inherits(INTEGER, NUMBER);
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.validate = function(value) {
INTEGER.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid integer', value));
}
......@@ -284,16 +258,15 @@ INTEGER.prototype.validate = function(value) {
* @property BIGINT
*/
var BIGINT = NUMBER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
function BIGINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof BIGINT)) return new BIGINT(options);
NUMBER.call(this, options);
});
}
inherits(BIGINT, NUMBER);
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.validate = function(value) {
BIGINT.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid bigint', value));
}
......@@ -308,17 +281,15 @@ BIGINT.prototype.validate = function(value) {
*
* @property FLOAT
*/
var FLOAT = NUMBER.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
function FLOAT(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
if (!(this instanceof FLOAT)) return new FLOAT(options);
NUMBER.call(this, options);
});
}
inherits(FLOAT, NUMBER);
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.validate = function(value) {
FLOAT.prototype.validate = function validate(value) {
if (!Validator.isFloat(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid float', value));
}
......@@ -333,14 +304,12 @@ FLOAT.prototype.validate = function(value) {
*
* @property REAL
*/
var REAL = NUMBER.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
function REAL(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
if (!(this instanceof REAL)) return new REAL(options);
NUMBER.call(this, options);
});
}
inherits(REAL, NUMBER);
REAL.prototype.key = REAL.key = 'REAL';
......@@ -351,14 +320,12 @@ REAL.prototype.key = REAL.key = 'REAL';
*
* @property DOUBLE
*/
var DOUBLE = NUMBER.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
function DOUBLE(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals};
if (!(this instanceof DOUBLE)) return new DOUBLE(options);
NUMBER.call(this, options);
});
}
inherits(DOUBLE, NUMBER);
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
......@@ -369,24 +336,22 @@ DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
*
* @property DECIMAL
*/
var DECIMAL = NUMBER.inherits(function(precision, scale) {
var options = typeof precision === 'object' && precision || {
precision: precision,
scale: scale
};
function DECIMAL(precision, scale) {
const options = typeof precision === 'object' && precision || {precision, scale};
if (!(this instanceof DECIMAL)) return new DECIMAL(options);
NUMBER.call(this, options);
});
}
inherits(DECIMAL, NUMBER);
DECIMAL.prototype.key = DECIMAL.key = 'DECIMAL';
DECIMAL.prototype.toSql = function() {
DECIMAL.prototype.toSql = function toSql() {
if (this._precision || this._scale) {
return 'DECIMAL(' + [this._precision, this._scale].filter(_.identity).join(',') + ')';
}
return 'DECIMAL';
};
DECIMAL.prototype.validate = function(value) {
DECIMAL.prototype.validate = function validate(value) {
if (!Validator.isDecimal(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid decimal', value));
}
......@@ -394,31 +359,34 @@ DECIMAL.prototype.validate = function(value) {
return true;
};
[FLOAT, DOUBLE, REAL].forEach(function (floating) {
for (const floating of [FLOAT, DOUBLE, REAL]) {
floating.prototype.escape = false;
floating.prototype.$stringify = function (value) {
floating.prototype.$stringify = function $stringify(value) {
if (isNaN(value)) {
return "'NaN'";
} else if (!isFinite(value)) {
var sign = value < 0 ? '-' : '';
const sign = value < 0 ? '-' : '';
return "'" + sign + "Infinity'";
}
return value;
};
});
}
/**
* A boolean / tinyint column, depending on dialect
* @property BOOLEAN
*/
var BOOLEAN = ABSTRACT.inherits();
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
}
inherits(BOOLEAN, ABSTRACT);
BOOLEAN.prototype.key = BOOLEAN.key = 'BOOLEAN';
BOOLEAN.prototype.toSql = function() {
BOOLEAN.prototype.toSql = function toSql() {
return 'TINYINT(1)';
};
BOOLEAN.prototype.validate = function(value) {
BOOLEAN.prototype.validate = function validate(value) {
if (!Validator.isBoolean(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid boolean', value));
}
......@@ -431,10 +399,13 @@ BOOLEAN.prototype.validate = function(value) {
* @property TIME
*/
var TIME = ABSTRACT.inherits();
function TIME() {
if (!(this instanceof TIME)) return new TIME();
}
inherits(TIME, ABSTRACT);
TIME.prototype.key = TIME.key = 'TIME';
TIME.prototype.toSql = function() {
TIME.prototype.toSql = function toSql() {
return 'TIME';
};
......@@ -442,22 +413,21 @@ TIME.prototype.toSql = function() {
* A datetime column
* @property DATE
*/
var DATE = ABSTRACT.inherits(function (length) {
var options = typeof length === 'object' && length || {
length: length
};
function DATE(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof DATE)) return new DATE(options);
this.options = options;
this._length = options.length || '';
});
}
inherits(DATE, ABSTRACT);
DATE.prototype.key = DATE.key = 'DATE';
DATE.prototype.toSql = function() {
DATE.prototype.toSql = function toSql() {
return 'DATETIME';
};
DATE.prototype.validate = function(value) {
DATE.prototype.validate = function validate(value) {
if (!_.isDate(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid date', value));
}
......@@ -465,7 +435,7 @@ DATE.prototype.validate = function(value) {
return true;
};
DATE.prototype.$applyTimezone = function (date, options) {
DATE.prototype.$applyTimezone = function $applyTimezone(date, options) {
if (options.timezone) {
if (momentTz.tz.zone(options.timezone)) {
date = momentTz(date).tz(options.timezone);
......@@ -479,7 +449,7 @@ DATE.prototype.$applyTimezone = function (date, options) {
return date;
};
DATE.prototype.$stringify = function (date, options) {
DATE.prototype.$stringify = function $stringify(date, options) {
date = this.$applyTimezone(date, options);
// Z here means current timezone, _not_ UTC
......@@ -491,10 +461,9 @@ DATE.prototype.$stringify = function (date, options) {
* @property DATEONLY
*/
var DATEONLY = function() {
function DATEONLY() {
if (!(this instanceof DATEONLY)) return new DATEONLY();
ABSTRACT.apply(this, arguments);
};
}
util.inherits(DATEONLY, ABSTRACT);
DATEONLY.prototype.key = DATEONLY.key = 'DATEONLY';
......@@ -507,10 +476,13 @@ DATEONLY.prototype.toSql = function() {
* @property HSTORE
*/
var HSTORE = ABSTRACT.inherits();
function HSTORE() {
if (!(this instanceof HSTORE)) return new HSTORE();
}
inherits(HSTORE, ABSTRACT);
HSTORE.prototype.key = HSTORE.key = 'HSTORE';
HSTORE.prototype.validate = function(value) {
HSTORE.prototype.validate = function validate(value) {
if (!_.isPlainObject(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid hstore', value));
}
......@@ -522,18 +494,17 @@ HSTORE.prototype.validate = function(value) {
* A JSON string column. Only available in postgres.
* @property JSON
*/
var JSONTYPE = function() {
function JSONTYPE() {
if (!(this instanceof JSONTYPE)) return new JSONTYPE();
ABSTRACT.apply(this, arguments);
};
util.inherits(JSONTYPE, ABSTRACT);
}
inherits(JSONTYPE, ABSTRACT);
JSONTYPE.prototype.key = JSONTYPE.key = 'JSON';
JSONTYPE.prototype.validate = function(value) {
JSONTYPE.prototype.validate = function validate(value) {
return true;
};
JSONTYPE.prototype.$stringify = function (value, options) {
JSONTYPE.prototype.$stringify = function $stringify(value, options) {
return JSON.stringify(value);
};
......@@ -541,11 +512,11 @@ JSONTYPE.prototype.$stringify = function (value, options) {
* A pre-processed JSON data column. Only available in postgres.
* @property JSONB
*/
var JSONB = function() {
function JSONB() {
if (!(this instanceof JSONB)) return new JSONB();
JSONTYPE.apply(this, arguments);
};
util.inherits(JSONB, JSONTYPE);
JSONTYPE.call(this);
}
inherits(JSONB, JSONTYPE);
JSONB.prototype.key = JSONB.key = 'JSONB';
......@@ -553,7 +524,10 @@ JSONB.prototype.key = JSONB.key = 'JSONB';
* A default value of the current timestamp
* @property NOW
*/
var NOW = ABSTRACT.inherits();
function NOW() {
if (!(this instanceof NOW)) return new NOW();
}
inherits(NOW, ABSTRACT);
NOW.prototype.key = NOW.key = 'NOW';
......@@ -562,18 +536,16 @@ NOW.prototype.key = NOW.key = 'NOW';
*
* @property BLOB
*/
var BLOB = ABSTRACT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
function BLOB(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof BLOB)) return new BLOB(options);
this.options = options;
this._length = options.length || '';
});
}
inherits(BLOB, ABSTRACT);
BLOB.prototype.key = BLOB.key = 'BLOB';
BLOB.prototype.toSql = function() {
BLOB.prototype.toSql = function toSql() {
switch (this._length.toLowerCase()) {
case 'tiny':
return 'TINYBLOB';
......@@ -585,7 +557,7 @@ BLOB.prototype.toSql = function() {
return this.key;
}
};
BLOB.prototype.validate = function(value) {
BLOB.prototype.validate = function validate(value) {
if (!_.isString(value) && !Buffer.isBuffer(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid blob', value));
}
......@@ -594,7 +566,7 @@ BLOB.prototype.validate = function(value) {
};
BLOB.prototype.escape = false;
BLOB.prototype.$stringify = function (value) {
BLOB.prototype.$stringify = function $stringify(value) {
if (!Buffer.isBuffer(value)) {
if (Array.isArray(value)) {
value = new Buffer(value);
......@@ -602,12 +574,12 @@ BLOB.prototype.$stringify = function (value) {
value = new Buffer(value.toString());
}
}
var hex = value.toString('hex');
const hex = value.toString('hex');
return this.$hexify(hex);
};
BLOB.prototype.$hexify = function (hex) {
BLOB.prototype.$hexify = function $hexify(hex) {
return "X'" + hex + "'";
};
......@@ -618,8 +590,8 @@ BLOB.prototype.$hexify = function (hex) {
* @property RANGE
*/
var RANGE = ABSTRACT.inherits(function (subtype) {
var options = _.isPlainObject(subtype) ? subtype : { subtype: subtype };
function RANGE(subtype) {
const options = _.isPlainObject(subtype) ? subtype : {subtype};
if (!options.subtype) options.subtype = new INTEGER();
......@@ -628,13 +600,13 @@ var RANGE = ABSTRACT.inherits(function (subtype) {
}
if (!(this instanceof RANGE)) return new RANGE(options);
ABSTRACT.apply(this, arguments);
this._subtype = options.subtype.key;
this.options = options;
});
}
inherits(RANGE, ABSTRACT);
var pgRangeSubtypes = {
const pgRangeSubtypes = {
integer: 'int4range',
bigint: 'int8range',
decimal: 'numrange',
......@@ -653,13 +625,13 @@ const pgRangeCastTypes = {
};
RANGE.prototype.key = RANGE.key = 'RANGE';
RANGE.prototype.toSql = function() {
RANGE.prototype.toSql = function toSql() {
return pgRangeSubtypes[this._subtype.toLowerCase()];
};
RANGE.prototype.toCastType = function() {
RANGE.prototype.toCastType = function toCastType() {
return pgRangeCastTypes[this._subtype.toLowerCase()];
};
RANGE.prototype.validate = function(value) {
RANGE.prototype.validate = function validate(value) {
if (_.isPlainObject(value) && value.inclusive) {
value = value.inclusive;
}
......@@ -680,10 +652,13 @@ RANGE.prototype.validate = function(value) {
* A column storing a unique universal identifier. Use with `UUIDV1` or `UUIDV4` for default values.
* @property UUID
*/
var UUID = ABSTRACT.inherits();
function UUID() {
if (!(this instanceof UUID)) return new UUID();
}
inherits(UUID, ABSTRACT);
UUID.prototype.key = UUID.key = 'UUID';
UUID.prototype.validate = function(value, options) {
UUID.prototype.validate = function validate(value, options) {
if (!_.isString(value) || !Validator.isUUID(value) && (!options || !options.acceptStrings)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuid', value));
}
......@@ -696,14 +671,13 @@ UUID.prototype.validate = function(value, options) {
* @property UUIDV1
*/
var UUIDV1 = function() {
function UUIDV1() {
if (!(this instanceof UUIDV1)) return new UUIDV1();
ABSTRACT.apply(this, arguments);
};
util.inherits(UUIDV1, ABSTRACT);
}
inherits(UUIDV1, ABSTRACT);
UUIDV1.prototype.key = UUIDV1.key = 'UUIDV1';
UUIDV1.prototype.validate = function(value, options) {
UUIDV1.prototype.validate = function validate(value, options) {
if (!_.isString(value) || !Validator.isUUID(value) && (!options || !options.acceptStrings)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuid', value));
}
......@@ -716,14 +690,13 @@ UUIDV1.prototype.validate = function(value, options) {
* @property UUIDV4
*/
var UUIDV4 = function() {
function UUIDV4() {
if (!(this instanceof UUIDV4)) return new UUIDV4();
ABSTRACT.apply(this, arguments);
};
util.inherits(UUIDV4, ABSTRACT);
}
inherits(UUIDV4, ABSTRACT);
UUIDV4.prototype.key = UUIDV4.key = 'UUIDV4';
UUIDV4.prototype.validate = function(value, options) {
UUIDV4.prototype.validate = function validate(value, options) {
if (!_.isString(value) || !Validator.isUUID(value, 4) && (!options || !options.acceptStrings)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid uuidv4', value));
}
......@@ -773,14 +746,14 @@ UUIDV4.prototype.validate = function(value, options) {
* @property VIRTUAL
* @alias NONE
*/
var VIRTUAL = function(ReturnType, fields) {
function VIRTUAL(ReturnType, fields) {
if (!(this instanceof VIRTUAL)) return new VIRTUAL(ReturnType, fields);
if (typeof ReturnType === 'function') ReturnType = new ReturnType();
this.returnType = ReturnType;
this.fields = fields;
};
util.inherits(VIRTUAL, ABSTRACT);
}
inherits(VIRTUAL, ABSTRACT);
VIRTUAL.prototype.key = VIRTUAL.key = 'VIRTUAL';
......@@ -789,19 +762,20 @@ VIRTUAL.prototype.key = VIRTUAL.key = 'VIRTUAL';
*
* @property ENUM
*/
var ENUM = ABSTRACT.inherits(function(value) {
var options = typeof value === 'object' && !Array.isArray(value) && value || {
values: Array.prototype.slice.call(arguments).reduce(function(result, element) {
function ENUM(value) {
const options = typeof value === 'object' && !Array.isArray(value) && value || {
values: Array.prototype.slice.call(arguments).reduce((result, element) => {
return result.concat(Array.isArray(element) ? element : [element]);
}, [])
};
if (!(this instanceof ENUM)) return new ENUM(options);
this.values = options.values;
this.options = options;
});
}
inherits(ENUM, ABSTRACT);
ENUM.prototype.key = ENUM.key = 'ENUM';
ENUM.prototype.validate = function(value) {
ENUM.prototype.validate = function validate(value) {
if (!_.includes(this.values, value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid choice in %j', value, this.values));
}
......@@ -813,31 +787,29 @@ ENUM.prototype.validate = function(value) {
* An array of `type`, e.g. `DataTypes.ARRAY(DataTypes.DECIMAL)`. Only available in postgres.
* @property ARRAY
*/
var ARRAY = function(type) {
var options = _.isPlainObject(type) ? type : {
type: type
};
function ARRAY(type) {
const options = _.isPlainObject(type) ? type : {type};
if (!(this instanceof ARRAY)) return new ARRAY(options);
this.type = typeof options.type === 'function' ? new options.type() : options.type;
};
util.inherits(ARRAY, ABSTRACT);
}
inherits(ARRAY, ABSTRACT);
ARRAY.prototype.key = ARRAY.key = 'ARRAY';
ARRAY.prototype.toSql = function() {
ARRAY.prototype.toSql = function toSql() {
return this.type.toSql() + '[]';
};
ARRAY.prototype.validate = function(value) {
ARRAY.prototype.validate = function validate(value) {
if (!_.isArray(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid array', value));
}
return true;
};
ARRAY.is = function(obj, type) {
ARRAY.is = function is(obj, type) {
return obj instanceof ARRAY && obj.type instanceof type;
};
var helpers = {
const helpers = {
BINARY: [STRING, CHAR],
UNSIGNED: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL],
ZEROFILL: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL],
......@@ -858,37 +830,37 @@ var helpers = {
*
* ```js
* // Create a new point:
* var point = { type: 'Point', coordinates: [39.807222,-76.984722]};
* const point = { type: 'Point', coordinates: [39.807222,-76.984722]};
*
* User.create({username: 'username', geometry: point }).then(function(newUser) {
* User.create({username: 'username', geometry: point }).then(newUser => {
* ...
* });
*
* // Create a new linestring:
* var line = { type: 'LineString', 'coordinates': [ [100.0, 0.0], [101.0, 1.0] ] };
* const line = { type: 'LineString', 'coordinates': [ [100.0, 0.0], [101.0, 1.0] ] };
*
* User.create({username: 'username', geometry: line }).then(function(newUser) {
* User.create({username: 'username', geometry: line }).then(newUser => {
* ...
* });
*
* // Create a new polygon:
* var polygon = { type: 'Polygon', coordinates: [
* const polygon = { type: 'Polygon', coordinates: [
* [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
* [100.0, 1.0], [100.0, 0.0] ]
* ]};
*
* User.create({username: 'username', geometry: polygon }).then(function(newUser) {
* User.create({username: 'username', geometry: polygon }).then(newUser => {
* ...
* });
* // Create a new point with a custom SRID:
* var point = {
* const point = {
* type: 'Point',
* coordinates: [39.807222,-76.984722],
* crs: { type: 'name', properties: { name: 'EPSG:4326'} }
* };
*
* User.create({username: 'username', geometry: point }).then(function(newUser) {
* User.create({username: 'username', geometry: point }).then(newUser => {
* ...
* });
* ```
......@@ -896,23 +868,21 @@ var helpers = {
* @property GEOMETRY
*/
var GEOMETRY = ABSTRACT.inherits(function(type, srid) {
var options = _.isPlainObject(type) ? type : {
type: type,
srid: srid
};
function GEOMETRY(type, srid) {
const options = _.isPlainObject(type) ? type : {type, srid};
if (!(this instanceof GEOMETRY)) return new GEOMETRY(options);
this.options = options;
this.type = options.type;
this.srid = options.srid;
});
}
inherits(GEOMETRY, ABSTRACT);
GEOMETRY.prototype.key = GEOMETRY.key = 'GEOMETRY';
GEOMETRY.prototype.escape = false;
GEOMETRY.prototype.$stringify = function (value) {
GEOMETRY.prototype.$stringify = function $stringify(value) {
return 'GeomFromText(\'' + Wkt.convert(value) + '\')';
};
......@@ -921,32 +891,30 @@ GEOMETRY.prototype.$stringify = function (value) {
* @property GEOGRAPHY
*/
var GEOGRAPHY = ABSTRACT.inherits(function(type, srid) {
var options = _.isPlainObject(type) ? type : {
type: type,
srid: srid
};
function GEOGRAPHY(type, srid) {
const options = _.isPlainObject(type) ? type : {type, srid};
if (!(this instanceof GEOGRAPHY)) return new GEOGRAPHY(options);
this.options = options;
this.type = options.type;
this.srid = options.srid;
});
}
inherits(GEOGRAPHY, ABSTRACT);
GEOGRAPHY.prototype.key = GEOGRAPHY.key = 'GEOGRAPHY';
GEOGRAPHY.prototype.escape = false;
GEOGRAPHY.prototype.$stringify = function (value) {
GEOGRAPHY.prototype.$stringify = function $stringify(value) {
return 'GeomFromText(\'' + Wkt.convert(value) + '\')';
};
Object.keys(helpers).forEach(function (helper) {
helpers[helper].forEach(function (DataType) {
for (const helper of Object.keys(helpers)) {
for (const DataType of helpers[helper]) {
if (!DataType[helper]) {
Object.defineProperty(DataType, helper, {
get: function() {
var dataType = new DataType();
get() {
const dataType = new DataType();
if (typeof dataType[helper] === 'object') {
return dataType;
}
......@@ -954,45 +922,45 @@ Object.keys(helpers).forEach(function (helper) {
}
});
}
});
});
var dataTypes = {
ABSTRACT: ABSTRACT,
STRING: STRING,
CHAR: CHAR,
TEXT: TEXT,
NUMBER: NUMBER,
INTEGER: INTEGER,
BIGINT: BIGINT,
FLOAT: FLOAT,
TIME: TIME,
DATE: DATE,
DATEONLY: DATEONLY,
BOOLEAN: BOOLEAN,
NOW: NOW,
BLOB: BLOB,
DECIMAL: DECIMAL,
}
}
const dataTypes = {
ABSTRACT,
STRING,
CHAR,
TEXT,
NUMBER,
INTEGER,
BIGINT,
FLOAT,
TIME,
DATE,
DATEONLY,
BOOLEAN,
NOW,
BLOB,
DECIMAL,
NUMERIC: DECIMAL,
UUID: UUID,
UUIDV1: UUIDV1,
UUIDV4: UUIDV4,
HSTORE: HSTORE,
UUID,
UUIDV1,
UUIDV4,
HSTORE,
JSON: JSONTYPE,
JSONB: JSONB,
VIRTUAL: VIRTUAL,
ARRAY: ARRAY,
JSONB,
VIRTUAL,
ARRAY,
NONE: VIRTUAL,
ENUM: ENUM,
RANGE: RANGE,
REAL: REAL,
DOUBLE: DOUBLE,
ENUM,
RANGE,
REAL,
DOUBLE,
'DOUBLE PRECISION': DOUBLE,
GEOMETRY: GEOMETRY,
GEOGRAPHY: GEOGRAPHY
GEOMETRY,
GEOGRAPHY
};
_.each(dataTypes, function (dataType) {
_.each(dataTypes, dataType => {
dataType.types = {};
});
......
'use strict';
var _ = require('lodash');
const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
BaseTypes.DATE.types.mssql = [42];
BaseTypes.STRING.types.mssql = [231, 173];
......@@ -24,9 +25,13 @@ module.exports = function (BaseTypes) {
// BaseTypes.GEOMETRY.types.mssql = [240]; // not yet supported
BaseTypes.GEOMETRY.types.mssql = false;
var BLOB = BaseTypes.BLOB.inherits();
function BLOB(length) {
if (!(this instanceof BLOB)) return new BLOB(length);
BaseTypes.BLOB.apply(this, arguments);
}
inherits(BLOB, BaseTypes.BLOB);
BLOB.prototype.toSql = function() {
BLOB.prototype.toSql = function toSql() {
if (this._length) {
if (this._length.toLowerCase() === 'tiny') { // tiny = 2^8
warn('MSSQL does not support BLOB with the `length` = `tiny` option. `VARBINARY(256)` will be used instead.');
......@@ -37,13 +42,17 @@ module.exports = function (BaseTypes) {
return 'VARBINARY(MAX)';
};
BLOB.prototype.$hexify = function (hex) {
BLOB.prototype.$hexify = function $hexify(hex) {
return '0x' + hex;
};
var STRING = BaseTypes.STRING.inherits();
function STRING(length, binary) {
if (!(this instanceof STRING)) return new STRING(length, binary);
BaseTypes.STRING.apply(this, arguments);
}
inherits(STRING, BaseTypes.STRING);
STRING.prototype.toSql = function() {
STRING.prototype.toSql = function toSql() {
if (!this._binary) {
return 'NVARCHAR(' + this._length + ')';
} else{
......@@ -52,7 +61,7 @@ module.exports = function (BaseTypes) {
};
STRING.prototype.escape = false;
STRING.prototype.$stringify = function (value, options) {
STRING.prototype.$stringify = function $stringify(value, options) {
if (this._binary) {
return BLOB.prototype.$stringify(value);
} else {
......@@ -60,9 +69,13 @@ module.exports = function (BaseTypes) {
}
};
var TEXT = BaseTypes.TEXT.inherits();
function TEXT(length) {
if (!(this instanceof TEXT)) return new TEXT(length);
BaseTypes.TEXT.apply(this, arguments);
}
inherits(TEXT, BaseTypes.TEXT);
TEXT.prototype.toSql = function() {
TEXT.prototype.toSql = function toSql() {
// TEXT is deprecated in mssql and it would normally be saved as a non-unicode string.
// Using unicode is just future proof
if (this._length) {
......@@ -75,39 +88,55 @@ module.exports = function (BaseTypes) {
return 'NVARCHAR(MAX)';
};
var BOOLEAN = BaseTypes.BOOLEAN.inherits();
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
}
inherits(BOOLEAN, BaseTypes.BOOLEAN);
BOOLEAN.prototype.toSql = function() {
BOOLEAN.prototype.toSql = function toSql() {
return 'BIT';
};
var UUID = BaseTypes.UUID.inherits();
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
}
inherits(UUID, BaseTypes.UUID);
UUID.prototype.toSql = function() {
UUID.prototype.toSql = function toSql() {
return 'CHAR(36)';
};
var NOW = BaseTypes.NOW.inherits();
function NOW() {
if (!(this instanceof NOW)) return new NOW();
BaseTypes.NOW.apply(this, arguments);
}
inherits(NOW, BaseTypes.NOW);
NOW.prototype.toSql = function() {
NOW.prototype.toSql = function toSql() {
return 'GETDATE()';
};
var DATE = BaseTypes.DATE.inherits();
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function() {
DATE.prototype.toSql = function toSql() {
return 'DATETIME2';
};
DATE.prototype.$stringify = function (date, options) {
DATE.prototype.$stringify = function $stringify(date, options) {
date = this.$applyTimezone(date, options);
// mssql not allow +timezone datetime format
return date.format('YYYY-MM-DD HH:mm:ss.SSS');
};
var INTEGER = BaseTypes.INTEGER.inherits(function() {
if (!(this instanceof INTEGER)) return new INTEGER();
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
// MSSQL does not support any options for integer
......@@ -118,10 +147,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(INTEGER, BaseTypes.INTEGER);
var BIGINT = BaseTypes.BIGINT.inherits(function() {
if (!(this instanceof BIGINT)) return new BIGINT();
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
// MSSQL does not support any options for bigint
......@@ -132,10 +162,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(BIGINT, BaseTypes.BIGINT);
var REAL = BaseTypes.REAL.inherits(function() {
if (!(this instanceof REAL)) return new REAL();
function REAL(length, decimals) {
if (!(this instanceof REAL)) return new REAL(length, decimals);
BaseTypes.REAL.apply(this, arguments);
// MSSQL does not support any options for real
......@@ -146,10 +177,11 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(REAL, BaseTypes.REAL);
var FLOAT = BaseTypes.FLOAT.inherits(function() {
if (!(this instanceof FLOAT)) return new FLOAT();
function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments);
// MSSQL does only support lengths as option.
......@@ -169,32 +201,42 @@ module.exports = function (BaseTypes) {
warn('MSSQL does not support Float zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined;
}
});
}
inherits(FLOAT, BaseTypes.FLOAT);
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
var ENUM = BaseTypes.ENUM.inherits();
ENUM.prototype.toSql = function() {
ENUM.prototype.toSql = function toSql() {
return 'VARCHAR(255)';
};
var exports = {
BLOB: BLOB,
BOOLEAN: BOOLEAN,
ENUM: ENUM,
STRING: STRING,
UUID: UUID,
DATE: DATE,
NOW: NOW,
INTEGER: INTEGER,
BIGINT: BIGINT,
REAL: REAL,
FLOAT: FLOAT,
TEXT: TEXT
const exports = {
BLOB,
BOOLEAN,
ENUM,
STRING,
UUID,
DATE,
NOW,
INTEGER,
BIGINT,
REAL,
FLOAT,
TEXT
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
DataType.extend = function extend(oldType) {
return new DataType(oldType.options);
};
}
......
'use strict';
var wkx = require('wkx')
, _ = require('lodash')
, moment = require('moment-timezone');
const wkx = require('wkx');
const _ = require('lodash');
const moment = require('moment-timezone');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
module.exports = BaseTypes => {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
BaseTypes.DATE.types.mysql = ['DATETIME'];
......@@ -24,13 +25,17 @@ module.exports = function (BaseTypes) {
BaseTypes.REAL.types.mysql = ['DOUBLE'];
BaseTypes.DOUBLE.types.mysql = ['DOUBLE'];
var DATE = BaseTypes.DATE.inherits();
function DATE(length) {
if (!(this instanceof DATE)) return new Date(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function () {
DATE.prototype.toSql = function toSql() {
return 'DATETIME' + (this._length ? '(' + this._length + ')' : '');
};
DATE.prototype.$stringify = function (date, options) {
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) {
......@@ -40,7 +45,7 @@ module.exports = function (BaseTypes) {
return date.format('YYYY-MM-DD HH:mm:ss');
};
DATE.parse = function (value, options) {
DATE.parse = function parse(value, options) {
value = value.string();
if (value === null) {
......@@ -56,15 +61,20 @@ module.exports = function (BaseTypes) {
return value;
};
var UUID = BaseTypes.UUID.inherits();
function UUID() {
if (!(this instanceof UUID)) return new UUID();
BaseTypes.UUID.apply(this, arguments);
}
inherits(UUID, BaseTypes.UUID);
UUID.prototype.toSql = function() {
UUID.prototype.toSql = function toSql() {
return 'CHAR(36) BINARY';
};
var SUPPORTED_GEOMETRY_TYPES = ['POINT', 'LINESTRING', 'POLYGON'];
var GEOMETRY = BaseTypes.GEOMETRY.inherits(function() {
if (!(this instanceof GEOMETRY)) return new GEOMETRY();
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)) {
......@@ -74,9 +84,10 @@ module.exports = function (BaseTypes) {
} else {
throw new Error('Supported geometry types are: ' + SUPPORTED_GEOMETRY_TYPES.join(', '));
}
});
}
inherits(GEOMETRY, BaseTypes.GEOMETRY);
GEOMETRY.parse = GEOMETRY.prototype.parse = function(value) {
GEOMETRY.parse = GEOMETRY.prototype.parse = function parse(value) {
value = value.buffer();
//MySQL doesn't support POINT EMPTY, https://dev.mysql.com/worklog/task/?id=2381
......@@ -90,31 +101,37 @@ module.exports = function (BaseTypes) {
return wkx.Geometry.parse(value).toGeoJSON();
};
GEOMETRY.prototype.toSql = function() {
GEOMETRY.prototype.toSql = function toSql() {
return this.sqlType;
};
var ENUM = BaseTypes.ENUM.inherits();
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function (options) {
return 'ENUM(' + _.map(this.values, function(value) {
return options.escape(value);
}).join(', ') + ')';
ENUM.prototype.toSql = function toSql(options) {
return 'ENUM(' + _.map(this.values, value => options.escape(value)).join(', ') + ')';
};
BaseTypes.GEOMETRY.types.mysql = ['GEOMETRY'];
var exports = {
ENUM: ENUM,
DATE: DATE,
UUID: UUID,
GEOMETRY: GEOMETRY
const exports = {
ENUM,
DATE,
UUID,
GEOMETRY
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
DataType.extend = function extend(oldType) {
return new DataType(oldType.options);
};
}
......
......@@ -2,11 +2,12 @@
/*jshint -W110 */
var _ = require('lodash')
, wkx = require('wkx');
const _ = require('lodash');
const wkx = require('wkx');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'http://www.postgresql.org/docs/9.4/static/datatype.html');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'http://www.postgresql.org/docs/9.4/static/datatype.html');
BaseTypes.UUID.types.postgres = {
oids: [2950],
......@@ -33,9 +34,13 @@ module.exports = function (BaseTypes) {
array_oids: [1183]
};
var DECIMAL = BaseTypes.DECIMAL.inherits();
function DECIMAL(precision, scale) {
if (!(this instanceof DECIMAL)) return new DECIMAL(precision, scale);
BaseTypes.DECIMAL.apply(this, arguments);
}
inherits(DECIMAL, BaseTypes.DECIMAL);
DECIMAL.parse = function (value) {
DECIMAL.parse = function parse(value) {
return value;
};
......@@ -45,8 +50,13 @@ module.exports = function (BaseTypes) {
array_oids: [1231]
};
var STRING = BaseTypes.STRING.inherits();
STRING.prototype.toSql = function () {
function STRING(length, binary) {
if (!(this instanceof STRING)) return new STRING(length, binary);
BaseTypes.STRING.apply(this, arguments);
}
inherits(STRING, BaseTypes.STRING);
STRING.prototype.toSql = function toSql() {
if (this._binary) {
return 'BYTEA';
}
......@@ -58,9 +68,13 @@ module.exports = function (BaseTypes) {
array_oids: [1015]
};
var TEXT = BaseTypes.TEXT.inherits();
function TEXT(length) {
if (!(this instanceof TEXT)) return new TEXT(length);
BaseTypes.TEXT.apply(this, arguments);
}
inherits(TEXT, BaseTypes.TEXT);
TEXT.prototype.toSql = function() {
TEXT.prototype.toSql = function toSql() {
if (this._length) {
warn('PostgreSQL does not support TEXT with options. Plain `TEXT` will be used instead.');
this._length = undefined;
......@@ -73,9 +87,13 @@ module.exports = function (BaseTypes) {
array_oids: [1009]
};
var CHAR = BaseTypes.CHAR.inherits();
function CHAR(length, binary) {
if (!(this instanceof CHAR)) return new CHAR(length, binary);
BaseTypes.CHAR.apply(this, arguments);
}
inherits(CHAR, BaseTypes.CHAR);
CHAR.prototype.toSql = function() {
CHAR.prototype.toSql = function toSql() {
if (this._binary) {
return 'BYTEA';
}
......@@ -87,9 +105,13 @@ module.exports = function (BaseTypes) {
array_oids: [1002, 1014]
};
var BOOLEAN = BaseTypes.BOOLEAN.inherits();
function BOOLEAN() {
if (!(this instanceof BOOLEAN)) return new BOOLEAN();
BaseTypes.BOOLEAN.apply(this, arguments);
}
inherits(BOOLEAN, BaseTypes.BOOLEAN);
BOOLEAN.prototype.toSql = function() {
BOOLEAN.prototype.toSql = function toSql() {
return 'BOOLEAN';
};
......@@ -98,9 +120,13 @@ module.exports = function (BaseTypes) {
array_oids: [1000]
};
var DATE = BaseTypes.DATE.inherits();
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.prototype.toSql = function() {
DATE.prototype.toSql = function toSql() {
return 'TIMESTAMP WITH TIME ZONE';
};
......@@ -109,8 +135,8 @@ module.exports = function (BaseTypes) {
array_oids: [1185]
};
var INTEGER = BaseTypes.INTEGER.inherits(function() {
if (!(this instanceof INTEGER)) return new INTEGER();
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
// POSTGRES does not support any parameters for integer
......@@ -121,9 +147,10 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(INTEGER, BaseTypes.INTEGER);
INTEGER.parse = function (value) {
INTEGER.parse = function parse(value) {
return parseInt(value, 10);
};
......@@ -133,8 +160,8 @@ module.exports = function (BaseTypes) {
array_oids: [1007]
};
var BIGINT = BaseTypes.BIGINT.inherits(function() {
if (!(this instanceof BIGINT)) return new BIGINT();
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
// POSTGRES does not support any parameters for bigint
......@@ -145,7 +172,8 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(BIGINT, BaseTypes.BIGINT);
// int8
BaseTypes.BIGINT.types.postgres = {
......@@ -153,8 +181,8 @@ module.exports = function (BaseTypes) {
array_oids: [1016]
};
var REAL = BaseTypes.REAL.inherits(function() {
if (!(this instanceof REAL)) return new REAL();
function REAL(length, decimals) {
if (!(this instanceof REAL)) return new REAL(length, decimals);
BaseTypes.REAL.apply(this, arguments);
// POSTGRES does not support any parameters for real
......@@ -165,7 +193,8 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(REAL, BaseTypes.REAL);
// float4
BaseTypes.REAL.types.postgres = {
......@@ -173,8 +202,8 @@ module.exports = function (BaseTypes) {
array_oids: [1021]
};
var DOUBLE = BaseTypes.DOUBLE.inherits(function() {
if (!(this instanceof DOUBLE)) return new DOUBLE();
function DOUBLE(length, decimals) {
if (!(this instanceof DOUBLE)) return new DOUBLE(length, decimals);
BaseTypes.DOUBLE.apply(this, arguments);
// POSTGRES does not support any parameters for double
......@@ -185,7 +214,8 @@ module.exports = function (BaseTypes) {
this._unsigned = undefined;
this._zerofill = undefined;
}
});
}
inherits(DOUBLE, BaseTypes.DOUBLE);
// float8
BaseTypes.DOUBLE.types.postgres = {
......@@ -193,8 +223,8 @@ module.exports = function (BaseTypes) {
array_oids: [1022]
};
var FLOAT = BaseTypes.FLOAT.inherits(function() {
if (!(this instanceof FLOAT)) return new FLOAT();
function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments);
// POSTGRES does only support lengths as parameter.
......@@ -215,12 +245,17 @@ module.exports = function (BaseTypes) {
warn('PostgreSQL does not support FLOAT zerofill. `ZEROFILL` was removed.');
this._zerofill = undefined;
}
});
}
inherits(FLOAT, BaseTypes.FLOAT);
delete FLOAT.parse; // Float has no separate type in PG
var BLOB = BaseTypes.BLOB.inherits();
function BLOB(length) {
if (!(this instanceof BLOB)) return new BLOB(length);
BaseTypes.BLOB.apply(this, arguments);
}
inherits(BLOB, BaseTypes.BLOB);
BLOB.prototype.toSql = function() {
BLOB.prototype.toSql = function toSql() {
if (this._length) {
warn('PostgreSQL does not support BLOB (BYTEA) with options. Plain `BYTEA` will be used instead.');
this._length = undefined;
......@@ -228,7 +263,7 @@ module.exports = function (BaseTypes) {
return 'BYTEA';
};
BLOB.prototype.$hexify = function (hex) {
BLOB.prototype.$hexify = function $hexify(hex) {
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
return "E'\\\\x" + hex + "'";
};
......@@ -238,10 +273,14 @@ module.exports = function (BaseTypes) {
array_oids: [1001]
};
var GEOMETRY = BaseTypes.GEOMETRY.inherits();
function GEOMETRY(type, srid) {
if (!(this instanceof GEOMETRY)) return new GEOMETRY(type, srid);
BaseTypes.GEOMETRY.apply(this, arguments);
}
inherits(GEOMETRY, BaseTypes.GEOMETRY);
GEOMETRY.prototype.toSql = function() {
var result = this.key;
GEOMETRY.prototype.toSql = function toSql() {
let result = this.key;
if (this.type){
result += '(' + this.type;
......@@ -261,19 +300,23 @@ module.exports = function (BaseTypes) {
array_oids: []
};
GEOMETRY.parse = GEOMETRY.prototype.parse = function(value) {
var b = new Buffer(value, 'hex');
GEOMETRY.parse = GEOMETRY.prototype.parse = function parse(value) {
const b = new Buffer(value, 'hex');
return wkx.Geometry.parse(b).toGeoJSON();
};
GEOMETRY.prototype.$stringify = function (value) {
GEOMETRY.prototype.$stringify = function $stringify(value) {
return 'ST_GeomFromGeoJSON(\'' + JSON.stringify(value) + '\')';
};
var GEOGRAPHY = BaseTypes.GEOGRAPHY.inherits();
function GEOGRAPHY(type, srid) {
if (!(this instanceof GEOGRAPHY)) return new GEOGRAPHY(type, srid);
BaseTypes.GEOGRAPHY.apply(this, arguments);
}
inherits(GEOGRAPHY, BaseTypes.GEOGRAPHY);
GEOGRAPHY.prototype.toSql = function() {
var result = 'GEOGRAPHY';
GEOGRAPHY.prototype.toSql = function toSql() {
let result = 'GEOGRAPHY';
if (this.type){
result += '(' + this.type;
......@@ -293,17 +336,17 @@ module.exports = function (BaseTypes) {
array_oids: []
};
GEOGRAPHY.parse = GEOGRAPHY.prototype.parse = function(value) {
var b = new Buffer(value, 'hex');
GEOGRAPHY.parse = GEOGRAPHY.prototype.parse = function parse(value) {
const b = new Buffer(value, 'hex');
return wkx.Geometry.parse(b).toGeoJSON();
};
GEOGRAPHY.prototype.$stringify = function (value) {
GEOGRAPHY.prototype.$stringify = function $stringify(value) {
return 'ST_GeomFromGeoJSON(\'' + JSON.stringify(value) + '\')';
};
var hstore;
var HSTORE = BaseTypes.HSTORE.inherits(function () {
let hstore;
function HSTORE() {
if (!(this instanceof HSTORE)) return new HSTORE();
BaseTypes.HSTORE.apply(this, arguments);
......@@ -311,9 +354,10 @@ module.exports = function (BaseTypes) {
// All datatype files are loaded at import - make sure we don't load the hstore parser before a hstore is instantiated
hstore = require('./hstore');
}
});
}
inherits(HSTORE, BaseTypes.HSTORE);
HSTORE.parse = function (value) {
HSTORE.parse = function parse(value) {
if (!hstore) {
// All datatype files are loaded at import - make sure we don't load the hstore parser before a hstore is instantiated
hstore = require('./hstore');
......@@ -322,7 +366,7 @@ module.exports = function (BaseTypes) {
};
HSTORE.prototype.escape = false;
HSTORE.prototype.$stringify = function (value) {
HSTORE.prototype.$stringify = function $stringify(value) {
if (!hstore) {
// All datatype files are loaded at import - make sure we don't load the hstore parser before a hstore is instantiated
hstore = require('./hstore');
......@@ -335,7 +379,11 @@ module.exports = function (BaseTypes) {
array_oids: []
};
var RANGE = BaseTypes.RANGE.inherits();
function RANGE(subtype) {
if (!(this instanceof RANGE)) return new RANGE(subtype);
BaseTypes.RANGE.apply(this, arguments);
}
inherits(RANGE, BaseTypes.RANGE);
RANGE.oid_map = {
3904: 1007, // int4
......@@ -352,26 +400,26 @@ module.exports = function (BaseTypes) {
3927: 20,
};
var range = require('./range');
RANGE.parse = function (value, oid, getTypeParser) {
var parser = getTypeParser(RANGE.oid_map[oid]);
const range = require('./range');
RANGE.parse = function parse(value, oid, getTypeParser) {
const parser = getTypeParser(RANGE.oid_map[oid]);
return range.parse(value, parser);
};
RANGE.prototype.escape = false;
RANGE.prototype.$stringify = function (values, options) {
RANGE.prototype.$stringify = function $stringify(values, options) {
if (!Array.isArray(values)) {
return "'" + this.options.subtype.stringify(values, options) + "'::" +
this.toCastType();
}
var valuesStringified = values.map(function (value) {
const valuesStringified = values.map(value => {
if (this.options.subtype.stringify) {
return this.options.subtype.stringify(value, options);
} else {
return options.escape(value);
}
}, this);
});
// Array.map does not preserve extra array properties
valuesStringified.inclusive = values.inclusive;
......@@ -385,8 +433,8 @@ module.exports = function (BaseTypes) {
};
BaseTypes.ARRAY.prototype.escape = false;
BaseTypes.ARRAY.prototype.$stringify = function (values, options) {
var str = 'ARRAY[' + values.map(function (value) {
BaseTypes.ARRAY.prototype.$stringify = function $stringify(values, options) {
let str = 'ARRAY[' + values.map(value => {
if (this.type && this.type.stringify) {
value = this.type.stringify(value, options);
......@@ -404,31 +452,29 @@ module.exports = function (BaseTypes) {
return str;
};
var exports = {
DECIMAL: DECIMAL,
BLOB: BLOB,
STRING: STRING,
CHAR: CHAR,
TEXT: TEXT,
INTEGER: INTEGER,
BOOLEAN: BOOLEAN,
DATE: DATE,
BIGINT: BIGINT,
REAL: REAL,
const exports = {
DECIMAL,
BLOB,
STRING,
CHAR,
TEXT,
INTEGER,
BOOLEAN,
DATE,
BIGINT,
REAL,
'DOUBLE PRECISION': DOUBLE,
FLOAT: FLOAT,
GEOMETRY: GEOMETRY,
GEOGRAPHY: GEOGRAPHY,
HSTORE: HSTORE,
RANGE: RANGE
FLOAT,
GEOMETRY,
GEOGRAPHY,
HSTORE,
RANGE
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
return new DataType(oldType.options);
};
DataType.extend = oldType => new DataType(oldType.options);
}
});
......
'use strict';
var _ = require('lodash');
const _ = require('lodash');
const inherits = require('../../utils/inherits');
module.exports = function (BaseTypes) {
var warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
BaseTypes.DATE.types.sqlite = ['DATETIME'];
BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY'];
......@@ -23,8 +24,13 @@ module.exports = function (BaseTypes) {
BaseTypes.DOUBLE.types.sqlite = ['DOUBLE PRECISION'];
BaseTypes.GEOMETRY.types.sqlite = false;
var DATE = BaseTypes.DATE.inherits();
DATE.parse = function (date, options) {
function DATE(length) {
if (!(this instanceof DATE)) return new DATE(length);
BaseTypes.DATE.apply(this, arguments);
}
inherits(DATE, BaseTypes.DATE);
DATE.parse = function parse(date, options) {
if (date.indexOf('+') === -1) {
// For backwards compat. Dates inserted by sequelize < 2.0dev12 will not have a timestamp set
return new Date(date + options.timezone);
......@@ -33,8 +39,13 @@ module.exports = function (BaseTypes) {
}
};
var STRING = BaseTypes.STRING.inherits();
STRING.prototype.toSql = function() {
function STRING(length, binary) {
if (!(this instanceof STRING)) return new STRING(length, binary);
BaseTypes.STRING.apply(this, arguments);
}
inherits(STRING, BaseTypes.STRING);
STRING.prototype.toSql = function toSql() {
if (this._binary) {
return 'VARCHAR BINARY(' + this._length + ')';
} else {
......@@ -42,8 +53,13 @@ module.exports = function (BaseTypes) {
}
};
var TEXT = BaseTypes.TEXT.inherits();
TEXT.prototype.toSql = function() {
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;
......@@ -51,8 +67,13 @@ module.exports = function (BaseTypes) {
return 'TEXT';
};
var CHAR = BaseTypes.CHAR.inherits();
CHAR.prototype.toSql = function() {
function CHAR(length, binary) {
if (!(this instanceof CHAR)) return new CHAR(length, binary);
BaseTypes.CHAR.apply(this, arguments);
}
inherits(CHAR, BaseTypes.CHAR);
CHAR.prototype.toSql = function toSql() {
if (this._binary) {
return 'CHAR BINARY(' + this._length + ')';
} else {
......@@ -60,9 +81,14 @@ module.exports = function (BaseTypes) {
}
};
var NUMBER = BaseTypes.NUMBER.inherits();
NUMBER.prototype.toSql = function() {
var result = this.key;
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';
......@@ -81,71 +107,55 @@ module.exports = function (BaseTypes) {
return result;
};
var INTEGER = BaseTypes.INTEGER.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof INTEGER)) return new INTEGER(options);
BaseTypes.INTEGER.call(this, options);
});
INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.toSql = function() {
function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments);
}
inherits(INTEGER, BaseTypes.INTEGER);
INTEGER.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var BIGINT = BaseTypes.BIGINT.inherits(function(length) {
var options = typeof length === 'object' && length || {
length: length
};
if (!(this instanceof BIGINT)) return new BIGINT(options);
BaseTypes.BIGINT.call(this, options);
});
BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.toSql = function() {
function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments);
}
inherits(BIGINT, BaseTypes.BIGINT);
BIGINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var FLOAT = BaseTypes.FLOAT.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof FLOAT)) return new FLOAT(options);
BaseTypes.FLOAT.call(this, options);
});
FLOAT.prototype.key = FLOAT.key = 'FLOAT';
FLOAT.prototype.toSql = function() {
function FLOAT(length, decimals) {
if (!(this instanceof FLOAT)) return new FLOAT(length, decimals);
BaseTypes.FLOAT.apply(this, arguments);
}
inherits(FLOAT, BaseTypes.FLOAT);
FLOAT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
var DOUBLE = BaseTypes.DOUBLE.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof DOUBLE)) return new DOUBLE(options);
BaseTypes.DOUBLE.call(this, options);
});
DOUBLE.prototype.key = DOUBLE.key = 'DOUBLE PRECISION';
DOUBLE.prototype.toSql = function() {
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);
};
var REAL = BaseTypes.REAL.inherits(function(length, decimals) {
var options = typeof length === 'object' && length || {
length: length,
decimals: decimals
};
if (!(this instanceof REAL)) return new REAL(options);
BaseTypes.REAL.call(this, options);
});
REAL.prototype.key = REAL.key = 'REAL';
REAL.prototype.toSql = function() {
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);
};
[FLOAT, DOUBLE, REAL].forEach(function (floating) {
floating.parse = function (value) {
[FLOAT, DOUBLE, REAL].forEach(floating => {
floating.parse = function parse(value) {
if (_.isString(value)) {
if (value === 'NaN') {
return NaN;
......@@ -159,30 +169,38 @@ module.exports = function (BaseTypes) {
};
});
var ENUM = BaseTypes.ENUM.inherits();
function ENUM() {
if (!(this instanceof ENUM)) {
const obj = Object.create(ENUM.prototype);
ENUM.apply(obj, arguments);
return obj;
}
BaseTypes.ENUM.apply(this, arguments);
}
inherits(ENUM, BaseTypes.ENUM);
ENUM.prototype.toSql = function () {
ENUM.prototype.toSql = function toSql() {
return 'TEXT';
};
var exports = {
DATE: DATE,
STRING: STRING,
CHAR: CHAR,
NUMBER: NUMBER,
FLOAT: FLOAT,
REAL: REAL,
const exports = {
DATE,
STRING,
CHAR,
NUMBER,
FLOAT,
REAL,
'DOUBLE PRECISION': DOUBLE,
INTEGER: INTEGER,
BIGINT: BIGINT,
TEXT: TEXT,
ENUM: ENUM
INTEGER,
BIGINT,
TEXT,
ENUM
};
_.forIn(exports, function (DataType, key) {
_.forIn(exports, (DataType, key) => {
if (!DataType.key) DataType.key = key;
if (!DataType.extend) {
DataType.extend = function(oldType) {
DataType.extend = oldType => {
return new DataType(oldType.options);
};
}
......
'use strict';
const util = require('util');
const _ = require('lodash');
/** like util.inherits, but also copies over static properties */
function inherits(constructor, superConstructor) {
util.inherits(constructor, superConstructor); // Instance (prototype) methods
_.extend(constructor, superConstructor); // Static methods
}
module.exports = inherits;
module.exports.inherits = inherits;
module.exports.default = inherits;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!