Skip to content

Latest commit

 

History

History
172 lines (139 loc) · 4 KB

README.md

File metadata and controls

172 lines (139 loc) · 4 KB

TypeORM Audit (@n4it/typeorm-audit)

Efficient entity history tracking for TypeORM.

N4IT Logo

License npm version npm downloads Renovate PRs welcome

Installation

To install the latest version:

npm install @n4it/typeorm-audit --save

Or using Yarn:

$ yarn add @n4it/typeorm-audit

Usage

To enable auditing, decorate your entities with @Audit():

import { Audit } from "@n4it/typeorm-audit";
import { Entity, Column } from "typeorm";

@Audit() // Enable auditing for this entity
@Entity()
export class User {
  @Column() firstName: string;
  @Column() lastName: string;
  @Column() age: number;
}

Integrate with your DataSource:

import { withAuditDataSource } from "@n4it/typeorm-audit";
import { DataSource } from "typeorm";

const dataSource = await withAuditDataSource(
  new DataSource({
    type: 'sqlite',
    database: ':memory:',
    synchronize: true,
    logging: 'all',
    entities: [User],
  })
);

await dataSource.initialize();

Or using getAuditOptions:

import { getAuditOptions } from "@n4it/typeorm-audit";
import { DataSource } from "typeorm";

const dataSource = new DataSource(
  await getAuditOptions({
    type: 'sqlite',
    database: ':memory:',
    synchronize: true,
    logging: 'all',
    entities: [User],
  })
);

await dataSource.initialize();

Advanced Features

Specify Modified By:

@Audit({
    getModifiedBy: async (connection, newEntity) => {
      // Use the connection to query the database
      return newEntity.lastModifiedBy ?? 1;
    }
})
@Entity()
export class User {
  @Column() firstName: string;
  @Column() lastName: string;
  @Column() age: number;
}

Use a single audit table:

@Audit({ tableName: "audit", saveEntityType: true })
@Entity()
export class User {
  @Column() firstName: string;
  @Column() lastName: string;
  @Column() age: number;
}

@Audit({ tableName: "audit", saveEntityType: true })
@Entity()
export class Company {
  @Column() name: string;
}

Migrations

Enhance your DataSource with auditing capabilities:

import { DataSource } from "typeorm";
import { join } from "path";
import { withAuditDataSource } from "@n4it/typeorm-audit";

export default withAuditDataSource(
  new DataSource({
    type: "postgres",
    useUTC: true,
    host: process.env.host,
    port: process.env.port,
    username: process.env.user,
    password: process.env.password,
    database: process.env.name,
    synchronize: false,
    entities: [`${join(process.cwd(), "src", "entities")}/*`],
    migrations: [`${join(__dirname, "migrations")}/*`],
    migrationsTableName: "migrations",
    logging: true,
  }),
);

Support

We welcome contributions of all forms. Please feel free to submit a pull request or open an issue. Star us on GitHub!

Contributors

Code Contributors

This project exists thanks to all the people who contribute.

Financial Contributors

Organizations

Currently this project is sponsored and maintained by N4IT. Get in touch if you want to become a sponsor.

License

GPL-3.0