nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.06k stars 538 forks source link

Queries are returning a duplicated id element inside an Array #823

Open sebabromberg opened 1 year ago

sebabromberg commented 1 year ago

Bug Report

Al queries "findOne" or "find" are returning the id field as Array with duplicated id value inside. TypeORM is returning ok, i've consoled the returning values. The controller layer is failing.

Current behavior

"id": [ 14, 14 ],

Input Code

Model:

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import {
  IsString,
  MinLength,
  IsDefined,
  IsEmail,
  IsBoolean,
} from 'class-validator';
import { Modules } from './modules.model';

@Entity({ name: 'dbo.GLOBAL_usuarios' })
export class User {
  @PrimaryGeneratedColumn({ type: 'integer' })
  id: number;

  @Column()
  @IsDefined({ always: true })
  @IsEmail()
  @MinLength(2, { always: true })
  email: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(2, { always: true })
  username: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(2, { always: true })
  nombre: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(2, { always: true })
  apellido: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(2, { always: true })
  password: string;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(2, { always: true })
  telefono: string;

  @Column()
  @IsBoolean()
  estado: boolean;

  @Column()
  @IsDefined({ always: true })
  @IsString({ always: true })
  @MinLength(2, { always: true })
  secret: string;

  @Column()
  usuario_creacion: number;

  @Column()
  usuario_actualizacion: number;

  @Column()
  recibir_email: boolean;

  @Column()
  fecha_creacion: Date;

  @Column()
  fecha_actualizacion: Date;

  @OneToMany(() => Modules, (module) => module.idUsuario, { eager: true })
  modules: Modules[];
}

Controller:

import {
  Body,
  Controller,
  Delete,
  Post,
  UseGuards,
  UseInterceptors,
  Patch,
  Param,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiTags,
} from '@nestjs/swagger';
import {
  Crud,
  CrudController,
  CrudRequest,
  CrudRequestInterceptor,
  Override,
  ParsedBody,
  ParsedRequest,
} from '@nestjsx/crud';
import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard';
import { createHash } from 'crypto';
import { User } from '../model/users.model';
import { UsersService } from '../service/users.service';
import AddUserModuleDto from '../dto/addUserModule.dto';
import * as speakeasy from 'speakeasy';

@Crud({
  model: {
    type: User,
  },
  params: {
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  query: {
    exclude: ['password'],
    join: {
      modules: {
        eager: true,
      },
    },
  },
})
@Controller('users')
@ApiTags('Usuarios')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
export class UsersController implements CrudController<User> {
  constructor(public service: UsersService) {}
}

Expected behavior

Returning the id as a number, not an array.

Possible Solution

Keep "typeorm": "^0.2.37" instead of 0.3.x

Environment


 "dependencies": {
    "@nestjs-modules/mailer": "^1.8.1",
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.0.0",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/swagger": "^6.3.0",
    "@nestjs/typeorm": "^9.0.1",
    "@nestjsx/crud": "^5.0.0-alpha.3",
    "@nestjsx/crud-typeorm": "^5.0.0-alpha.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "compression": "^1.7.4",
    "cookie-parser": "^1.4.6",
    "express-session": "^1.17.3",
    "helmet": "^6.1.5",
    "mssql": "^9.1.1",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "speakeasy": "^2.0.0",
    "typeorm": "^0.3.15"
  },

Repository with minimal reproduction

N/A

MrMaz commented 1 year ago

This was fixed with this PR https://github.com/nestjsx/crud/pull/797

afilp commented 1 year ago

Hello @MrMaz Is this package maintained (seems not?), or should we try other forks (for example: https://github.com/gid-oss/dataui-nestjs-crud)? It would be nice if nestjs itself were supporting such an essential package. Thanks.