From c23a78002d634fc569a783dbe8bb196effc0f656 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Sep 2023 22:29:07 +0200 Subject: [PATCH 1/3] fix(pinia-orm): `Uncaught TypeError: parent.$fields()[key] is undefined` when inserting nested data with polymorphic relationships --- packages/pinia-orm/src/schema/Schema.ts | 3 +- .../feature/relations/morph_many_save.spec.ts | 103 ++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/packages/pinia-orm/src/schema/Schema.ts b/packages/pinia-orm/src/schema/Schema.ts index ba4891a5e..de4799ac2 100644 --- a/packages/pinia-orm/src/schema/Schema.ts +++ b/packages/pinia-orm/src/schema/Schema.ts @@ -113,7 +113,8 @@ export class Schema { // If the `key` is not `null`, that means this record is a nested // relationship of the parent model. In this case, we'll attach any // missing foreign keys to the record first. - if (key !== null) { (parent.$fields()[key] as Relation).attach(parentRecord, record) } + console.log('schema', parent.$fields(), key, model.$entity(), parent.$entity()) + if (key !== null) { (parent.$fields()[key] as Relation)?.attach(parentRecord, record) } // Next, we'll generate any missing primary key fields defined as // uid field. diff --git a/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts b/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts index f50dad93a..9e703b840 100644 --- a/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts +++ b/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts @@ -122,4 +122,107 @@ describe('feature/relations/morph_many_save', () => { } }) }) + + it('can save complicated polymorphs', () => { + Model.clearRegistries() + class Comment extends Model { + static entity = "comments"; + static fields() { + return { + id: this.number(0), + url: this.string(""), + content_id: this.number(0), + content_type: this.string(""), + content: this.morphTo([Video, Post], "content_id", "content_type"), + creator_id: this.string(null), + creator: this.belongsTo(Person, "creator_id"), + }; + } + } + + class Person extends Model { + static entity = "person"; + static primaryKey = "id"; + + static fields() { + return { + id: this.uid(), + job: this.attr(""), + comments: this.hasMany(Comment, "creator_id"), + }; + } + } + + class Video extends Model { + static entity = "videos"; + static fields() { + return { + id: this.number(0), + link: this.string(""), + comments: this.morphMany(Comment, "content_id", "content_type"), + }; + } + } + class Post extends Model { + static entity = "posts"; + static fields() { + return { + id: this.number(0), + title: this.string(""), + comments: this.morphMany(Comment, "content_id", "content_type"), + }; + } + } + const person = useRepo(Person); + + person.save({ + id: "p", + job: "dev", + comments: [ + { + id: 1, + content_id: 4, + content_type: 'posts', + content: { id: 4, title: 'a post' }, // comment this out to remove the error + creator_id: 'p' + }, + { + id: 2, + content_id: 3, + content_type: "videos", + content: { id: 3, link: 'test' }, // comment this out to remove the error + creator_id: 'p' + }, + ], + }); + + assertState({ + posts: { + 4: { id: 4, title: 'a post' } + }, + videos: { + 3: { id: 3, link: 'test' } + }, + comments: { + 1: { + id: 1, + url: '', + creator_id: 'p', + content_id: 4, + content_type: 'posts', + }, + 2: { + id: 2, + url: '', + creator_id: 'p', + content_id: 3, + content_type: 'videos', + } + }, + person: { + 'p': { id: 'p', job: 'dev' } + } + + }) + }) }) From 4eab4a3667f6ff69073631b535c5de19e42a8a16 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Sep 2023 22:31:17 +0200 Subject: [PATCH 2/3] refactor(pinia-orm): linting --- .../feature/relations/morph_many_save.spec.ts | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts b/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts index 9e703b840..bc6acf8dc 100644 --- a/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts +++ b/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts @@ -126,58 +126,58 @@ describe('feature/relations/morph_many_save', () => { it('can save complicated polymorphs', () => { Model.clearRegistries() class Comment extends Model { - static entity = "comments"; - static fields() { + static entity = 'comments' + static fields () { return { id: this.number(0), - url: this.string(""), + url: this.string(''), content_id: this.number(0), - content_type: this.string(""), - content: this.morphTo([Video, Post], "content_id", "content_type"), + content_type: this.string(''), + content: this.morphTo([Video, Post], 'content_id', 'content_type'), creator_id: this.string(null), - creator: this.belongsTo(Person, "creator_id"), - }; + creator: this.belongsTo(Person, 'creator_id') + } } } class Person extends Model { - static entity = "person"; - static primaryKey = "id"; + static entity = 'person' + static primaryKey = 'id' - static fields() { + static fields () { return { id: this.uid(), - job: this.attr(""), - comments: this.hasMany(Comment, "creator_id"), - }; + job: this.attr(''), + comments: this.hasMany(Comment, 'creator_id') + } } } class Video extends Model { - static entity = "videos"; - static fields() { + static entity = 'videos' + static fields () { return { id: this.number(0), - link: this.string(""), - comments: this.morphMany(Comment, "content_id", "content_type"), - }; + link: this.string(''), + comments: this.morphMany(Comment, 'content_id', 'content_type') + } } } class Post extends Model { - static entity = "posts"; - static fields() { + static entity = 'posts' + static fields () { return { id: this.number(0), - title: this.string(""), - comments: this.morphMany(Comment, "content_id", "content_type"), - }; + title: this.string(''), + comments: this.morphMany(Comment, 'content_id', 'content_type') + } } } - const person = useRepo(Person); + const person = useRepo(Person) person.save({ - id: "p", - job: "dev", + id: 'p', + job: 'dev', comments: [ { id: 1, @@ -189,12 +189,12 @@ describe('feature/relations/morph_many_save', () => { { id: 2, content_id: 3, - content_type: "videos", + content_type: 'videos', content: { id: 3, link: 'test' }, // comment this out to remove the error creator_id: 'p' - }, - ], - }); + } + ] + }) assertState({ posts: { @@ -209,18 +209,18 @@ describe('feature/relations/morph_many_save', () => { url: '', creator_id: 'p', content_id: 4, - content_type: 'posts', + content_type: 'posts' }, 2: { id: 2, url: '', creator_id: 'p', content_id: 3, - content_type: 'videos', + content_type: 'videos' } }, person: { - 'p': { id: 'p', job: 'dev' } + p: { id: 'p', job: 'dev' } } }) From af66c144bb1979a0401e54e9a0931aa442225796 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 12 Sep 2023 22:32:38 +0200 Subject: [PATCH 3/3] refactor(pinia-orm): remove comment --- .../pinia-orm/tests/feature/relations/morph_many_save.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts b/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts index bc6acf8dc..8e3e4876d 100644 --- a/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts +++ b/packages/pinia-orm/tests/feature/relations/morph_many_save.spec.ts @@ -183,14 +183,14 @@ describe('feature/relations/morph_many_save', () => { id: 1, content_id: 4, content_type: 'posts', - content: { id: 4, title: 'a post' }, // comment this out to remove the error + content: { id: 4, title: 'a post' }, creator_id: 'p' }, { id: 2, content_id: 3, content_type: 'videos', - content: { id: 3, link: 'test' }, // comment this out to remove the error + content: { id: 3, link: 'test' }, creator_id: 'p' } ]