diff --git a/packages/core/database/src/__tests__/operator/empty-operator.test.ts b/packages/core/database/src/__tests__/operator/empty-operator.test.ts index e46b870fc6..f48c628761 100644 --- a/packages/core/database/src/__tests__/operator/empty-operator.test.ts +++ b/packages/core/database/src/__tests__/operator/empty-operator.test.ts @@ -7,7 +7,7 @@ describe('empty operator', () => { let db: Database; let User: Collection; - + let Profile: Collection; afterEach(async () => { await db.close(); }); @@ -16,7 +16,23 @@ describe('empty operator', () => { db = mockDatabase({}); User = db.collection({ name: 'users', - fields: [{ type: 'string', name: 'name' }], + fields: [ + { type: 'string', name: 'name' }, + { + type: 'hasOne', + name: 'profile', + }, + ], + }); + + Profile = db.collection({ + name: 'profiles', + fields: [ + { + type: 'string', + name: 'address', + }, + ], }); await db.sync({ @@ -74,4 +90,56 @@ describe('empty operator', () => { expect(result.length).toEqual(2); }); + + test('string not empty', async () => { + const u1 = await User.repository.create({ + values: { + name: 'u1', + }, + }); + + const u2 = await User.repository.create({ + values: { + name: '', + }, + }); + + const result = await User.repository.find({ + filter: { + 'name.$notEmpty': true, + }, + }); + + expect(result.length).toEqual(1); + expect(result[0].get('id')).toEqual(u1.get('id')); + }); + + test('string not empty with association', async () => { + const u1 = await User.repository.create({ + values: { + name: 'u1', + profile: { + address: 'a1', + }, + }, + }); + + const u2 = await User.repository.create({ + values: { + name: 'u2', + profile: { + address: '', + }, + }, + }); + + const result = await User.repository.find({ + filter: { + 'profile.address.$notEmpty': true, + }, + }); + + expect(result.length).toEqual(1); + expect(result[0].get('id')).toEqual(u1.get('id')); + }); }); diff --git a/packages/core/database/src/operators/empty.ts b/packages/core/database/src/operators/empty.ts index 6ad772d189..9f55a778d7 100644 --- a/packages/core/database/src/operators/empty.ts +++ b/packages/core/database/src/operators/empty.ts @@ -18,11 +18,15 @@ const findFilterFieldType = (ctx) => { const associationPath = path; for (const association of associationPath) { - if (lodash.isNumber(parseInt(association)) || association.startsWith('$')) { + if (lodash.isFinite(parseInt(association)) || association.startsWith('$')) { continue; } - model = model.associations[association].target; + const modelAssociation = model.associations[association]; + if (!modelAssociation) { + break; + } + model = modelAssociation.target; } const collection = db.modelCollection.get(model); diff --git a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/http-api/view-collection.test.ts b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/http-api/view-collection.test.ts index b3bd7d1b5f..a4c523d3b9 100644 --- a/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/http-api/view-collection.test.ts +++ b/packages/plugins/@nocobase/plugin-collection-manager/src/server/__tests__/http-api/view-collection.test.ts @@ -21,8 +21,8 @@ describe('view collection', () => { db = app.db; - collectionRepository = app.db.getCollection('collections').repository; - fieldsRepository = app.db.getCollection('fields').repository; + collectionRepository = app.db.getRepository('collections'); + fieldsRepository = app.db.getRepository('fields'); agent = app.agent(); testViewName = `view_${uid(6)}`; @@ -283,7 +283,7 @@ SELECT * FROM numbers; }); it('should list collections fields with source interface', async () => { - await app.db.getCollection('collections').repository.create({ + await app.db.getRepository('collections').create({ values: { name: 'users', fields: [