Обновите тип объекта в наследовании одной таблицы - PullRequest
0 голосов
/ 22 февраля 2020

В сценарии Single Table Inheritance , есть ли способ обновить сохраненный объект BaseEntity в базе данных как объект InheritedEntity?

Пожалуйста, рассмотрите следующий сценарий:


@Entity()
@TableInheritance({column: {name: 'type', type: "varchar"}})
export class BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;
}


@ChildEntity()
export class ChildEntityOne extends BaseEntity {
  @Column()
  job: string;
}

//
// Create and save a BaseEntity object in database
//
const base = new BaseEntity();
base.title = 'Foo';
await getRepository(BaseEntity).save(base);
//
// Now I have the following in my database
// { id: 1, title: 'Foo', job: null, type: 'BaseEntity' } 
// 

//
// And later I want to query this object form the database and, 
// based on the application logic, cast it to ChideEntityOne and,
// add set its job filed and update it in the database as a ChildEntityOne entity.
//
const entity = await getRepository(BaseEntity).findOne(1);
const childEntity = baseEntity as ChildEntityOne;
childEntity.job = 'Bar';
await getRepository(ChildEntityOne).save(childEntity);

//
// Now I get this in my database:
//
// { id: 1, title: 'Foo', job: null, type: 'BaseEntity' } 
// { id: 2, title: 'Foo', job: 'Bar', type: 'ChildEntityOne' } 
//
//
// But I want to have this in my database: 
// { id: 1, title: 'Foo', job: 'Bar', type: 'ChildEntityOne' } 
//

Есть ли какой-нибудь чистый способ реализовать этот сценарий с использованием TypeORM? Или, что более важно, это логический сценарий, основанный на определениях ИППП?

...