This package has been deprecated

Author message:

This package was moved to another scope, please look at https://www.npmjs.com/package/@tomas-light/mapper-js

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

1.1.1 • Public • Published

mapper-js

Auto mapper for you models like AutoMapper in .Net

Installation

yarn add @tomas_light/mapper-js

How to use

MenuService.js

import { Mapper } from '@tomas_light/mapper-js';

export class MenuService {
  update(menu) {
    const entity = Mapper.map(
      'Menu',
      'MenuEntity',
      menu
    );

    const menuEntity = this.db.update(entity);
  }
}

or with Typescript:

MenuService.ts

import { Mapper } from '@tomas_light/mapper-js';
import { MenuEntity } from './MenuEntity';
import { Menu } from './Menu';

export class MenuService {
  update(menu: Menu) {
    const entity = Mapper.map<MenuEntity>(
      nameof<Menu>(),
      nameof<MenuEntity>(),
      menu
    );

    const menuEntity = this.db.update(entity);
  }
}

P.S. nameof in example from ts-nameof package (here https://www.npmjs.com/package/ts-nameof)

How to configure

You have an interface and its implementation

Menu.ts

export interface Menu {
  id: number;
  name?: string;
  createDate: Date;
  lastUpdate: Date;
  author?: User;
  dishes?: DishInMenu[];
}

MenuEntity.ts

export interface MenuEntity {
  id: number;
  create_date: string;
  last_update: string;
  author_id?: number;
  name?: string;
}

Add mapping profile for your models

EntityMappingProfile.js

import { MapFunction, MappingProfile, MappingProfileBase } from '@tomas_light/mapper-js';
import { MenuEntity } from './MenuEntity';
import { Menu } from './Menu';

export class EntityMappingProfile extends MappingProfileBase {
  get() {
    return [
      new MapFunction(
        'Menu',
        'MenuEntity',
        EntityMappingProfile.mapMenuToMenuEntity
      ),
      new MapFunction(
        'MenuEntity',
        'Menu',
        EntityMappingProfile.mapMenuEntityToMenu
      ),
    ];
  }

  static mapMenuToMenuEntity(menu) {
    return {
      id: menu.id,
      create_date: menu.createDate.toISOString(),
      last_update: menu.lastUpdate.toISOString(),
      author_id: menu.author?.id,
      name: menu.name,
    }
  }

  static mapMenuEntityToMenu(entity) {
    return {
      id: entity.id,
      createDate: new Date(entity.create_date),
      lastUpdate: new Date(entity.last_update),
      name: entity.name,
    }
  }
}

or with Typescript

EntityMappingProfile.ts

import { MapFunction, MappingProfile, MappingProfileBase } from '@tomas_light/mapper-js';
import { MenuEntity } from './MenuEntity';
import { Menu } from './Menu';

export class EntityMappingProfile extends MappingProfileBase implements MappingProfile {
  get(): MapFunction[] {
    return [
      new MapFunction(
        nameof<Menu>(),
        nameof<MenuEntity>(),
        EntityMappingProfile.mapMenuToMenuEntity
      ),
      new MapFunction(
        nameof<MenuEntity>(),
        nameof<Menu>(),
        EntityMappingProfile.mapMenuEntityToMenu
      ),
    ];
  }

  private static mapMenuToMenuEntity(menu: Menu): MenuEntity {
    return {
      id: menu.id,
      create_date: menu.createDate.toISOString(),
      last_update: menu.lastUpdate.toISOString(),
      author_id: menu.author?.id,
      name: menu.name,
    }
  }

  private static mapMenuEntityToMenu(entity: MenuAttributes): Menu {
    return {
      id: entity.id,
      createDate: new Date(entity.create_date),
      lastUpdate: new Date(entity.last_update),
      name: entity.name,
    }
  }
}

After that, register your profiles in Mapper. Don't forget to call this function in your entrypoint file.

configureMapper.ts

import { Mapper } from '@tomas_light/mapper-js';
import { EntityMappingProfile } from './EntityMappingProfile';

export function configureMapper() {
  Mapper.addProfiles([
    new EntityMappingProfile(),
  ]);
}

You can see more examples in mapper-js/src/Mapper.test.ts

Package Sidebar

Install

npm i @tomas_light/mapper-js

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

14.4 kB

Total Files

21

Last publish

Collaborators

  • tomas-light