fix: count of multi filter target keys (#5251)

This commit is contained in:
ChengLei Shao 2024-09-11 14:32:59 +08:00 committed by GitHub
parent 51417a24de
commit da4a5b56e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 1 deletions

View File

@ -21,6 +21,60 @@ describe('multi filter target key', () => {
await db.close(); await db.close();
}); });
it('should get count of multi filter target keys', async () => {
const Student = db.collection({
name: 'students',
autoGenId: false,
filterTargetKey: ['name', 'classId'],
fields: [
{
name: 'name',
type: 'string',
primaryKey: true,
},
{
name: 'classId',
type: 'bigInt',
primaryKey: true,
},
{
name: 'age',
type: 'integer',
},
],
});
await db.sync();
const s1 = await Student.repository.create({
values: {
name: 's1',
classId: 1,
age: 10,
},
});
const s2 = await Student.repository.create({
values: {
name: 's1',
classId: 2,
age: 10,
},
});
const s3 = await Student.repository.create({
values: {
name: 's3',
classId: 2,
age: 10,
},
});
const count = await Student.repository.count({});
expect(count).toBe(3);
});
it('should set multi filter target keys', async () => { it('should set multi filter target keys', async () => {
const Student = db.collection({ const Student = db.collection({
name: 'students', name: 'students',

View File

@ -187,6 +187,10 @@ export class Collection<
return this.model.primaryKeyAttribute; return this.model.primaryKeyAttribute;
} }
isMultiFilterTargetKey() {
return Array.isArray(this.filterTargetKey) && this.filterTargetKey.length > 1;
}
get name() { get name() {
return this.options.name; return this.options.name;
} }

View File

@ -326,7 +326,7 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
const queryOptions: any = { const queryOptions: any = {
...options, ...options,
distinct: Boolean(this.collection.model.primaryKeyAttribute), distinct: Boolean(this.collection.model.primaryKeyAttribute) && !this.collection.isMultiFilterTargetKey(),
}; };
if (queryOptions.include?.length === 0) { if (queryOptions.include?.length === 0) {