DEV Community

Abhinav
Abhinav

Posted on

2

🌟 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 image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs