> For the complete documentation index, see [llms.txt](https://docs.soapjs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.soapjs.com/components/mapper.md).

# Mapper

A **mapper** converts between a domain Entity and its persistence Model, keeping storage details out of the domain. Implement the `Mapper<EntityType, ModelType>` interface in the feature's `data/` folder.

```typescript
import { Mapper } from '@soapjs/soap';
import { Character } from '../domain/character.entity';
import { CharacterDocument } from './character.dto';

export class CharacterMapper implements Mapper<Character, CharacterDocument> {
  toEntity(doc: CharacterDocument): Character {
    return new Character(doc._id, doc.name, doc.universe, doc.alignment, doc.createdAt, doc.updatedAt);
  }

  toModel(entity: Character): CharacterDocument {
    return {
      _id: entity.id,
      name: entity.name,
      universe: entity.universe,
      alignment: entity.alignment,
      createdAt: entity.createdAt,
      updatedAt: entity.updatedAt,
    };
  }
}
```

* `toEntity(model)` — DB document → domain entity (e.g. `_id` → `id`)
* `toModel(entity)` — domain entity → DB document

The mapper is handed to a `DatabaseContext` alongside a Source:

```typescript
const context = new DatabaseContext(source, new CharacterMapper(), soapMongo.sessions);
```
