diff --git a/packages/repository/src/__tests__/acceptance/belongs-to.relation.acceptance.ts b/packages/repository/src/__tests__/acceptance/belongs-to.relation.acceptance.ts index 135db145af54..e4ea0e085e0d 100644 --- a/packages/repository/src/__tests__/acceptance/belongs-to.relation.acceptance.ts +++ b/packages/repository/src/__tests__/acceptance/belongs-to.relation.acceptance.ts @@ -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 @@ -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 () => { @@ -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 { @@ -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() { @@ -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() { diff --git a/packages/repository/src/__tests__/fixtures/models/customer.model.ts b/packages/repository/src/__tests__/fixtures/models/customer.model.ts index ae7bf09d12a2..fd18dc9e82dd 100644 --- a/packages/repository/src/__tests__/fixtures/models/customer.model.ts +++ b/packages/repository/src/__tests__/fixtures/models/customer.model.ts @@ -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 { @@ -35,6 +35,7 @@ export class Customer extends Entity { export interface CustomerRelations { address?: AddressWithRelations; + orders?: OrderWithRelations[]; customers?: CustomerWithRelations[]; parentCustomer?: CustomerWithRelations; } diff --git a/packages/repository/src/__tests__/fixtures/models/index.ts b/packages/repository/src/__tests__/fixtures/models/index.ts index be01cc1bfd19..9c1ee60840f0 100644 --- a/packages/repository/src/__tests__/fixtures/models/index.ts +++ b/packages/repository/src/__tests__/fixtures/models/index.ts @@ -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'; diff --git a/packages/repository/src/__tests__/fixtures/models/order.model.ts b/packages/repository/src/__tests__/fixtures/models/order.model.ts index 3e6ab1504e8b..4202f85f9cc5 100644 --- a/packages/repository/src/__tests__/fixtures/models/order.model.ts +++ b/packages/repository/src/__tests__/fixtures/models/order.model.ts @@ -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 { @@ -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; diff --git a/packages/repository/src/__tests__/fixtures/models/shipment.model.ts b/packages/repository/src/__tests__/fixtures/models/shipment.model.ts new file mode 100644 index 000000000000..cf9787af8448 --- /dev/null +++ b/packages/repository/src/__tests__/fixtures/models/shipment.model.ts @@ -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) { + super(data); + } +} + +export interface ShipmentRelations { + orders?: OrderWithRelations[]; +} + +export type ShipmentWithRelations = Shipment & ShipmentRelations; diff --git a/packages/repository/src/__tests__/fixtures/repositories/address.repository.ts b/packages/repository/src/__tests__/fixtures/repositories/address.repository.ts index 3be973cef13c..9b86f39706ac 100644 --- a/packages/repository/src/__tests__/fixtures/repositories/address.repository.ts +++ b/packages/repository/src/__tests__/fixtures/repositories/address.repository.ts @@ -29,7 +29,7 @@ export class AddressRepository extends DefaultCrudRepository< customerRepositoryGetter: Getter, ) { super(Address, db); - this.customer = this._createBelongsToAccessorFor( + this.customer = this.createBelongsToAccessorFor( 'customer', customerRepositoryGetter, ); diff --git a/packages/repository/src/__tests__/fixtures/repositories/index.ts b/packages/repository/src/__tests__/fixtures/repositories/index.ts index 1052585f6f6b..30f070573c57 100644 --- a/packages/repository/src/__tests__/fixtures/repositories/index.ts +++ b/packages/repository/src/__tests__/fixtures/repositories/index.ts @@ -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'; diff --git a/packages/repository/src/__tests__/fixtures/repositories/order.repository.ts b/packages/repository/src/__tests__/fixtures/repositories/order.repository.ts index 2d83b256458e..e6dfbcc2d69d 100644 --- a/packages/repository/src/__tests__/fixtures/repositories/order.repository.ts +++ b/packages/repository/src/__tests__/fixtures/repositories/order.repository.ts @@ -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, @@ -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, + @repository.getter('ShipmentRepository') + shipmentRepositoryGetter: Getter, ) { super(Order, db); this.customer = this.createBelongsToAccessorFor( 'customer', customerRepositoryGetter, ); + this.shipment = this.createBelongsToAccessorFor( + 'shipment_id', + shipmentRepositoryGetter, + ); } } diff --git a/packages/repository/src/__tests__/fixtures/repositories/shipment.repository.ts b/packages/repository/src/__tests__/fixtures/repositories/shipment.repository.ts new file mode 100644 index 000000000000..9b7aea3b768a --- /dev/null +++ b/packages/repository/src/__tests__/fixtures/repositories/shipment.repository.ts @@ -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, + ) { + super(Shipment, db); + this.orders = this.createHasManyRepositoryFactoryFor( + 'shipmentOrders', + orderRepositoryGetter, + ); + } +}