typeorm

Best way to handle one-to-many with type-graphql typeorm and dataloader

我怕爱的太早我们不能终老 提交于 2021-02-19 01:38:08
问题 I'm trying to figure out the best way to handle a one-to-many relationship using type-graphql and typeorm with a postgresql db (using apollo server with express). I have a user table which has a one-to-many relation with a courses table. The way I am currently handling this is to use the @RelationId field to create a column of userCourseIds and then using @FieldResolver with dataloader to batch fetch the courses that belong to that user(s). My issue is that with the @RelationId field, a

TypeORM Polymorphic Relations

我们两清 提交于 2021-02-18 22:24:45
问题 I am migrating a Laravel app to Node app using TypeORM. Has anyone been able to implement something similar to Laravel's Polymorphic Relations in TypeOrm? Example schema I am trying to reproduce: export class Notification { id: string; attachable_id: number; attachable_type: string; } I want to be able to to have a notification.attachable relation that could be of any type. Then, ideally, I can eager load a user with their last x notifications, with the attachable on each notification. 回答1:

TypeORM Polymorphic Relations

荒凉一梦 提交于 2021-02-18 22:24:12
问题 I am migrating a Laravel app to Node app using TypeORM. Has anyone been able to implement something similar to Laravel's Polymorphic Relations in TypeOrm? Example schema I am trying to reproduce: export class Notification { id: string; attachable_id: number; attachable_type: string; } I want to be able to to have a notification.attachable relation that could be of any type. Then, ideally, I can eager load a user with their last x notifications, with the attachable on each notification. 回答1:

How to use Parameterized query using TypeORM for postgres database and nodejs as the application's back-end server

和自甴很熟 提交于 2021-02-18 18:11:14
问题 I want to fetch the rows from a postgres table where name = SUPREME INT'L, Note: this string has a single quote in between the name characters. I am using TypeORM as an ORM, POSTGRESQL as the database. My query: import { getConnection } from 'typeorm'; const connection = getConnection(); var query = `SELECT * from skusimulations where "name"= ? `; const output =await connection.query(query, ['SUPREME INT'L']) I am getting error while executing this, I want to escape the single quote by using

How to use Parameterized query using TypeORM for postgres database and nodejs as the application's back-end server

你离开我真会死。 提交于 2021-02-18 18:08:25
问题 I want to fetch the rows from a postgres table where name = SUPREME INT'L, Note: this string has a single quote in between the name characters. I am using TypeORM as an ORM, POSTGRESQL as the database. My query: import { getConnection } from 'typeorm'; const connection = getConnection(); var query = `SELECT * from skusimulations where "name"= ? `; const output =await connection.query(query, ['SUPREME INT'L']) I am getting error while executing this, I want to escape the single quote by using

How to select a specific column in typeorm many to many relationship

倖福魔咒の 提交于 2021-02-11 15:34:21
问题 I have an n:m relationship with a custom join table in TYPEORM. Entity1 @Entity({ name: 'users' }) export class User extends BaseModel { @PrimaryGeneratedColumn() id!: number; @Column({ type: 'varchar', length: 50 }) @IsNotEmpty() username!: string; @Column({ unique: true, type: 'varchar', length: 50 }) @IsEmail() @IsNotEmpty() email!: string; @CreateDateColumn({ type: 'timestamp' }) createdAt!: Date; @UpdateDateColumn({ type: 'timestamp' }) updatedAt!: Date; @DeleteDateColumn({ type:

How to select a specific column in typeorm many to many relationship

喜夏-厌秋 提交于 2021-02-11 15:33:37
问题 I have an n:m relationship with a custom join table in TYPEORM. Entity1 @Entity({ name: 'users' }) export class User extends BaseModel { @PrimaryGeneratedColumn() id!: number; @Column({ type: 'varchar', length: 50 }) @IsNotEmpty() username!: string; @Column({ unique: true, type: 'varchar', length: 50 }) @IsEmail() @IsNotEmpty() email!: string; @CreateDateColumn({ type: 'timestamp' }) createdAt!: Date; @UpdateDateColumn({ type: 'timestamp' }) updatedAt!: Date; @DeleteDateColumn({ type:

How to select a specific column in typeorm many to many relationship

℡╲_俬逩灬. 提交于 2021-02-11 15:33:18
问题 I have an n:m relationship with a custom join table in TYPEORM. Entity1 @Entity({ name: 'users' }) export class User extends BaseModel { @PrimaryGeneratedColumn() id!: number; @Column({ type: 'varchar', length: 50 }) @IsNotEmpty() username!: string; @Column({ unique: true, type: 'varchar', length: 50 }) @IsEmail() @IsNotEmpty() email!: string; @CreateDateColumn({ type: 'timestamp' }) createdAt!: Date; @UpdateDateColumn({ type: 'timestamp' }) updatedAt!: Date; @DeleteDateColumn({ type:

Insert data in database depending by relation

徘徊边缘 提交于 2021-02-11 12:28:00
问题 I am using typeorm, Nest Js and postgresql database. I have the next entities: import {Entity, Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, JoinColumn, OneToOne} from 'typeorm'; import {User} from "./user.entity"; import {MetaImages} from "./meta-images.entity"; @Entity() export class Cars { @PrimaryGeneratedColumn({name: "carId"}) carId: number; @OneToMany(() => CarsColors, c => c.carId, { cascade: true }) carsColors: CarsColors[]; } /// Colors Entity @Entity() export class

NestJs/TypeORM: How to save Many to Many

给你一囗甜甜゛ 提交于 2021-02-10 19:39:34
问题 I am trying to save a Many to Many relationship. Each entity by itself is working great. Here are the two entity files I am working with: // location.entity @ManyToMany(type => User, user => user.locations, { eager: true, cascade: true }) @JoinTable() users: User[]; // user.entity @ManyToMany(type => Location, location => location.users, { eager: false }) locations: Location[]; Here is the user repository: // user.repository ... user.locations = locations; // [1,2,3] user.uuid = uuidv4(); try