0.4.7 • Published 5 years ago

tsmodels v0.4.7

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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:

# from `npm`
npm i tsmodels --save

# from `yarn`
yarn add tsmodels --save

Usage

It is necessary to inherit the model from the class Model:

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
// ...

export class Town extends Model {
  @Alias('main_title') public mainTitle: string;
}
User model
// ...

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;
}
// ...

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:

{
  "name": "Kyle Katarn", 
  "lastName": "Katarn", 
  "towns": [
    {
      "mainTitle": "Sulonia"
    },
    {
      "mainTitle": "Sulon"
    }
  ],
  "townMain": {
    "mainTitle": "Suloniaaaa3"
  }
}
_toJSON()

User will be:

{
  "name": "Kyle Katarn", 
  "last_name": "Katarn", 
  "towns": [
    {
      "main_title": "Sulonia"
    },
    {
      "main_title": "Sulon"
    }
  ],
  "main_town": {
    "main_title": "Suloniaaaa3"
  }
}
0.4.7

5 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.7

6 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.1.1

7 years ago

0.0.1

7 years ago