Skip to content

Commit

Permalink
test(repository): use custom name for foreign key in relation
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jul 9, 2019
1 parent bdf5d7a commit c843223
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Application} from '@loopback/core';
import {expect} from '@loopback/testlab';
import {
ApplicationWithRepositories,
juggler,
repository,
RepositoryMixin,
ApplicationWithRepositories,
} from '../..';
import {CustomerRepository, OrderRepository} from '../fixtures/repositories';
import {expect} from '@loopback/testlab';
import {Application} from '@loopback/core';
import {
CustomerRepository,
OrderRepository,
ShipmentRepository,
} from '../fixtures/repositories';

describe('BelongsTo relation', () => {
// Given a Customer and Order models - see definitions at the bottom
Expand All @@ -20,9 +24,10 @@ describe('BelongsTo relation', () => {
let controller: OrderController;
let customerRepo: CustomerRepository;
let orderRepo: OrderRepository;
let shipmentRepo: ShipmentRepository;

before(givenApplicationWithMemoryDB);
before(givenBoundCrudRepositoriesForCustomerAndOrder);
before(givenBoundCrudRepositories);
before(givenOrderController);

beforeEach(async () => {
Expand All @@ -39,6 +44,19 @@ describe('BelongsTo relation', () => {
expect(result).to.deepEqual(customer);
});

it('can find shipment of order with a custom foreign key name', async () => {
const shipment = await shipmentRepo.create({
name: 'Tuesday morning shipment',
});
const order = await orderRepo.create({
// eslint-disable-next-line @typescript-eslint/camelcase
shipment_id: shipment.id,
description: 'Order that is shipped Tuesday morning',
});
const result = await controller.findOrderShipment(order.id);
expect(result).to.deepEqual(shipment);
});

//--- HELPERS ---//

class OrderController {
Expand All @@ -49,6 +67,10 @@ describe('BelongsTo relation', () => {
async findOwnerOfOrder(orderId: string) {
return await this.orderRepository.customer(orderId);
}

async findOrderShipment(orderId: string) {
return await this.orderRepository.shipment(orderId);
}
}

function givenApplicationWithMemoryDB() {
Expand All @@ -57,11 +79,13 @@ describe('BelongsTo relation', () => {
app.dataSource(new juggler.DataSource({name: 'db', connector: 'memory'}));
}

async function givenBoundCrudRepositoriesForCustomerAndOrder() {
async function givenBoundCrudRepositories() {
app.repository(CustomerRepository);
app.repository(OrderRepository);
app.repository(ShipmentRepository);
customerRepo = await app.getRepository(CustomerRepository);
orderRepo = await app.getRepository(OrderRepository);
shipmentRepo = await app.getRepository(ShipmentRepository);
}

async function givenOrderController() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {belongsTo, Entity, hasMany, hasOne, model, property} from '../../..';
import {Address, AddressWithRelations} from './address.model';
import {Order} from './order.model';
import {Order, OrderWithRelations} from './order.model';

@model()
export class Customer extends Entity {
Expand Down Expand Up @@ -35,6 +35,7 @@ export class Customer extends Entity {

export interface CustomerRelations {
address?: AddressWithRelations;
orders?: OrderWithRelations[];
customers?: CustomerWithRelations[];
parentCustomer?: CustomerWithRelations;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/repository/src/__tests__/fixtures/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './address.model';
export * from './customer.model';
export * from './order.model';
export * from './product.model';
export * from './address.model';
export * from './shipment.model';
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {belongsTo, Entity, model, property} from '../../..';
import {Customer, CustomerWithRelations} from './customer.model';
import {Shipment, ShipmentWithRelations} from './shipment.model';

@model()
export class Order extends Entity {
Expand All @@ -28,10 +29,14 @@ export class Order extends Entity {

@belongsTo(() => Customer)
customerId: number;

@belongsTo(() => Shipment)
shipment_id: number;
}

export interface OrderRelations {
customer?: CustomerWithRelations;
shipment?: ShipmentWithRelations;
}

export type OrderWithRelations = Order & OrderRelations;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Entity, hasMany, model, property} from '../../..';
import {Order, OrderWithRelations} from './order.model';

@model()
export class Shipment extends Entity {
@property({
type: 'number',
id: true,
})
id: number;

@property({type: 'string'})
name: string;

@hasMany(() => Order, {keyTo: 'shipment_id'})
shipmentOrders: Order[];

constructor(data?: Partial<Shipment>) {
super(data);
}
}

export interface ShipmentRelations {
orders?: OrderWithRelations[];
}

export type ShipmentWithRelations = Shipment & ShipmentRelations;
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AddressRepository extends DefaultCrudRepository<
customerRepositoryGetter: Getter<CustomerRepository>,
) {
super(Address, db);
this.customer = this._createBelongsToAccessorFor(
this.customer = this.createBelongsToAccessorFor(
'customer',
customerRepositoryGetter,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './address.repository';
export * from './customer.repository';
export * from './order.repository';
export * from './product.repository';
export * from './address.repository';
export * from './shipment.repository';
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
juggler,
repository,
} from '../../..';
import {Customer, Order, OrderRelations} from '../models';
import {CustomerRepository} from '../repositories';
import {Customer, Order, OrderRelations, Shipment} from '../models';
import {CustomerRepository, ShipmentRepository} from '../repositories';

export class OrderRepository extends DefaultCrudRepository<
Order,
Expand All @@ -22,16 +22,26 @@ export class OrderRepository extends DefaultCrudRepository<
Customer,
typeof Order.prototype.id
>;
public readonly shipment: BelongsToAccessor<
Shipment,
typeof Order.prototype.id
>;

constructor(
@inject('datasources.db') protected db: juggler.DataSource,
@repository.getter('CustomerRepository')
customerRepositoryGetter: Getter<CustomerRepository>,
@repository.getter('ShipmentRepository')
shipmentRepositoryGetter: Getter<ShipmentRepository>,
) {
super(Order, db);
this.customer = this.createBelongsToAccessorFor(
'customer',
customerRepositoryGetter,
);
this.shipment = this.createBelongsToAccessorFor(
'shipment_id',
shipmentRepositoryGetter,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Getter, inject} from '@loopback/context';
import {OrderRepository} from '.';
import {
DefaultCrudRepository,
HasManyRepositoryFactory,
juggler,
repository,
} from '../../..';
import {Order, Shipment, ShipmentRelations} from '../models';

export class ShipmentRepository extends DefaultCrudRepository<
Shipment,
typeof Shipment.prototype.id,
ShipmentRelations
> {
public readonly orders: HasManyRepositoryFactory<
Order,
typeof Shipment.prototype.id
>;

constructor(
@inject('datasources.db') protected db: juggler.DataSource,
@repository.getter('OrderRepository')
orderRepositoryGetter: Getter<OrderRepository>,
) {
super(Shipment, db);
this.orders = this.createHasManyRepositoryFactoryFor(
'shipmentOrders',
orderRepositoryGetter,
);
}
}

0 comments on commit c843223

Please sign in to comment.