Gathering detailed insights and metrics for @operato/typeorm-history
Gathering detailed insights and metrics for @operato/typeorm-history
npm install @operato/typeorm-history
Typescript
Module System
Node Version
NPM Version
60.7
Supply Chain
78.6
Quality
96
Maintenance
100
Vulnerability
99.3
License
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
257,585
Last Day
29
Last Week
1,509
Last Month
8,042
Last Year
138,874
Minified
Minified + Gzipped
Latest Version
9.0.0-beta.37
Package Id
@operato/typeorm-history@9.0.0-beta.37
Unpacked Size
19.77 kB
Size
4.90 kB
File Count
14
NPM Version
lerna/6.6.2/node@v20.15.0+arm64 (darwin)
Node Version
20.15.0
Published on
Feb 01, 2025
Cumulative downloads
Total Downloads
Last Day
-90%
29
Compared to previous day
Last Week
40.4%
1,509
Compared to previous week
Last Month
-24.3%
8,042
Compared to previous month
Last Year
21.8%
138,874
Compared to previous year
1
3
*** This is a package cloned from @anchan828/typeorm-history which was suddenly deprecated. The reason I cloned this code is not to steal the original author's effort, but to prevent my code from suddenly becoming obsolete, even for a moment. ***
Create History Entity for TypeORM
Tested: mysql, postgres and sqlite.
Warning
This package only works withrepository.save
andrepository.remove
. You can't userepository.insert
/repository.update
/repository.delete
.
1$ npm i --save typeorm @operato/typeorm-history
1@Entity() 2class TestEntity extends BaseEntity { 3 @PrimaryGeneratedColumn() 4 public id!: number; 5 6 @Column() 7 public test!: string; 8}
You need to add the same column properties as TestEntity
.
1@Entity() 2class TestHistoryEntity extends BaseEntity implements HistoryEntityInterface<TestEntity> { 3 // id is a property that holds the id of the TestHistoryEntity. 4 @PrimaryGeneratedColumn() 5 public id!: number; 6 7 @Column() 8 public test!: string; 9 10 // originalID is a property that holds the id of the TestEntity. 11 @HistoryOriginalIdColumn() 12 public originalID!: number; 13 14 @HistoryActionColumn() 15 public action!: HistoryActionType; 16}
Note: Starting with version 0.5.0, column names can be defined freely.
1@Entity() 2class TestHistoryEntity extends BaseEntity implements HistoryEntityInterface<TestEntity> { 3 @PrimaryGeneratedColumn() 4 public id!: number; 5 6 @Column() 7 public test!: string; 8 9 // You can use any property name you like. 10 @HistoryOriginalIdColumn() 11 public history_originalID!: number; 12 13 // You can rename column name 14 @HistoryActionColumn({ name: "history_action" }) 15 public action!: HistoryActionType; 16}
1@Entity() 2class TestHistoryEntity extends TestEntity implements HistoryEntityInterface { 3 @HistoryOriginalIdColumn() 4 public originalID!: number; 5 6 @HistoryActionColumn() 7 public action!: HistoryActionType; 8}
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.
1@EventSubscriber() 2class TestHistoryEntitySubscriber extends HistoryEntitySubscriber<TestEntity, TestHistoryEntity> { 3 public get entity() { 4 return TestEntity; 5 } 6 public get historyEntity() { 7 return TestHistoryEntity; 8 } 9}
1await createConnection({ 2 type: "mysql", 3 entities: [TestEntity, TestHistoryEntity], 4 subscribers: [TestHistoryEntitySubscriber], 5 username: "root", 6 password: "test", 7 database: "test", 8});
1// Insert 2const testEntity = await TestEntity.create({ test: "test" }).save(); 3 4// Update 5testEntity.test = "updated"; 6await testEntity.save(); 7 8// Remove 9await testEntity.remove();
You can hook before/after insert/update/remove history.
1@EventSubscriber() 2class TestHistoryEntitySubscriber extends HistoryEntitySubscriber<TestEntity, TestHistoryEntity> { 3 public get entity() { 4 return TestEntity; 5 } 6 7 public get historyEntity() { 8 return TestHistoryEntity; 9 } 10 11 public beforeInsertHistory( 12 history: HistoryEntityType, 13 entity: Readonly<EntityType>, 14 ): HistoryEntityType | Promise<HistoryEntityType> { 15 return history; 16 } 17 18 public afterInsertHistory(history: HistoryEntityType, entity: Readonly<EntityType>): void | Promise<void> {} 19 20 public beforeUpdateHistory( 21 history: HistoryEntityType, 22 entity: Readonly<EntityType>, 23 ): HistoryEntityType | Promise<HistoryEntityType> { 24 return history; 25 } 26 27 public afterUpdateHistory(history: HistoryEntityType, entity: Readonly<EntityType>): void | Promise<void> {} 28 29 public beforeRemoveHistory( 30 history: HistoryEntityType, 31 entity: Readonly<EntityType>, 32 ): HistoryEntityType | Promise<HistoryEntityType> { 33 return history; 34 } 35 36 public afterRemoveHistory(history: HistoryEntityType, entity: Readonly<EntityType>): void | Promise<void> {} 37}
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.
1export class DropAllUniqueOfTestHistoryEntity1625470736630 { 2 async up(queryRunner: QueryRunner): Promise<void> { 3 await dropUniqueIndices(queryRunner, TestHistoryEntity /* "test_history_entity" */); 4 } 5 6 async down(queryRunner: QueryRunner): Promise<void> {} 7}
If you want to regenerate the index with the unique flag removed, please set keepIndex to true.
1export class DropAllUniqueOfTestHistoryEntity1625470736630 { 2 async up(queryRunner: QueryRunner): Promise<void> { 3 await dropUniqueIndices(queryRunner, "test_history_entity", true); 4 } 5}
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.
1@HistoryActionColumn({ type: "varchar" }) 2public action!: HistoryActionType;
No vulnerabilities found.
No security vulnerabilities found.