From db511a33fa4af1d76f5eadf4ee40ef966ed42c1e Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 31 Mar 2023 13:58:35 +0800 Subject: [PATCH] fix: find with attributes and group (#1411) * fix: find with attributes and group * fix: error * fix: test --------- Co-authored-by: Chareice --- .../core/database/src/__tests__/group.test.ts | 50 +++++++++++++++++++ packages/core/database/src/options-parser.ts | 6 +++ 2 files changed, 56 insertions(+) create mode 100644 packages/core/database/src/__tests__/group.test.ts diff --git a/packages/core/database/src/__tests__/group.test.ts b/packages/core/database/src/__tests__/group.test.ts new file mode 100644 index 000000000..5d5f4d0ad --- /dev/null +++ b/packages/core/database/src/__tests__/group.test.ts @@ -0,0 +1,50 @@ +import { Database, mockDatabase } from '@nocobase/database'; +import sequelize from 'sequelize'; + +describe('migrator', () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase({ + tablePrefix: 'test_', + }); + + await db.clean({ drop: true }); + }); + + afterEach(async () => { + await db.close(); + }); + + test('addMigrations', async () => { + db.collection({ + name: 'test', + fields: [ + { + type: 'string', + name: 'status', + }, + ], + }); + + await db.sync(); + + const r = db.getRepository('test'); + + await r.create({ + values: [{ status: 's1' }, { status: 's1' }, { status: 's1' }, { status: 's2' }, { status: 's3' }], + }); + + const result = await r.find({ + attributes: ['status', [sequelize.fn('COUNT', sequelize.col('id')), 'count']], + sort: 'status', + group: 'status', + }); + + expect(result.map((r) => r.toJSON())).toMatchObject([ + { status: 's1', count: 3 }, + { status: 's2', count: 1 }, + { status: 's3', count: 1 }, + ]); + }); +}); diff --git a/packages/core/database/src/options-parser.ts b/packages/core/database/src/options-parser.ts index 23e68442a..26c45061e 100644 --- a/packages/core/database/src/options-parser.ts +++ b/packages/core/database/src/options-parser.ts @@ -120,6 +120,12 @@ export class OptionsParser { const appends = this.options?.appends || []; const except = []; + if (this.options?.attributes) { + return { + attributes: this.options.attributes, + }; + } + let attributes: FindAttributeOptions = { include: [], exclude: [],