1.2.1 • Published 11 months ago

@n4it/typeorm-audit v1.2.1

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
11 months ago

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

1.2.1

11 months ago

1.2.0

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago