Skip to content
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: support sortBy on joined table #1327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG-Unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Deprecations

### New features

- [Query] Add `Q.sortBy({column:'columnName', table:'tableName'})` to support sorting on joined tables
### Fixes

### Performance
Expand Down
13 changes: 13 additions & 0 deletions src/QueryDescription/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,19 @@ describe('buildQueryDescription', () => {
sortBy: [{ type: 'sortBy', sortColumn: 'sortable_column', sortOrder: 'desc' }],
})
})
it('supports sorting query on joined table', () => {
const query = Q.buildQueryDescription([
Q.sortBy({ column: 'sortable_column', table: 'joinedTable' }, Q.desc),
])
expect(query).toEqual({
where: [],
joinTables: [],
nestedJoinTables: [],
sortBy: [
{ type: 'sortBy', sortColumn: 'sortable_column', sortOrder: 'desc', table: 'joinedTable' },
],
})
})
it('does not support skip operator without take operator', () => {
expect(() => {
Q.buildQueryDescription([Q.skip(100)])
Expand Down
3 changes: 2 additions & 1 deletion src/QueryDescription/operators.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
On,
SortOrder,
SortBy,
SortColumn,
Take,
Skip,
JoinTables,
Expand Down Expand Up @@ -114,7 +115,7 @@ export const or: ArrayOrSpreadFn<Where, Or>
export const asc: SortOrder
export const desc: SortOrder

export function sortBy(sortColumn: ColumnName, sortOrder?: SortOrder): SortBy
export function sortBy(sortColumn: SortColumn, sortOrder?: SortOrder): SortBy

export function take(count: number): Take

Expand Down
9 changes: 7 additions & 2 deletions src/QueryDescription/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
On,
SortOrder,
SortBy,
SortColumn,
Take,
Skip,
JoinTables,
Expand Down Expand Up @@ -212,12 +213,16 @@ export const or: ArrayOrSpreadFn<Where, Or> = (...args): Or => {
export const asc: SortOrder = 'asc'
export const desc: SortOrder = 'desc'

export function sortBy(sortColumn: ColumnName, sortOrder: SortOrder = asc): SortBy {
export function sortBy(sortColumn: SortColumn, sortOrder: SortOrder = asc): SortBy {
invariant(
sortOrder === 'asc' || sortOrder === 'desc',
`Invalid sortOrder argument received in Q.sortBy (valid: asc, desc)`,
)
return { type: 'sortBy', sortColumn: checkName(sortColumn), sortOrder }

const sortCol = typeof sortColumn === 'object' ? sortColumn.column : sortColumn
const table = typeof sortColumn === 'object' ? sortColumn.table : undefined

return { type: 'sortBy', sortColumn: checkName(sortCol), sortOrder, table }
}

export function take(count: number): Take {
Expand Down
2 changes: 2 additions & 0 deletions src/QueryDescription/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export type On = $RE<{
export type SortOrder = 'asc' | 'desc'
export const asc: SortOrder
export const desc: SortOrder
export type SortColumn = ColumnName | $RE<{column: ColumnName, table: TableName<any>}>
export type SortBy = $RE<{
type: 'sortBy'
sortColumn: ColumnName
sortOrder: SortOrder
table?: TableName<any>
}>
export type Take = $RE<{
type: 'take'
Expand Down
3 changes: 3 additions & 0 deletions src/QueryDescription/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ export type On = $RE<{
conditions: Where[],
}>
export type SortOrder = 'asc' | 'desc'

export type SortColumn = ColumnName | $RE<{column: ColumnName, table: TableName<any>}>
export type SortBy = $RE<{
type: 'sortBy',
sortColumn: ColumnName,
sortOrder: SortOrder,
table?: TableName<any>,
}>
export type Take = $RE<{
type: 'take',
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/databaseTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@ function joinTest(
skipCount?: boolean,
skipLoki?: boolean,
skipSqlite?: boolean,
checkOrder?: boolean,
}>,
): void {
joinTests.push(options)
Expand Down Expand Up @@ -1140,6 +1141,28 @@ joinTest({
{ id: 'n6' }, // bad TT
],
})
joinTest({
name: `can perform Q.sort on joined table`,
query: [
Q.experimentalJoinTables(['tag_assignments']),
Q.sortBy({ column: 'text1', table: 'tag_assignments' }),
],
extraRecords: {
tag_assignments: [
{ id: 'tt1', text1: 'z', task_id: 'm6' },
{ id: 'tt2', text1: 'y', task_id: 'm8' },
{ id: 'tt3', text1: 'x', task_id: 'm7' },
{ id: 'tt4', text1: 'w', task_id: 'n5' },
{ id: 'tt5', text1: 'v', task_id: 'n6' },
{ id: 'tt6', text1: 'u', task_id: 'm2' },
{ id: 'tt7', text1: 'z', task_id: 'm2' },
],
},
matching: [{ id: 'm2' }, { id: 'n6' }, { id: 'n5' }, { id: 'm7' }, { id: 'm8' }, { id: 'm6' }],
nonMatching: [],
checkOrder: true,
skipLoki: true,
})
joinTest({
name: `can perform Q.on's nested in Q.on`,
query: [
Expand Down
6 changes: 6 additions & 0 deletions src/adapters/lokijs/worker/executeQuery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow

import { invariant } from '../../../utils/common'

import type { SerializedQuery } from '../../../Query'

import type { DirtyRaw } from '../../../RawRecord'
Expand Down Expand Up @@ -31,6 +33,10 @@ function performQuery(query: SerializedQuery, loki: Loki): LokiResultset {
// Step three: sort, skip, take
const { sortBy, take, skip } = query.description
if (sortBy.length) {
if (process.env.NODE_ENV !== 'production') {
invariant(!sortBy.some((sort) => sort.table), 'sortBy is not supported on joined table')
}

resultset = resultset.compoundsort(
sortBy.map(({ sortColumn, sortOrder }) => [sortColumn, sortOrder === 'desc']),
)
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/sqlite/encodeQuery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const encodeOrderBy = (table: TableName<any>, sortBys: SortBy[]) => {
}
const orderBys = sortBys
.map((sortBy) => {
return `"${table}"."${sortBy.sortColumn}" ${sortBy.sortOrder}`
return `"${sortBy.table ?? table}"."${sortBy.sortColumn}" ${sortBy.sortOrder}`
})
.join(', ')
return ` order by ${orderBys}`
Expand Down
5 changes: 5 additions & 0 deletions src/adapters/sqlite/encodeQuery/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ describe('SQLite encodeQuery', () => {
`select "tasks".* from "tasks" where "tasks"."_status" is not 'deleted' order by "tasks"."sortable_column" desc, "tasks"."sortable_column2" asc`,
)
})
it('encodes order by clause on table', () => {
expect(encoded([Q.sortBy({ column: 'sortable_column', table: 'table' }, Q.desc)])).toBe(
`select "tasks".* from "tasks" where "tasks"."_status" is not 'deleted' order by "table"."sortable_column" desc`,
)
})
it('encodes limit clause', () => {
expect(encoded([Q.take(100)])).toBe(
`select "tasks".* from "tasks" where "tasks"."_status" is not 'deleted' limit 100`,
Expand Down