typeorm-revisions
Description
Provides a Revision History Subscriber for TypeORM Entities
Tested: sqlite, mysql5, mysql8 and postgres.
Installation
$ npm i --save typeorm typeorm-revisions
Quick Start
1. Create your own Entity
@Entity()
class MyModel extends BaseEntity {
@PrimaryGeneratedColumn()
public id!: number;
@Column()
public name!: string;
@Column()
public email!: string;
}
2. Create an Entity to mantain a Revision History
@Entity()
class MyModelHistory extends MyModel implements HistoryEntityInterface {
@Column()
public originalID!: number;
@HistoryActionColumn()
public action!: HistoryActionType;
}
3. Create a Entity Subscriber for monitor your Entity and persist a Revison History
@EventSubscriber()
class MyModelHistorySubscriber extends HistoryEntitySubscriber<MyModel, MyModelHistory> {
public get entity() {
return MyModel;
}
public get historyEntity() {
return MyModelHistory;
}
}
4. Create connection
await createConnection({
type: "sqlite",
entities: [MyModel, MyModelHistory],
subscribers: [MyModelHistorySubscriber],
database: "db.sql",
});
5. Insert/Update/Remove entity
const testEntity = await MyModel.create({ test: "test" }).save();
testEntity.test = "updated";
await testEntity.save();
await testEntity.remove();
Advanced
You can hook before/after insert/update/remove history.
@EventSubscriber()
class MyModelHistorySubscriber extends HistoryEntitySubscriber<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> {
}
}
License
MIT