Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow mapping uuid to id field #807

Open
zetti-caletti opened this issue Oct 12, 2022 · 0 comments
Open

allow mapping uuid to id field #807

zetti-caletti opened this issue Oct 12, 2022 · 0 comments

Comments

@zetti-caletti
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant