In a real-world system, data needs to be stored in a secure, automatically backed-up, and easily scalable location. Instead of manually installing a database on a virtual server, we will use Amazon RDS (Relational Database Service).
To interact with Amazon RDS from the Node.js source code (Backend), we will use Prisma ORM. Prisma makes defining table structures and querying data much safer and more intuitive compared to writing raw SQL queries.
ecommerce-ai-db.postgres.Note: In a real production environment, you should select No. We are enabling it here so your local machine can connect for the demo.
Once the RDS status shows as Available, you need to allow your personal computer to connect to it:
ecommerce-ai-db.ecommerce-ai-db.xxxxxx.rds.amazonaws.com).In your Backend source code folder, perform all the following steps to connect and set up the database:
Code:
# 1. Install Prisma and Client libraries
npm install prisma --save-dev
npm install @prisma/client
# 2. Initialize Prisma structure (creates .env file and prisma folder)
npx prisma init
# 3. Configure the environment variable in the .env file (replace with your Password and Endpoint)
# DATABASE_URL="postgresql://postgres:<Your_Password>@<Your_Endpoint>:5432/postgres?schema=public"
# 4. Define the content for the prisma/schema.prisma file: ( Example )
# generator client {
# provider = "prisma-client-js"
# }
# datasource db {
# provider = "postgresql"
# url = env("DATABASE_URL")
# }
# model Customer {
# id String @id @default(uuid())
# firstName String? @map("first_name")
# lastName String? @map("last_name")
# accountName String @map("accountName")
# mail String @unique
# phoneNumber String? @map("phone_number") @db.VarChar(10)
#
# passwordHash String? @map("password_hash") // Changed to Optional (?)
# googleId String? @unique @map("google_id") // Stores Google identifier ID
# authProvider String @default("LOCAL") @map("auth_provider") // Marker: LOCAL, GOOGLE
#
# refreshToken String? @map("refresh_token")
# avatarUrl String? @map("avatar_url")
# isActive Boolean @default(true) @map("is_active")
# gender String? @db.VarChar(10)
# birthday DateTime? @db.Date
# skinProfile Json? @map("skin_profile")
# tier Int?
# createdAt DateTime @default(now()) @map("created_at")
# addressId String? @map("address_id")
#
# resetTokens PasswordResetToken[]
# address Address? @relation(fields: [addressId], references: [id])
# carts Cart[]
# CouponUsage CouponUsage[]
#
# @@map("customer")
# }
# 5. Push the table structure to Amazon RDS
npx prisma db push