Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withRecursive method #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ export class Query<M extends Model = Model> {
return this
}

/**
* Set to eager load the passed relationship recursively.
* Should be a parent / child relationship within a single model.
*/
withRecursive(name: string, depth: number = 3): Query<M> {
this.with(name, (query) => {
depth > 0 && query.withRecursive(name, depth - 1)
})

return this
}

/**
* Set to eager load all top-level relationships. Constraint is set for all relationships.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ export class Repository<M extends Model = Model> {
return this.query().with(name, callback)
}

/**
* Set the relationship that should be eager loaded recursively.
* Should be a parent / child relationship within a single model.
*/
withRecursive(name: string, depth?: number): Query<M> {
return this.query().withRecursive(name, depth)
}

/**
* Set to eager load all top-level relationships. Constraint is set for all relationships.
*/
Expand Down
118 changes: 117 additions & 1 deletion test/feature/relations/eager_loads/eager_loads_recursive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertModel, createStore, fillState } from 'test/Helpers'
import { Model, Attr, Str, BelongsTo, HasOne } from '@/index'
import { Model, Attr, Str, BelongsTo, HasOne, Num, HasMany } from '@/index'

describe('feature/relations/eager_loads_recursive', () => {
class User extends Model {
Expand All @@ -23,6 +23,17 @@ describe('feature/relations/eager_loads_recursive', () => {
user!: User
}

class Node extends Model {
static entity = 'nodes'

@Attr() id!: number
@Str('') name!: string

@Num(null, { nullable: true }) parentId!: number | null
@HasMany(() => Node, 'parentId') children!: Node[]
@BelongsTo(() => Node, 'parentId') parent!: Node
}

it('eager loads all relations recursively', () => {
const store = createStore()

Expand Down Expand Up @@ -66,4 +77,109 @@ describe('feature/relations/eager_loads_recursive', () => {
name: 'John Doe'
})
})

it('eager loads all parents recursively', () => {
const store = createStore()

fillState(store, {
nodes: {
1: { id: 1, name: 'Root', parentId: null },
2: { id: 2, name: 'Root Child 1', parentId: 1 },
3: { id: 3, name: 'Root Child 2', parentId: 1 },
4: { id: 4, name: 'Grandchild', parentId: 3 }
}
})

const user = store.$repo(Node).withRecursive('parent').find(4)!

expect(user.parent).toBeInstanceOf(Node)
expect(user.parent.parent).toBeInstanceOf(Node)
assertModel(user.parent.parent, {
id: 1,
name: 'Root',
parentId: null,
parent: null,
children: undefined
})
})

it('eager loads all parents recursively with limit', () => {
const store = createStore()

fillState(store, {
nodes: {
1: { id: 1, name: 'Root', parentId: null },
2: { id: 2, name: 'Root Child 1', parentId: 1 },
3: { id: 3, name: 'Root Child 2', parentId: 1 },
4: { id: 4, name: 'Grandchild', parentId: 3 },
5: { id: 5, name: 'Great Grandchild', parentId: 4 }
}
})

const user = store.$repo(Node).withRecursive('parent', 1).find(5)!

expect(user.parent).toBeInstanceOf(Node)
expect(user.parent.parent).toBeInstanceOf(Node)
expect(user.parent.parent.parent).toBeUndefined()
assertModel(user.parent.parent, {
id: 3,
name: 'Root Child 2',
parentId: 1,
parent: undefined,
children: undefined
})
})

it('eager loads all children recursively', () => {
const store = createStore()

fillState(store, {
nodes: {
1: { id: 1, name: 'Root', parentId: null },
2: { id: 2, name: 'Root Child 1', parentId: 1 },
3: { id: 3, name: 'Root Child 2', parentId: 1 },
4: { id: 4, name: 'Grandchild', parentId: 3 }
}
})

const user = store.$repo(Node).withRecursive('children').find(1)!

expect(user.children).toHaveLength(2)
expect(user.children[1].children).toHaveLength(1)
expect(user.children[1].children[0]).toBeInstanceOf(Node)
assertModel(user.children[1].children[0], {
id: 4,
name: 'Grandchild',
parentId: 3,
parent: undefined,
children: []
})
})

it('eager loads all children recursively with limit', () => {
const store = createStore()

fillState(store, {
nodes: {
1: { id: 1, name: 'Root', parentId: null },
2: { id: 2, name: 'Root Child 1', parentId: 1 },
3: { id: 3, name: 'Root Child 2', parentId: 1 },
4: { id: 4, name: 'Grandchild', parentId: 3 },
5: { id: 5, name: 'Great Grandchild', parentId: 4 }
}
})

const user = store.$repo(Node).withRecursive('children', 1).find(1)!

expect(user.children).toHaveLength(2)
expect(user.children[1].children).toHaveLength(1)
expect(user.children[1].children[0]).toBeInstanceOf(Node)
assertModel(user.children[1].children[0], {
id: 4,
name: 'Grandchild',
parentId: 3,
parent: undefined,
children: undefined
})
})
})