@anchan828/typeorm-history

中文文档
1.0.38发布于 2022/12/04MITTypeScriptCommonJS零依赖近 30 天下载 1983

History Entity for TypeORM

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录13 节
  1. Description
  2. Installation
  3. Quick Start
  4. 1. Create Entity
  5. 2. Create History Entity
  6. 3. Create Entity Subscriber for History
  7. 4. Create connection
  8. 5. Insert/Update/Remove entity
  9. Advanced
  10. Hooks
  11. Drop unique indices
  12. Overwrite action column type
  13. License

@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();

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

版本与发布记录

@anchan828/typeorm-history 共发布 184 个版本,首次发布于 2019/04/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.38latest2022/12/043 年前
1.0.372022/11/273 年前
1.0.362022/11/203 年前
1.0.352022/11/133 年前
1.0.342022/11/064 年前
1.0.332022/10/304 年前

关于 @anchan828/typeorm-history 的常见问题

@anchan828/typeorm-history 是什么?

History Entity for TypeORM注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @anchan828/typeorm-history?

在项目目录运行 npm install @anchan828/typeorm-history,或使用 pnpm add / yarn add / bun add。

@anchan828/typeorm-history 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anchan828/typeorm-history 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anchan828/typeorm-history 可以商用吗?

@anchan828/typeorm-history 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anchan828/typeorm-history 的下载量有多少?

根据 npm 官方下载统计,@anchan828/typeorm-history 在 2026/10/06 – 2026/11/04 这 30 天内被下载 1,983 次,日均约 66 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anchan828/typeorm-history 还在维护吗?

最新版本 1.0.38 发布于 2022/12/04(3 年前);至今共发布 184 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/04/02。作者已将其标记为弃用,新项目建议寻找替代方案。