diff --git a/packages/core/actions/src/__tests__/update-or-create.test.ts b/packages/core/actions/src/__tests__/update-or-create.test.ts index 61a1fb4f39..84c8722c2f 100644 --- a/packages/core/actions/src/__tests__/update-or-create.test.ts +++ b/packages/core/actions/src/__tests__/update-or-create.test.ts @@ -50,4 +50,49 @@ describe('update or create', () => { expect(post).not.toBeNull(); expect(post['title']).toEqual('t1'); }); + + test('update or create with empty values', async () => { + await Post.repository.create({ values: { title: 't1' } }); + + const response = await app + .agent() + .resource('posts') + .updateOrCreate({ + values: {}, + filterKeys: ['title'], + }); + + expect(response.statusCode).toEqual(200); + const post = await Post.repository.findOne({ + filter: { + 'title.$empty': true, + }, + }); + + expect(post).not.toBeNull(); + + await app + .agent() + .resource('posts') + .updateOrCreate({ + values: {}, + filterKeys: ['title'], + }); + + expect( + await Post.repository.count({ + filter: { + 'title.$empty': true, + }, + }), + ).toEqual(1); + + expect( + await Post.repository.count({ + filter: { + title: 't1', + }, + }), + ).toEqual(1); + }); }); diff --git a/packages/core/database/src/__tests__/repository/create.test.ts b/packages/core/database/src/__tests__/repository/create.test.ts index 87ba4a43d6..bc63f6bdf9 100644 --- a/packages/core/database/src/__tests__/repository/create.test.ts +++ b/packages/core/database/src/__tests__/repository/create.test.ts @@ -191,8 +191,11 @@ describe('create', () => { }, }, }); + expect(u1.name).toEqual('u1'); + const group = await u1.get('group'); + expect(group.name).toEqual('g1'); const u2 = await User.repository.firstOrCreate({ diff --git a/packages/core/database/src/repository.ts b/packages/core/database/src/repository.ts index 463bb5e138..353b83cb17 100644 --- a/packages/core/database/src/repository.ts +++ b/packages/core/database/src/repository.ts @@ -242,11 +242,8 @@ export class Repository) { - const filterAnd = []; - const flattedValues = flatten(values); - - const keyWithOutArrayIndex = (key) => { + public static valuesToFilter(values: Values = {}, filterKeys: Array) { + const removeArrayIndexInKey = (key) => { const chunks = key.split('.'); return chunks .filter((chunk) => { @@ -255,29 +252,36 @@ export class Repository