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

RFC: Resolver for inclusion of related models #3387

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b24b9c2
feat: introduce Repository API for handling inclusion of related models
bajtos Jun 25, 2019
9522c7c
feat: generic HasMany inclusion resolver (in example only)
bajtos Jun 25, 2019
a707ba7
refactor: move inclusion-resolver to repository package
bajtos Jun 27, 2019
5295d69
feat: belongsTo inclusion resolver
bajtos Jun 27, 2019
c684710
feat: add HasOne inclusion resolver
bajtos Jul 1, 2019
5e318ea
docs: add SPIKE docs
bajtos Jul 1, 2019
96f205c
refactor: move uniq & isBsonType to a new helper file
bajtos Jul 2, 2019
3fcf088
refactor: extract findByForeignKeys
bajtos Jul 2, 2019
a624d97
feat(repository): add "keyFrom" to HasOne/HasMany resolved metadata
bajtos Jul 4, 2019
e9a776d
refactor(repository): introduce assignTargetsOf*Relation
bajtos Jul 4, 2019
b58757b
feat(repository): introduce Capabilities and implement inq splitting
bajtos Jul 4, 2019
672ed48
fix(repository): fix bug in splitByPageSize
bajtos Jul 8, 2019
38d4804
feat: update after retrieving data with related models
bajtos Jul 8, 2019
fafec31
docs: document the spike and the proposal
bajtos Jul 18, 2019
9088a0b
fix: rename this.registerForbiddenInclusion
bajtos Jul 19, 2019
71ae5fe
refactor: rework resolver lookup table from Object to Map
bajtos Jul 19, 2019
56cdcd0
refactor: rework inclusion resolvers to simple functions
bajtos Jul 19, 2019
9475912
refactor: simplify InclusionResolver to return an array of targets
bajtos Jul 19, 2019
5f06621
feat: expose resolvers on relation repository factories
bajtos Jul 19, 2019
68bfa00
fix: address code review feedback
bajtos Jul 22, 2019
52548c9
fix: typo
bajtos Jul 22, 2019
0948683
test: temporarily skip failing test
bajtos Jul 22, 2019
2fa5df6
docs: add the a list of implementation tasks
bajtos Jul 23, 2019
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
772 changes: 772 additions & 0 deletions _SPIKE_.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ export const MONGODB_CONFIG: DataSourceOptions = {

export const MONGODB_FEATURES: Partial<CrudFeatures> = {
idType: 'string',

// TODO: we should run the test suite against two connector configurations:
// - one with "strictObjectID" set to true,
// - the other with "strictObjectID" turned off (the default).
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
givenEmptyDatabase,
givenTodoInstance,
givenTodoListImageInstance,
givenTodoListInstance,
testdb,
} from '../helpers';
Expand All @@ -23,6 +24,10 @@ describe('TodoListRepository', () => {
async () => todoListImageRepo,
);
todoRepo = new TodoRepository(testdb, async () => todoListRepo);
todoListImageRepo = new TodoListImageRepository(
testdb,
async () => todoListRepo,
);
});

beforeEach(givenEmptyDatabase);
Expand All @@ -43,6 +48,21 @@ describe('TodoListRepository', () => {
]);
});

it('includes Todos in findOne method result', async () => {
const list = await givenTodoListInstance(todoListRepo);
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});

const response = await todoListRepo.findOne({
where: {id: list.id},
include: [{relation: 'todos'}],
});

expect(toJSON(response)).to.deepEqual({
...toJSON(list),
todos: [toJSON(todo)],
});
});

it('includes Todos in findById result', async () => {
const list = await givenTodoListInstance(todoListRepo);
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});
Expand All @@ -56,4 +76,22 @@ describe('TodoListRepository', () => {
todos: [toJSON(todo)],
});
});

it('includes TodoListImage in find method result', async () => {
const list = await givenTodoListInstance(todoListRepo);
const image = await givenTodoListImageInstance(todoListImageRepo, {
todoListId: list.id,
});

const response = await todoListRepo.find({
include: [{relation: 'image'}],
});

expect(toJSON(response)).to.deepEqual([
{
...toJSON(list),
image: toJSON(image),
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ describe('TodoRepository', () => {
todoList: toJSON(list),
});
});

it('includes TodoList in findOne result', async () => {
const list = await givenTodoListInstance(todoListRepo, {});
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});

const response = await todoRepo.findOne({
where: {id: todo.id},
include: [{relation: 'todoList'}],
});

expect(toJSON(response)).to.deepEqual({
...toJSON(todo),
todoList: toJSON(list),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import {Getter, inject} from '@loopback/core';
import {
BelongsToAccessor,
DefaultCrudRepository,
Filter,
Options,
repository,
} from '@loopback/repository';
import {DbDataSource} from '../datasources';
import {
TodoList,
TodoListImage,
TodoListImageRelations,
TodoListImageWithRelations,
} from '../models';
import {TodoList, TodoListImage, TodoListImageRelations} from '../models';
import {TodoListRepository} from './todo-list.repository';

export class TodoListImageRepository extends DefaultCrudRepository<
Expand All @@ -39,55 +32,6 @@ export class TodoListImageRepository extends DefaultCrudRepository<
'todoList',
todoListRepositoryGetter,
);
}

async find(
filter?: Filter<TodoListImage>,
options?: Options,
): Promise<TodoListImageWithRelations[]> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = {...filter, include: undefined};

const result = await super.find(filter, options);

// poor-mans inclusion resolver, this should be handled by DefaultCrudRepo
// and use `inq` operator to fetch related todo-lists in fewer DB queries
// this is a temporary implementation, please see
// https://github.com/strongloop/loopback-next/issues/3195
if (include && include.length && include[0].relation === 'todoList') {
await Promise.all(
result.map(async r => {
// eslint-disable-next-line require-atomic-updates
r.todoList = await this.todoList(r.id);
}),
);
}

return result;
}

async findById(
id: typeof TodoListImage.prototype.id,
filter?: Filter<TodoListImage>,
options?: Options,
): Promise<TodoListImageWithRelations> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = {...filter, include: undefined};

const result = await super.findById(id, filter, options);

// poor-mans inclusion resolver, this should be handled by DefaultCrudRepo
// and use `inq` operator to fetch related todo-lists in fewer DB queries
// this is a temporary implementation, please see
// https://github.com/strongloop/loopback-next/issues/3195
if (include && include.length && include[0].relation === 'todoList') {
result.todoList = await this.todoList(result.id);
}

return result;
this.registerInclusion('todoList', this.todoList.inclusionResolver);
}
}
63 changes: 5 additions & 58 deletions examples/todo-list/src/repositories/todo-list.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,12 @@
import {Getter, inject} from '@loopback/core';
import {
DefaultCrudRepository,
Filter,
HasManyRepositoryFactory,
HasOneRepositoryFactory,
juggler,
Options,
repository,
} from '@loopback/repository';
import {
Todo,
TodoList,
TodoListImage,
TodoListRelations,
TodoListWithRelations,
} from '../models';
import {Todo, TodoList, TodoListImage, TodoListRelations} from '../models';
import {TodoListImageRepository} from './todo-list-image.repository';
import {TodoRepository} from './todo.repository';

Expand All @@ -45,66 +37,21 @@ export class TodoListRepository extends DefaultCrudRepository<
protected todoListImageRepositoryGetter: Getter<TodoListImageRepository>,
) {
super(TodoList, dataSource);

this.todos = this.createHasManyRepositoryFactoryFor(
'todos',
todoRepositoryGetter,
);
this.registerInclusion('todos', this.todos.inclusionResolver);

this.image = this.createHasOneRepositoryFactoryFor(
'image',
todoListImageRepositoryGetter,
);
this.registerInclusion('image', this.image.inclusionResolver);
}

public findByTitle(title: string) {
return this.findOne({where: {title}});
}

async find(
filter?: Filter<TodoList>,
options?: Options,
): Promise<TodoListWithRelations[]> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = {...filter, include: undefined};
const result = await super.find(filter, options);

// poor-mans inclusion resolver, this should be handled by DefaultCrudRepo
// and use `inq` operator to fetch related todos in fewer DB queries
// this is a temporary implementation, please see
// https://github.com/strongloop/loopback-next/issues/3195
if (include && include.length && include[0].relation === 'todos') {
await Promise.all(
result.map(async r => {
// eslint-disable-next-line require-atomic-updates
r.todos = await this.todos(r.id).find();
}),
);
}

return result;
}

async findById(
id: typeof TodoList.prototype.id,
filter?: Filter<TodoList>,
options?: Options,
): Promise<TodoListWithRelations> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = {...filter, include: undefined};

const result = await super.findById(id, filter, options);

// poor-mans inclusion resolver, this should be handled by DefaultCrudRepo
// and use `inq` operator to fetch related todos in fewer DB queries
// this is a temporary implementation, please see
// https://github.com/strongloop/loopback-next/issues/3195
if (include && include.length && include[0].relation === 'todos') {
result.todos = await this.todos(result.id).find();
}

return result;
}
}
55 changes: 2 additions & 53 deletions examples/todo-list/src/repositories/todo.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import {Getter, inject} from '@loopback/core';
import {
BelongsToAccessor,
DefaultCrudRepository,
Filter,
juggler,
Options,
repository,
} from '@loopback/repository';
import {Todo, TodoList, TodoRelations, TodoWithRelations} from '../models';
import {Todo, TodoList, TodoRelations} from '../models';
import {TodoListRepository} from './todo-list.repository';

export class TodoRepository extends DefaultCrudRepository<
Expand All @@ -36,55 +34,6 @@ export class TodoRepository extends DefaultCrudRepository<
'todoList',
todoListRepositoryGetter,
);
}

async find(
filter?: Filter<Todo>,
options?: Options,
): Promise<TodoWithRelations[]> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = {...filter, include: undefined};

const result = await super.find(filter, options);

// poor-mans inclusion resolver, this should be handled by DefaultCrudRepo
// and use `inq` operator to fetch related todo-lists in fewer DB queries
// this is a temporary implementation, please see
// https://github.com/strongloop/loopback-next/issues/3195
if (include && include.length && include[0].relation === 'todoList') {
await Promise.all(
result.map(async r => {
// eslint-disable-next-line require-atomic-updates
r.todoList = await this.todoList(r.id);
}),
);
}

return result;
}

async findById(
id: typeof Todo.prototype.id,
filter?: Filter<Todo>,
options?: Options,
): Promise<TodoWithRelations> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = {...filter, include: undefined};

const result = await super.findById(id, filter, options);

// poor-mans inclusion resolver, this should be handled by DefaultCrudRepo
// and use `inq` operator to fetch related todo-lists in fewer DB queries
// this is a temporary implementation, please see
// https://github.com/strongloop/loopback-next/issues/3195
if (include && include.length && include[0].relation === 'todoList') {
result.todoList = await this.todoList(result.id);
}

return result;
this.registerInclusion('todoList', this.todoList.inclusionResolver);
}
}
3 changes: 2 additions & 1 deletion packages/repository-tests/src/crud-test-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function crudRepositoryTestSuite(
const features: CrudFeatures = {
idType: 'string',
freeFormProperties: true,
inclusionResolvers: true,
...partialFeatures,
};

Expand Down Expand Up @@ -70,7 +71,7 @@ export function crudRepositoryTestSuite(
suite.name,
dataSourceOptions,
'class ' + repositoryClass.name,
partialFeatures,
features,
);
suite(dataSourceOptions, repositoryClass, features);
}
Expand Down
Loading