import { Ctx, Field, ID, ObjectType } from 'type-graphql'; import { BaseEntity, Column, CreateDateColumn, DeleteDateColumn, Entity, Index, OneToMany, PrimaryGeneratedColumn, UpdateDateColumn, } from 'typeorm'; import { ContextType } from '@/types'; import { ProductSource } from './types'; import { Page } from '.'; @ObjectType() @Entity({ name: 'products' }) @Index(['firebaseId'], { unique: true }) export default class Product extends BaseEntity { @Field(() => ID) @PrimaryGeneratedColumn('uuid') id!: string; @Field(() => String) @Column({ type: 'varchar', }) firebaseId!: string; @Field(() => [Page]) @OneToMany(() => Page, (page) => page.product, { lazy: true, }) pages!: Promise; @Field(() => [String], { nullable: true, }) @Column({ type: 'varchar', nullable: true, array: true, }) googleShoppingIDs?: string[]; @Column({ type: 'enum', enum: ProductSource, nullable: true, }) source?: ProductSource; @Field(() => Date) @CreateDateColumn({ type: 'timestamp with time zone', }) createdAt!: Date; @Field(() => Date, { nullable: true, }) @UpdateDateColumn({ type: 'timestamp with time zone', nullable: true, default: null, }) updatedAt!: Date; @Field(() => Date, { nullable: true, }) @DeleteDateColumn({ type: 'timestamp with time zone', nullable: true, default: null, }) deletedAt!: Date; @Field(() => Date, { nullable: true }) @Column({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP', nullable: true, }) lastScrapedAt?: Date | null; @Field(() => String, { nullable: true, }) @Column({ type: 'varchar', nullable: true, default: null, }) featuredImage!: string | null; @Field(() => Page, { nullable: true, }) async lowestPricePage( @Ctx() context: ContextType ): Promise { return ( await context.dbDataLoader.pagesByProductFirebaseIdPriceAsc.load( this.firebaseId ) )[0]; } }