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
Comments
Oh I really like this idea! 😍 |
This is similar to how Slack does IDs:
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. |
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. |
Hey! Is this something that might be considered being added to Prisma? Would a shot at a PR help? |
has anyone been able to implement this? |
You can probably use middlewares to do that, at least for top level |
Doing it with middlewares has a few drawbacks:
|
In an effort to keep the discussion going, I would like to consider how this looks from a schema standpoint a little further. SyntaxAttributes like model User {
id String @default(value:cuid(), prefix: "user_") @id
} DrawbacksOne of the biggest drawbacks to this approach is prefix & suffix are very much Would love to see how others would like to see it implemented |
@Albert-Gao original suggestion seems more straightforward and flexible than an explicit prefix/suffix.
A similar variation borrowing from javascript interpolated strings could look like
|
We need this! |
Hi guys, this could be very useful for our team too 🔥 |
This topic is also discussed here #6719 |
Would really help our team, also would like to see a composite string type to go along with it for a more strict typing: |
@Albert-Gao - you might be interested in this: #6719 (comment) |
@id
field
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? |
Yes, Client middleware or Client extension (or handle on the database side somehow without Prisma involvement). |
So any updates on this? Is this implemented? |
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. |
Maybe this can be another option of how to make it: @@index(["student-", id], map: "prefixedID") |
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. |
I want this too!! |
For those looking, there's a pretty good solution available here. |
@rnbrady Your solution does not include the prefix. We need to define the extensions for each table. It's harder than the proposal. |
This comment was marked as outdated.
This comment was marked as outdated.
My solution was is to use a prisma client extension with this package https://github.com/jetify-com/typeid-js to generate My ID schema for every prisma model is:
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);
},
},
},
}); |
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 :)
The text was updated successfully, but these errors were encountered: