Logo

dev-resources.site

for different kinds of informations.

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

Published at
12/16/2024
Categories
webdev
nestjs
typescript
typeorm
Author
abhivyaktii
Categories
4 categories in total
webdev
open
nestjs
open
typescript
open
typeorm
open
Author
11 person written this
abhivyaktii
open
๐ŸŒŸ 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! ๐Ÿ‘‡

typeorm Article's
30 articles in total
Favicon
Creating Typescript app with decorator-based dependency injection ๐Ÿ’‰
Favicon
ORM and Migrating/Adding Data to MySql Database from MongoDb using TypeOrm in javaScript
Favicon
๐ŸŒŸ NestJS + Databases: Making Snake Case Seamless!๐Ÿ
Favicon
NestJS TypeORM and Multi-Tenancy
Favicon
Nx + TypeORM + NestJS + Migrations
Favicon
Handling TypeORM migrations in Electron apps
Favicon
How to manage multiple environments with dotenv and Databases config in NestJS
Favicon
Sveltekit + TypeScript + TypeORM + ESM
Favicon
Using TypeORM with TSX: A Smoother Development Experience
Favicon
Prisma or TypeORM ?
Favicon
Mock TypeORM Package
Favicon
Connecting a Serverless PostgreSQL Database (Neon) to NestJS Using the Config Service
Favicon
NestJS and TypeORMโ€Šโ€”โ€ŠEfficient Schema-Level Multi-Tenancy with Auto Generated Migrations: A DXย Approach
Favicon
Getting Started with NestJS and TypeORM: A Beginner's Guide
Favicon
Migration - Module query with TypeORM version 0.3.x
Favicon
Double bind the foreign key to avoid unnecessary JOIN in TypeORM
Favicon
Handling Migrations on NestJS with TypeORM
Favicon
match all conditions in the first array and at least one condition for the second array typeorm
Favicon
Taming cross-service database transactions in NestJS with AsyncLocalStorage
Favicon
Announcing Version 2.0 of nestjs-DbValidator
Favicon
Optimizing SQL Queries by 23x!!!
Favicon
Building my own PostgresGUI with TypeORM+TypeGraphQl class generaion
Favicon
TypeORM | Query Builder
Favicon
NestJs์—์„œ TypeORM์„ ์‚ฌ์šฉํ•˜์—ฌ MySQL ์—ฐ๋™, 2024-01-25
Favicon
Defining Custom Many-to-many Relationship in NestJS TypeORM.
Favicon
4. Building an Abstract Repository
Favicon
3. Building a Common Repository for Nest.js Microservices
Favicon
TypeORM - remove children with orphanedRowAction
Favicon
Migrating NestJS project with TypeORM to Prisma
Favicon
Authentication part 2 using NestJS

Featured ones: