Skip to content

Commit

Permalink
feat(repository): add belongsToUniquely sugar syntax method
Browse files Browse the repository at this point in the history
  • Loading branch information
biniam authored and b-admike committed Dec 4, 2018
1 parent 1ad970f commit 1b5b66a
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 264 deletions.
122 changes: 0 additions & 122 deletions examples/todo-list/src/controllers/author.controller.ts

This file was deleted.

3 changes: 1 addition & 2 deletions examples/todo-list/src/controllers/index.ts
Expand Up @@ -6,5 +6,4 @@
export * from './todo.controller';
export * from './todo-list.controller';
export * from './todo-list-todo.controller';
export * from './todo-list-author.controller';
export * from './author.controller';
export * from './todo-list-image.controller';
37 changes: 0 additions & 37 deletions examples/todo-list/src/controllers/todo-list-author.controller.ts

This file was deleted.

37 changes: 37 additions & 0 deletions examples/todo-list/src/controllers/todo-list-image.controller.ts
@@ -0,0 +1,37 @@
import {TodoListRepository} from '../repositories';
import {repository} from '@loopback/repository';
import {param, post, requestBody, get} from '@loopback/rest';
import {TodoListImage} from '../models';

export class TodoListImageController {
constructor(
@repository(TodoListRepository) protected todoListRepo: TodoListRepository,
) {}

@post('/todo-lists/{id}/image', {
responses: {
'200': {
description: 'create todoListImage model instance',
content: {'application/json': {schema: {'x-ts-type': TodoListImage}}},
},
},
})
async create(
@param.path.number('id') id: number,
@requestBody() image: TodoListImage,
): Promise<TodoListImage> {
return await this.todoListRepo.image(id).create(image);
}

@get('/todo-lists/{id}/image', {
responses: {
'200': {
description: 'The image belonging to the TodoList',
content: {'application/json': {schema: {'x-ts-type': TodoListImage}}},
},
},
})
async find(@param.path.number('id') id: number): Promise<TodoListImage> {
return await this.todoListRepo.image(id).get();
}
}
26 changes: 0 additions & 26 deletions examples/todo-list/src/models/author.model.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/todo-list/src/models/index.ts
Expand Up @@ -5,4 +5,4 @@

export * from './todo.model';
export * from './todo-list.model';
export * from './author.model';
export * from './todo-list-image.model';
20 changes: 20 additions & 0 deletions examples/todo-list/src/models/todo-list-image.model.ts
@@ -0,0 +1,20 @@
import {Entity, model, property, belongsToUniquely} from '@loopback/repository';
import {TodoList} from './todo-list.model';

@model()
export class TodoListImage extends Entity {
@belongsToUniquely(() => TodoList)
todoListId?: number;

@property({
required: true,
})
// Ideally we would use Buffer type here, but
// that is not supported yet.
// see https://github.com/strongloop/loopback-next/issues/1742
value: string;

constructor(data?: Partial<TodoListImage>) {
super(data);
}
}
6 changes: 3 additions & 3 deletions examples/todo-list/src/models/todo-list.model.ts
Expand Up @@ -5,7 +5,7 @@

import {Entity, model, property, hasMany, hasOne} from '@loopback/repository';
import {Todo} from './todo.model';
import {Author} from './author.model';
import {TodoListImage} from './todo-list-image.model';

@model()
export class TodoList extends Entity {
Expand All @@ -29,8 +29,8 @@ export class TodoList extends Entity {
@hasMany(() => Todo)
todos: Todo[];

@hasOne(() => Author)
author: Author;
@hasOne(() => TodoListImage)
image: TodoListImage;

constructor(data?: Partial<TodoList>) {
super(data);
Expand Down
1 change: 0 additions & 1 deletion examples/todo-list/src/repositories/index.ts
Expand Up @@ -5,4 +5,3 @@

export * from './todo.repository';
export * from './todo-list.repository';
export * from './author.repository';
@@ -1,28 +1,27 @@
import {
DefaultCrudRepository,
juggler,
repository,
BelongsToAccessor,
} from '@loopback/repository';
import {Author, TodoList} from '../models';
import {TodoListImage, TodoList} from '../models';
import {DbDataSource} from '../datasources';
import {inject, Getter} from '@loopback/core';
import {TodoListRepository} from './todo-list.repository';

export class AuthorRepository extends DefaultCrudRepository<
Author,
typeof Author.prototype.todoListId
export class TodoListImageRepository extends DefaultCrudRepository<
TodoListImage,
typeof TodoListImage.prototype.todoListId
> {
public readonly todoList: BelongsToAccessor<
TodoList,
typeof Author.prototype.todoListId
typeof TodoListImage.prototype.todoListId
>;
constructor(
@inject('datasources.db') dataSource: DbDataSource,
@repository.getter('TodoListRepository')
protected todoListRepositoryGetter: Getter<TodoListRepository>,
) {
super(Author, dataSource);
super(TodoListImage, dataSource);
this.todoList = this._createBelongsToAccessorFor(
'todoList',
todoListRepositoryGetter,
Expand Down
16 changes: 8 additions & 8 deletions examples/todo-list/src/repositories/todo-list.repository.ts
Expand Up @@ -11,9 +11,9 @@ import {
repository,
HasOneRepositoryFactory,
} from '@loopback/repository';
import {Todo, TodoList, Author} from '../models';
import {Todo, TodoList, TodoListImage} from '../models';
import {TodoRepository} from './todo.repository';
import {AuthorRepository} from './author.repository';
import {TodoListImageRepository} from './todo-list-image.repository';

export class TodoListRepository extends DefaultCrudRepository<
TodoList,
Expand All @@ -23,25 +23,25 @@ export class TodoListRepository extends DefaultCrudRepository<
Todo,
typeof TodoList.prototype.id
>;
public readonly author: HasOneRepositoryFactory<
Author,
public readonly image: HasOneRepositoryFactory<
TodoListImage,
typeof TodoList.prototype.id
>;

constructor(
@inject('datasources.db') dataSource: juggler.DataSource,
@repository.getter('TodoRepository')
protected todoRepositoryGetter: Getter<TodoRepository>,
@repository.getter('AuthorRepository')
protected todoListImageRepositoryGetter: Getter<AuthorRepository>,
@repository.getter('TodoListImageRepository')
protected todoListImageRepositoryGetter: Getter<TodoListImageRepository>,
) {
super(TodoList, dataSource);
this.todos = this._createHasManyRepositoryFactoryFor(
'todos',
todoRepositoryGetter,
);
this.author = this._createHasOneRepositoryFactoryFor(
'author',
this.image = this._createHasOneRepositoryFactoryFor(
'image',
todoListImageRepositoryGetter,
);
}
Expand Down

0 comments on commit 1b5b66a

Please sign in to comment.