回环4如何在嵌套关系中使用自定义作用域的字段过滤器?

问题描述 投票:0回答:1

我有四种模式--采购、采购产品、供应商、产品。

  1. 采购有很多采购产品,反之亦然。
  2. 购货属于产品,反之亦然
  3. 供应商有很多采购和副产品

现在用下面的代码,我得到了所有的采购、采购-产品和供应商(带字段过滤),但不能对产品使用字段过滤。

所以,谁能告诉我如何在嵌套关系中使用自定义作用域的字段过滤器?

  async find(

  ): Promise<Purchase[]> {
    return this.purchaseRepository.find(
      {

        include: [{
          relation: 'purchaseProducts',
          scope: {
            fields: { name: true, product_id: true }, // not working
            include: [{ relation: 'products' }],
          }

        }, {

          relation: 'vendors',
          scope: {
            fields: { address_line1: false, city: false, state: false, pincode: false, gst_number: false, // working }
          }
        }]
      }
    );

  }

采购模型.ts

import { Entity, model, property, hasMany, belongsTo } from '@loopback/repository';
import { PurchaseProduct } from './purchase-product.model';
import { Vendor } from './vendor.model';

@model({ settings: { strict: true } })
export class Purchase extends Entity {


  @property({
    type: 'string',
    id: true,

  })
  purchase_id: string;


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


  @property({
    type: 'number',
    required: true,

  })
  totalprice?: number;
  @hasMany(() => PurchaseProduct, { keyTo: 'purchase_id' })
  purchaseProducts: PurchaseProduct[];

  @belongsTo(() => Vendor, { name: 'vendors' })
  vendor_id: number;
  // Define well-known properties here

  // Indexer property to allow additional data
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  [prop: string]: any;

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

export interface PurchaseRelations {
  // describe navigational properties here
}

export type PurchaseWithRelations = Purchase & PurchaseRelations;

采购-产品模型.ts

import { Entity, model, property, belongsTo } from '@loopback/repository';
import { Purchase } from './purchase.model';
import { Product } from './product.model';

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

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

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

  @belongsTo(() => Purchase, { name: 'purchases' })
  purchase_id: string;

  @belongsTo(() => Product, { name: 'products' })
  product_id: number;
  // Define well-known properties here

  // Indexer property to allow additional data
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  [prop: string]: any;

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

export interface PurchaseProductRelations {
  // describe navigational properties here
}

export type PurchaseProductWithRelations = PurchaseProduct & PurchaseProductRelations;

产品模型.ts

import { Entity, model, property, hasMany, hasOne, belongsTo } from '@loopback/repository';
import { Purchase } from './purchase.model';
import { OrderedProducts } from './ordered-products.model';
import {PurchaseProduct} from './purchase-product.model';

@model({ settings: { strict: true } })
export class Product extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,

  })
  product_id?: number;

  @property({
    type: 'string',
    required: true,
    index: {
      unique: true
    }

  })
  name: string;

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

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



  @hasMany(() => OrderedProducts, { keyTo: 'product_id' })
  orderedProducts: OrderedProducts[];

  @hasOne(() => ProductPrice, { keyTo: 'product_id' })
  productPrices: ProductPrice;

  @hasMany(() => PurchaseProduct, {keyTo: 'product_id'})
  purchaseProducts: PurchaseProduct[];
  // Define well-known properties here

  // Indexer property to allow additional data
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  [prop: string]: any;

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

export interface ProductRelations {
  // describe navigational properties here
}

export type ProductWithRelations = Product & ProductRelations;

供应商.模型.ts

import { Entity, model, property, hasMany } from '@loopback/repository';
import { Purchase } from './purchase.model';

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

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

  @property({
    type: 'string',
    required: true,
    index: {
      unique: true
    }
  })
  mobile_no: string;

  @property({
    type: 'string',
    index: {
      unique: true
    }
  })
  email?: string;


  @hasMany(() => Purchase, { keyTo: 'vendor_id' })
  purchases: Purchase[];

  // Define well-known properties here

  // Indexer property to allow additional data
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  [prop: string]: any;

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

export interface VendorRelations {
  // describe navigational properties here
}

export type VendorWithRelations = Vendor & VendorRelations;
loopback loopback4
1个回答
0
投票

试着在 purchaseProducts 字段中加入 purchase_id。当你使用field: {key: true}你排除了所有其他的字段,那么ORM(如果你使用的是非关系型数据库,那么ODM)就不能进行连接,因为它需要foreignKey来实现。

例子(我使用的字段符号等于:{key: true}。fields:{key: true}) :

{
   "include":[
      {
         "relation":"userCourses",
         "scope":{
            "offset":0,
            "limit":100,
            "skip":0,
            "order":[
               "id"
            ],
            "fields":[
               "id",
               "user_id",
               "group_id"
            ],
            "include":[
               {
                  "relation":"group",
                  "scope":{
                     "fields":[
                        "id",
                        "otherField" 
                     ]
                  }
               }
            ]
         }
      }
   ]
}

0
投票

这是最终的工作方案。

return this.vendorRepository.purchases(id).find({
      include: [{

        relation: 'purchaseProducts',
        scope: {
          fields: { id: false },
          include: [{
            relation: 'products',
            scope: {
              fields: { product_id: true, name: true }
            }
          }],

        },

      },

      ]
    });
© www.soinside.com 2019 - 2024. All rights reserved.