Typescript 如何在postgresql上存储typeorm的json对象数组?
我有一个假定包含json对象数组的用户实体,但我找不到typeorm本机存储的json数组类型,我不确定如何使用typeorm将它们存储在数据库中 当前的设置使用typegraphql,但我对它感到困惑 我的用户实体大致如下所示:Typescript 如何在postgresql上存储typeorm的json对象数组?,typescript,postgresql,graphql,typeorm,typegraphql,Typescript,Postgresql,Graphql,Typeorm,Typegraphql,我有一个假定包含json对象数组的用户实体,但我找不到typeorm本机存储的json数组类型,我不确定如何使用typeorm将它们存储在数据库中 当前的设置使用typegraphql,但我对它感到困惑 我的用户实体大致如下所示: User.ts import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, ManyToOne } from "typeorm"; import { Field, Int, Object
User.ts
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, ManyToOne } from "typeorm";
import { Field, Int, ObjectType, ID } from "type-graphql";
**//THIS ENTITY**
@ObjectType()
export class Softwares {
@Field(() => Number)
id: number;
@Field()
category: string;
@Field(() => [String])
list: string[];
}
@ObjectType()
export class About {
@Field( { nullable: true } )
@Column("text", { nullable: true })
about_header_message: string;
**//THIS PART**
@Field(() => [Softwares], { nullable: true })
softwares: Softwares[];
@Field(() => [[String]], { nullable: true })
@Column("text", { nullable: true, array: true })
info: string[][];
}
@ObjectType()
@Entity()
export class User extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number;
@Field({ nullable: true })
@Column("text", { nullable: true })
title_name: string;
@Field({ nullable: true })
@Column("text", { nullable: true })
first_name: string;
@Field({ nullable: true })
@Column("text", { nullable: true })
last_name: string;
@Field((type) => About)
@Column(() => About)
about: About;
}
请注意,About类被继承到普通用户实体中。我这样做是因为get请求的最终GraphQL应该如下所示:
{
title_name: "Sky Johnson",
first_name: "Sky",
last_name: "Johnson",
about: {
about_header_message: "A passionate developer, artist, and photographer",
info: [
["University", "University of Colorado Boulder - Cumulative GPA: 3.376"],
["Graduation", "May Class of 2020"],
["Degree", "Computer Science (BS) - Degree GPA: 3.514"],
["2nd Degree", "Creative Technology and Design (BS) - Degree GPA: 3.657"],
["Minor", "Applied Mathematics: Theoretical Statistics"],
["Languages", "English: Native", "日本語:~N4 University Experience"],
],
softwares: [
{
id: 1,
category: "Favorite Languages",
list: [
"C# + Unity",
"Python",
"Java",
"Scala",
"Javascript/Typescript",
"C/C++ (limited)",
"Processing/Arduino",
"OpenSCAD",
],
},
{
id: 2,
category: "Python Libraries",
list: [
"Tensor Flow",
"MatPlotLib",
"NetworkX",
"Seaborn",
"SciKit",
"Keras",
],
},
],
},
};
我尝试在about类型的softwares字段的typeorm中使用manytone关系,但我无法使其工作,因为我认为用户上about类的继承性质将其搞乱(因为一个用户可以有许多软件对象,但软件列表在about实体中)
如果有人有任何想法,这将有很大帮助。谢谢 Typeform和Postgres支持
jsonb
字段
@Column('jsonb')
tasks: Foo[];
我想我是通过将软件实体拆分成一个新实体并使用`@manytone((type)=>User,(User)=>User.about.softwares)调用多对一来解决这个问题的`