nestjsx / crud

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

REST query param "fileds" not working #736

Open apperside opened 3 years ago

apperside commented 3 years ago

Hello, I am trying to integrate your awesome library in my project. I have started by trying to generate rest routes for the following model

import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from "typeorm";
import { WebOrdersAccessCodes } from "./WebOrdersAccessCodes";
import { WebEvents } from "./WebEvents";

@Index("user_id", ["userId"], {})
@Index("event_id", ["eventId"], {})
@Entity("web_bookings")
export class WebBookings {
  @PrimaryGeneratedColumn({ type: "bigint", name: "id" })
  public id!: string;

  @Column("bigint", { primary: true, name: "event_id" })
  public eventId!: string | null;

  @Column("bigint", { primary: true, name: "user_id" })
  public userId!: number | null;

  @Column("bigint", { primary: true, name: "shopify_order_id" })
  public shopifyOrderId!: number | null;

  @Column("varchar", { name: "user_email", nullable: true, length: 255 })
  public userEmail!: string | null;

  @Column("datetime", { name: "booked_date" })
  public bookedDate!: Date;

  @Column("varchar", {
    name: "shopify_order_token",
    nullable: true,
    length: 256,
  })
  public shopifyOrderToken!: string | null;

  @OneToMany("web_orders_access_codes", "order")
  public webOrdersAccessCodes!: WebOrdersAccessCodes[];

  @ManyToOne("web_events", "webBookings", {
    onDelete: "NO ACTION",
    onUpdate: "NO ACTION",
  })
  @JoinColumn([{ name: "event_id", referencedColumnName: "id" }])
  public event!: WebEvents;
}

The routes are properly generated and I can see them on swagger and I can use them. When I try to use the /api/web-bookings endpoint, all records are properly returned, and also the filters work.

Screenshot 2021-09-06 at 10 00 22

As you can see, all data and fields are returned, and also filters are workings.

BTW, I do not have any idea why the "fields" query param is not working:

Screenshot 2021-09-06 at 09 57 17

As you can see, I am requesting only field, but not only that field is returned anyway, also other fields are unreasonably discarded.

This is my controller

import { Controller } from "@nestjs/common";
import { Crud, CrudController } from "@nestjsx/crud";
import { WebBookings } from './../../../typeorm/entities/WebBookings';
import { WebBookingsService } from "./web-bookings.service";

@Crud({
    model: {
        type: WebBookings,
    }
})
@Controller("crud/web-bookings")
export class WebBookingsController implements CrudController<WebBookings> {
    constructor(public service: WebBookingsService) { }
}

And this is my service

import { WebBookings } from './../../../typeorm/entities/WebBookings';
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { TypeOrmCrudService } from "@nestjsx/crud-typeorm";

@Injectable()
export class WebBookingsService extends TypeOrmCrudService<WebBookings> {
    constructor(@InjectRepository(WebBookings) repo) {
        super(repo);
    }
}

I am using this library version

"@nestjsx/crud": "^5.0.0-alpha.3",
"@nestjsx/crud-typeorm": "^5.0.0-alpha.3",

But I have also tried with

"@nestjsx/crud": "^4.6.2",
"@nestjsx/crud-typeorm": "^4.6.2",

What am I doing wrong?

Thank you