Skip to content

Commit

Permalink
fixup! apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
biniam committed Dec 3, 2018
1 parent 77b033a commit 3e3a9ac
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {TodoListRepository} from '../repositories';
import {repository, Filter} from '@loopback/repository';
import {repository} from '@loopback/repository';
import {param, post, requestBody, get} from '@loopback/rest';
import {TodoListImage} from '../models';

export class TodoListAuthorController {
export class TodoListImageController {
constructor(
@repository(TodoListRepository) protected todoListRepo: TodoListRepository,
) {}
Expand Down
1 change: 0 additions & 1 deletion examples/todo-list/src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@

export * from './todo.repository';
export * from './todo-list.repository';
export * from './author.repository';
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {BelongsToDefinition, RelationType} from '../relation.types';

/**
* Decorator for belongsTo
* @param targetResolver
* @param definition
* @param targetResolver A resolver function that retruns the target model for
* a belongsTo relation
* @param definition Optional metadata for setting up a belongsTo relation
* @param propertyMeta Optional property metadata to call the proprety decorator
* with
* @returns {(target: Object, key:string)}
*/
export function belongsTo<T extends Entity>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export type HasManyRepositoryFactory<Target extends Entity, ForeignKeyType> = (
* via a HasMany relation, then, the relational repository returned by the
* factory function would be constrained by a Customer model instance's id(s).
*
* @param relationMeta The relation metadata used to describe the
* @param relationMetadata The relation metadata used to describe the
* relationship and determine how to apply the constraint.
* @param targetRepo The repository which represents the target model of a
* @param targetRepositoryGetter The repository which represents the target model of a
* relation attached to a datasource.
* @returns The factory function which accepts a foreign key value to constrain
* the given target repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export type HasOneRepositoryFactory<Target extends Entity, ForeignKeyType> = (
* via a HasOne relation, then, the relational repository returned by the
* factory function would be constrained by a Customer model instance's id(s).
*
* @param relationMeta The relation metadata used to describe the
* @param relationMetadata The relation metadata used to describe the
* relationship and determine how to apply the constraint.
* @param targetRepo The repository which represents the target model of a
* @param targetRepositoryGetter The repository which represents the target model of a
* relation attached to a datasource.
* @returns The factory function which accepts a foreign key value to constrain
* the given target repository
Expand Down Expand Up @@ -56,10 +56,10 @@ export function createHasOneRepositoryFactory<
type HasOneResolvedDefinition = HasOneDefinition & {keyTo: string};

/**
* Resolves given hasMany metadata if target is specified to be a resolver.
* Resolves given hasOne metadata if target is specified to be a resolver.
* Mainly used to infer what the `keyTo` property should be from the target's
* belongsTo metadata
* @param relationMeta hasMany metadata to resolve
* hasOne metadata
* @param relationMeta hasOne metadata to resolve
*/
function resolveHasOneMetadata(
relationMeta: HasOneDefinition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {Filter, Where} from '../../query';
import {
constrainDataObject,
constrainFilter,
} from '../../repositories/constraint-utils';
import {EntityCrudRepository} from '../../repositories/repository';
EntityCrudRepository,
} from '../../repositories';
import {EntityNotFoundError} from '../../errors';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {Application} from '@loopback/core';
import {expect} from '@loopback/testlab';
import {expect, toJSON} from '@loopback/testlab';
import * as _ from 'lodash';
import {
ApplicationWithRepositories,
Expand All @@ -15,7 +15,6 @@ import {
} from '../..';
import {Address} from '../fixtures/models';
import {CustomerRepository, AddressRepository} from '../fixtures/repositories';
import {Where} from '../..';

describe('hasOne relation', () => {
// Given a Customer and Address models - see definitions at the bottom
Expand All @@ -32,9 +31,6 @@ describe('hasOne relation', () => {

beforeEach(async () => {
await addressRepo.deleteAll();
});

beforeEach(async () => {
existingCustomerId = (await givenPersistedCustomerInstance()).id;
});

Expand All @@ -51,7 +47,7 @@ describe('hasOne relation', () => {
expect(persisted.toObject()).to.deepEqual(address.toObject());
});

it("doesn't allow to create related model instance twice", async () => {
it('refuses to create related model instance twice', async () => {
const address = await controller.createCustomerAddress(existingCustomerId, {
street: '123 test avenue',
});
Expand Down Expand Up @@ -83,15 +79,15 @@ describe('hasOne relation', () => {
existingCustomerId,
);
expect(foundAddress).to.containEql(address);
expect(foundAddress).to.not.containEql(notMyAddress);
expect(toJSON(foundAddress)).to.deepEqual(toJSON(address));

const persisted = await addressRepo.find({
where: {customerId: existingCustomerId},
});
expect(persisted[0]).to.deepEqual(foundAddress);
});

it('does not allow where filter to find related model instance', async () => {
it.only('does not allow where filter to find related model instance', async () => {
const address = await controller.createCustomerAddress(existingCustomerId, {
street: '123 test avenue',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('createHasOneRepositoryFactory', () => {
it('rejects relations with keyTo that is not an id', () => {
const relationMeta = givenHasOneDefinition({
name: 'order',
target: () => Order,
target: () => ModelWithoutIndexFK,
});

expect(() =>
Expand All @@ -97,7 +97,7 @@ describe('createHasOneRepositoryFactory', () => {
it('rejects relations with keyTo that is a generated id property', () => {
const relationMeta = givenHasOneDefinition({
name: 'profile',
target: () => Profile,
target: () => ModelWithGeneratedFK,
});

expect(() =>
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('createHasOneRepositoryFactory', () => {
province: String;
}

class Order extends Entity {
class ModelWithoutIndexFK extends Entity {
static definition = new ModelDefinition('Order')
.addProperty('customerId', {
type: 'string',
Expand All @@ -142,18 +142,23 @@ describe('createHasOneRepositoryFactory', () => {
type: 'string',
required: true,
})
.addProperty('id', {
type: 'number',
id: true,
})

.addProperty('isShipped', {
type: 'boolean',
required: false,
});
id: number;
customerId: string;
description: string;
isShipped: boolean;
}

class Profile extends Entity {
static definition = new ModelDefinition('Profile')
class ModelWithGeneratedFK extends Entity {
static definition = new ModelDefinition('ModelWithGeneratedFK')
.addProperty('customerId', {
type: 'string',
id: true,
Expand Down

0 comments on commit 3e3a9ac

Please sign in to comment.