Skip to content

Commit ea6203b

Browse files
authored
fix: nested relations not getting normalized correctly (#50)
1 parent f82488a commit ea6203b

File tree

7 files changed

+70
-14
lines changed

7 files changed

+70
-14
lines changed

src/model/Model.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,7 @@ export class Model {
232232

233233
ownerKey = ownerKey ?? instance.$getLocalKey()
234234

235-
return new HasManyBy(
236-
instance,
237-
related.newRawInstance(),
238-
foreignKey,
239-
ownerKey
240-
)
235+
return new HasManyBy(this.newRawInstance(), instance, foreignKey, ownerKey)
241236
}
242237

243238
/**

src/model/attributes/relations/BelongsTo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class BelongsTo extends Relation {
5151
* Define the normalizr schema for the relation.
5252
*/
5353
define(schema: Schema): NormalizrSchema {
54-
return schema.one(this.child)
54+
return schema.one(this.child, this.parent)
5555
}
5656

5757
/**

src/model/attributes/relations/HasMany.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class HasMany extends Relation {
4141
* Define the normalizr schema for the relation.
4242
*/
4343
define(schema: Schema): NormalizrSchema {
44-
return schema.many(this.related)
44+
return schema.many(this.related, this.parent)
4545
}
4646

4747
/**

src/model/attributes/relations/HasManyBy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class HasManyBy extends Relation {
5151
* Define the normalizr schema for the relation.
5252
*/
5353
define(schema: Schema): NormalizrSchema {
54-
return schema.many(this.child)
54+
return schema.many(this.child, this.parent)
5555
}
5656

5757
/**

src/model/attributes/relations/HasOne.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class HasOne extends Relation {
4141
* Define the normalizr schema for the relation.
4242
*/
4343
define(schema: Schema): NormalizrSchema {
44-
return schema.one(this.related)
44+
return schema.one(this.related, this.parent)
4545
}
4646

4747
/**

src/schema/Schema.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ export class Schema {
2525
/**
2626
* Create a single schema.
2727
*/
28-
one(model?: Model): Normalizr.Entity {
28+
one(model?: Model, parent?: Model): Normalizr.Entity {
2929
model = model || this.model
30+
parent = parent || this.model
3031

3132
const entity = model.$entity()
3233

3334
if (this.schemas[entity]) {
3435
return this.schemas[entity]
3536
}
3637

37-
const schema = this.newEntity(model, this.model)
38+
const schema = this.newEntity(model, parent)
3839

3940
this.schemas[entity] = schema
4041

@@ -48,8 +49,8 @@ export class Schema {
4849
/**
4950
* Create an array schema for the given model.
5051
*/
51-
many(model: Model): Normalizr.Array {
52-
return new Normalizr.Array(this.one(model))
52+
many(model: Model, parent?: Model): Normalizr.Array {
53+
return new Normalizr.Array(this.one(model, parent))
5354
}
5455

5556
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createStore, assertState } from 'test/Helpers'
2+
import { Model, Attr, Str, BelongsTo, HasMany } from '@/index'
3+
4+
describe('feature/relations/nested/nested_relations', () => {
5+
class User extends Model {
6+
static entity = 'users'
7+
8+
@Attr() id!: number
9+
@Str('') name!: string
10+
11+
@HasMany(() => Follower, 'userId')
12+
followers!: Follower[]
13+
}
14+
15+
class Follower extends Model {
16+
static entity = 'followers'
17+
18+
@Attr() id!: number
19+
@Attr() userId!: number
20+
}
21+
22+
class Post extends Model {
23+
static entity = 'posts'
24+
25+
@Attr() id!: number
26+
@Attr() userId!: number | null
27+
@Str('') title!: string
28+
29+
@BelongsTo(() => User, 'userId')
30+
author!: User | null
31+
}
32+
33+
it('inserts a nested relations with missing foreign key', async () => {
34+
const store = createStore()
35+
36+
await store.$repo(Post).insert({
37+
id: 1,
38+
userId: 1,
39+
title: 'Title 01',
40+
author: {
41+
id: 1,
42+
name: 'John Doe',
43+
followers: [{ id: 1 }, { id: 2 }]
44+
}
45+
})
46+
47+
assertState(store, {
48+
users: {
49+
1: { id: 1, name: 'John Doe' }
50+
},
51+
followers: {
52+
1: { id: 1, userId: 1 },
53+
2: { id: 2, userId: 1 }
54+
},
55+
posts: {
56+
1: { id: 1, userId: 1, title: 'Title 01' }
57+
}
58+
})
59+
})
60+
})

0 commit comments

Comments
 (0)