@longzai-intelligence-shared-kernel/mapper
v0.1.6
Published
Downloads
382
Readme
@longzai-intelligence-shared-kernel/mapper
Mapper 类型定义包,提供 ORM Mapper 和 Application Mapper 的统一接口类型。
安装
bun add @longzai-intelligence-shared-kernel/mapper使用
ORM Mapper 接口
import type { OrmMapper } from '@longzai-intelligence-shared-kernel/mapper';
type UserOrmMapper = OrmMapper<UserEntity, UserOrmEntity>;
// 实现示例
class UserOrmMapperImpl implements UserOrmMapper {
toDomain(orm: UserOrmEntity): UserEntity {
// 转换逻辑
}
toOrm(entity: UserEntity): UserOrmEntity {
// 转换逻辑
}
}Application Mapper 接口
import type { ApplicationMapper } from '@longzai-intelligence-shared-kernel/mapper';
type UserMapper = ApplicationMapper<UserEntity, UserDTO>;
// 实现示例
class UserMapperImpl implements UserMapper {
toDTO(entity: UserEntity): UserDTO {
// 转换逻辑
}
toDTOList(entities: UserEntity[]): UserDTO[] {
return entities.map((entity) => this.toDTO(entity));
}
}API
OrmMapper<TDomain, TOrm>
ORM Mapper 接口类型,定义领域实体与 ORM 实体之间的双向转换契约。
| 方法 | 签名 | 说明 |
| ---------- | --------------------------- | ------------------------- |
| toDomain | (orm: TOrm) => TDomain | 将 ORM 实体转换为领域实体 |
| toOrm | (entity: TDomain) => TOrm | 将领域实体转换为 ORM 实体 |
ApplicationMapper<TDomain, TDTO>
Application Mapper 接口类型,定义领域实体与 DTO 之间的转换契约。
| 方法 | 签名 | 说明 |
| ----------- | --------------------------------- | ------------------------ |
| toDTO | (entity: TDomain) => TDTO | 将领域实体转换为 DTO |
| toDTOList | (entities: TDomain[]) => TDTO[] | 批量将领域实体转换为 DTO |
