nestjsx / crud

NestJs CRUD for RESTful APIs

Home Page:https://github.com/nestjsx/crud/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

allow mapping uuid to id field

zetti-caletti opened this issue · comments

Feature Request

Map exposed primary key to another one internally.

Is your feature request related to a problem? Please describe.

I took this example from https://github.com/shinework/nest-crud-nested-controller and modified it to add a uuid field.
The problem is if I have the uuid as the Slug-Parameter and Autor and Article are links with the id field. Then I can't reference that in the params-Parameter of the Crud-Decorator.
I want to map the uuid-Slug to the id field.
Hope it's clear enough.

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  @Generated("uuid")
  uuid: string;

  @Column()
  username: string

  @ApiProperty({ type: () => Article })
  @OneToMany(
    () => Article,
    article => article.author,
  )
  articles: Article[]
}

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  @Generated("uuid")
  uuid: string;

  @Column('text')
  content: string

  @ApiProperty({ type: () => Author })
  @ManyToOne(
    () => Author,
    author => author.articles,
  )
  author: Author

  @Column()
  authorId: number
}

@Crud({
  model: {
    type: Article,
  },
  params: {
    authorId: {
      // authorId is the uuid of the author, but I want to map it to the id of the author
      // because in the database I use integers for primary and foreign keys
      field: 'authorId',
      type: 'uuid',
    },
  },
})
@ApiTags('articles')
@Controller('/authors/:authorId/articles')
export class ArticleController implements CrudController<Article> {
  constructor(public service: ArticleService) {}
}

Describe the solution you'd like

@Crud({
  model: {
    type: Article,
  },
  params: {
    authorId: {
      field: 'author.uuid', // new feature to map the uuid 'authorId' to the uuid field of author (to fetch the author by uuid instead of id)
      type: 'uuid',
    },
  },
})
@ApiTags('articles')
@Controller('/authors/:authorId/articles')
export class ArticleController implements CrudController<Article> {
  constructor(public service: ArticleService) {}
}

Teachability, Documentation, Adoption, Migration Strategy

What is the motivation / use case for changing the behavior?

For the API I want to expose the uuid as the primary key but internally I want to use integers as primary and foreign keys.