Implementation of the model system for the TypeScript language
- 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
Install tsmodels
from npm
or yarn
:
# from `npm`
npm i tsmodels --save
# from `yarn`
yarn add tsmodels --save
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.new<modeType>(<model-type>, <data>)
- Creates an instance of pass model class with dataModel.newCollection<modeType>(<model-type>, <data[]>)
- Creates an instances collection of pass model class with data array
Decorator of fields for model
- Optional param
field_name
- Name of server format field - Optional param
type
- Optional. Type of related model
Convert model to object using Alias's metadata
Parameters: {string[]} only - Array of field's aliases to export
// ...
export class Town extends Model {
@Alias('main_title') public mainTitle: string;
}
// ...
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();
}
}
User will be:
{
"name": "Kyle Katarn",
"lastName": "Katarn",
"towns": [
{
"mainTitle": "Sulonia"
},
{
"mainTitle": "Sulon"
}
],
"townMain": {
"mainTitle": "Suloniaaaa3"
}
}
User will be:
{
"name": "Kyle Katarn",
"last_name": "Katarn",
"towns": [
{
"main_title": "Sulonia"
},
{
"main_title": "Sulon"
}
],
"main_town": {
"main_title": "Suloniaaaa3"
}
}