Database & Models
Spin up your database in Docker, then describe the two things this app stores: users and products. Use the Postgres tab (TypeORM) or the MongoDB tab (Mongoose) — whichever you picked.
Postgres vs MongoDB
Relational tables (Postgres) or flexible documents (MongoDB). Either works fine for this app — pick one and follow its tab everywhere.
Set Up the Database
No messy local install — Postgres runs in a local Docker container, and MongoDB runs in the cloud via MongoDB Atlas.
Create a docker-compose.yml file in your database directory (or project root) to spin up the container. To keep sensitive credentials secure and out of version control, we use environment variables that reference a local .env file:
version: "3.8"
services:
db:
image: postgres:16
container_name: inv-pg
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:Run the compose file in detached mode:
$ docker compose up -dThen define your connection string in the backend's .env file:
# server/.env →
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
POSTGRES_DB=inventory
DATABASE_URL="postgresql://postgres:secret@localhost:5432/inventory"docker compose start / docker compose stop (or docker compose down) to manage your container. MongoDB Atlas is hosted in the cloud, so it is always online and doesn't require daily commands. In the final step, we'll expand our Docker Compose setup to run the entire backend and frontend stack together.The Data Model
Two tables/collections power everything. Keep them small — you can always add fields later.
isVerified blocks login until the email link is clicked. minStockLevel is the threshold that triggers a low-stock alert when quantity drops to or below it. createdBy links each product to the user who owns it, so users only see their own inventory.Connect & Define the Models
Pick your database tab. Each shows the connection plus both Express and NestJS wiring.
EntitySchema definitions, which require no decorator configuration or build pipeline.Express Project Structure
server/
├── src/
│ ├── config/
│ │ └── db.js # TypeORM DataSource instance (db connection)
│ ├── models/
│ │ ├── User.js # User database entity schema
│ │ └── Product.js # Product database entity schema
│ └── app.js # App entry file (initializes DataSource)
└── server.js # Server entry pointEntities
import { EntitySchema } from "typeorm";
export const User = new EntitySchema({
name: "User",
tableName: "user",
columns: {
id: { primary: true, type: "uuid", generated: "uuid" },
name: { type: "varchar" },
email: { type: "varchar", unique: true },
passwordHash: { type: "varchar" },
isVerified: { type: "boolean", default: false },
verificationToken: { type: "varchar", nullable: true },
verificationTokenExpiry: { type: "timestamp", nullable: true },
createdAt: { type: "timestamp", createDate: true },
},
});import { EntitySchema } from "typeorm";
export const Product = new EntitySchema({
name: "Product",
tableName: "product",
columns: {
id: { primary: true, type: "uuid", generated: "uuid" },
name: { type: "varchar" },
description: { type: "varchar", nullable: true, default: "" },
sku: { type: "varchar", unique: true },
category: { type: "varchar" },
price: { type: "float", default: 0 },
quantity: { type: "int", default: 0 },
minStockLevel: { type: "int", default: 10 },
createdBy: { type: "uuid" },
createdAt: { type: "timestamp", createDate: true },
updatedAt: { type: "timestamp", updateDate: true },
},
});Database Configuration
import { DataSource } from "typeorm";
import { User } from "../models/User.js";
import { Product } from "../models/Product.js";
export const AppDataSource = new DataSource({
type: "postgres",
url: process.env.DATABASE_URL,
entities: [User, Product],
synchronize: true, // auto-creates tables in dev — turn OFF in prod
});
// in app.js, before app.listen():
await AppDataSource.initialize();Confirm the Connection
Restart your backend. On boot you should see the connection log and (for Postgres) two tables created.
- ✓Database is running (Docker container for Postgres, cloud Atlas cluster for MongoDB)
- ✓Backend logs "connected" with no errors on start
- ✓
UserandProductmodels are defined and imported