@elatonsev/js-models
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

Js Models

Package allows to describe models with relations in your app, create model objects from the REST API payload and serialize model object to JSON.

Installation

yarn add @elatonsev/js-models

Examples

Define models

import { Model, attr, belongsTo, hasMany } from '@elatonsev/js-models';

class Client extends Model {
  name = attr('string');
  schedule = attr('json', {defaultValue: []});
}

class Product extends Model {
  name = attr('string');
}

class Order extends Model {
  name = attr('string', {defaultValue: 'Hi'});
  number = attr('number', {defaultValue: 0});
  isValid = attr('boolean');
  date = attr('date');
  products = hasMany(Product);
  client = belongsTo(Client);
}

Define models using TypeScript

import { Model, attr, belongsTo, hasMany } from '@elatonsev/js-models';

class Client extends Model {
  name = attr('string');
  customArray = attr('json', {defaultValue: []});
  customObject = attr('json', {defaultValue: {}});
}

class Product extends Model {
  name = attr('string');
}

class Order extends Model {
  name = attr('string', {defaultValue: 'Hi'});
  number = attr('number', {defaultValue: 0});
  isValid = attr('boolean');
  date = attr('date');
  products = hasMany<Product>(Product);
  client = belongsTo<Client>(Client);
}

Create objects from nested JSON payload

const order = new Order().pushPayload({
  id: 1,
  date: '2021-05-06',
  name: 'Test name',
  is_valid: false,
  number: '123',
  products: [{
    id: 2,
    name: 'Maffin'
  },
  {
    id: 3,
    name: 'Coffee'
  }],
  client: {
    id: 1,
    name: 'Client1',
    custom_array: [500, 512, 525, 640],
    custom_object: {'key1': 'value1', 'key2': {'nested_key1': 'nested_value'}}
  }
});

Serialize objects to JSON payload

client.serialize();

Serialize objects to JSON payload using custom serializer

const orderSerializer = function(serializedOrder) {
  return {
    ...serializedOrder,
    date: serializedOrder.date ? moment(serializedOrder.date).format('YYYY-MM-DD') : null
  }
}
order.serialize(orderSerializer);

Package Sidebar

Install

npm i @elatonsev/js-models

Weekly Downloads

132

Version

2.0.4

License

MIT

Unpacked Size

12.1 kB

Total Files

7

Last publish

Collaborators

  • elatonsev