File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -643,6 +643,27 @@ export class ModelQueryBuilder
643643 return this . addWhereHas ( relationName , 'not' , operator , value )
644644 }
645645
646+ /**
647+ * Order results by random value.
648+ */
649+ orderByRandom ( seed = '' ) {
650+ switch ( this . client . dialect . name ) {
651+ case 'sqlite3' :
652+ case 'better-sqlite3' :
653+ case 'postgres' :
654+ case 'redshift' :
655+ return this . orderByRaw ( 'RANDOM()' )
656+ case 'mysql' :
657+ return this . orderByRaw ( `RAND(${ seed } )` )
658+ case 'mssql' :
659+ return this . orderByRaw ( 'NEWID()' )
660+ case 'oracledb' :
661+ return this . orderByRaw ( 'dbms_random.value' )
662+ default :
663+ throw new Error ( `Cannot order by random for the given dialect ${ this . client . dialect . name } ` )
664+ }
665+ }
666+
646667 /**
647668 * Define a relationship to be preloaded
648669 */
You can’t perform that action at this time.
0 commit comments