Provides a Revision History Subscriber for TypeORM Entities
Tested: sqlite, mysql5, mysql8 and postgres.
$ npm i --save typeorm typeorm-revisions
@Entity()
class MyModel extends BaseEntity {
@PrimaryGeneratedColumn()
public id!: number;
@Column()
public name!: string;
@Column()
public email!: string;
}
@Entity()
class MyModelHistory extends MyModel implements HistoryEntityInterface {
@Column()
public originalID!: number;
@HistoryActionColumn()
public action!: HistoryActionType;
}
@EventSubscriber()
class MyModelHistorySubscriber extends HistorySubscriber<MyModel, MyModelHistory> {
public get entity() {
return MyModel;
}
public get historyEntity() {
return MyModelHistory;
}
}
await createConnection({
type: "sqlite",
entities: [MyModel, MyModelHistory],
subscribers: [MyModelHistorySubscriber],
database: "db.sql",
});
// Insert
const testEntity = await MyModel.create({ test: "test" }).save();
// Update
testEntity.test = "updated";
await testEntity.save();
// Remove
await testEntity.remove();
You can hook before/after insert/update/remove history.
@EventSubscriber()
class MyModelHistorySubscriber extends HistorySubscriber<MyModel, MyModelHistory> {
public get entity() {
return MyModel;
}
public get historyEntity() {
return MyModelHistory;
}
public beforeHistory(action: HistoryActionType, history: MyModelHistory): void | Promise<void> {
}
public beforeHistory(action: HistoryActionType, history: MyModelHistory): void | Promise<void> {
}
}