# typescript-model

> Typed entity modeling library with attribute sanitization and validation.

Latest version **1.3.1** (published 2020-09-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install typescript-model
pnpm add typescript-model
yarn add typescript-model
bun add typescript-model
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2020-09-16 |
| First published | 2018-11-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Adam Pancutt |
| Maintainers | apancutt |
| Keywords | typescript, entity, model, sanitize, sanitization, validate, validation |

## Links

- npm: https://www.npmjs.com/package/typescript-model
- Repository: https://github.com/apancutt/typescript-model
- Homepage: https://github.com/apancutt/typescript-model#readme
- Issues: https://github.com/apancutt/typescript-model/issues
- npm.io page: https://npm.io/package/typescript-model

## Dependencies (1)

- [validator](https://npm.io/package/validator.md) 13

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.3.1 (latest) — 2020-09-16
- 1.3.0 — 2020-09-16
- 1.2.5 — 2020-07-17
- 1.2.4 — 2020-07-17
- 1.2.3 — 2019-12-29
- 1.2.2 — 2019-07-15
- 1.2.1 — 2019-06-06
- 1.2.0 — 2018-11-22
- 1.1.2 — 2018-11-18
- 1.1.1 — 2018-11-18
- 1.1.0 — 2018-11-18
- 1.0.0 — 2018-11-16
- 0.0.5 — 2018-11-16
- 0.0.4 — 2018-11-16
- 0.0.3 — 2018-11-16
- … 2 more at https://npm.io/package/typescript-model/versions

## README

# typescript-model

Typed entity modeling library with attribute sanitization and validation.

## Installation

```shell
npm install --save typescript-model
```

## Usage

An example `User` model is shown below.

```typescript
import { Model, Config as BaseConfig, Sanitizers, Validators, sanitizers, validators } from 'typescript-model';

export interface Config extends BaseConfig {
  minUsernameLength: number,
};

export class User extends Model<Config> {

  public email: string;
  public id?: number;
  public username: string;
  public verified: boolean;

  static config: Config = {
    ...Model.config,
    minUsernameLength: 5,
  };

  static sanitizers: Sanitizers<User> = {
    ...Model.sanitizers,
    email: (model, key, value) => sanitizers.string(value),
    id: (model, key, value) => undefined !== value ? sanitizers.integer(value) : undefined,
    username: (model, key, value) => sanitizers.string(value),
    verified: (model, key, value) => sanitizers.boolean(value),
  };

  static validators: Validators<User> = {
    ...Model.validators,
    email: (model, key, value) => validators.email(value),
    id: (model, key, value) => undefined === value || validators.integer(value, { min: 1 }),
    username: (model, key, value) => validators.string(value, { min: model.config.minUsernameLength }),
    verified: (model, key, value) => validators.boolean(value),
  };

  public constructor(email: string, username: string, verified: boolean = false, config: Partial<Config> = {}) {
    super({ ...User.config, ...config as object } as Config);
    this.email = email;
    this.username = username;
    this.verified = verified;
  }

}
```

Instantiation:

```typescript
import { User } from './User';

const user = new User('foo@example.com', 'foo', false);

console.log(user);

// User { email: 'foo@example.com', username: 'foo', verified: false }
```

 Review the `__tests__` directory for additional usage examples.

## Motivation

There are various ORM libraries available for Typescript and JavaScript but all are tightly coupled with Data Access Mappers and underlying data stores.

This library provides just the low-level requirements of an entity model without the concern of how data is mapped.

The specific use-case that led to it's inception was to be able to share entity models between API and client applications. While an API application maps data sourced from arbitrary - and usually multiple - data stores, the client application would map data sourced from the API.

Both applications would share a common package that provides the model definitions containing common domain and business logic while each being able to implement their own data mapping functionality, either by extending the base entities with `create()`, `update()`, etc. implementations (Active Record pattern) or with dedicated data mapper classes (Repository pattern).

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