diff --git a/src/database/query_builder/chainable.ts b/src/database/query_builder/chainable.ts index d7ec061b..0caa9bcf 100644 --- a/src/database/query_builder/chainable.ts +++ b/src/database/query_builder/chainable.ts @@ -16,6 +16,7 @@ import { isObject } from '../../utils/index.js' import { RawQueryBuilder } from './raw.js' import { RawBuilder } from '../static_builder/raw.js' import { ReferenceBuilder } from '../static_builder/reference.js' +import type { DialectContract } from '../../types/database.js' /** * The chainable query builder to construct SQL queries for selecting, updating and @@ -105,6 +106,7 @@ export abstract class Chainable extends Macroable implements ChainableContract { constructor( public knexQuery: Knex.QueryBuilder, private queryCallback: DBQueryCallback, + private dialect: DialectContract, public keysResolver?: (columnName: string) => string ) { super() @@ -1701,6 +1703,27 @@ export abstract class Chainable extends Macroable implements ChainableContract { return this } + /** + * Order results by random value. + */ + orderByRandom(seed = '') { + switch (this.dialect.name) { + case 'sqlite3': + case 'better-sqlite3': + case 'postgres': + case 'redshift': + return this.orderByRaw('RANDOM()') + case 'mysql': + return this.orderByRaw(`RAND(${seed})`) + case 'mssql': + return this.orderByRaw('NEWID()') + case 'oracledb': + return this.orderByRaw('dbms_random.value') + default: + throw new Error(`Cannot order by random for the given dialect ${this.dialect.name}`) + } + } + /** * Define select offset */ diff --git a/src/database/query_builder/database.ts b/src/database/query_builder/database.ts index 3e4a567d..32fe35a4 100644 --- a/src/database/query_builder/database.ts +++ b/src/database/query_builder/database.ts @@ -70,7 +70,7 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil public client: QueryClientContract, public keysResolver?: (columnName: string) => string ) { - super(builder, queryCallback, keysResolver) + super(builder, queryCallback, client.dialect, keysResolver) this.debugQueries = this.client.debug } @@ -281,27 +281,6 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil return this } - /** - * Order results by random value. - */ - orderByRandom(seed = '') { - switch (this.client.dialect.name) { - case 'sqlite3': - case 'better-sqlite3': - case 'postgres': - case 'redshift': - return this.orderByRaw('RANDOM()') - case 'mysql': - return this.orderByRaw(`RAND(${seed})`) - case 'mssql': - return this.orderByRaw('NEWID()') - case 'oracledb': - return this.orderByRaw('dbms_random.value') - default: - throw new Error(`Cannot order by random for the given dialect ${this.client.dialect.name}`) - } - } - /** * Executes the query */ diff --git a/src/orm/query_builder/index.ts b/src/orm/query_builder/index.ts index 096a4165..5ba2e154 100644 --- a/src/orm/query_builder/index.ts +++ b/src/orm/query_builder/index.ts @@ -150,6 +150,7 @@ export class ModelQueryBuilder super( builder, customFn, + client.dialect, model.$keys.attributesToColumns.resolve.bind(model.$keys.attributesToColumns) ) @@ -643,27 +644,6 @@ export class ModelQueryBuilder return this.addWhereHas(relationName, 'not', operator, value) } - /** - * Order results by random value. - */ - orderByRandom(seed = '') { - switch (this.client.dialect.name) { - case 'sqlite3': - case 'better-sqlite3': - case 'postgres': - case 'redshift': - return this.orderByRaw('RANDOM()') - case 'mysql': - return this.orderByRaw(`RAND(${seed})`) - case 'mssql': - return this.orderByRaw('NEWID()') - case 'oracledb': - return this.orderByRaw('dbms_random.value') - default: - throw new Error(`Cannot order by random for the given dialect ${this.client.dialect.name}`) - } - } - /** * Define a relationship to be preloaded */ diff --git a/test/database/query_builder.spec.ts b/test/database/query_builder.spec.ts index 3c4b1eea..12e43eec 100644 --- a/test/database/query_builder.spec.ts +++ b/test/database/query_builder.spec.ts @@ -5474,12 +5474,18 @@ test.group('Query Builder | orderByRandom', (group) => { }, ]) - const users = await db.from('users').orderByRandom() - const users2 = await db.from('users').orderByRandom() + const users = [] + + for (let i = 0; i < 10; i++) { + const result = await db.from('users').orderByRandom() + + users.push(result.map((user) => user.id)) + } + + // TODO: Check which assertion is better to use - assert.notEqual(users[0].id, users2[0].id) await connection.disconnect() - }).retry(3) + }) }) test.group('Query Builder | offset', (group) => {