Skip to content

Commit 6f32643

Browse files
mccabemjsushantdhiman
authored andcommitted
feat: sequelize.random() (#8847)
1 parent 40f7d21 commit 6f32643

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

docs/querying.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ Subtask.findAll({
429429

430430
// Will order by age ascending assuming ascending is the default order when direction is omitted
431431
order: sequelize.col('age')
432+
433+
// Will order by age randomly based on the dialect (instead of fn('RAND') or fn('RANDOM'))
434+
order: sequelize.random()
432435
})
433436
```
434437

lib/sequelize.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,20 @@ class Sequelize {
772772
databaseVersion(options) {
773773
return this.getQueryInterface().databaseVersion(options);
774774
}
775+
776+
/**
777+
* Get the fn for random based on the dialect
778+
*
779+
* @return {Sequelize.fn}
780+
*/
781+
random() {
782+
const dialect = this.getDialect();
783+
if (_.includes(['postgres', 'sqlite'], dialect)) {
784+
return this.fn('RANDOM');
785+
} else {
786+
return this.fn('RAND');
787+
}
788+
}
775789

776790
/**
777791
* Creates an object representing a database function. This can be used in search queries, both in where and order parts, and as default values in column definitions.

test/unit/sql/order.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
344344
postgres: 'SELECT "Subtask"."id", "Subtask"."name", "Subtask"."createdAt", "Task"."id" AS "Task.id", "Task"."name" AS "Task.name", "Task"."created_at" AS "Task.createdAt", "Task->Project"."id" AS "Task.Project.id", "Task->Project"."name" AS "Task.Project.name", "Task->Project"."created_at" AS "Task.Project.createdAt" FROM "subtask" AS "Subtask" INNER JOIN "task" AS "Task" ON "Subtask"."task_id" = "Task"."id" INNER JOIN "project" AS "Task->Project" ON "Task"."project_id" = "Task->Project"."id" ORDER BY "Task->Project"."created_at" ASC, "Task->Project"."created_at", "Task"."created_at" ASC, "Task"."created_at", "Task->Project"."created_at" ASC, "Task->Project"."created_at", "Task"."created_at" ASC, "Task"."created_at", "Task->Project"."created_at" ASC, "Task->Project"."created_at", "Task"."created_at" ASC, "Task"."created_at", "Task->Project"."created_at" ASC, "Task->Project"."created_at", "Task"."created_at" ASC, "Task"."created_at", "Subtask"."created_at" ASC, "Subtask"."created_at", "Subtask"."created_at";'
345345
});
346346

347+
testsql({
348+
model: Subtask,
349+
attributes: ['id', 'name'],
350+
order: [
351+
Support.sequelize.random()
352+
]
353+
}, {
354+
mssql: 'SELECT [id], [name] FROM [subtask] AS [Subtask] ORDER BY RAND();',
355+
mysql: 'SELECT `id`, `name` FROM `subtask` AS `Subtask` ORDER BY RAND();',
356+
postgres: 'SELECT "id", "name" FROM "subtask" AS "Subtask" ORDER BY RANDOM();',
357+
sqlite: 'SELECT `id`, `name` FROM `subtask` AS `Subtask` ORDER BY RANDOM();'
358+
});
359+
347360
describe('Invalid', () => {
348361
it('Error on invalid association', () => {
349362
return expect(Subtask.findAll({

0 commit comments

Comments
 (0)