File tree Expand file tree Collapse file tree 4 files changed +35
-47
lines changed Expand file tree Collapse file tree 4 files changed +35
-47
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import { isObject } from '../../utils/index.js'
1616import { RawQueryBuilder } from './raw.js'
1717import { RawBuilder } from '../static_builder/raw.js'
1818import { ReferenceBuilder } from '../static_builder/reference.js'
19+ import type { DialectContract } from '../../types/database.js'
1920
2021/**
2122 * The chainable query builder to construct SQL queries for selecting, updating and
@@ -105,6 +106,7 @@ export abstract class Chainable extends Macroable implements ChainableContract {
105106 constructor (
106107 public knexQuery : Knex . QueryBuilder ,
107108 private queryCallback : DBQueryCallback ,
109+ private dialect : DialectContract ,
108110 public keysResolver ?: ( columnName : string ) => string
109111 ) {
110112 super ( )
@@ -1701,6 +1703,27 @@ export abstract class Chainable extends Macroable implements ChainableContract {
17011703 return this
17021704 }
17031705
1706+ /**
1707+ * Order results by random value.
1708+ */
1709+ orderByRandom ( seed = '' ) {
1710+ switch ( this . dialect . name ) {
1711+ case 'sqlite3' :
1712+ case 'better-sqlite3' :
1713+ case 'postgres' :
1714+ case 'redshift' :
1715+ return this . orderByRaw ( 'RANDOM()' )
1716+ case 'mysql' :
1717+ return this . orderByRaw ( `RAND(${ seed } )` )
1718+ case 'mssql' :
1719+ return this . orderByRaw ( 'NEWID()' )
1720+ case 'oracledb' :
1721+ return this . orderByRaw ( 'dbms_random.value' )
1722+ default :
1723+ throw new Error ( `Cannot order by random for the given dialect ${ this . dialect . name } ` )
1724+ }
1725+ }
1726+
17041727 /**
17051728 * Define select offset
17061729 */
Original file line number Diff line number Diff line change @@ -70,7 +70,7 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
7070 public client : QueryClientContract ,
7171 public keysResolver ?: ( columnName : string ) => string
7272 ) {
73- super ( builder , queryCallback , keysResolver )
73+ super ( builder , queryCallback , client . dialect , keysResolver )
7474 this . debugQueries = this . client . debug
7575 }
7676
@@ -281,27 +281,6 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
281281 return this
282282 }
283283
284- /**
285- * Order results by random value.
286- */
287- orderByRandom ( seed = '' ) {
288- switch ( this . client . dialect . name ) {
289- case 'sqlite3' :
290- case 'better-sqlite3' :
291- case 'postgres' :
292- case 'redshift' :
293- return this . orderByRaw ( 'RANDOM()' )
294- case 'mysql' :
295- return this . orderByRaw ( `RAND(${ seed } )` )
296- case 'mssql' :
297- return this . orderByRaw ( 'NEWID()' )
298- case 'oracledb' :
299- return this . orderByRaw ( 'dbms_random.value' )
300- default :
301- throw new Error ( `Cannot order by random for the given dialect ${ this . client . dialect . name } ` )
302- }
303- }
304-
305284 /**
306285 * Executes the query
307286 */
Original file line number Diff line number Diff line change @@ -150,6 +150,7 @@ export class ModelQueryBuilder
150150 super (
151151 builder ,
152152 customFn ,
153+ client . dialect ,
153154 model . $keys . attributesToColumns . resolve . bind ( model . $keys . attributesToColumns )
154155 )
155156
@@ -644,27 +645,6 @@ export class ModelQueryBuilder
644645 return this . addWhereHas ( relationName , 'not' , operator , value )
645646 }
646647
647- /**
648- * Order results by random value.
649- */
650- orderByRandom ( seed = '' ) {
651- switch ( this . client . dialect . name ) {
652- case 'sqlite3' :
653- case 'better-sqlite3' :
654- case 'postgres' :
655- case 'redshift' :
656- return this . orderByRaw ( 'RANDOM()' )
657- case 'mysql' :
658- return this . orderByRaw ( `RAND(${ seed } )` )
659- case 'mssql' :
660- return this . orderByRaw ( 'NEWID()' )
661- case 'oracledb' :
662- return this . orderByRaw ( 'dbms_random.value' )
663- default :
664- throw new Error ( `Cannot order by random for the given dialect ${ this . client . dialect . name } ` )
665- }
666- }
667-
668648 /**
669649 * Define a relationship to be preloaded
670650 */
Original file line number Diff line number Diff line change @@ -5474,12 +5474,18 @@ test.group('Query Builder | orderByRandom', (group) => {
54745474 } ,
54755475 ] )
54765476
5477- const users = await db . from ( 'users' ) . orderByRandom ( )
5478- const users2 = await db . from ( 'users' ) . orderByRandom ( )
5477+ const users = [ ]
5478+
5479+ for ( let i = 0 ; i < 10 ; i ++ ) {
5480+ const result = await db . from ( 'users' ) . orderByRandom ( )
5481+
5482+ users . push ( result . map ( ( user ) => user . id ) )
5483+ }
5484+
5485+ // TODO: Check which assertion is better to use
54795486
5480- assert . notEqual ( users [ 0 ] . id , users2 [ 0 ] . id )
54815487 await connection . disconnect ( )
5482- } ) . retry ( 3 )
5488+ } )
54835489} )
54845490
54855491test . group ( 'Query Builder | offset' , ( group ) => {
You can’t perform that action at this time.
0 commit comments