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

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