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

fix(sort): use -1 for descending same as mongo syntax #1176

Merged
merged 1 commit into from May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/runtime/query/match/utils.ts
Expand Up @@ -70,7 +70,7 @@ export const sortList = (data: any[], params: SortOptions) => {
// `null` values are treated as `"null"` strings and ordered alphabetically
// Turn `null` values into `undefined` so they place at the end of the list
.map(value => value === null ? undefined : value)
if (params[key] === 0) {
if (params[key] === -1) {
values.reverse()
}
return comperable.compare(values[0], values[1])
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/types.d.ts
Expand Up @@ -148,7 +148,7 @@ export interface SortParams {
}

export interface SortFields {
[field: string]: 0 | 1
[field: string]: -1 | 1
}

export type SortOptions = SortParams | SortFields
Expand Down
2 changes: 1 addition & 1 deletion test/features/query/query.test.ts
Expand Up @@ -43,7 +43,7 @@ describe('Database Provider', () => {
const nameAsc = await createQuery(pipelineFetcher).sort({ name: 1 }).find()
assert(nameAsc[0].name === database[0].name)

const nameDesc = await createQuery(pipelineFetcher).sort({ name: 0 }).find()
const nameDesc = await createQuery(pipelineFetcher).sort({ name: -1 }).find()
assert(nameDesc[0].name === database[database.length - 1].name)
})

Expand Down
10 changes: 5 additions & 5 deletions test/features/query/utils.test.ts
Expand Up @@ -74,7 +74,7 @@ describe('query utils', () => {
{ a: 2, b: 1 }
])

expect(sortList(data, { a: 0 })).toEqual([
expect(sortList(data, { a: -1 })).toEqual([
{ a: 2, b: 1 },
{ a: 1, b: 2 },
{ a: 1, b: 1 }
Expand All @@ -86,7 +86,7 @@ describe('query utils', () => {
{ a: 1, b: 2 }
])

expect(sortList(data, { b: 0 })).toEqual([
expect(sortList(data, { b: -1 })).toEqual([
{ a: 1, b: 2 },
{ a: 2, b: 1 },
{ a: 1, b: 1 }
Expand All @@ -97,8 +97,8 @@ describe('query utils', () => {
const data = [{ data: { a: 1, b: 2 } }, { data: { a: 2, b: 1 } }, { data: { a: 1, b: 1 } }]

// sort by a descending
const aDesc = sortList(data, { 'data.a': 0 })
expect(sortList(aDesc, { 'data.b': 0 })).toEqual([
const aDesc = sortList(data, { 'data.a': -1 })
expect(sortList(aDesc, { 'data.b': -1 })).toEqual([
{ data: { a: 1, b: 2 } },
{ data: { a: 2, b: 1 } },
{ data: { a: 1, b: 1 } }
Expand All @@ -112,6 +112,6 @@ describe('query utils', () => {
])

// Sort again by a desc.
expect(sortList(aDesc, { 'data.a': 0 })).toEqual(aDesc)
expect(sortList(aDesc, { 'data.a': -1 })).toEqual(aDesc)
})
})