# tsmodels

> An implementation of models for TypeScript

Latest version **0.4.7** (published 2019-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install tsmodels
pnpm add tsmodels
yarn add tsmodels
bun add tsmodels
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.7 |
| Published | 2019-05-10 |
| First published | 2017-08-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 149.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Igor Kasparov |
| Maintainers | ikasparov |
| Keywords | typescript, ts, model, angular, ngx, angular-models, aliases, convert, instance |

## Links

- npm: https://www.npmjs.com/package/tsmodels
- Repository: https://github.com/ikasparov/tsmodels
- Homepage: https://github.com/ikasparov/tsmodels#readme
- Issues: https://github.com/ikasparov/tsmodels/issues
- npm.io page: https://npm.io/package/tsmodels

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 0.4.7 (latest) — 2019-05-10
- 0.4.6 — 2018-04-22
- 0.4.5 — 2018-04-22
- 0.4.4 — 2018-04-22
- 0.4.3 — 2018-04-22
- 0.4.2 — 2018-04-19
- 0.4.1 — 2018-03-16
- 0.4.0 — 2018-03-03
- 0.3.1 — 2018-02-12
- 0.3.0 — 2018-02-05
- 0.2.7 — 2017-12-20
- 0.2.6 — 2017-10-01
- 0.2.5 — 2017-09-06
- 0.2.4 — 2017-09-04
- 0.2.3 — 2017-09-04
- … 3 more at https://npm.io/package/tsmodels/versions

## README

# tsmodels
Implementation of the model system for the TypeScript language

## Features
- Convert format fields from server to client and back
- Creates instances of models rather than typing (with conversion)
- Perceives both nested (connected) models and collections of any depth

## Installation
Install `tsmodels` from `npm` or `yarn`:
```bash
# from `npm`
npm i tsmodels --save

# from `yarn`
yarn add tsmodels --save
```

## Usage
It is necessary to inherit the model from the class `Model`:
```typescript
import { Alias, Model } from 'tsmodels';

export class User extends Model {
  // ...
}
```
For the model instance, methods will be available `_fromJSON(value)` and `_toJSON()` methods

### Model clear static methods

- `Model.new<modeType>(<model-type>, <data>)` - Creates an instance of pass model class with data
- `Model.newCollection<modeType>(<model-type>, <data[]>)` - Creates an instances collection of pass model class with data array

### @Alias(...)
Decorator of fields for model

- Optional param `field_name` - Name of server format field
- Optional param `type` - Optional. Type of related model

### _toJSON()

Convert model to object using Alias's metadata

Parameters:
  {string[]} only - Array of field's aliases to export

## Example

##### Town model
```typescript
// ...

export class Town extends Model {
  @Alias('main_title') public mainTitle: string;
}
```

##### User model
```typescript
// ...

export class User extends AppnModel {
  @Alias() public name: string;
  @Alias('last_name') public lastName: string;
  @Alias('towns', Town) public towns: Town[];
  @Alias('main_town', Town) public townMain: Town;
}
```

```typescript
// ...

const townMock = { main_title: 'Sulonia' };
const townMock2 = { main_title: 'Sulon' };

const userMock = {
  name: 'Kyle Katarn',
  last_name: 'Katarn',
  towns: [townMock, townMock2],
  main_town: [townMock2],
};

class ModelExample {
  public user: User = new User();
  
  constructor() {
    this.user._fromJSON(userMock);
    this.user._toJSON();
  }
}
```

##### _fromJSON()
User will be:
```json
{
  "name": "Kyle Katarn", 
  "lastName": "Katarn", 
  "towns": [
    {
      "mainTitle": "Sulonia"
    },
    {
      "mainTitle": "Sulon"
    }
  ],
  "townMain": {
    "mainTitle": "Suloniaaaa3"
  }
}
```

##### _toJSON()
User will be:
```json
{
  "name": "Kyle Katarn", 
  "last_name": "Katarn", 
  "towns": [
    {
      "main_title": "Sulonia"
    },
    {
      "main_title": "Sulon"
    }
  ],
  "main_town": {
    "main_title": "Suloniaaaa3"
  }
}
```

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