-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
base: 21.x
Are you sure you want to change the base?
Conversation
659e85b
to
6e4b415
Compare
3e464c7
to
a18da11
Compare
a18da11
to
e3d9c7e
Compare
I am not sure how to deal with the |
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)) | ||
} |
There was a problem hiding this comment.
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:
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()
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).