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:
parent
3fc74002ff
commit
db511a33fa
50
packages/core/database/src/__tests__/group.test.ts
Normal file
50
packages/core/database/src/__tests__/group.test.ts
Normal 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 },
|
||||
]);
|
||||
});
|
||||
});
|
@ -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: [],
|
||||
|
Loading…
Reference in New Issue
Block a user