Other Data Types

Apart from the most common data types mentioned in the Model Basics guide, Sequelize provides several other data types.

Ranges (PostgreSQL only)

  1. DataTypes.RANGE(DataTypes.INTEGER) // int4range
  2. DataTypes.RANGE(DataTypes.BIGINT) // int8range
  3. DataTypes.RANGE(DataTypes.DATE) // tstzrange
  4. DataTypes.RANGE(DataTypes.DATEONLY) // daterange
  5. DataTypes.RANGE(DataTypes.DECIMAL) // numrange

Since range types have extra information for their bound inclusion/exclusion it’s not very straightforward to just use a tuple to represent them in javascript.

When supplying ranges as values you can choose from the following APIs:

  1. // defaults to inclusive lower bound, exclusive upper bound
  2. const range = [
  3. new Date(Date.UTC(2016, 0, 1)),
  4. new Date(Date.UTC(2016, 1, 1))
  5. ];
  6. // '["2016-01-01 00:00:00+00:00", "2016-02-01 00:00:00+00:00")'
  7. // control inclusion
  8. const range = [
  9. { value: new Date(Date.UTC(2016, 0, 1)), inclusive: false },
  10. { value: new Date(Date.UTC(2016, 1, 1)), inclusive: true },
  11. ];
  12. // '("2016-01-01 00:00:00+00:00", "2016-02-01 00:00:00+00:00"]'
  13. // composite form
  14. const range = [
  15. { value: new Date(Date.UTC(2016, 0, 1)), inclusive: false },
  16. new Date(Date.UTC(2016, 1, 1)),
  17. ];
  18. // '("2016-01-01 00:00:00+00:00", "2016-02-01 00:00:00+00:00")'
  19. const Timeline = sequelize.define('Timeline', {
  20. range: DataTypes.RANGE(DataTypes.DATE)
  21. });
  22. await Timeline.create({ range });

However, retrieved range values always come in the form of an array of objects. For example, if the stored value is ("2016-01-01 00:00:00+00:00", "2016-02-01 00:00:00+00:00"], after a finder query you will get:

  1. [
  2. { value: Date, inclusive: false },
  3. { value: Date, inclusive: true }
  4. ]

You will need to call reload() after updating an instance with a range type or use the returning: true option.

Special Cases

  1. // empty range:
  2. Timeline.create({ range: [] }); // range = 'empty'
  3. // Unbounded range:
  4. Timeline.create({ range: [null, null] }); // range = '[,)'
  5. // range = '[,"2016-01-01 00:00:00+00:00")'
  6. Timeline.create({ range: [null, new Date(Date.UTC(2016, 0, 1))] });
  7. // Infinite range:
  8. // range = '[-infinity,"2016-01-01 00:00:00+00:00")'
  9. Timeline.create({ range: [-Infinity, new Date(Date.UTC(2016, 0, 1))] });

BLOBs

  1. DataTypes.BLOB // BLOB (bytea for PostgreSQL)
  2. DataTypes.BLOB('tiny') // TINYBLOB (bytea for PostgreSQL)
  3. DataTypes.BLOB('medium') // MEDIUMBLOB (bytea for PostgreSQL)
  4. DataTypes.BLOB('long') // LONGBLOB (bytea for PostgreSQL)

The blob datatype allows you to insert data both as strings and as buffers. However, when a blob is retrieved from database with Sequelize, it will always be retrieved as a buffer.

ENUMs

The ENUM is a data type that accepts only a few values, specified as a list.

  1. DataTypes.ENUM('foo', 'bar') // An ENUM with allowed values 'foo' and 'bar'

ENUMs can also be specified with the values field of the column definition, as follows:

  1. sequelize.define('foo', {
  2. states: {
  3. type: DataTypes.ENUM,
  4. values: ['active', 'pending', 'deleted']
  5. }
  6. });

JSON (SQLite, MySQL, MariaDB and PostgreSQL only)

The DataTypes.JSON data type is only supported for SQLite, MySQL, MariaDB and PostgreSQL. However, there is a minimum support for MSSQL (see below).

Note for PostgreSQL

The JSON data type in PostgreSQL stores the value as plain text, as opposed to binary representation. If you simply want to store and retrieve a JSON representation, using JSON will take less disk space and less time to build from its input representation. However, if you want to do any operations on the JSON value, you should prefer the JSONB data type described below.

JSONB (PostgreSQL only)

PostgreSQL also supports a JSONB data type: DataTypes.JSONB. It can be queried in three different ways:

  1. // Nested object
  2. await Foo.findOne({
  3. where: {
  4. meta: {
  5. video: {
  6. url: {
  7. [Op.ne]: null
  8. }
  9. }
  10. }
  11. }
  12. });
  13. // Nested key
  14. await Foo.findOne({
  15. where: {
  16. "meta.audio.length": {
  17. [Op.gt]: 20
  18. }
  19. }
  20. });
  21. // Containment
  22. await Foo.findOne({
  23. where: {
  24. meta: {
  25. [Op.contains]: {
  26. site: {
  27. url: 'http://google.com'
  28. }
  29. }
  30. }
  31. }
  32. });

MSSQL

MSSQL does not have a JSON data type, however it does provide some support for JSON stored as strings through certain functions since SQL Server 2016. Using these functions, you will be able to query the JSON stored in the string, but any returned values will need to be parsed seperately.

  1. // ISJSON - to test if a string contains valid JSON
  2. await User.findAll({
  3. where: sequelize.where(sequelize.fn('ISJSON', sequelize.col('userDetails')), 1)
  4. })
  5. // JSON_VALUE - extract a scalar value from a JSON string
  6. await User.findAll({
  7. attributes: [[ sequelize.fn('JSON_VALUE', sequelize.col('userDetails'), '$.address.Line1'), 'address line 1']]
  8. })
  9. // JSON_VALUE - query a scalar value from a JSON string
  10. await User.findAll({
  11. where: sequelize.where(sequelize.fn('JSON_VALUE', sequelize.col('userDetails'), '$.address.Line1'), '14, Foo Street')
  12. })
  13. // JSON_QUERY - extract an object or array
  14. await User.findAll({
  15. attributes: [[ sequelize.fn('JSON_QUERY', sequelize.col('userDetails'), '$.address'), 'full address']]
  16. })

Others

  1. DataTypes.ARRAY(/* DataTypes.SOMETHING */) // Defines an array of DataTypes.SOMETHING. PostgreSQL only.
  2. DataTypes.CIDR // CIDR PostgreSQL only
  3. DataTypes.INET // INET PostgreSQL only
  4. DataTypes.MACADDR // MACADDR PostgreSQL only
  5. DataTypes.GEOMETRY // Spatial column. PostgreSQL (with PostGIS) or MySQL only.
  6. DataTypes.GEOMETRY('POINT') // Spatial column with geometry type. PostgreSQL (with PostGIS) or MySQL only.
  7. DataTypes.GEOMETRY('POINT', 4326) // Spatial column with geometry type and SRID. PostgreSQL (with PostGIS) or MySQL only.