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

Is it possible to customize the id field? #11

Closed
Bnaya opened this issue Dec 26, 2018 · 12 comments
Closed

Is it possible to customize the id field? #11

Bnaya opened this issue Dec 26, 2018 · 12 comments
Assignees

Comments

@Bnaya
Copy link

Bnaya commented Dec 26, 2018

The lib is looking for field named id automaticity,
my id name is different or sometimes i even don't have one/some times its composited

Thanks!

@michaelyali
Copy link
Member

I think it's possible. We already have params in CrudOptions object, where you can map id path param to an entity's field. But some adjustment in RepositoryService is needed in that case. I'll try to do this ASAP. Cheers!

@Bnaya
Copy link
Author

Bnaya commented Dec 27, 2018

Thank you!

@dhaspden
Copy link

dhaspden commented Jan 11, 2019

I took a look briefly at this since I was also interested in having this. I think it might be a bit more nuanced because a lot of places assume number type for the identifier field (CrudController and the decorators which setup parsing the parameter to a number.)

I was trying to think about how we could use the params in CrudOptions and figured it might be unintuitive to map them somehow to the service since you could potentially have more than one attribute in that params option. I wonder if maybe a new RestfulOptions, maybe like idColumnName would make more sense.

This leads me to another nuance which is handling the case where this id column isn't a number but instead something like a UUID. We would need a way to specify the type of that identifier field. I'm no expert on Typescript (yet) but perhaps that could be solved with yet another option, say idColumnType? I'm not sure if it'd work but you could also make the service a two value generic where T is the entity type and U is the type of the key?

Sorry about puking my thoughts out on the page but hopefully that gets through. I'd be really interested in seeing this happen since I like to have uuid columns for my public facing API to avoid having to disclose internal database ids.

Edit

If you set the params object to be

{
  id: 'uuid'
}

For example, would the library process that correctly? If so I guess we could technically parse the identifier something along the lines of

const idFieldName = params && params.id ? params.id : 'id';

D

@NotAmaan
Copy link

Valid point by @dhaspden
@nestjsx/crud assumes the id column to be a number in both the CrudController.getOneBase and RepositoryService.getOne

We rarely use the type number as an ID type of entities which are available to public to avoid making the total number of records in our database easy to discover.

@netremo
Copy link

netremo commented Feb 25, 2019

+1: id should be type string as option (UUID)

@niuba
Copy link

niuba commented Mar 8, 2019

+1: id should be type string as option (UUID)

@niuba
Copy link

niuba commented Mar 8, 2019

...createParamMetadata(RouteParamtypes.PARAM, 0, [setParseIntPipe()], 'id'),

change to createParamMetadata(RouteParamtypes.PARAM, 0, [], 'id'),

@michaelyali michaelyali mentioned this issue Mar 20, 2019
@michaelyali
Copy link
Member

Shipped with v3.0.0

@Bnaya
Copy link
Author

Bnaya commented Mar 28, 2019

Thank you very much

@Bnaya
Copy link
Author

Bnaya commented Mar 28, 2019

Ok,
I'm not sure how to use it XD
I want the name of the id field to be brand_id

@Bnaya
Copy link
Author

Bnaya commented Mar 28, 2019

ok, this just worked for me in the entity level:

  @PrimaryColumn({
    name: "bt_brand_id",
    length: 20,
    type: "varchar",
  })
  public id!: string;

But if there's a way on the that lib level, i think i will prefer it

@chriszrc
Copy link

I can now do just this:

@PrimaryGeneratedColumn({
        type:"integer",
        name:"population_id"
        })
    population_id:number;

and it's working-

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

7 participants