Skip to content
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

Closed
balibou opened this issue Feb 16, 2022 · 6 comments
Closed

How would look like a prisma-mapper.base.ts ? #41

balibou opened this issue Feb 16, 2022 · 6 comments

Comments

@balibou
Copy link

balibou commented Feb 16, 2022

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 !

@Sairyss
Copy link
Owner

Sairyss commented Feb 16, 2022

Prisma generates typescript types/interfaces for you: https://www.prisma.io/docs/concepts/components/prisma-client/advanced-type-safety
In a mapper you will just need to map your domain entity to match that type so you can save it into a db

@balibou
Copy link
Author

balibou commented Feb 16, 2022

Ah great ! thanks for your explanation and the link provided !

@balibou balibou closed this as completed Feb 16, 2022
@balibou
Copy link
Author

balibou commented Feb 16, 2022

@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

@balibou balibou reopened this Feb 16, 2022
@Sairyss
Copy link
Owner

Sairyss commented Feb 17, 2022

I don't use Prisma, but according to their docs it generates TS types based on your schemas and exports them from @prisma/client/index.d.ts https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#type-definitions.

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

@balibou
Copy link
Author

balibou commented Feb 20, 2022

Thanks @Sairyss I'm gonna try this :)

@balibou
Copy link
Author

balibou commented Feb 20, 2022

Everything working like a charm, thanks @Sairyss !

@balibou balibou closed this as completed Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants