@anchan828/typeorm-history

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

历史实体 类型ORM

此软件包已被标记为弃用

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

中文使用文档
文档目录13 节
  1. 说明
  2. 安装
  3. 快速启动
  4. 1. 创建实体
  5. 2. 创建历史实体
  6. 3. 创建历史实体订阅者
  7. 4. 创建连接
  8. 5. 插入/更新/删除实体
  9. 高级
  10. 钩子
  11. 丢弃独有索引
  12. 覆盖动作列类型
  13. 许可证

@anchan828/typeorm-历史

npm NPM

说明

创建历史实体 类型ORM

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

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

安装

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

快速启动

1. 创建实体

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

  @Column()
  public test!: string;
}

2. 创建历史实体

您需要添加与 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;
}

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

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

继承实体类

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

  @HistoryActionColumn()
  public action!: HistoryActionType;
}

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

3. 创建历史实体订阅者

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

4. 创建连接

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

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

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

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

// Remove
await testEntity.remove();

高级

钩子

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

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

丢弃独有索引

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

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

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

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

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

覆盖动作列类型

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

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

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

许可证

麻省理工学院

版本与发布记录

@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 是什么?

历史实体 类型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。作者已将其标记为弃用,新项目建议寻找替代方案。