Last active
November 24, 2020 20:54
-
-
Save f-huang/76a2cc209f94ce6fa86c075a8b103d64 to your computer and use it in GitHub Desktop.
Example of entity typing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const invoiceRepository = queryRunner.manager.getRepository("invoice"); | |
// or | |
const invoiceRepository = connection.getRepository<Invoice>("invoice"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Entity, PrimaryGeneratedColumn, ManyToOne, Column } from "typeorm"; | |
import { Base } from "./base.entity.ts" | |
import { User } from "./user.entity.ts" | |
@Entity() | |
export class Invoice extends Base { | |
@ManyToOne((_type) => User) | |
importedBy!: User | undefined; | |
@Column({ nullable: false }) | |
importedById!: User["id"]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in module.ts | |
import { Module } from "@nestjs/common"; | |
import { TypeOrmModule } from "@nestjs/typeorm"; | |
import { InvoiceRepository } from "./invoice.repository.ts"; | |
@Module({ | |
imports: [TypeOrmModule.forFeature(InvoiceRepository)] | |
}) | |
export class InvoiceModule {} | |
// in service.ts | |
import { Injectable } from "@nestjs/common"; | |
import { InvoiceRepository } from "./invoice.repository.ts"; | |
@Injectable() | |
export class InvoiceService { | |
constructor(private readonly invoiceRepository: InvoiceRepository) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Module } from "@nestjs/common"; | |
import { TypeOrmModule } from "@nestjs/typeorm"; | |
import { Invoice } from "./invoice.entity.ts"; | |
@Module({ | |
imports: [TypeOrmModule.forFeature(Invoice)] | |
}) | |
export class InvoiceModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@nestjs/common"; | |
import { InjectRepository, Repository, TypeOrmModule } from "@nestjs/typeorm"; | |
import { Invoice } from "./invoice.entity.ts"; | |
@Injectable() | |
export class InvoiceService { | |
constructor(@InjectRepository(Invoice) private readonly invoiceRepository: Repository<Invoice>) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Entity, PrimaryGeneratedColumn, ManyToOne, Column } from "typeorm"; | |
import { Base } from "./base.entity.ts" | |
import { User } from "./user.entity.ts" | |
@Entity() | |
export class Invoice extends Base { | |
@ManyToOne((_type) => User) | |
importedBy!: User | undefined; | |
@Column({ nullable: false }) | |
importedById!: User["id"]; | |
} | |
interface UserWithImportedBy extends User { | |
importedBy: NonNullable<User["importedBy"]>; | |
} | |
const isUserWithImportedBy = (user: User): user is UserWithImportedBy => { | |
return user.importedBy != null; | |
} | |
//When querying in repository | |
public async findWithImportedBy(userId: User["id"]): Promise<UserWithImportedBy> { | |
const user = await this.findOneOrFail({ | |
where: { id: userId }, | |
relations: ["importedBy"], | |
}); | |
if (!isUserWithImportedBy(user)) { | |
throw new TypeError(`Could not load relation importedBy for user ${userId}`); | |
} | |
return user; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BaseEntity, Entity, PrimaryGeneratedColumn } from "typeorm"; | |
@Entity() | |
export class User extends BaseEntity { | |
@PrimaryGeneratedColumn("uuid") | |
id!: string & { __brand: "userId" }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment