Typescript 打字、博士后及;NestJS:过滤多个列
我正在使用postgresSQL、Typeorm和NestJs开发web应用程序。 我有几个实体:Typescript 打字、博士后及;NestJS:过滤多个列,typescript,postgresql,nestjs,typeorm,Typescript,Postgresql,Nestjs,Typeorm,我正在使用postgresSQL、Typeorm和NestJs开发web应用程序。 我有几个实体: @Entity() export class Product { @PrimaryGeneratedColumn() id: string; @Column() name: string; @ManyToMany( () => Property, { nullable: true, eager: true }, ) @JoinTable()
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: string;
@Column()
name: string;
@ManyToMany(
() => Property,
{ nullable: true, eager: true },
)
@JoinTable()
properties: Property[];
}
@Entity()
export class Property {
@PrimaryGeneratedColumn()
id: string;
@Column({ unique: true })
key: string;
@Column()
name: string
}
我试图通过属性来获取产品。
这在sql中给出:
;with r as (
SELECT
bien.*,
ARRAY['usage.toto'::varchar] = array_agg(p.key) as compare1
from bien
INNER JOIN bien_properties_property bpp on bien.id = bpp."bienId"
INNER JOIN property p on p.id = bpp."propertyId"
GROUP BY bien.id
)
select * from r
where compare1
有人能帮我吗
Thx要访问
属性
表上的产品
表,您需要声明双向关系,如上所述。因此,它将是:
@Entity()
export class Product {
@PrimaryGeneratedColumn()
id: string;
@Column()
name: string;
@ManyToMany(
() => Property,
property => property.products,
{ nullable: true, eager: true },
)
@JoinTable()
properties: Property[];
}
@Entity()
export class Property {
@PrimaryGeneratedColumn()
id: string;
@Column({ unique: true })
key: string;
@Column()
name: string
@@ManyToMany(
() => Product,
product => product.properties,
)
products: Product[];
}
现在,要获得其产品的属性,您需要加入,如下所示:
//...
const propertyRepository = connection.getRepository(Property);
const properties = await propertyRepository.find({ relations: ["products"] });
//...
它不起作用,我尝试过:让queryBuilder=this.productRepository.createQueryBuilder(“产品”).leftJoin('product.properties','property')。其中属性中的('usage.stockage')返回queryBuilder.getManyAndCount(),我得到一个错误我认为您的问题出在
的where
,什么是用法
?您说过希望“按属性获取产品”。在上面显示的代码中,您试图按产品获取属性。我建议您尝试以下操作:this.productRepository.createQueryBuilder(“产品”).leftJoinAndSelect('product.properties','property').getManyAndCount()
或尝试我在回复中添加的代码。“usage.stockage”是我的属性之一,我正在尝试获取具有特定属性的产品。类似products.FindByproperty([''usage.stockage'])的内容将返回所有具有精确属性的产品:['usage.stockage']。我不想返回带有属性字段的prodcut。我不知道我是否是clearHmmm,我想现在更清楚了。但是用法
是属性
表?或用法的一列。库存
位于名称(或其他)处Property
table?第二个选项后面的列是:this.productRepository.createQueryBuilder(“产品”).leftJoin('product.properties','Property')。其中(“Property.name=:propertyName:'usage.stockage')。getManyAndCount();
.usage.stockage是属性表中名为“key”的列的值,这是正确的。这对一个来说是可行的,但我想用数组进行测试,比如:this.productRepository.createQueryBuilder(“product”).leftJoin('product.properties','property')。其中(“properties=:props”,{props:[“usage.stockage”,“usage.other”]}).getManyAndCount()