In my app, I want user to have notification items (news) of different type. User
and NewsItem
must have one-to-many relationship, but also, NewsItem
is just a base class for the different types of actual news items.
Here's my base NewsItem
class:
@Entity()
@TableInheritance({ column: { type: "varchar", name: "type"}})
export class NewsItem {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne(type => User, user => user.news)
receiver: Promise<User>;
}
Here's the User
it's attached to:
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
// (a lot of irrelevant stuff removed)
@OneToMany(type => NewsItem, newsItem => newsItem.receiver)
news: Promise<NewsItem[]>;
}
And here's an example of a concrete news item:
@ChildEntity()
export class ConcreteNewsItem extends NewsItem {
@Column()
someData: number;
}
It all seems straightfoward enough from the documentation. Here's what I don't get, however: how do I go through a user's news
, check what exact type each of them is, and get an object of a concrete news item type for each of them?
As I understand it, TypeORM will create a column called type
in SQL definition of news_item
table - but this column is not available on NewsItem
class itself, and I can't understand how to build a query that would actually give me the ConcreteNewsItem
objects!