# @anchan828/typeorm-history 中文文档

规范地址：https://npmcn.com/package/%40anchan828/typeorm-history/

版本：1\.0\.38
npm 元数据更新时间：2022-12-04T07:27:09.965Z
中文译文更新时间：2026-11-07T08:30:16.246Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40anchan828/typeorm-history
原文对照：https://npmcn.com/package/%40anchan828/typeorm-history/?lang=original
项目主页：https://github.com/anchan828/typeorm-helpers/tree/master/packages/history#readme
源码仓库：https://github.com/anchan828/typeorm-helpers

弃用说明：Package no longer supported\. Contact Support at https://www\.npmjs\.com/support for more info\.

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：21.5 kB，15 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2019-04-02T08:11:56.866Z
- 最新版本发布：2022-12-04T04:04:27.261Z
- 近 30 天下载量：1983（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：184（近 12 个月 0 个）

## 中文简介

历史实体 [类型ORM](http://typeorm.io/)

## 安装

```sh
npm install @anchan828/typeorm-history
```

也可以使用：`pnpm add @anchan828/typeorm-history`、`yarn add @anchan828/typeorm-history`、`bun add @anchan828/typeorm-history`

## README

# @anchan828/typeorm-历史

![npm](https://img.shields.io/npm/v/@anchan828/typeorm-history.svg) ![NPM](https://img.shields.io/npm/l/@anchan828/typeorm-history.svg)

## 说明

创建历史实体 [类型ORM](http://typeorm.io/)

被测试的: mysql, postgres and sqlite. 中国植物物种信息数据库.

> **3⁄4 ̄ ̧漯B**  
> 此软件包只与 `repository.save` 财务报告和审定财务报表 `repository.remove`。 。 。 . 你用不着 `repository.insert`编号 :`repository.update`编号 :`repository.delete`。 。 。 .

## 安装

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

## 快速启动

### 1\. 创建实体

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

  @Column()
  public test!: string;
}
```

### 2\. 创建历史实体

您需要添加与 `TestEntity`。 。 。 .

```ts
@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;
}
```

注:从0.5.0版本起,可以自由定义列名.

```ts
@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;
}
```

#### 继承实体类

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

  @HistoryActionColumn()
  public action!: HistoryActionType;
}
```

注:您可以通过继承原实体类来创建历史实体. 这对于简单的实体来说很容易做到。 然而,如果你想保留独特的索引,就有一个问题。 像很多用户一样 [报告的问题](https://github.com/anchan828/typeorm-helpers/issues/779),当您继承一个类时,您也继承了装饰器,您不能覆盖它。 这意味着,您无法取消对特殊索引的限制 `@Index` 包括:.

### 3\. 创建历史实体订阅者

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

### 4\. 创建连接

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

### 5\. 插入/更新/删除实体

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

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

// Remove
await testEntity.remove();
```

![](https://i.gyazo.com/c5c25280122bfe423533f8db269d04f4.png)

## 高级

### 钩子

您可以在插入/更新/删除历史之前/之后勾通.

```ts
@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> {}
}
```

### 丢弃独有索引

历史表存储着多个内容相同的实体,所以如果有一个独特的索引,就不会有很好的效果. 你需要放弃所有独特的指数. 所以我为移民准备了一个助手功能.

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

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

如果您想要用被移除的唯一旗帜重新生成索引, 请设置Index 以真实性 .

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

### 覆盖动作列类型

默认情况下,动作列的类型使用enum. 然而,根据所使用的数据库,可能无法提供enum.

在这种情况下,您可以将 Collection 选项作为第一个参数传递给历史动作库来覆盖它 .

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

## 许可证

[麻省理工学院](https://npmcn.com/package/%40anchan828/typeorm-history/LICENSE)

## 常见问题

### @anchan828/typeorm-history 是什么？

历史实体 类型ORM注意：该包已被作者标记为弃用——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。作者已将其标记为弃用，新项目建议寻找替代方案。

