lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.54k stars 499 forks source link

How to use Prisma model with tsoa #1481

Closed ArkasDev closed 1 year ago

ArkasDev commented 1 year ago

I use Prisma as ORM service. Prisma provides its own schema for the model. Of course I want to use this model in the api interface. Unfortunately this is not recognized as is present just as "string".

Is it possible to use the Prisma model with tsoa?

user-controller.ts

import {User} from "@prisma/client";
import {Body, Controller, Example, Get, Post, Put, Route, SuccessResponse, Tags} from "tsoa";

@Route("user")
@Tags('user')
export class UserController extends Controller {

    @Post()
    @SuccessResponse("201", "Created")
    public async create(@Body() user: User): Promise<User> {
    }
}

prisma.schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id                String           @id @default(auto()) @map("_id") @db.ObjectId
  email             String           @unique @map("_email")
}

Screenshot 2023-09-09 at 12 20 32

github-actions[bot] commented 1 year ago

Hello there ArkasDev 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

ArkasDev commented 1 year ago

I solved this problem using @brakebein/prisma-generator-nestjs-dto generator.

generator model {
  provider                        = "prisma-generator-nestjs-dto"
  output                          = "./model"
  outputToNestJsResourceStructure = "false"
  flatResourceStructure           = "false"
  exportRelationModifierClasses   = "true"
  reExport                        = "false"
  createDtoPrefix                 = "Create"
  updateDtoPrefix                 = "Update"
  dtoSuffix                       = "Dto"
  entityPrefix                    = ""
  entitySuffix                    = ""
  classValidation                 = "false"
  fileNamingStyle                 = "camel"
  noDependencies                  = "true"
  outputType                      = "class"
  definiteAssignmentAssertion     = "false"
  requiredResponseApiProperty     = "true"
  prettier                        = "true"
}