tsmodels
TypeScript icon, indicating that this package has built-in type declarations

0.4.7 • Public • Published

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"
  }
}

Package Sidebar

Install

npm i tsmodels

Weekly Downloads

5

Version

0.4.7

License

MIT

Unpacked Size

153 kB

Total Files

17

Last publish

Collaborators

  • ikasparov