Skip to content

Commit

Permalink
feat(query_builder): move orderByRandom to chainable class
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Sep 4, 2024
1 parent c057385 commit 6e4b415
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 47 deletions.
23 changes: 23 additions & 0 deletions src/database/query_builder/chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
*/
Expand Down
23 changes: 1 addition & 22 deletions src/database/query_builder/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
*/
Expand Down
22 changes: 1 addition & 21 deletions src/orm/query_builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class ModelQueryBuilder
super(
builder,
customFn,
client.dialect,
model.$keys.attributesToColumns.resolve.bind(model.$keys.attributesToColumns)
)

Expand Down Expand Up @@ -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
*/
Expand Down
14 changes: 10 additions & 4 deletions test/database/query_builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 6e4b415

Please sign in to comment.