# loopback-history-extension

> DataModels with history extension for Loopback v4

Latest version **6.1.0** (published 2020-03-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install loopback-history-extension
pnpm add loopback-history-extension
yarn add loopback-history-extension
bun add loopback-history-extension
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.1.0 |
| Published | 2020-03-31 |
| First published | 2019-10-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.9 |
| Dependencies | 5 |
| Unpacked size | 29.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | KoLiBer |
| Maintainers | koliber |
| Keywords | loopback-extension, loopback |

## Links

- npm: https://www.npmjs.com/package/loopback-history-extension
- Repository: https://github.com/koliberr136a1/loopback-history-extension
- Homepage: https://github.com/koliberr136a1/loopback-history-extension#readme
- Issues: https://github.com/koliberr136a1/loopback-history-extension/issues
- npm.io page: https://npm.io/package/loopback-history-extension

## Dependencies (5)

- [tslib](https://npm.io/package/tslib.md) ^1.11.1
- [@loopback/boot](https://npm.io/package/@loopback/boot.md) ^2.0.2
- [@loopback/core](https://npm.io/package/@loopback/core.md) ^2.2.0
- [@loopback/context](https://npm.io/package/@loopback/context.md) ^3.2.0
- [@loopback/repository](https://npm.io/package/@loopback/repository.md) ^2.0.2

## Recent versions

- 6.1.0 (latest) — 2020-03-31
- 6.0.0 — 2020-02-04
- 5.5.0 — 2020-02-02
- 5.4.0 — 2020-01-08
- 5.3.0 — 2020-01-04
- 5.2.0 — 2020-01-04
- 5.1.0 — 2020-01-03
- 5.0.0 — 2020-01-03
- 4.5.0 — 2020-01-02
- 4.4.0 — 2019-12-26
- 4.3.0 — 2019-12-23
- 4.2.0 — 2019-12-23
- 4.1.0 — 2019-12-11
- 4.0.0 — 2019-12-11
- 3.2.0 — 2019-12-06
- … 18 more at https://npm.io/package/loopback-history-extension/versions

## README

# loopback-history-extension

Saving history of `Create`, `Update`, `Delete` of a table sometimes is a big problem in data model design level.

A good approach for saving history is about adding some columns to your tables:

1. `UID`: A unique record identifier of the history
2. `BeginDate`: Record creation date
3. `EndDate`: Record deletion date
4. `ID`: History of one data is accessable using their same ID

-   Per every `Create` we will create a new record in table
-   Per every `Update` we will invalid last history, create new record
-   Per every `Delete` we will invalid last history

Now, using this simple extension you can add all history features to your models and repositories.

---

## Installation

```bash
npm i --save loopback-history-extension
```

---

## Usage

### History Model

1. Change your model parent class from `Entity` to `HistoryEntity`
2. Remove `id` property from your model declaration

#### Example

Change your model from:

```ts
@model()
export class User extends Entity {
    @property({
        type: "string",
        unique: true,
        id: true
    })
    id: string;

    @property({
        type: "string",
        default: ""
    })
    username: string;

    constructor(data?: Partial<User>) {
        super(data);
    }
}
```

To:

```ts
import { HistoryEntity } from "loopback-history-extension";

@model()
export class User extends HistoryEntity {
    @property({
        type: "string",
        default: ""
    })
    username: string;

    constructor(data?: Partial<User>) {
        super(data);
    }
}
```

---

#### Tip

Don't use `unique` columns in your models

Convert your model from:

```ts
export class User extends Entity {
    @property({
        type: "string",
        required: true,
        index: {
            unique: true
        }
    })
    username: string;
}
```

To:

```ts
export class User extends HistoryEntity {
    @property({
        type: "string",
        required: true
    })
    username: string;
}
```

---

### History Repository Mixin

Change your repository parent class from `DefaultCrudRepository` to `HistoryCrudRepositoryMixin()()`

#### Example

Change your repository from:

```ts
export class UserRepository extends DefaultCrudRepository<
    User,
    typeof User.prototype.id,
    UserRelations
> {
    // ...
}
```

To:

```ts
import { HistoryCrudRepositoryMixin } from "loopback-history-extension";

export class UserRepository extends HistoryCrudRepositoryMixin<
    User,
    UserRelations
>()() {
    // ...
}
```

---

## Contributions

-   [KoLiBer](https://www.linkedin.com/in/mohammad-hosein-nemati-665b1813b/)

## License

This project is licensed under the [MIT license](LICENSE).  
Copyright (c) KoLiBer (koliberr136a1@gmail.com)

---
_Source: https://npm.io/package/loopback-history-extension · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
