From 850035166b44d003c1c0f0d038ab62eeb6ec248c Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Mon, 30 May 2022 14:28:11 +0200 Subject: [PATCH] fix(sort): `-1` for descending same as mongo syntax --- src/runtime/query/match/utils.ts | 2 +- src/runtime/types.d.ts | 2 +- test/features/query/query.test.ts | 2 +- test/features/query/utils.test.ts | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/runtime/query/match/utils.ts b/src/runtime/query/match/utils.ts index 84ae667cb..58b12f9a9 100644 --- a/src/runtime/query/match/utils.ts +++ b/src/runtime/query/match/utils.ts @@ -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]) diff --git a/src/runtime/types.d.ts b/src/runtime/types.d.ts index a85b405be..ab72affbc 100644 --- a/src/runtime/types.d.ts +++ b/src/runtime/types.d.ts @@ -148,7 +148,7 @@ export interface SortParams { } export interface SortFields { - [field: string]: 0 | 1 + [field: string]: -1 | 1 } export type SortOptions = SortParams | SortFields diff --git a/test/features/query/query.test.ts b/test/features/query/query.test.ts index c4d1645a9..edfd9968e 100644 --- a/test/features/query/query.test.ts +++ b/test/features/query/query.test.ts @@ -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) }) diff --git a/test/features/query/utils.test.ts b/test/features/query/utils.test.ts index bc4f8e009..dd3694576 100644 --- a/test/features/query/utils.test.ts +++ b/test/features/query/utils.test.ts @@ -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 } @@ -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 } @@ -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 } } @@ -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) }) })