首页 > 解决方案 > 如何在环回4中创建三个模型之间的关系?

问题描述

共有三种型号——

  1. 人物模型 -
import {Entity, model, property} from '@loopback/repository';

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

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

  @property({
    type: 'number',
    required: true,
  })
  age: number;


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

export interface PersonRelations {
  // describe navigational properties here
}

export type PersonWithRelations = Person & PersonRelations;

聊天组模型 -

import {Entity, model, property} from '@loopback/repository';

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

  @property({
    type: 'string',
  })
  chatGroupName?: string;


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

export interface ChatGroupRelations {
  // describe navigational properties here
}

export type ChatGroupWithRelations = ChatGroup & ChatGroupRelations;

聊天组成员模型 -

import {Entity, model, property} from '@loopback/repository';

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

  @property({
    type: 'number',
    required: true,
  })
  personId: number;

  @property({
    type: 'number',
    required: true,
  })
  chatGroupId: number;


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

export interface ChatGroupMembersRelations {
  // describe navigational properties here
}

export type ChatGroupMembersWithRelations = ChatGroupMembers & ChatGroupMembersRelations;

人员模型保存有关人名和年龄的信息。聊天组模型保存有关聊天组名称的信息。聊天组成员模型保存有关聊天组成员的信息,这些成员具有来自 Person 模型的 personId 和来自 ChatGroup 模型的 chatGroupId。

有什么方法可以在这些模型之间创建关系,以便我在一次 API 调用中获取聊天组及其成员和姓名的信息?

标签: loopback4

解决方案


推荐阅读