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

docs: thoroughly document DataTypes #11

Merged
merged 16 commits into from
Mar 19, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = {
'prefer-object-has-own': 'off',
'unicorn/prefer-at': 'off',
'unicorn/prefer-string-replace-all': 'off',

'unicorn/prefer-spread': 'off',
'unicorn/no-useless-undefined': 'off',
},
overrides: [{
files: ['*.mdx/**', '*.md/**'],
Expand Down
16 changes: 10 additions & 6 deletions docs/core-concepts/model-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,22 @@ 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'),
ephys marked this conversation as resolved.
Show resolved Hide resolved
}
});
```

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 Down Expand Up @@ -402,7 +406,7 @@ For UUIDs, use `DataTypes.UUID`. It becomes the `UUID` data type for PostgreSQL

### Others

There are other data types, covered in a [separate guide](../other-topics/other-data-types.md).
There are other data types, covered in a [separate guide](../other-topics/other-data-types.mdx).

## Column Options

Expand Down
69 changes: 68 additions & 1 deletion docs/core-concepts/model-querying-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,78 @@ 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.

Keep in mind, the provided range value can [define the bound inclusion/exclusion](../other-topics/other-data-types.md#ranges-postgresql-only) as well.
Keep in mind, the provided range value can [define the bound inclusion/exclusion](../other-topics/other-data-types.mdx#ranges-postgresql-only) as well.

```js
[Op.contains]: 2, // @> '2'::integer (PG range contains element operator)
Expand Down
2 changes: 1 addition & 1 deletion docs/other-topics/extending-data-types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Extending Data Types

Most likely the type you are trying to implement is already included in our built-in [DataTypes](./other-data-types.md). If a new datatype is not included, this manual will show how to write it yourself.
Most likely the type you are trying to implement is already included in our built-in [DataTypes](./other-data-types.mdx). 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.

Expand Down
192 changes: 0 additions & 192 deletions docs/other-topics/other-data-types.md

This file was deleted.

Loading