From adfac15aba5f103071ca3820b68230da2d770f7b Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 15 Feb 2022 17:36:32 +0800 Subject: [PATCH] fix(database): parse sort --- .gitignore | 1 + packages/database/src/options-parser.ts | 12 +++++++----- packages/database/src/repository.ts | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 271f48896..718d12197 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ db.sqlite coverage .umi /uploads +.env.test diff --git a/packages/database/src/options-parser.ts b/packages/database/src/options-parser.ts index 9c0de4790..06f6c1c74 100644 --- a/packages/database/src/options-parser.ts +++ b/packages/database/src/options-parser.ts @@ -1,8 +1,8 @@ -import { Appends, Except, FindOptions } from './repository'; -import FilterParser from './filter-parser'; import { FindAttributeOptions, ModelCtor, Op } from 'sequelize'; -import { Database } from './database'; import { Collection } from './collection'; +import { Database } from './database'; +import FilterParser from './filter-parser'; +import { Appends, Except, FindOptions } from './repository'; const debug = require('debug')('noco-database'); @@ -61,8 +61,10 @@ export class OptionsParser { * @protected */ protected parseSort(filterParams) { - const sort = this.options?.sort || []; - + let sort = this.options?.sort || []; + if (typeof sort === 'string') { + sort = sort.split(','); + } const orderParams = sort.map((sortKey: string) => { const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC'; const sortField: Array = sortKey.replace('-', '').split('.'); diff --git a/packages/database/src/repository.ts b/packages/database/src/repository.ts index 5ce1e43d5..d09d1b03f 100644 --- a/packages/database/src/repository.ts +++ b/packages/database/src/repository.ts @@ -49,7 +49,7 @@ export type Filter = any; export type Appends = string[]; export type Except = string[]; export type Fields = string[]; -export type Sort = string[]; +export type Sort = string[] | string; export type WhiteList = string[]; export type BlackList = string[];