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

1.0.3 • Public • Published

@ifjkt/typeorm-revisions

npm NPM

Description

Provides a Revision History Subscriber for TypeORM Entities

Tested: MySQL 8, MariaDB and Postgres

Installation

$ npm i --save typeorm @ifjkt/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 for your Audit table

@Entity()
class MyModelHistory extends MyModel implements HistoryEntityInterface {
  @Column({
    name: 'record_id',
    type: 'int',
  })
  recordId: number;

  @Column({
    name: 'revision_timestamp',
  })
  revisionTime: Date;

  @Column({
    name: 'revision_type',
  })
  revisionType: RevisionActionType;
}

3. Annotate your Audit table with the @AuditTable decorator and specify your audit fields

@Entity()
@AuditTable({
  recordIdProperty: 'recordId',
  revisionTypeProperty: 'revisionType',
  revisionTimestampProperty: 'revisionTime',
})
class MyModelHistory extends MyModel implements HistoryEntityInterface {
  @Column({
    name: 'record_id',
    type: 'int',
  })
  recordId: number;

  @Column({
    name: 'revision_timestamp',
  })
  revisionTime: Date;

  @Column({
    name: 'revision_type',
  })
  revisionType: RevisionActionType;
}

4. Create an Entity Subscriber for your entity to process Audit events

@EventSubscriber()
class MyModelHistorySubscriber extends HistorySubscriber<MyModel, MyModelHistory> {
  public entity = MyModel;
  public historyEntity = MyModelHistory;
}

License

MIT

Forked from https://github.com/ephillipe/typeorm-revisions

Package Sidebar

Install

npm i @ifjkt/typeorm-revisions

Weekly Downloads

11

Version

1.0.3

License

MIT

Unpacked Size

111 kB

Total Files

22

Last publish

Collaborators

  • ifjkt