Skip to content

Commit 9c47d46

Browse files
committed
feat: fill relation on "make"
1 parent d97dcb7 commit 9c47d46

File tree

7 files changed

+139
-9
lines changed

7 files changed

+139
-9
lines changed

src/model/attributes/relations/BelongsTo.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ export class BelongsTo extends Relation {
9797
: model.$setRelation(relation, null)
9898
})
9999
}
100+
101+
/**
102+
* Make a related model.
103+
*/
104+
make(element?: Element): Model | null {
105+
return element ? this.child.$newInstance(element) : null
106+
}
100107
}

src/model/attributes/relations/HasMany.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ export class HasMany extends Relation {
8181
return [result[this.foreignKey], result]
8282
})
8383
}
84+
85+
/**
86+
* Make related models.
87+
*/
88+
make(elements?: Element[]): Model[] {
89+
return elements
90+
? elements.map((element) => this.related.$newInstance(element))
91+
: []
92+
}
8493
}

src/model/attributes/relations/HasManyBy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,13 @@ export class HasManyBy extends Relation {
137137
return items
138138
}, [])
139139
}
140+
141+
/**
142+
* Make related models.
143+
*/
144+
make(elements?: Element[]): Model[] {
145+
return elements
146+
? elements.map((element) => this.child.$newInstance(element))
147+
: []
148+
}
140149
}

src/model/attributes/relations/HasOne.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,11 @@ export class HasOne extends Relation {
8181
return [result[this.foreignKey], result]
8282
})
8383
}
84+
85+
/**
86+
* Make a related model.
87+
*/
88+
make(element?: Element): Model | null {
89+
return element ? this.related.$newInstance(element) : null
90+
}
8491
}

src/model/attributes/relations/Relation.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,4 @@ export abstract class Relation extends Attribute {
9898
return dictionary
9999
}, {})
100100
}
101-
102-
/**
103-
* Make the value for the attribute.
104-
*/
105-
/* istanbul ignore next */
106-
make(_value: any): null {
107-
return null
108-
}
109101
}

src/repository/Repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export class Repository<M extends Model = Model> {
170170
*/
171171
make(attributes?: Element): M {
172172
return this.getModel().$newInstance(attributes, {
173-
relations: false
173+
relations: true
174174
})
175175
}
176176

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { createStore } from 'test/Helpers'
2+
import { Model, Attr, HasOne, BelongsTo, HasMany, HasManyBy } from '@/index'
3+
4+
describe('unit/model/Model_Relations', () => {
5+
class User extends Model {
6+
static entity = 'users'
7+
8+
@Attr() id!: number
9+
@Attr() countryId!: number
10+
@Attr() nameIds!: number[]
11+
12+
@HasOne(() => Phone, 'userId')
13+
phone!: Phone | null
14+
15+
@BelongsTo(() => Country, 'countryId')
16+
country!: Country | null
17+
18+
@HasMany(() => Post, 'userId')
19+
posts!: Post[]
20+
21+
@HasManyBy(() => Name, 'nameIds')
22+
names!: Name[]
23+
}
24+
25+
class Phone extends Model {
26+
static entity = 'phones'
27+
28+
@Attr() id!: number
29+
@Attr() userId!: number
30+
}
31+
32+
class Country extends Model {
33+
static entity = 'countries'
34+
35+
@Attr() id!: number
36+
}
37+
38+
class Post extends Model {
39+
static entity = 'posts'
40+
41+
@Attr() id!: number
42+
@Attr() userId!: number
43+
}
44+
45+
class Name extends Model {
46+
static entity = 'names'
47+
48+
@Attr() id!: number
49+
}
50+
51+
it('fills "has one" relation', () => {
52+
const store = createStore()
53+
54+
const user = store.$repo(User).make({
55+
id: 1,
56+
phone: {
57+
id: 2
58+
}
59+
})
60+
61+
expect(user.phone).toBeInstanceOf(Phone)
62+
expect(user.phone!.id).toBe(2)
63+
})
64+
65+
it('fills "belongs to" relation', () => {
66+
const store = createStore()
67+
68+
const user = store.$repo(User).make({
69+
id: 1,
70+
country: {
71+
id: 2
72+
}
73+
})
74+
75+
expect(user.country).toBeInstanceOf(Country)
76+
expect(user.country!.id).toBe(2)
77+
})
78+
79+
it('fills "has many" relation', () => {
80+
const store = createStore()
81+
82+
const user = store.$repo(User).make({
83+
id: 1,
84+
posts: [{ id: 2 }, { id: 3 }]
85+
})
86+
87+
expect(user.posts[0]).toBeInstanceOf(Post)
88+
expect(user.posts[1]).toBeInstanceOf(Post)
89+
expect(user.posts[0].id).toBe(2)
90+
expect(user.posts[1].id).toBe(3)
91+
})
92+
93+
it('fills "has many by" relation', () => {
94+
const store = createStore()
95+
96+
const user = store.$repo(User).make({
97+
id: 1,
98+
names: [{ id: 2 }, { id: 3 }]
99+
})
100+
101+
expect(user.names[0]).toBeInstanceOf(Name)
102+
expect(user.names[1]).toBeInstanceOf(Name)
103+
expect(user.names[0].id).toBe(2)
104+
expect(user.names[1].id).toBe(3)
105+
})
106+
})

0 commit comments

Comments
 (0)