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

Support adding prefix to an @id field #3391

Open
Albert-Gao opened this issue Dec 12, 2019 · 25 comments
Open

Support adding prefix to an @id field #3391

Albert-Gao opened this issue Dec 12, 2019 · 25 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/psl-wg topic: default topic: extend-client Extending the Prisma Client topic: schema

Comments

@Albert-Gao
Copy link

model User {
  id       String    @default(cuid()) @id
}

It works. But I want to generate an Id that is easier to refer like user_c2ajhsad8923jiesd, company_c3jkasdhjkhaskd

Something like this could be helpful, thanks :)

model User {
  id       String    @default("user_" + cuid()) @id
}
@Albert-Gao Albert-Gao changed the title Support adding prefix to id field Support adding prefix to an @id field Dec 12, 2019
@schickling
Copy link
Member

Oh I really like this idea! 😍

@schickling schickling transferred this issue from prisma/prisma Dec 12, 2019
@matthewmueller
Copy link
Contributor

matthewmueller commented Dec 12, 2019

This is similar to how Slack does IDs:

  • Users: U + uid(8)
  • Groups: G + uid(8)
  • Messages: M + uid(8)

I also like this approach because it allows you to better identify what type of data you're looking at. I'm hesitant to move more stuff prisma-level, but would very much like this as a database-level procedure we apply.

@Albert-Gao
Copy link
Author

Albert-Gao commented Dec 12, 2019

To manage nearly everything database related in Prisma (and that migration folder ) as a code-first approach, make it very easy to maintain. Which is a thing to love about the Entity framework from MS.

No matter how it is implemented in the end, as long as it is in the schema, all good :) And via reading the schema, you can get a sense of the ID pattern, which is even better

Thanks. Love Prisma.

@scriptcoded
Copy link

Hey!

Is this something that might be considered being added to Prisma? Would a shot at a PR help?

@baumant
Copy link

baumant commented Jun 17, 2021

has anyone been able to implement this?

@janpio
Copy link
Member

janpio commented Jun 17, 2021

You can probably use middlewares to do that, at least for top level create this should possible to match and then modify the string to include a prefix of your choice, e.g. decided via the model name.

@Zn4rK
Copy link

Zn4rK commented Aug 26, 2021

Doing it with middlewares has a few drawbacks:

  • You have to include cuid as a dependency.
  • You'll not be able to create entities in Prisma Studio.
  • You'll not have a unified place to put your prefixes (this one I can live with by just using triple slash comments).

@Vivalldi
Copy link

Vivalldi commented Jan 17, 2022

In an effort to keep the discussion going, I would like to consider how this looks from a schema standpoint a little further.

Syntax

Attributes like @relations() have shown that arguments are easy to use. Under the hood @default already has arguments.
As such, I would propose adding a prefix & suffix argument.

model User {
 id       String    @default(value:cuid(), prefix: "user_") @id
}

Drawbacks

One of the biggest drawbacks to this approach is prefix & suffix are very much String only modifiers. These arguments would not work with any other types. This could lead to excessive overloading as other types request their own @default customizations

Would love to see how others would like to see it implemented

@jsw
Copy link

jsw commented Jan 17, 2022

@Albert-Gao original suggestion seems more straightforward and flexible than an explicit prefix/suffix.

model User {
  id       String    @default("user_" + cuid()) @id
}

A similar variation borrowing from javascript interpolated strings could look like

mode User {
 id String @default(`user_${cuid()}`) @id

@JHIH-LEI
Copy link

JHIH-LEI commented May 8, 2022

We need this!

@codams
Copy link

codams commented May 9, 2022

Hi guys, this could be very useful for our team too 🔥

@Aymericr
Copy link

This topic is also discussed here #6719

@KevinEdry
Copy link

Would really help our team, also would like to see a composite string type to go along with it for a more strict typing:
type TableId = ``table_${string}``;

@smolak
Copy link

smolak commented Jul 9, 2022

@Albert-Gao - you might be interested in this: #6719 (comment)

@janpio janpio changed the title Support adding prefix to an @id field Support adding prefix to an @id field Nov 5, 2022
@millsp millsp added the topic: extend-client Extending the Prisma Client label Nov 15, 2022
@jgdkishan
Copy link

Has there been any development around this? I've been working on something that requires multiple cuids across tables and needed to prepend the value with a key name. Is the best option right now to just write a middleware?

@janpio
Copy link
Member

janpio commented Jan 25, 2023

Yes, Client middleware or Client extension (or handle on the database side somehow without Prisma involvement).

@SwapnilSoni1999
Copy link

So any updates on this? Is this implemented?

@SupremeDeity
Copy link

Still waiting for this to be implemented, i want to avoid using middleware for this and i am sadly not good enough to send a PR for this myself.

@nmfrankel
Copy link

Maybe this can be another option of how to make it:

@@index(["student-", id], map: "prefixedID")

@SupremeDeity
Copy link

Its been around 3 years or more since this was created. At this point i would probably want to create a PR of my own.

I have seen the other solutions and some of them seem like a lot of work. Instead the best way i can see from taking a quick look at the code is simply something like this:

id    String    @default(cuid("prefix_")) @id

TLDR: The rust implementation of CUID basically already prefixes a string to the start of the CUID:

static START_STR: &str = "c";

By adding an optional parameter to this function we can basically add a good fix to this problem and not have to mess with other unrelated functions and types.

I am currently not very familiar with the contribution process of this repository but if they take contributions i might bring up a commit in the near future.

@MartinHanewald
Copy link

I want this too!!

@rnbrady
Copy link

rnbrady commented Feb 5, 2024

For those looking, there's a pretty good solution available here.

@Melvynx
Copy link

Melvynx commented Apr 14, 2024

@rnbrady Your solution does not include the prefix. We need to define the extensions for each table. It's harder than the proposal.

@reinvertise

This comment was marked as outdated.

@ortonomy
Copy link

ortonomy commented May 3, 2024

My solution was is to use a prisma client extension with this package https://github.com/jetify-com/typeid-js to generate Type Ids (e.g. user_6aekggnvqc88jtbqydbg3cgvnc`). It's awkward because I have to fight the type checker which says these values are non nullable, but it's a small price to pay:

My ID schema for every prisma model is:

{model}Id       String  @unique @map("model_id")
{model}userUuid String  @unique @map("model_uuid")

and extension like:

const typeIdExtension = Prisma.defineExtension({
  name: 'typeId',
  query: {
    $allModels: {
      async create({ query, args, model }) {
        const dbModelName = model.toLowerCase();
        const idColumn = `${dbModelName}Id`;
        const uuidColumn = `${dbModelName}Uuid`;

        if (args.data[uuidColumn] === undefined || !args.data[uuidColumn]) {
          const uuid = uuidV4();
          args.data[uuidColumn] = uuid;
          args.data[idColumn] = TypeID.fromUUID(dbModelName, uuid).toString();
        }

        return query(args);
      },
    },
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/psl-wg topic: default topic: extend-client Extending the Prisma Client topic: schema
Projects
None yet
Development

No branches or pull requests