Как правильно настроить связи между моделями в Loopback?

Доброго времени суток.

В Loopback (кстати не нашел такого тэга) вникаю не так давно. Потому вот вопрос (но прежде немного кода)

Customer.model

import {Entity, model, property, hasMany} from '@loopback/repository';
import {Charge, ChargeWithRelations} from './charge.model';

@model({
  settings: {
    strictObjectIDCoercion: true,
    mongodb: {dataType: 'ObjectID'}
  }
})
export class Customer extends Entity {
  @property({
    type: 'string',
    id: true,
    mongodb: {dataType: 'ObjectID'},
  })
  id?: string;

    @property({
        type: 'string',
        required: true,
        jsonSchema: {
            maxLength: 50,
            minLength: 1,
        }
    })
    first_name: string;

    @property({
        type: 'string',
        required: true,
        jsonSchema: {
            maxLength: 50,
            minLength: 1,
        }
    })
    last_name: string;

    @property({
        type: 'string',
        format: 'email',
        required: true,
        jsonSchema: {
            format:'email'
        }
    })
    email: string;

    @property({
        type: 'string',
        jsonSchema: {
            maxLength: 200
        }
    })
    address_1?: string;

    @property({
        type: 'string',
        jsonSchema: {
            maxLength: 200
        }
    })
    address_2?: string;

    @property({
        type: 'string',
        jsonSchema: {
            maxLength: 200
        }
    })
    city?: string;

    @property({
        type: 'string',
        jsonSchema: {
            maxLength: 15
        }
    })
    state?: string;

    @property({
        type: 'string',
        jsonSchema: {
            maxLength: 10
        }
    })
    postal_code?: string;

  @hasMany(() => Charge, {keyTo: 'customerId'})
  charges?: Charge[];

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

export interface CustomerRelations {
    charges?: ChargeWithRelations[];
}

export type CustomerWithRelations = Customer & CustomerRelations;



charge.model

import {Entity, model, property,belongsTo} from '@loopback/repository';
import  {Customer, CustomerWithRelations} from './customer.model'

@model({
  settings: {
    strictObjectIDCoercion: true,
   // mongodb: {dataType: 'ObjectID'}
  }
})
export class Charge extends Entity {
  @property({
    type: 'string',
    id: true,
    mongodb: {dataType: 'ObjectID'},
  })
  id?: string;

  @property({
    type: 'number',
    required: true,
    dataType: "decimal",
    precision: 10,
    scale: 2,
    fixed: 2,
    jsonSchema: {
      type: "number",
      dataType: "decimal",
      precision: 10,
      scale: 2,
      fixed: 2,
    }
  })
  amount: number;

  @property({
    type: 'string',
    required: true,
    jsonSchema: {
      enum: ['ach', 'cc', 'cash']
    }
  })
  pay_type: string;

  @property({
    type: 'string',
    required: true,
    jsonSchema: {
      enum: ['ach', 'cc', 'cash']
    }
  })
  pay_from_id: string;

  @property({
    type: 'string',
    required: true,
    jsonSchema: {
      enum: ['ach', 'cc', 'cash']
    }
  })
  pay_to_id: string;

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

  getId() {
    return this.id;
  }

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

export interface ChargeRelations {
  customer: CustomerWithRelations;
}

export type ChargeWithRelations = Charge & ChargeRelations;



customer.repository
import {DefaultCrudRepository, repository, HasManyRepositoryFactory} from '@loopback/repository';
import {Customer, CustomerRelations, Charge} from '../models';
import {MongodbDataSource} from '../datasources';
import {inject, Getter} from '@loopback/core';
import {ChargeRepository} from './charge.repository';

export class CustomerRepository extends DefaultCrudRepository<
  Customer,
  typeof Customer.prototype.id,
  CustomerRelations
> {

  public readonly charges: HasManyRepositoryFactory<Charge, typeof Customer.prototype.id>;

  constructor(
    @inject('datasources.mongodb') dataSource: MongodbDataSource,
    @repository.getter('ChargeRepository')
    protected chargeRepositoryGetter: Getter<ChargeRepository>,
  ) {
    super(Customer, dataSource);
    this.charges = this.createHasManyRepositoryFactoryFor('charges', chargeRepositoryGetter,);
  }
}


charge.repository
import {DefaultCrudRepository, BelongsToAccessor, repository} from '@loopback/repository';
import {Charge, ChargeRelations, Customer} from '../models';
import {MongodbDataSource} from '../datasources';
import {inject, Getter} from '@loopback/core';
import {CustomerRepository} from './customer.repository'

export class ChargeRepository extends DefaultCrudRepository<
  Charge,
  typeof Charge.prototype.id,
  ChargeRelations
> {

  public readonly customer: BelongsToAccessor<
      Customer,
      typeof Charge.prototype.id
      >
  constructor(
    @inject('datasources.mongodb') dataSource: MongodbDataSource,

    @repository.getter('TodoListRepository')
    protected customerRepositoryGetter: Getter<CustomerRepository>,

  ) {
    super(Charge, dataSource);
    this.customer = this.createBelongsToAccessorFor(
        'customer',
        customerRepositoryGetter,
    );
  }
}


customer-charge-controller

import {
  Count,
  CountSchema,
  Filter,
  repository,
  Where,
} from '@loopback/repository';
import {
  del,
  get,
  getModelSchemaRef,
  getWhereSchemaFor,
  param,
  patch,
  post,
  requestBody,
} from '@loopback/rest';
import {
  Customer,
  Charge,
} from '../models';
import {CustomerRepository} from '../repositories';

export class CustomerChargeController {
  constructor(
    @repository(CustomerRepository) protected customerRepository: CustomerRepository,
  ) { }

  @post('/customers/{id}/charges', {
    responses: {
      '200': {
        description: 'Customer model instance',
        content: {'application/json': {schema: getModelSchemaRef(Charge)}},
      },
    },
  })
  async create(
    @param.path.string('id') id: typeof Customer.prototype.id,
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Charge, {
            exclude: ['id'],
            optional: ['customerId']
          }),
        },
      },
    }) charge: Omit<Charge, 'id'>,
  ): Promise<Charge> {
    return this.customerRepository.charges(id).create(charge);
  }
}


Так вот может я чего не понимаю. Но как бы я не описывал модели, из разных туториалов и т д, ни как не могу добиться, что бы после выполнения `/customers/{id}/charges`

Запись получались $ref: ObjectId('') с референсами

Сейчас в базе на коллекции `Customer` вообще нет изменений а в charge добавилась поле `customerId`

Есть ли какие варианты достичь этого?
  • Вопрос задан
  • 87 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы