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
4 changes: 2 additions & 2 deletions docs/other-topics/scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Project.init(
},
};
},
sequelize,
modelName: 'project',
},
sequelize,
modelName: 'project',
},
);
```
Expand Down
12 changes: 6 additions & 6 deletions docs/querying/json.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ we use the `[]` syntax for array index access, and `.` for object property acces
```ts
User.findAll({
where: {
'gameData.passwords[0]': 0451,
'gameData.passwords[0]': '0451',
},
});
```
Expand All @@ -94,10 +94,10 @@ produces
-- when accessing array indexes in postgres,
-- as Sequelize can sometimes use the "->" operator instead of "#>" for performance,
-- which uses a different syntax for indexes & keys
"gameData"#>ARRAY['passwords','0'] = '0451';
"gameData"#>ARRAY['passwords','0'] = '"0451"';

-- mysql & friends
JSON_EXTRACT(`gameData`, '$.passwords[0]') = '0451';
JSON_EXTRACT(`gameData`, '$.passwords[0]') = '"0451"';
```

## Comparing JSON values
Expand Down Expand Up @@ -228,10 +228,10 @@ produces

```sql
-- postgres
"jsonAttribute"->'age' = 18 AND "jsonAttribute"->0 = '"1"';
"jsonAttribute"->'age' = '18' AND "jsonAttribute"->0 = '"1"';

-- mysql & friends
JSON_EXTRACT(`jsonAttribute`, '$.age') = 18 AND JSON_EXTRACT(`jsonAttribute`, '$[0]') = '"1"';
JSON_EXTRACT(`jsonAttribute`, '$.age') = '18' AND JSON_EXTRACT(`jsonAttribute`, '$[0]') = '"1"';
```

## Path Segment Escaping
Expand Down Expand Up @@ -491,7 +491,7 @@ User.findAll({
SELECT * FROM "Users" WHERE "jsonAttribute"->'name' IS NULL OR "jsonAttribute"->'name' = 'null';

-- mysql & friends
SELECT * FROM "Users" WHERE JSON_EXTRACT(`jsonAttribute`, '$.name') IS NULL OR JSON_EXTRACT(`jsonAttribute`, '$.name') = '"null"';
SELECT * FROM "Users" WHERE JSON_EXTRACT(`jsonAttribute`, '$.name') IS NULL OR JSON_EXTRACT(`jsonAttribute`, '$.name') = 'null';
```

#### When to compare against either JSON `null` or SQL `NULL`
Expand Down
2 changes: 1 addition & 1 deletion docs/querying/operators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Post.findAll({
```

```sql
SELECT * FROM "posts" WHERE "title" LIKE 'Inflation is above 10\\%' ESCAPE '\\';
SELECT * FROM "posts" WHERE "title" LIKE 'Inflation is above 10\%' ESCAPE '\';
```

### Regexp Operator
Expand Down
12 changes: 6 additions & 6 deletions docs/querying/raw-queries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ SELECT * FROM "public"."users"
Using a Model class as an identifier will automatically use the table name of the Model:

```js
import { User, sql } from '@sequelize/core';
import { Model, sql } from '@sequelize/core';

class User extends Model {}

Expand Down Expand Up @@ -355,7 +355,7 @@ The separator can also be any SQL fragment:
```ts
const values = ['active', 'pending'];

await sequelize.query(sql`SELECT * FROM projects WHERE status IN ${sql.join(values, sql`, `)}`);
await sequelize.query(sql`SELECT * FROM projects WHERE status IN (${sql.join(values, sql`, `)})`);
```

```sql
Expand Down Expand Up @@ -530,7 +530,7 @@ In supported dialects, using `sql.uuidV4` and `sql.uuidV1` will generate the dia
In unsupported dialects, using these functions, except as [the default value of an attribute](../models/data-types.mdx#built-in-default-values-for-uuid), will throw an error.

```ts
sequelize.query(sql`INSERT INTO users (id) VALUES (${sql.uuidV4()})`);
sequelize.query(sql`INSERT INTO users (id) VALUES (${sql.uuidV4})`);
```

```sql
Expand All @@ -547,10 +547,10 @@ These functions are supported by the following dialects:
`sql.random` generates a random float between 0 and 1, using the dialect-specific function to do so.

```ts
await sequelize.query(sql`INSERT INTO lottery (ticket) VALUES (${sql.random()})`);
await sequelize.query(sql`INSERT INTO lottery (ticket) VALUES (${sql.random})`);
```

The SQL generated by `sql.random()` varies by dialect:
The SQL generated by `sql.random` varies by dialect:

<DialectTableFilter>

Expand All @@ -570,7 +570,7 @@ If you want to use the native `RANDOM()` function in SQLite, you can use [`sql`]

:::note About MSSQL

Using `sql.random()` in an `ORDER BY` clause is a common way to get random ordering,
Using `sql.random` in an `ORDER BY` clause is a common way to get random ordering,
however in MSSQL, it is better to use `NEWID()` for this purpose, as `RAND()` is evaluated only once per query, and will not give you random ordering.

:::
Expand Down
8 changes: 4 additions & 4 deletions docs/querying/select-in-depth.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ User.findAll({
});
```

You can remove that subquery, and revert back to a plain JOIN, by using the `subquery: false` option:
You can remove that subquery, and revert back to a plain JOIN, by using the `subQuery: false` option:

```ts
User.findAll({
Expand All @@ -596,7 +596,7 @@ User.findAll({
},
limit: 2,
// highlight-next-line
subquery: false,
subQuery: false,
});
```

Expand All @@ -617,7 +617,7 @@ User.findAll({
where: {
id: {
[Op.in]: sql`
SELECT DISTINCT "projects"."authorId" WHERE "projects"."name" = 'Project 1'
(SELECT DISTINCT "projects"."authorId" FROM "projects" WHERE "projects"."name" = 'Project 1')
`,
},
},
Expand Down Expand Up @@ -649,7 +649,7 @@ const author = await Author.findOne({
include: ['books'],
});

console.log(JSON.stringify(author[0]));
console.log(JSON.stringify(author));
```

Console output:
Expand Down
12 changes: 7 additions & 5 deletions docs/querying/sub-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import {
DataTypes,
InferCreationAttributes,
InferAttributes,
NonAttribute,
} from '@sequelize/core';
import {
Attribute,
AutoIncrement,
PrimaryKey,
NotNull,
HasMany,
} from '@sequelize/decorators-legacy';
} from '@sequelize/core/decorators-legacy';
import { SqliteDialect } from '@sequelize/sqlite3';

class Post extends Model<InferAttributes<Post>, InferCreationAttributes<Post>> {
Expand Down Expand Up @@ -70,6 +71,7 @@ class Reaction extends Model {
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: ':memory:',
pool: { max: 1, idle: Infinity, maxUses: Infinity },
models: [Post, Reaction],
});
```
Expand All @@ -84,11 +86,11 @@ import { sql, Op } from '@sequelize/core';
Post.findAll({
where: {
id: {
[Op.in]: sql`
[Op.in]: sql`(
SELECT DISTINCT "postId"
FROM "reactions" AS "reaction"
WHERE "reaction"."type" = 'Laugh'
`,
)`,
},
},
});
Expand All @@ -102,11 +104,11 @@ import { sql, Op } from '@sequelize/core';
function postHasReactionOfType(type: ReactionType) {
return {
id: {
[Op.in]: sql`
[Op.in]: sql`(
SELECT DISTINCT "postId"
FROM "reactions" AS "reaction"
WHERE "reaction"."type" = ${type}
`,
)`,
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions versioned_docs/version-6.x.x/other-topics/scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ Project.init(
},
};
},
sequelize,
modelName: 'project',
},
sequelize,
modelName: 'project',
},
);
```
Expand Down
Loading