How to exclude entity field from returned by controller JSON. NestJS + Typeorm

I’d suggest creating an interceptor that takes advantage of the class-transformer library: @Injectable() export class TransformInterceptor implements NestInterceptor { intercept( context: ExecutionContext, call$: Observable<any>, ): Observable<any> { return call$.pipe(map(data => classToPlain(data))); } } Then, simply exclude properties using @Exclude() decorator, for example: import { Exclude } from ‘class-transformer’; export class User { id: number; email: … Read more

Class-validator – validate array of objects

Add @Type(() => AuthParam) to your array and it should be working. Type decorator is required for nested objects(arrays). Your code becomes import { IsArray, ValidateNested, ArrayMinSize, ArrayMaxSize } from ‘class-validator’; import { AuthParam } from ‘./authParam.model’; import { Type } from ‘class-transformer’; export class SignInModel { @IsArray() @ValidateNested({ each: true }) @ArrayMinSize(2) @ArrayMaxSize(2) @Type(() … Read more

TypeORM Entity in NESTJS – Cannot use import statement outside a module

My assumption is that you have a TypeormModule configuration with an entities property that looks like this: entities: [‘src/**/*.entity.{ts,js}’] or like entities: [‘../**/*.entity.{ts,js}’] The error you are getting is because you are attempting to import a ts file in a js context. So long as you aren’t using webpack you can use this instead so … Read more

Inject TypeORM repository into NestJS service for mock data testing

Let’s assume we have a very simple service that finds a user entity by id: export class UserService { constructor(@InjectRepository(UserEntity) private userRepository: Repository<UserEntity>) { } async findUser(userId: string): Promise<UserEntity> { return this.userRepository.findOne(userId); } } Then you can mock the UserRepository with the following mock factory (add more methods as needed): // @ts-ignore export const repositoryMockFactory: … Read more

Why are cookies not sent to the server via getServerSideProps in Next.js?

That’s because the request inside getServerSideProps doesn’t run in the browser – where cookies are automatically sent on every request – but actually gets executed on the server, in a Node.js environment. This means you need to explicitly pass the cookies to the axios request to send them through. export async function getServerSideProps({ req }) … Read more