Skip to content

Commit 785c1a0

Browse files
Merge branch 'main' into renovate/prettier-3.x
2 parents 101f0c8 + cad5757 commit 785c1a0

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

docs/querying/raw-queries.mdx

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const users = await User.findAll({
3737

3838
In the above example, we used a variable in our raw SQL. Thanks to the `sql` tag, Sequelize will automatically escape that variable to remove any risk of SQL injection.
3939

40-
Sequelize supports two different ways to pass variables in raw SQL: **Replacements** and **Bind Parameters**.
40+
Sequelize supports two different ways to pass variables in raw SQL: **Replacements** and **Bind Parameters**.
4141
Replacements and bind parameters are available in all querying methods, and can be used together in the same query.
4242

4343
### Replacements
@@ -60,7 +60,7 @@ The `replacements` option must contain all bound values, or Sequelize will throw
6060
```js
6161
import { QueryTypes } from '@sequelize/core';
6262

63-
// This query use positional replacements
63+
// This query uses positional replacements
6464
await sequelize.query('SELECT * FROM projects WHERE status = ?', {
6565
replacements: ['active'],
6666
});
@@ -136,7 +136,7 @@ await Project.findAll({
136136
});
137137
```
138138

139-
Sequelize does not currently support a way to [specify the DataType of a bind parameter](https://github.com/sequelize/sequelize/issues/14410).
139+
Sequelize does not currently support a way to [specify the DataType of a bind parameter](https://github.com/sequelize/sequelize/issues/14410).
140140
Until such a feature is implemented, you can cast your bind parameters if you need to change their DataType:
141141

142142
```js
@@ -259,6 +259,34 @@ await sequelize.query(sql`SELECT * FROM ${sql.identifier('projects')}`);
259259
SELECT * FROM "projects"
260260
```
261261

262+
You can specify more than one identifier:
263+
264+
```js
265+
import { sql } from '@sequelize/core';
266+
267+
await sequelize.query(sql`SELECT * FROM ${sql.identifier('public', 'users')}`);
268+
```
269+
270+
```sql
271+
-- The identifier quotes are dialect-specific, this is an example for PostgreSQL
272+
SELECT * FROM "public"."users"
273+
```
274+
275+
Using a Model class as an identifier will automatically use the table name of the Model:
276+
277+
```js
278+
import { User, sql } from '@sequelize/core';
279+
280+
class User extends Model {}
281+
282+
await sequelize.query(sql`SELECT * FROM ${sql.identifier(User)}`);
283+
```
284+
285+
```sql
286+
-- The identifier quotes are dialect-specific, this is an example for PostgreSQL
287+
SELECT * FROM "users"
288+
```
289+
262290
### `sql.list`
263291

264292
When using an array as a variable in a query, Sequelize will by default treat it as an SQL array:
@@ -293,6 +321,46 @@ Read more about this in [#15142](https://github.com/sequelize/sequelize/issues/1
293321

294322
:::
295323

324+
### `sql.join`
325+
326+
The `sql.join` function can be used to join multiple SQL fragments together.
327+
It is designed to be the equivalent of [`Array.prototype.join`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) for SQL fragments.
328+
329+
```ts
330+
const columns = [sql.identifier('name'), sql.identifier('funding')];
331+
332+
await sequelize.query(sql`SELECT ${sql.join(columns, ', ')} FROM projects`);
333+
```
334+
335+
```sql
336+
-- The identifier quotes are dialect-specific, this is an example for PostgreSQL
337+
SELECT "name", "funding" FROM projects
338+
```
339+
340+
Like with the `sql` tag, all non-sql values in the array passed to `sql.join` will be escaped:
341+
342+
```ts
343+
const values = ['active', 'pending'];
344+
345+
await sequelize.query(sql`SELECT * FROM projects WHERE status IN (${sql.join(values, ', ')})`);
346+
```
347+
348+
```sql
349+
SELECT * FROM projects WHERE status IN ('active', 'pending')
350+
```
351+
352+
The separator can also be any SQL fragment:
353+
354+
```ts
355+
const values = ['active', 'pending'];
356+
357+
await sequelize.query(sql`SELECT * FROM projects WHERE status IN ${sql.join(values, sql`, `)}`);
358+
```
359+
360+
```sql
361+
SELECT * FROM projects WHERE status IN ('active', 'pending')
362+
```
363+
296364
### `sql.where`
297365

298366
The `sql.where` function can be used to generate an SQL condition from a JavaScript object, using the same syntax as the [`where` option of the `findAll` method](./select-in-depth.md#applying-where-clauses).

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@
7171
"lint-staged": {
7272
"*.{js,mjs,cjs,ts,mts,cts}": "eslint --fix --report-unused-disable-directives",
7373
"*": "prettier --write --ignore-unknown"
74-
}
74+
},
75+
"packageManager": "[email protected]+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
7576
}

yarn.lock

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5806,15 +5806,10 @@ caniuse-api@^3.0.0:
58065806
lodash.memoize "^4.1.2"
58075807
lodash.uniq "^4.5.0"
58085808

5809-
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001578, caniuse-lite@^1.0.30001580:
5810-
version "1.0.30001585"
5811-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001585.tgz#0b4e848d84919c783b2a41c13f7de8ce96744401"
5812-
integrity sha512-yr2BWR1yLXQ8fMpdS/4ZZXpseBgE7o4g41x3a6AJOqZuOi+iE/WdJYAuZ6Y95i4Ohd2Y+9MzIWRR+uGABH4s3Q==
5813-
5814-
caniuse-lite@^1.0.30001646, caniuse-lite@^1.0.30001669:
5815-
version "1.0.30001679"
5816-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001679.tgz#18c573b72f72ba70822194f6c39e7888597f9e32"
5817-
integrity sha512-j2YqID/YwpLnKzCmBOS4tlZdWprXm3ZmQLBH9ZBXFOhoxLA46fwyBvx6toCBWBmnuwUY/qB3kEU6gFx8qgCroA==
5809+
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001578, caniuse-lite@^1.0.30001580, caniuse-lite@^1.0.30001646, caniuse-lite@^1.0.30001669:
5810+
version "1.0.30001702"
5811+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001702.tgz"
5812+
integrity sha512-LoPe/D7zioC0REI5W73PeR1e1MLCipRGq/VkovJnd6Df+QVqT+vT33OXCp8QUd7kA7RZrHWxb1B36OQKI/0gOA==
58185813

58195814
ccount@^1.0.0:
58205815
version "1.1.0"

0 commit comments

Comments
 (0)