I'm encountering a challenge with my Prisma objects and could use some insights. I've defined the following Prisma objects, and my expectation is to optionally pass an id and perform an upsert. However, I'm consistently encountering the error message from Swagger:
"Missing field or invalid field type"
This occurs despite the id field being marked as optional in the definition. Any thoughts or suggestions on what might be causing this issue?
Thanks in advance for your help!
Model
model Vendor {
id String @id @unique() @default(uuid())
vendor String @unique()
}
// src/app-modules/vendor/dto/upsert-vendor.dto.ts
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class VendorDto {
@ApiProperty({ example: 'Vendor Name' })
@IsNotEmpty()
@IsString()
vendor: string;
@ApiProperty({ example: '1', required: false })
@IsOptional()
id?: string;
}
// src/app-modules/vendor/vendor.controller.ts
import { Controller, Get, Post, Body } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { VendorService } from './vendor.service';
import { VendorDto } from './dto/vendor.dto';
@ApiTags('Vendor')
@Controller('vendor')
export class VendorController {
constructor(private readonly vendorService: VendorService) {}
@Get()
@ApiOperation({ summary: 'Get all vendors' })
@ApiResponse({
status: 200,
description: 'Returns all vendors',
})
findAll() {
return this.vendorService.findAll();
}
@Post('upsert')
@ApiOperation({ summary: 'Upsert a vendor' })
@ApiResponse({
status: 201,
description: 'Vendor has been upserted successfully',
})
upsert(@Body() upsertVendorDto: VendorDto) {
return this.vendorService.upsert(upsertVendorDto);
}
}
Repository:
import { PrismaService } from '../prisma/prisma.service';
import { VendorDto } from './dto/vendor.dto';
@Injectable()
export class VendorRepository {
constructor(private prisma: PrismaService) {}
async findAll() {
return this.prisma.vendor.findMany();
}
async upsert({ vendor, id }: VendorDto) {
return this.prisma.vendor.upsert({
where: { id },
update: { vendor },
create: { vendor },
});
}
}
Service
import { Injectable } from '@nestjs/common';
import { VendorDto } from './dto/vendor.dto';
import { VendorRepository } from './vendor.repository';
@Injectable()
export class VendorService {
constructor(private vendorRepository: VendorRepository) {}
async findAll() {
return await this.vendorRepository.findAll();
}
async upsert(data: VendorDto) {
return this.vendorRepository.upsert(data);
}
}
Was able to fix it with this
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { VendorDto } from './dto/vendor.dto';
@Injectable()
export class VendorRepository {
constructor(private prisma: PrismaService) {}
async findAll() {
return await this.prisma.vendor.findMany();
}
async upsert({ name, domain, id }: VendorDto) {
return await this.prisma.vendor.upsert({
where: { id: id || '' },
update: { name, domain },
create: { name, domain },
});
}
async delete(id: string) {
return await this.prisma.vendor.delete({
where: { id },
});
}
}