1

I'm trying to implement many to many relationship in loopback using hasManyThrough , however in official documentation I'm unable to find any information regarding it . After researching a bit I have followed this. and unable to achieve desired data in filter query.

Patient Model

import {Entity, hasMany, model, property} from '@loopback/repository';
import {Appointment} from './appointment.model';
import {Physician} from './physician.model';

@model()
export class Patient extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;

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

  @hasMany(() => Physician, {through: {model: () => Appointment}})
  physicians: Physician[];

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

export interface PatientRelations {
  // describe navigational properties here
}

export type PatientWithRelations = Patient & PatientRelations;

Physician Model

import {Entity, hasMany, model, property} from '@loopback/repository';
import {Appointment} from './appointment.model';
import {Patient} from './patient.model';

@model()
export class Physician extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;

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

  @hasMany(() => Patient, {through: {model: () => Appointment}})
  patients: Patient[];

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

export interface PhysicianRelations {
  // describe navigational properties here
}

export type PhysicianWithRelations = Physician & PhysicianRelations;

Appointment Model

import {belongsTo, Entity, model, property} from '@loopback/repository';
import {Patient} from './patient.model';
import {Physician} from './physician.model';

@model()
export class Appointment extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;

  @property({
    type: 'boolean',
    required: true,
  })
  status: boolean;

  @belongsTo(() => Physician)
  physicianId: string;

  @belongsTo(() => Patient)
  patientId: string;

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

export interface AppointmentRelations {
  // describe navigational properties here
}

export type AppointmentWithRelations = Appointment & AppointmentRelations;

1 Answers1

0

This is not available in LB4 ? When using the CLI interface, lb4 relation only yields the 3 basic ones.

https://loopback.io/doc/en/lb3/HasManyThrough-relations.html