Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/core-concepts/model-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ DataTypes.STRING // VARCHAR(255)
DataTypes.STRING(1234) // VARCHAR(1234)
DataTypes.STRING.BINARY // VARCHAR BINARY
DataTypes.TEXT // TEXT
DataTypes.TEXT('tiny') // TINYTEXT
DataTypes.TEXT('tiny') // tiny for TINYTEXT, medium for MEDIUMTEXT and long for LONGTEXT
DataTypes.CITEXT // CITEXT PostgreSQL and SQLite only.
DataTypes.TSVECTOR // TSVECTOR PostgreSQL only.
```
Expand Down
17 changes: 11 additions & 6 deletions versioned_docs/version-6.x.x/core-concepts/model-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,23 @@ sequelize.define('User', {
});
```

Some special values, such as `DataTypes.NOW`, are also accepted:
It is possible to use `fn` to use a native SQL function as the default value:

```js
sequelize.define('Foo', {
bar: {
type: DataTypes.DATETIME,
defaultValue: DataTypes.NOW
// This way, the current date/time will be used to populate this column (at the moment of insertion)
myUuid: {
type: DataTypes.UUID,
defaultValue: fn('uuid_generate_v4'),
}
});
```


Sequelize provides a series of built-in default values you can use:

- [`DataTypes.NOW`](../other-topics/other-data-types.mdx#built-in-default-values-for-dates)
- [`DataTypes.UUIDV1`, `DataTypes.UUIDV4`](../other-topics/other-data-types.mdx#built-in-default-values-for-uuid)

## Data Types

Every column you define in your model must have a data type. Sequelize provides [a lot of built-in data types](https://github.com/sequelize/sequelize/blob/main/src/data-types.js). To access a built-in data type, you must import `DataTypes`:
Expand All @@ -334,7 +339,7 @@ DataTypes.STRING // VARCHAR(255)
DataTypes.STRING(1234) // VARCHAR(1234)
DataTypes.STRING.BINARY // VARCHAR BINARY
DataTypes.TEXT // TEXT
DataTypes.TEXT('tiny') // TINYTEXT
DataTypes.TEXT('tiny') // tiny for TINYTEXT, medium for MEDIUMTEXT and long for LONGTEXT
DataTypes.CITEXT // CITEXT PostgreSQL and SQLite only.
DataTypes.TSVECTOR // TSVECTOR PostgreSQL only.
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,68 @@ WHERE (
)
```

### Querying JSON

JSON can be queried in three different ways:

```js
// Nested object
await Foo.findOne({
where: {
meta: {
video: {
url: {
[Op.ne]: null
}
}
}
}
});
// Nested key
await Foo.findOne({
where: {
"meta.audio.length": {
[Op.gt]: 20
}
}
});
// Containment
await Foo.findOne({
where: {
meta: {
[Op.contains]: {
site: {
url: 'http://google.com'
}
}
}
}
});
```

#### 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.

```js
// ISJSON - to test if a string contains valid JSON
await User.findAll({
where: sequelize.where(sequelize.fn('ISJSON', sequelize.col('userDetails')), 1)
});
// JSON_VALUE - extract a scalar value from a JSON string
await User.findAll({
attributes: [[ sequelize.fn('JSON_VALUE', sequelize.col('userDetails'), '$.address.Line1'), 'address line 1']]
});
// JSON_VALUE - query a scalar value from a JSON string
await User.findAll({
where: sequelize.where(sequelize.fn('JSON_VALUE', sequelize.col('userDetails'), '$.address.Line1'), '14, Foo Street')
});
// JSON_QUERY - extract an object or array
await User.findAll({
attributes: [[ sequelize.fn('JSON_QUERY', sequelize.col('userDetails'), '$.address'), 'full address']]
});
```

### Postgres-only Range Operators

Range types can be queried with all supported operators.
Expand Down
Loading