Sequelize.DATE// DATETIME for mysql / sqlite, TIMESTAMP WITH TIME ZONE for postgres
Sequelize.DATE(6)// DATETIME(6) for mysql 5.6.4+. Fractional seconds support with up to 6 digits of precision
Sequelize.DATEONLY// DATE without time.
Sequelize.BOOLEAN// TINYINT(1)
Sequelize.ENUM('value 1','value 2')// An ENUM with allowed values 'value 1' and 'value 2'
Sequelize.ARRAY(Sequelize.TEXT)// Defines an array. PostgreSQL only.
Sequelize.ARRAY(Sequelize.ENUM)// Defines an array of ENUM. PostgreSQL only.
Sequelize.JSON// JSON column. PostgreSQL, SQLite and MySQL only.
Sequelize.JSONB// JSONB column. PostgreSQL only.
Sequelize.BLOB// BLOB (bytea for PostgreSQL)
Sequelize.BLOB('tiny')// TINYBLOB (bytea for PostgreSQL. Other options are medium and long)
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.ARRAY(Sequelize.RANGE(Sequelize.DATE))// Defines array of tstzrange ranges. PostgreSQL only.
Sequelize.GEOMETRY// Spatial column. PostgreSQL (with PostGIS) or MySQL only.
Sequelize.GEOMETRY('POINT')// Spatial column with geometry type. PostgreSQL (with PostGIS) or MySQL only.
Sequelize.GEOMETRY('POINT',4326)// Spatial column with geometry type and SRID. PostgreSQL (with PostGIS) or MySQL only.
```
The BLOB datatype allows you to insert data both as strings and as buffers. When you do a find or findAll on a model which has a BLOB column, that data will always be returned as a buffer.
If you are working with the PostgreSQL TIMESTAMP WITHOUT TIME ZONE and you need to parse it to a different timezone, please use the pg library's own parser:
// e.g., UTC offset. Use any offset that you would like.
});
```
In addition to the type mentioned above, integer, bigint, float and double also support unsigned and zerofill properties, which can be combined in any order:
Most likely the type you are trying to implement is already included in [DataTypes](/manual/tutorial/data-types.html). If a new datatype is not included, this manual will show how to write it yourself.
Sequelize doesn't create new datatypes in the database. This tutorial explains how to make Sequelize recognize new datatypes and assumes that those new datatypes are already created in the database.
To extend Sequelize datatypes, do it before any instance is created. This example creates a dummy `NEWTYPE` that replicates the built-in datatype `Sequelize.INTEGER(11).ZEROFILL.UNSIGNED`.
// Optional, disable escaping after stringifier. Not recommended.
// Warning: disables Sequelize protection against SQL injections
//DataTypes.NEWTYPE.escape = false
// Optional, parser for values received from the database
DataTypes.NEWTYPE.parse=functionparse(value){
returnNumber.parseInt(value)
}
// For convenience
Sequelize.NEWTYPE=DataTypes.NEWTYPE
}
```
After creating this new datatype, you need to map this datatype in each database dialect and make some adjustments.
## PostgreSQL
Let's say the name of the new datatype is `pg_new_type` in the postgres database. That name has to be mapped to `DataTypes.NEWTYPE`. Additionally, it is required to create a child postgres-specific datatype.
// Mandatory, create, override or reassign a postgres-specific parser
//PgTypes.NEWTYPE.parse = value => value;
PgTypes.NEWTYPE.parse=BaseTypes.NEWTYPE.parse;
// Optional, add or override methods of the postgres-specific datatype
// like toSql, escape, validate, _stringify, _sanitize...
}
```
### Ranges
After a new range type has been [defined in postgres](https://www.postgresql.org/docs/current/static/rangetypes.html#RANGETYPES-DEFINING), it is trivial to add it to Sequelize.
In this example the name of the postgres range type is `newtype_range` and the name of the underlying postgres datatype is `pg_new_type`. The key of `subtypes` and `castTypes` is the key of the Sequelize datatype `DataTypes.NEWTYPE.key`, in lower case.
```js
// myproject/lib/sequelize-additions.js
modules.exports = function sequelizeAdditions(Sequelize) {
DataTypes = Sequelize.DataTypes
/*
* Create new types
*/
...
/*
* Map new types
*/
...
/*
* Add suport for ranges
*/
// Add postgresql range, newtype comes from DataType.NEWTYPE.key in lower case
The comment option can also be used on a table, see [model configuration][0]
The comment option can also be used on a table, see [model configuration][0].
## Timestamps
...
...
@@ -110,170 +110,6 @@ module.exports = {
If you do not want timestamps on your models, only want some timestamps, or you are working with an existing database where the columns are named something else, jump straight on to [configuration ][0]to see how to do that.
## Data types
Below are some of the datatypes supported by sequelize. For a full and updated list, see [DataTypes](/variable/index.html#static-variable-DataTypes).
```js
Sequelize.STRING// VARCHAR(255)
Sequelize.STRING(1234)// VARCHAR(1234)
Sequelize.STRING.BINARY// VARCHAR BINARY
Sequelize.TEXT// TEXT
Sequelize.TEXT('tiny')// TINYTEXT
Sequelize.CITEXT// CITEXT PostgreSQL and SQLite only.
Sequelize.DATE// DATETIME for mysql / sqlite, TIMESTAMP WITH TIME ZONE for postgres
Sequelize.DATE(6)// DATETIME(6) for mysql 5.6.4+. Fractional seconds support with up to 6 digits of precision
Sequelize.DATEONLY// DATE without time.
Sequelize.BOOLEAN// TINYINT(1)
Sequelize.ENUM('value 1','value 2')// An ENUM with allowed values 'value 1' and 'value 2'
Sequelize.ARRAY(Sequelize.TEXT)// Defines an array. PostgreSQL only.
Sequelize.ARRAY(Sequelize.ENUM)// Defines an array of ENUM. PostgreSQL only.
Sequelize.JSON// JSON column. PostgreSQL, SQLite and MySQL only.
Sequelize.JSONB// JSONB column. PostgreSQL only.
Sequelize.BLOB// BLOB (bytea for PostgreSQL)
Sequelize.BLOB('tiny')// TINYBLOB (bytea for PostgreSQL. Other options are medium and long)
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.ARRAY(Sequelize.RANGE(Sequelize.DATE))// Defines array of tstzrange ranges. PostgreSQL only.
Sequelize.GEOMETRY// Spatial column. PostgreSQL (with PostGIS) or MySQL only.
Sequelize.GEOMETRY('POINT')// Spatial column with geometry type. PostgreSQL (with PostGIS) or MySQL only.
Sequelize.GEOMETRY('POINT',4326)// Spatial column with geometry type and SRID. PostgreSQL (with PostGIS) or MySQL only.
```
The BLOB data type allows you to insert data both as strings and as buffers. When you do a find or findAll on a model which has a BLOB column, that data will always be returned as a buffer.
If you are working with the PostgreSQL TIMESTAMP WITHOUT TIME ZONE and you need to parse it to a different timezone, please use the pg library's own parser:
// e.g., UTC offset. Use any offset that you would like.
});
```
In addition to the type mentioned above, integer, bigint, float and double also support unsigned and zerofill properties, which can be combined in any order: