fix: find with attributes and group (#1411)

* fix: find with attributes and group

* fix: error

* fix: test

---------

Co-authored-by: Chareice <chareice@live.com>
This commit is contained in:
chenos 2023-03-31 13:58:35 +08:00 committed by GitHub
parent 3fc74002ff
commit db511a33fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -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 },
]);
});
});

View File

@ -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: [],