Custom field name or rename field in model for NextAuth.js (Prisma adapter)

Hey! I'm not a fan of fields named id, but rather like to have speaking names like userId or postId, which makes the code easier to read imo. I'm using the Prisma adapter and added all needed models to my schema. In the User model, I would like to use a field name userId instead of id. Is this possible?
9 Replies
Samathingamajig
Samathingamajig•2y ago
are you looking to just change the name in the database (keeping id itself when you get an object) or to change it in both places?
bennettdev
bennettdevOP•2y ago
I don't really care how the field is named in the DB, I just want a different name in my code (via TypeScript). I guess having the same name in the model (TS) and the DB is better for consistency, but I have no strong opinion on that.
Samathingamajig
Samathingamajig•2y ago
https://github.com/nextauthjs/next-auth/blob/main/packages/adapter-prisma/src/index.ts the problem is they have id hardcoded in the adapter if you wanted this, you'd need to copy this file and modify it, then manually keep it up to date for completeness, note that it is standard for id to be the identifier of a row, and somethingId be a foreign key, but it's not a forced thing in general
bennettdev
bennettdevOP•2y ago
oh, not gonna happen, way to brittle for my taste 😂 thanks for your input okay, good to know. Still, having a speaking id is more important to me gonna create a feature request at NextAuth.js
Samathingamajig
Samathingamajig•2y ago
check if it exists first
bennettdev
bennettdevOP•2y ago
did this before posting here, haven't found anything
Samathingamajig
Samathingamajig•2y ago
ok
bennettdev
bennettdevOP•2y ago
@Samathingamajig Maybe this can be done with Prisma's @map functionality? Something like:
model User {
userId String @id @default(cuid()) @map("id")
model User {
userId String @id @default(cuid()) @map("id")
ah I guess the code in NextAuth.js' Prisma adapter is still not aware of the different field name....
Samathingamajig
Samathingamajig•2y ago
nameInDatabase Type @map("nameInJS")
nameInDatabase Type @map("nameInJS")
i was refering to @map when i said
just change the name in the database (keeping id itself when you get an object)
Want results from more Discord servers?
Add your server