I am currently using TypeORM and TypeScript and try to delete data. Since I want to delete based on the owner of 'Customer' I thought I'd had to use JOINS. Since I am not very fluent with more advanced concepts like JOINS in SQL, I would appreciate any help with a small explanation. Thank you.
The following code throws this exception:
"message": "\"customer\" alias was not found. Maybe you forgot to join it?",
@Resolver()
export class RootResolver {
@Mutation(() => Boolean)
async deleteAllDataByOwner(
@Arg('ownerEmail') ownerEmail: string,
@Ctx() { conn }: AppContext,
): Promise<boolean | undefined> {
const deleteVisitsPromise = conn.createQueryBuilder()
.leftJoinAndSelect("customer.visits", "visit")
.delete()
.from(Customer)
.where("customer.owner = :owner", { owner: ownerEmail })
.execute()
const deleteCustomersPromise = conn.createQueryBuilder()
.delete()
.from(Customer)
.where("customer.owner = :owner", { owner: ownerEmail })
.execute()
return await Promise.all(
[deleteCustomersPromise, deleteVisitsPromise]
).then(_ => {
return true
})
}
}
@ObjectType()
@Entity({ name: 'customers' })
export class Customer {
@Field()
@PrimaryColumn()
uuid: string
@Field(() => [Visit], { nullable: true })
@OneToMany(() => Visit, (visit) => visit.customer)
visits?: (Visit | null)[]
// ...
}
@ObjectType()
@Entity({ name: 'visits' })
export class Visit {
@Field()
@PrimaryColumn()
uuid: string
@Field(() => Customer)
@ManyToOne(() => Customer, (customer) => customer.visits, {
onDelete: 'CASCADE'
})
customer: Customer
// ...
}