1.0.38 • Published 1 year ago

@anchan828/typeorm-history v1.0.38

Weekly downloads
523
License
MIT
Repository
github
Last release
1 year ago

@anchan828/typeorm-history

npm NPM

Description

Create History Entity for TypeORM

Tested: mysql, postgres and sqlite.

Warning
This package only works with repository.save and repository.remove. You can't use repository.insert/repository.update/repository.delete.

Installation

$ npm i --save typeorm @anchan828/typeorm-history

Quick Start

1. Create Entity

@Entity()
class TestEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column()
  public test!: string;
}

2. Create History Entity

You need to add the same column properties as TestEntity.

@Entity()
class TestHistoryEntity extends BaseEntity implements HistoryEntityInterface<TestEntity> {
  // id is a property that holds the id of the TestHistoryEntity.
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column()
  public test!: string;

  // originalID is a property that holds the id of the TestEntity.
  @HistoryOriginalIdColumn()
  public originalID!: number;

  @HistoryActionColumn()
  public action!: HistoryActionType;
}

Note: Starting with version 0.5.0, column names can be defined freely.

@Entity()
class TestHistoryEntity extends BaseEntity implements HistoryEntityInterface<TestEntity> {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column()
  public test!: string;

  // You can use any property name you like.
  @HistoryOriginalIdColumn()
  public history_originalID!: number;

  // You can rename column name
  @HistoryActionColumn({ name: "history_action" })
  public action!: HistoryActionType;
}

Inheriting a Entity class

@Entity()
class TestHistoryEntity extends TestEntity implements HistoryEntityInterface {
  @HistoryOriginalIdColumn()
  public originalID!: number;

  @HistoryActionColumn()
  public action!: HistoryActionType;
}

Note: You can create a History entity by inheriting from the original Entity class. This is easy to do for simple entities. However, if you want to keep unique indexes, there is a problem. As many users have reported the problem, when you inherit a class, you also inherit the decorator, and you cannot overwrite it. This means that you cannot remove the restriction of unique indexes of @Index etc.

3. Create Entity Subscriber for History

@EventSubscriber()
class TestHistoryEntitySubscriber extends HistoryEntitySubscriber<TestEntity, TestHistoryEntity> {
  public get entity() {
    return TestEntity;
  }
  public get historyEntity() {
    return TestHistoryEntity;
  }
}

4. Create connection

await createConnection({
  type: "mysql",
  entities: [TestEntity, TestHistoryEntity],
  subscribers: [TestHistoryEntitySubscriber],
  username: "root",
  password: "test",
  database: "test",
});

5. Insert/Update/Remove entity

// Insert
const testEntity = await TestEntity.create({ test: "test" }).save();

// Update
testEntity.test = "updated";
await testEntity.save();

// Remove
await testEntity.remove();

npm.io

Advanced

Hooks

You can hook before/after insert/update/remove history.

@EventSubscriber()
class TestHistoryEntitySubscriber extends HistoryEntitySubscriber<TestEntity, TestHistoryEntity> {
  public get entity() {
    return TestEntity;
  }

  public get historyEntity() {
    return TestHistoryEntity;
  }

  public beforeInsertHistory(
    history: HistoryEntityType,
    entity: Readonly<EntityType>,
  ): HistoryEntityType | Promise<HistoryEntityType> {
    return history;
  }

  public afterInsertHistory(history: HistoryEntityType, entity: Readonly<EntityType>): void | Promise<void> {}

  public beforeUpdateHistory(
    history: HistoryEntityType,
    entity: Readonly<EntityType>,
  ): HistoryEntityType | Promise<HistoryEntityType> {
    return history;
  }

  public afterUpdateHistory(history: HistoryEntityType, entity: Readonly<EntityType>): void | Promise<void> {}

  public beforeRemoveHistory(
    history: HistoryEntityType,
    entity: Readonly<EntityType>,
  ): HistoryEntityType | Promise<HistoryEntityType> {
    return history;
  }

  public afterRemoveHistory(history: HistoryEntityType, entity: Readonly<EntityType>): void | Promise<void> {}
}

Drop unique indices

The history table stores multiple entities with the same content, so it won't work well if there is a unique index. You will need to drop the all unique indices. So I prepared a helper function for migration.

export class DropAllUniqueOfTestHistoryEntity1625470736630 {
  async up(queryRunner: QueryRunner): Promise<void> {
    await dropUniqueIndices(queryRunner, TestHistoryEntity /* "test_history_entity" */);
  }

  async down(queryRunner: QueryRunner): Promise<void> {}
}

If you want to regenerate the index with the unique flag removed, please set keepIndex to true.

export class DropAllUniqueOfTestHistoryEntity1625470736630 {
  async up(queryRunner: QueryRunner): Promise<void> {
    await dropUniqueIndices(queryRunner, "test_history_entity", true);
  }
}

Overwrite action column type

By default, the type of the action column uses enum. However, depending on the database used, enum may not be available.

In this case, you can pass ColumnOptions as the first argument to the HistoryActionColumn decorator to override it.

@HistoryActionColumn({ type: "varchar" })
public action!: HistoryActionType;

License

MIT

1.0.38

1 year ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

2 years ago

1.0.29

2 years ago

1.0.30

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.9.0

2 years ago

0.8.11

2 years ago

0.8.10

2 years ago

0.8.9

2 years ago

0.8.8

2 years ago

0.8.5

2 years ago

0.8.4

2 years ago

0.8.7

2 years ago

0.8.6

2 years ago

0.7.20

2 years ago

0.7.19

2 years ago

0.7.18

2 years ago

0.7.17

2 years ago

0.7.16

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.8.3

2 years ago

0.8.2

2 years ago

0.7.13

2 years ago

0.7.12

3 years ago

0.7.15

2 years ago

0.7.14

2 years ago

0.7.11

3 years ago

0.7.10

3 years ago

0.7.9

3 years ago

0.7.8

3 years ago

0.7.7

3 years ago

0.7.6

3 years ago

0.7.5

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.6

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.4.39

3 years ago

0.5.0

3 years ago

0.4.38

3 years ago

0.4.37

3 years ago

0.4.36

3 years ago

0.4.35

3 years ago

0.4.34

3 years ago

0.4.33

3 years ago

0.4.31

3 years ago

0.4.32

3 years ago

0.4.30

3 years ago

0.4.28

3 years ago

0.4.29

3 years ago

0.4.27

3 years ago

0.4.26

3 years ago

0.4.25

3 years ago

0.4.24

3 years ago

0.4.23

3 years ago

0.4.22

3 years ago

0.4.21

3 years ago

0.4.20

3 years ago

0.4.19

3 years ago

0.4.18

3 years ago

0.4.17

3 years ago

0.4.16

3 years ago

0.4.15

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.10

3 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.65

4 years ago

0.3.64

4 years ago

0.3.63

4 years ago

0.3.61

4 years ago

0.3.60

4 years ago

0.3.59

4 years ago

0.3.58

4 years ago

0.3.57

4 years ago

0.3.56

4 years ago

0.3.55

4 years ago

0.3.54

4 years ago

0.3.53

4 years ago

0.3.52

4 years ago

0.3.51

4 years ago

0.3.50

4 years ago

0.3.49

4 years ago

0.3.48

4 years ago

0.3.47

4 years ago

0.3.46

4 years ago

0.3.45

4 years ago

0.3.44

4 years ago

0.3.43

4 years ago

0.3.42

4 years ago

0.3.41

4 years ago

0.3.40

4 years ago

0.3.39

4 years ago

0.3.38

4 years ago

0.3.37

4 years ago

0.3.36

4 years ago

0.3.35

4 years ago

0.3.34

4 years ago

0.3.33

4 years ago

0.3.32

4 years ago

0.3.31

4 years ago

0.3.30

4 years ago

0.3.29

4 years ago

0.3.28

4 years ago

0.3.26

5 years ago

0.3.25

5 years ago

0.3.24

5 years ago

0.3.22

5 years ago

0.3.21

5 years ago

0.3.20

5 years ago

0.3.19

5 years ago

0.3.18

5 years ago

0.3.17

5 years ago

0.3.16

5 years ago

0.3.15

5 years ago

0.3.14

5 years ago

0.3.13

5 years ago

0.3.12

5 years ago

0.3.11

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago