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

feat(query_builder): add orderByRandom #1011

Open
wants to merge 4 commits into
base: 21.x
Choose a base branch
from
Open

Conversation

RomainLanz
Copy link
Member

Hey there! 👋🏻

This PR adds a orderByRandom method to retrieve records in a random order.
It defines the right keyword for each dialect.

The test may be flaky, I have set it up to run 3 times in case it fails.

A seed can be pass when using MySQL (same as Laravel implementation).

@RomainLanz
Copy link
Member Author

I am not sure how to deal with the mssql error. We are not passing multiple NEWID() to the order by clause. 🤔

Comment on lines +5459 to +5483
const db = getQueryBuilder(getQueryClient(connection))
await getInsertBuilder(getQueryClient(connection))
.table('users')
.multiInsert([
{
username: 'virk',
email: '[email protected]',
},
{
username: 'romain',
email: '[email protected]',
},
{
username: 'nikk',
email: '[email protected]',
},
])

const userResults: number[][] = []

for (let i = 0; i < 10; i++) {
const result = await db.from('users').orderByRandom()

userResults.push(result.map((user) => user.id))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it was reusing the same query builder, so it would carry over the previous order by.

I found that if you use a new query builder within the for loop, it works as expected:

Suggested change
const db = getQueryBuilder(getQueryClient(connection))
await getInsertBuilder(getQueryClient(connection))
.table('users')
.multiInsert([
{
username: 'virk',
email: '[email protected]',
},
{
username: 'romain',
email: '[email protected]',
},
{
username: 'nikk',
email: '[email protected]',
},
])
const userResults: number[][] = []
for (let i = 0; i < 10; i++) {
const result = await db.from('users').orderByRandom()
userResults.push(result.map((user) => user.id))
}
const client = getQueryClient(connection)
await getInsertBuilder(getQueryClient(connection))
.table('users')
.multiInsert([
{
username: 'virk',
email: '[email protected]',
},
{
username: 'romain',
email: '[email protected]',
},
{
username: 'nikk',
email: '[email protected]',
},
])
const userResults: number[][] = []
for (let i = 0; i < 10; i++) {
const result = await getQueryBuilder(client).from('users').orderByRandom()
userResults.push(result.map((user) => user.id))
}

Or clear the order by clauses like so:

const result = await db.from('users').clearOrder().orderByRandom()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants