From bf16db14f7c8ded05eb1d976d7167b7e84d99e26 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Thu, 20 Apr 2023 17:32:10 -0300 Subject: [PATCH 1/2] feat: improve lookup performance and allow composite key lookup --- packages/pinia-orm/src/query/Query.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/pinia-orm/src/query/Query.ts b/packages/pinia-orm/src/query/Query.ts index 497c9da49..521eb78b1 100644 --- a/packages/pinia-orm/src/query/Query.ts +++ b/packages/pinia-orm/src/query/Query.ts @@ -406,9 +406,17 @@ export class Query { const data = this.commit('all') const collection = [] as Collection - for (const id in data) { - if (ids === undefined || ids.length === 0 || ids.includes(id)) - collection.push(this.hydrate(data[id], { visible: this.visible, hidden: this.hidden, operation: 'get' })) + const deduplicatedIds = new Set(ids) + + if (deduplicatedIds.size > 0) { + deduplicatedIds.forEach((id) => { + if (data[id]) + collection.push(this.hydrate(data[id], { visible: this.visible, hidden: this.hidden, operation: 'get' })) + }) + } + else { + Object.values(data) + .forEach((value: any) => collection.push(this.hydrate(value, { visible: this.visible, hidden: this.hidden, operation: 'get' }))) } return collection @@ -490,10 +498,16 @@ export class Query { * Retrieve models by processing all filters set to the query chain. */ select(): Collection { - const whereIds = this.wheres.find(where => where.field === this.model.$getKeyName())?.value let ids: string[] = [] - if (whereIds) + // store the original wheres so multiple selects don't alter the result + const originalWheres = this.wheres + const whereIdsIndex = this.wheres.findIndex(where => where.field === this.model.$getKeyName()) + if (whereIdsIndex > -1) { + const whereIds = this.wheres[whereIdsIndex].value ids = ((isFunction(whereIds) ? [] : isArray(whereIds) ? whereIds : [whereIds]) || []).map(String) || [] + if (ids.length > 0) + this.wheres = [...this.wheres.slice(0, whereIdsIndex), ...this.wheres.slice(whereIdsIndex + 1)] + } let models = this.storeFind(ids) @@ -501,6 +515,8 @@ export class Query { models = this.filterOrder(models) models = this.filterLimit(models) + this.wheres = originalWheres + return models } From 408073809bc63463b7332f7662d44d45f9981df7 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Mon, 4 Sep 2023 17:18:27 +0200 Subject: [PATCH 2/2] test(pinia-orm): add find composite key test --- .../feature/repository/retrieves_find.spec.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/pinia-orm/tests/feature/repository/retrieves_find.spec.ts b/packages/pinia-orm/tests/feature/repository/retrieves_find.spec.ts index 2bcfba137..03511ae6d 100644 --- a/packages/pinia-orm/tests/feature/repository/retrieves_find.spec.ts +++ b/packages/pinia-orm/tests/feature/repository/retrieves_find.spec.ts @@ -102,4 +102,24 @@ describe('feature/repository/retrieves_find', () => { { id: 3, name: 'Johnny Doe' } ]) }) + + it('can find records with composite key', () => { + class UserComposite extends Model { + static entity = 'user_composites'; + static primaryKey = ['id', 'secondId']; + @Attr(null) declare id: number; + @Attr(null) declare secondId: number; + } + + const userRepo = useRepo(UserComposite) + + userRepo.save({ + id: 1, + secondId: 2, + }); + + const user = userRepo.find('[1,2]') + + expect(user).not.toBe(null) + }) })