From 219996957913dffc1ac3099a178bf6b4f8988037 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Thu, 26 Sep 2024 08:21:02 +0800 Subject: [PATCH] fix: scope key error (#5322) * fix: scope key error * fix: group by scope key * fix: test --- .../src/__tests__/fields/sort-field.test.ts | 20 ++++++++++++------- packages/core/database/src/database.ts | 4 ---- .../core/database/src/fields/sort-field.ts | 10 ++++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/core/database/src/__tests__/fields/sort-field.test.ts b/packages/core/database/src/__tests__/fields/sort-field.test.ts index f769d070d6..1e0881dece 100644 --- a/packages/core/database/src/__tests__/fields/sort-field.test.ts +++ b/packages/core/database/src/__tests__/fields/sort-field.test.ts @@ -11,7 +11,7 @@ import { Database } from '../../database'; import { mockDatabase } from '../'; import { SortField } from '../../fields'; -describe('string field', () => { +describe('sort field', () => { let db: Database; beforeEach(async () => { @@ -37,7 +37,7 @@ describe('string field', () => { }, { type: 'string', - name: 'scopeKey', + name: 'someField', }, ], }); @@ -45,13 +45,19 @@ describe('string field', () => { await db.sync(); await Test.repository.create({ - values: { - name: 't1', - scopeKey: 'a', - }, + values: [ + { + name: 't1', + someField: 'a', + }, + { + name: 't2', + someField: 'b', + }, + ], }); - Test.setField('scopeKeySort', { type: 'sort', scopeKey: 'scopeKey' }); + Test.setField('scopeKeySort', { type: 'sort', scopeKey: 'someField' }); await db.sync(); }); diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index a3b813b96b..6ef92765b6 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -460,10 +460,6 @@ export class Database extends EventEmitter implements AsyncEmitter { } if (options.underscored) { - if (lodash.get(options, 'sortable.scopeKey')) { - options.sortable.scopeKey = snakeCase(options.sortable.scopeKey); - } - if (lodash.get(options, 'indexes')) { // change index fields to snake case options.indexes = options.indexes.map((index) => { diff --git a/packages/core/database/src/fields/sort-field.ts b/packages/core/database/src/fields/sort-field.ts index da4a77fd3e..a988602703 100644 --- a/packages/core/database/src/fields/sort-field.ts +++ b/packages/core/database/src/fields/sort-field.ts @@ -9,7 +9,7 @@ import { Mutex } from 'async-mutex'; import { isNumber } from 'lodash'; -import { DataTypes } from 'sequelize'; +import { DataTypes, Sequelize } from 'sequelize'; import { BaseColumnFieldOptions, Field } from './field'; const sortFieldMutex = new Mutex(); @@ -171,10 +171,12 @@ export class SortField extends Field { }; const scopeKey = this.options.scopeKey; + if (scopeKey) { - const groups = await this.collection.repository.find({ - attributes: [scopeKey], - group: [scopeKey], + const scopeKeyColumn = this.collection.model.rawAttributes[scopeKey].field; + + const groups = await this.collection.model.findAll({ + attributes: [[Sequelize.fn('DISTINCT', Sequelize.col(scopeKeyColumn)), scopeKey]], raw: true, transaction, });