Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 6f32643e
authored
Jan 10, 2018
by
Michael McCabe
Committed by
Sushant
Jan 10, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: sequelize.random() (#8847)
1 parent
40f7d216
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
0 deletions
docs/querying.md
lib/sequelize.js
test/unit/sql/order.test.js
docs/querying.md
View file @
6f32643
...
...
@@ -429,6 +429,9 @@ Subtask.findAll({
// Will order by age ascending assuming ascending is the default order when direction is omitted
order
:
sequelize
.
col
(
'age'
)
// Will order by age randomly based on the dialect (instead of fn('RAND') or fn('RANDOM'))
order
:
sequelize
.
random
()
})
```
...
...
lib/sequelize.js
View file @
6f32643
...
...
@@ -772,6 +772,20 @@ class Sequelize {
databaseVersion
(
options
)
{
return
this
.
getQueryInterface
().
databaseVersion
(
options
);
}
/**
* Get the fn for random based on the dialect
*
* @return {Sequelize.fn}
*/
random
()
{
const
dialect
=
this
.
getDialect
();
if
(
_
.
includes
([
'postgres'
,
'sqlite'
],
dialect
))
{
return
this
.
fn
(
'RANDOM'
);
}
else
{
return
this
.
fn
(
'RAND'
);
}
}
/**
* 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
View file @
6f32643
...
...
@@ -344,6 +344,19 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
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";'
});
testsql
({
model
:
Subtask
,
attributes
:
[
'id'
,
'name'
],
order
:
[
Support
.
sequelize
.
random
()
]
},
{
mssql
:
'SELECT [id], [name] FROM [subtask] AS [Subtask] ORDER BY RAND();'
,
mysql
:
'SELECT `id`, `name` FROM `subtask` AS `Subtask` ORDER BY RAND();'
,
postgres
:
'SELECT "id", "name" FROM "subtask" AS "Subtask" ORDER BY RANDOM();'
,
sqlite
:
'SELECT `id`, `name` FROM `subtask` AS `Subtask` ORDER BY RANDOM();'
});
describe
(
'Invalid'
,
()
=>
{
it
(
'Error on invalid association'
,
()
=>
{
return
expect
(
Subtask
.
findAll
({
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment