DEV Community

Abhinav
Abhinav

Posted on

1

๐ŸŒŸ NestJS + Databases: Making Snake Case Seamless!๐Ÿ

When working with databases in NestJS, one common challenge is managing column naming conventionsโ€”specifically converting camelCase in your code to snake_case in the database. Luckily, the solution doesnโ€™t need to be custom code in most cases! ๐Ÿš€

Hereโ€™s how to handle it effectively:

๐Ÿ”น TypeORM: Use a NamingStrategy, like the popular typeorm-naming-strategies package. It automates the conversion of column and table names to snake_case.

import { SnakeNamingStrategy } from 'typeorm-naming-strategies';

const config = {
  namingStrategy: new SnakeNamingStrategy(),
  // Other TypeORM configs
};
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Prisma: Simply use the @map directive in your schema to map camelCase fields to snake_case database columns.

model User {
  firstName String @map("first_name")
}
Enter fullscreen mode Exit fullscreen mode

โœจ Pro Tip: While you could build a custom NestJS pipe for this transformation, it's better to rely on your ORM for these mappingsโ€”itโ€™s cleaner and more efficient. Let the ORM do the heavy lifting for database conventions! ๐Ÿ› ๏ธ

Have you faced similar challenges while working with ORMs in NestJS? Let me know how you handled them! ๐Ÿ‘‡

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Cloudinary image

Zoom pan, gen fill, restore, overlay, upscale, crop, resize...

Chain advanced transformations through a set of image and video APIs while optimizing assets by 90%.

Explore

๐Ÿ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay