From b4631d1cd55a4aa20806dd857281e79eb2cb4c45 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Sun, 17 Sep 2023 14:08:12 +0200 Subject: [PATCH] feat(pinia-orm): Composite primary key can now be used with `destroy` and `onDelete` --- packages/pinia-orm/src/model/Model.ts | 2 +- packages/pinia-orm/src/query/Query.ts | 10 +++------- .../repository/delete_with_relations.spec.ts | 16 ++++++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/pinia-orm/src/model/Model.ts b/packages/pinia-orm/src/model/Model.ts index 1ce74bfad..9f8fa9c65 100644 --- a/packages/pinia-orm/src/model/Model.ts +++ b/packages/pinia-orm/src/model/Model.ts @@ -817,7 +817,7 @@ export class Model { if (this.$hasCompositeKey()) { const compositeKey = this.$getCompositeKey(record) - return concatCompositeKey ? compositeKey?.join('') ?? null : compositeKey + return concatCompositeKey ? '[' + compositeKey?.join(',') + ']' : compositeKey } const id = record[this.$getKeyName() as string] diff --git a/packages/pinia-orm/src/query/Query.ts b/packages/pinia-orm/src/query/Query.ts index c94f8d5a7..7ece9318e 100644 --- a/packages/pinia-orm/src/query/Query.ts +++ b/packages/pinia-orm/src/query/Query.ts @@ -1,7 +1,8 @@ import type { Pinia } from 'pinia' import { acceptHMRUpdate } from 'pinia' import { - assert, compareWithOperator, generateKey, + compareWithOperator, + generateKey, groupBy, isArray, isEmpty, @@ -859,11 +860,6 @@ export class Query { destroy(ids: (string | number)[]): Collection destroy(id: string | number): Item destroy (ids: any): any { - assert(!this.model.$hasCompositeKey(), [ - 'You can\'t use the `destroy` method on a model with a composite key.', - 'Please use `delete` method instead.' - ]) - return isArray(ids) ? this.destroyMany(ids) : this.destroyOne(ids) } @@ -928,7 +924,7 @@ export class Query { if (fields[name] instanceof Relation && relation.onDeleteMode && model[name]) { const models = isArray(model[name]) ? model[name] : [model[name]] const relationIds = models.map((relation: M) => { - return relation[relation.$getLocalKey()] + return relation.$getKey(undefined, true) }) const record: Record = {} diff --git a/packages/pinia-orm/tests/feature/repository/delete_with_relations.spec.ts b/packages/pinia-orm/tests/feature/repository/delete_with_relations.spec.ts index cfc1778d9..09f54e070 100644 --- a/packages/pinia-orm/tests/feature/repository/delete_with_relations.spec.ts +++ b/packages/pinia-orm/tests/feature/repository/delete_with_relations.spec.ts @@ -29,7 +29,10 @@ describe('feature/repository/delete_with_relations', () => { class Comment extends Model { static entity = 'comments' + static primaryKey = ['id', 'comment_id'] + @Num(0) declare id: number + @Num(0) declare comment_id: number @Num(0) declare postId: number @Str('') declare title: string } @@ -111,8 +114,8 @@ describe('feature/repository/delete_with_relations', () => { id: 1, title: 'Title 01', comments: [ - { id: 3, title: 'Title 03' }, - { id: 4, title: 'Title 04' } + { id: 3, comment_id: 3, title: 'Title 03' }, + { id: 4, comment_id: 3, title: 'Title 04' } ], extraComments: [ { id: 3, title: 'Title 03' }, @@ -131,8 +134,8 @@ describe('feature/repository/delete_with_relations', () => { id: 3, title: 'Title 03', comments: [ - { id: 1, title: 'Title 01' }, - { id: 2, title: 'Title 02' } + { id: 1, comment_id: 3, title: 'Title 01' }, + { id: 2, comment_id: 3, title: 'Title 02' } ], extraComments: [ { id: 1, title: 'Title 01' }, @@ -169,8 +172,8 @@ describe('feature/repository/delete_with_relations', () => { 2: { id: 2, userId: 1, title: 'Title 02' } }, comments: { - 3: { id: 3, postId: 1, title: 'Title 03' }, - 4: { id: 4, postId: 1, title: 'Title 04' } + '[3,3]': { id: 3, comment_id: 3, postId: 1, title: 'Title 03' }, + '[4,3]': { id: 4, comment_id: 3, postId: 1, title: 'Title 04' } }, extraComments: { 3: { id: 3, postId: 1, title: 'Title 03' }, @@ -187,6 +190,7 @@ describe('feature/repository/delete_with_relations', () => { }) it('works with morph relations', () => { + Model.clearRegistries() class Comment extends Model { static entity = 'comments'