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

Commit 2c3a6e71 by Gabe Gorelick Committed by Sushant

feat(data-types): network types for Postgres (#9571)

1 parent b903384e
...@@ -150,6 +150,10 @@ Sequelize.BLOB('tiny') // TINYBLOB (bytea for PostgreSQL. Other o ...@@ -150,6 +150,10 @@ Sequelize.BLOB('tiny') // TINYBLOB (bytea for PostgreSQL. Other o
Sequelize.UUID // UUID datatype for PostgreSQL and SQLite, CHAR(36) BINARY for MySQL (use defaultValue: Sequelize.UUIDV1 or Sequelize.UUIDV4 to make sequelize generate the ids automatically) Sequelize.UUID // UUID datatype for PostgreSQL and SQLite, CHAR(36) BINARY for MySQL (use defaultValue: Sequelize.UUIDV1 or Sequelize.UUIDV4 to make sequelize generate the ids automatically)
Sequelize.CIDR // CIDR datatype for PostgreSQL
Sequelize.INET // INET datatype for PostgreSQL
Sequelize.MACADDR // MACADDR datatype for PostgreSQL
Sequelize.RANGE(Sequelize.INTEGER) // Defines int4range range. PostgreSQL only. Sequelize.RANGE(Sequelize.INTEGER) // Defines int4range range. PostgreSQL only.
Sequelize.RANGE(Sequelize.BIGINT) // Defined int8range range. PostgreSQL only. Sequelize.RANGE(Sequelize.BIGINT) // Defined int8range range. PostgreSQL only.
Sequelize.RANGE(Sequelize.DATE) // Defines tstzrange range. PostgreSQL only. Sequelize.RANGE(Sequelize.DATE) // Defines tstzrange range. PostgreSQL only.
......
...@@ -726,6 +726,51 @@ GEOGRAPHY.prototype._stringify = function _stringify(value, options) { ...@@ -726,6 +726,51 @@ GEOGRAPHY.prototype._stringify = function _stringify(value, options) {
return 'GeomFromText(' + options.escape(Wkt.convert(value)) + ')'; return 'GeomFromText(' + options.escape(Wkt.convert(value)) + ')';
}; };
function CIDR() {
if (!(this instanceof CIDR)) return new CIDR();
}
inherits(CIDR, ABSTRACT);
CIDR.prototype.key = CIDR.key = 'CIDR';
CIDR.prototype.validate = function validate(value) {
if (!_.isString(value) || !Validator.isIPRange(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid CIDR', value));
}
return true;
};
function INET() {
if (!(this instanceof INET)) return new INET();
}
inherits(INET, ABSTRACT);
INET.prototype.key = INET.key = 'INET';
INET.prototype.validate = function validate(value) {
if (!_.isString(value) || !Validator.isIP(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid INET', value));
}
return true;
};
function MACADDR() {
if (!(this instanceof MACADDR)) return new MACADDR();
}
inherits(MACADDR, ABSTRACT);
MACADDR.prototype.key = MACADDR.key = 'MACADDR';
MACADDR.prototype.validate = function validate(value) {
if (!_.isString(value) || !Validator.isMACAddress(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid MACADDR', value));
}
return true;
};
for (const helper of Object.keys(helpers)) { for (const helper of Object.keys(helpers)) {
for (const DataType of helpers[helper]) { for (const DataType of helpers[helper]) {
if (!DataType[helper]) { if (!DataType[helper]) {
...@@ -927,7 +972,10 @@ const DataTypes = module.exports = { ...@@ -927,7 +972,10 @@ const DataTypes = module.exports = {
DOUBLE, DOUBLE,
'DOUBLE PRECISION': DOUBLE, 'DOUBLE PRECISION': DOUBLE,
GEOMETRY, GEOMETRY,
GEOGRAPHY GEOGRAPHY,
CIDR,
INET,
MACADDR
}; };
_.each(DataTypes, dataType => { _.each(DataTypes, dataType => {
......
...@@ -21,6 +21,21 @@ module.exports = BaseTypes => { ...@@ -21,6 +21,21 @@ module.exports = BaseTypes => {
array_oids: [2951] array_oids: [2951]
}; };
BaseTypes.CIDR.types.postgres = {
oids: [650],
array_oids: [651]
};
BaseTypes.INET.types.postgres = {
oids: [869],
array_oids: [1041]
};
BaseTypes.MACADDR.types.postgres = {
oids: [829],
array_oids: [1040]
};
BaseTypes.JSON.types.postgres = { BaseTypes.JSON.types.postgres = {
oids: [114], oids: [114],
array_oids: [199] array_oids: [199]
......
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
"terraformer-wkt-parser": "^1.1.2", "terraformer-wkt-parser": "^1.1.2",
"toposort-class": "^1.0.1", "toposort-class": "^1.0.1",
"uuid": "^3.2.1", "uuid": "^3.2.1",
"validator": "^9.4.1", "validator": "^10.4.0",
"wkx": "^0.4.1" "wkx": "^0.4.1"
}, },
"devDependencies": { "devDependencies": {
......
...@@ -242,6 +242,36 @@ describe(Support.getTestDialectTeaser('DataTypes'), () => { ...@@ -242,6 +242,36 @@ describe(Support.getTestDialectTeaser('DataTypes'), () => {
} }
}); });
it('calls parse and stringify for CIDR', () => {
const Type = new Sequelize.CIDR();
if (['postgres'].indexOf(dialect) !== -1) {
return testSuccess(Type, '10.1.2.3/32');
} else {
testFailure(Type);
}
});
it('calls parse and stringify for INET', () => {
const Type = new Sequelize.INET();
if (['postgres'].indexOf(dialect) !== -1) {
return testSuccess(Type, '127.0.0.1');
} else {
testFailure(Type);
}
});
it('calls parse and stringify for MACADDR', () => {
const Type = new Sequelize.MACADDR();
if (['postgres'].indexOf(dialect) !== -1) {
return testSuccess(Type, '01:23:45:67:89:ab');
} else {
testFailure(Type);
}
});
it('calls parse and stringify for ENUM', () => { it('calls parse and stringify for ENUM', () => {
const Type = new Sequelize.ENUM('hat', 'cat'); const Type = new Sequelize.ENUM('hat', 'cat');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!