Skip to content

Commit

Permalink
feat: fill relation on "make"
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Jan 28, 2021
1 parent d97dcb7 commit 9c47d46
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/model/attributes/relations/BelongsTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,11 @@ export class BelongsTo extends Relation {
: model.$setRelation(relation, null)
})
}

/**
* Make a related model.
*/
make(element?: Element): Model | null {
return element ? this.child.$newInstance(element) : null
}
}
9 changes: 9 additions & 0 deletions src/model/attributes/relations/HasMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ export class HasMany extends Relation {
return [result[this.foreignKey], result]
})
}

/**
* Make related models.
*/
make(elements?: Element[]): Model[] {
return elements
? elements.map((element) => this.related.$newInstance(element))
: []
}
}
9 changes: 9 additions & 0 deletions src/model/attributes/relations/HasManyBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,13 @@ export class HasManyBy extends Relation {
return items
}, [])
}

/**
* Make related models.
*/
make(elements?: Element[]): Model[] {
return elements
? elements.map((element) => this.child.$newInstance(element))
: []
}
}
7 changes: 7 additions & 0 deletions src/model/attributes/relations/HasOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ export class HasOne extends Relation {
return [result[this.foreignKey], result]
})
}

/**
* Make a related model.
*/
make(element?: Element): Model | null {
return element ? this.related.$newInstance(element) : null
}
}
8 changes: 0 additions & 8 deletions src/model/attributes/relations/Relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,4 @@ export abstract class Relation extends Attribute {
return dictionary
}, {})
}

/**
* Make the value for the attribute.
*/
/* istanbul ignore next */
make(_value: any): null {
return null
}
}
2 changes: 1 addition & 1 deletion src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class Repository<M extends Model = Model> {
*/
make(attributes?: Element): M {
return this.getModel().$newInstance(attributes, {
relations: false
relations: true
})
}

Expand Down
106 changes: 106 additions & 0 deletions test/unit/model/Model_Relations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { createStore } from 'test/Helpers'
import { Model, Attr, HasOne, BelongsTo, HasMany, HasManyBy } from '@/index'

describe('unit/model/Model_Relations', () => {
class User extends Model {
static entity = 'users'

@Attr() id!: number
@Attr() countryId!: number
@Attr() nameIds!: number[]

@HasOne(() => Phone, 'userId')
phone!: Phone | null

@BelongsTo(() => Country, 'countryId')
country!: Country | null

@HasMany(() => Post, 'userId')
posts!: Post[]

@HasManyBy(() => Name, 'nameIds')
names!: Name[]
}

class Phone extends Model {
static entity = 'phones'

@Attr() id!: number
@Attr() userId!: number
}

class Country extends Model {
static entity = 'countries'

@Attr() id!: number
}

class Post extends Model {
static entity = 'posts'

@Attr() id!: number
@Attr() userId!: number
}

class Name extends Model {
static entity = 'names'

@Attr() id!: number
}

it('fills "has one" relation', () => {
const store = createStore()

const user = store.$repo(User).make({
id: 1,
phone: {
id: 2
}
})

expect(user.phone).toBeInstanceOf(Phone)
expect(user.phone!.id).toBe(2)
})

it('fills "belongs to" relation', () => {
const store = createStore()

const user = store.$repo(User).make({
id: 1,
country: {
id: 2
}
})

expect(user.country).toBeInstanceOf(Country)
expect(user.country!.id).toBe(2)
})

it('fills "has many" relation', () => {
const store = createStore()

const user = store.$repo(User).make({
id: 1,
posts: [{ id: 2 }, { id: 3 }]
})

expect(user.posts[0]).toBeInstanceOf(Post)
expect(user.posts[1]).toBeInstanceOf(Post)
expect(user.posts[0].id).toBe(2)
expect(user.posts[1].id).toBe(3)
})

it('fills "has many by" relation', () => {
const store = createStore()

const user = store.$repo(User).make({
id: 1,
names: [{ id: 2 }, { id: 3 }]
})

expect(user.names[0]).toBeInstanceOf(Name)
expect(user.names[1]).toBeInstanceOf(Name)
expect(user.names[0].id).toBe(2)
expect(user.names[1].id).toBe(3)
})
})

0 comments on commit 9c47d46

Please sign in to comment.