typeorm-revisions
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

typeorm-revisions

npm NPM

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

// Insert
const testEntity = await MyModel.create({ test: "test" }).save();
 
// Update
testEntity.test = "updated";
await testEntity.save();
 
// Remove
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

Package Sidebar

Install

npm i typeorm-revisions

Weekly Downloads

180

Version

0.0.5

License

MIT

Unpacked Size

371 kB

Total Files

28

Last publish

Collaborators

  • ephillipe