This is the official plugin from @automapper
to work with Sequelize
enabled projects. sequelize
is essentially extending from classes
with some modification to work with Sequelize#Model
type.
npm i @automapper/sequelize
or with yarn
:
yarn add @automapper/sequelize
@automapper/sequelize
depends on sequelize
, @automapper/classes
, and all of @automapper/classes
peerDependencies
npm i sequelize @automapper/{core,classes} reflect-metadata
npm i --save-dev @types/sequelize @automapper/types
or with yarn
:
yarn add sequelize @automapper/{core,classes} reflect-metadata
yarn add --dev @types/sequelize @automapper/types
@automapper/sequelize
providessequelize
as a(options: SequelizeInitializerOptions) => MapPluginInitializer
. Passsequelize()
tocreateMapper
to create aMapper
that usessequelize
plugin.
export interface SequelizeInitializerOptions {
valueGetter?: <
TGetterResult extends Record<string, unknown> = any,
TModel extends Model<TGetterResult> = Model<TGetterResult>
>(
model: TModel
) => TGetterResult;
init?: <
TDestination extends Dictionary<TDestination> = any,
TModel extends Model<TDestination> = Model<TDestination>
>(
destination: TModel,
destinationObj: TDestination
) => TDestination;
}
valueGetter
is an optional getter that, if passed in, will be used to extract the value of the model after instantiation. By default, it will usesequelize#Model.get()
if it's available.init
is an optional init function that, if passed in, will be used to instantiate aSequelize#Model
after map. By default, it will usenew Model(mapResult)
import { sequelize } from '@automapper/sequelize';
import { Model } from 'sequelize-typescript';
const mapper = createMapper({
...,
pluginInitializer: sequelize()
/**
* or
* sequelize({
* valueGetter: model => model.get(getterOptions),
* init: (model, mapResult) => model.init(mapResult, initOptions)
* })
*/
})
class User extends Model {}
mapper.createMap(UserVm, User);
mapper.map(userVm, User, UserVm);
Read more about this plugin on sequelize documentation