-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How would look like a prisma-mapper.base.ts ? #41
Comments
Prisma generates typescript types/interfaces for you: https://www.prisma.io/docs/concepts/components/prisma-client/advanced-type-safety |
Ah great ! thanks for your explanation and the link provided ! |
@Sairyss is there any chance you can provide an example of repository for using prisma typescript types/interfaces with mapper ? (for both methods toDomainEntity and toOrmEntity) I'm totally stuck on the implementation of the classes |
I don't use Prisma, but according to their docs it generates TS types based on your schemas and exports them from The mapper would look something like this: // user type exported from prisma, not sure if the "from" is correct but it should be somewhere in your project
import { User as PrismaUser } from "@prisma/client";
export class UserOrmMapper extends OrmMapper<UserEntity, PrismaUser> {
protected toOrmProps(entity: UserEntity): PrismaUser {
const props = entity.getPropsCopy();
const prismaUser: PrismaUser = {
email: props.email.value,
role: props.role,
};
return prismaUser;
}
protected toDomainProps(prismaUser: PrismaUser): EntityProps<UserProps> {
const id = new UUID(prismaUser.id);
const props: UserProps = {
email: new Email(prismaUser.email),
role: prismaUser.role,
};
return { id, props };
}
} Repository can look something like this: export class UserRepository {
protected constructor(
protected readonly mapper: OrmMapper<UserEntity, PrismaUser>,
) {}
private readonly prisma = new PrismaClient();
async save(entity: UserEntity): Promise<UserEntity> {
entity.validate(); // Protecting invariant before saving
const ormEntity = this.mapper.toOrmEntity(entity);
const user = await this.prisma.user.create({ data: ormEntity })
return this.mapper.toDomainEntity(user);
}
} You might need to modify base classes slightly to support prisma instead of typeorm, but the general principles are the same |
Thanks @Sairyss I'm gonna try this :) |
Everything working like a charm, thanks @Sairyss ! |
Thanks a lot for this repo !
I would like to know how it would be possible to use a mapper with Prisma (like orm-mapper.base.ts).
As far as I understand, it sounds that it's not meant for creating model inside a class, but only inside a schema.prisma file
Thanks for any help !
The text was updated successfully, but these errors were encountered: