Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data-types): CIDR, INET, MACADDR support for Postgres #9567

Merged
merged 2 commits into from Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/models-definition.md
Expand Up @@ -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.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.BIGINT) // Defined int8range range. PostgreSQL only.
Sequelize.RANGE(Sequelize.DATE) // Defines tstzrange range. PostgreSQL only.
Expand Down
50 changes: 49 additions & 1 deletion lib/data-types.js
Expand Up @@ -751,6 +751,51 @@ GEOGRAPHY.prototype._bindParam = function _bindParam(value, options) {
return 'GeomFromText(' + options.bindParam(wkx.Geometry.parseGeoJSON(value).toWkt()) + ')';
};

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 DataType of helpers[helper]) {
if (!DataType[helper]) {
Expand Down Expand Up @@ -952,7 +997,10 @@ const DataTypes = module.exports = {
DOUBLE,
'DOUBLE PRECISION': DOUBLE,
GEOMETRY,
GEOGRAPHY
GEOGRAPHY,
CIDR,
INET,
MACADDR
};

_.each(DataTypes, dataType => {
Expand Down
15 changes: 15 additions & 0 deletions lib/dialects/postgres/data-types.js
Expand Up @@ -21,6 +21,21 @@ module.exports = BaseTypes => {
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 = {
oids: [114],
array_oids: [199]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -32,7 +32,7 @@
"semver": "^5.5.0",
"toposort-class": "^1.0.1",
"uuid": "^3.2.1",
"validator": "^10.0.0",
"validator": "^10.4.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for isIPRange.

"wkx": "^0.4.5"
},
"devDependencies": {
Expand Down
30 changes: 30 additions & 0 deletions test/integration/data-types.test.js
Expand Up @@ -262,6 +262,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', () => {
const Type = new Sequelize.ENUM('hat', 'cat');

Expand Down