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
};
๐น 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")
}
โจ 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! ๐
Top comments (0)