Backend API for the Educado application built with Express, TypeScript, and PostgreSQL.
- Node.js with Express
- TypeScript
- PostgreSQL database
- Sequelize ORM
- Node.js (v14 or higher)
- PostgreSQL database (local or hosted)
- Install dependencies:
npm install- Create a
.envfile based on.env.example:
cp .env.example .env- Update the
.envfile with your PostgreSQL connection string:
POSTGRES_URI=postgresql://username:password@localhost:5432/educado_dev
PORT=5001
Install PostgreSQL locally and create a database:
# On macOS with Homebrew
brew install postgresql
brew services start postgresql
# Create database
createdb educado_dev- Sign up for a PostgreSQL hosting service (e.g., Neon, Supabase)
- Create a new database
- Copy the connection string to your
.envfile
npm run devnpm run build
npm startsrc/
├── config/
│ └── database.ts # Sequelize database configuration
├── models/
│ ├── index.ts # Model exports and associations
│ └── user.model.ts # User model
├── routes/
│ └── user/ # User routes
├── types/
│ └── user-types/ # TypeScript types
└── index.ts # Application entry point
The application uses Sequelize ORM with PostgreSQL. On startup, it will:
- Test the database connection
- Sync all models (create tables if they don't exist)
Note: To reset the database, change syncDatabase(false) to syncDatabase(true) in src/index.ts. This will drop all tables and recreate them. Use with caution!
- Create a new model file in
src/models/(e.g.,post.model.ts) - Define the model using Sequelize:
import { DataTypes, Model } from 'sequelize'
import { sequelize } from '../config/database'
export class Post extends Model {
declare id: string
declare title: string
declare content: string
declare userId: string
declare createdAt: Date
declare updatedAt: Date
}
Post.init(
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
content: {
type: DataTypes.TEXT,
allowNull: false,
},
userId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'user',
key: 'id',
},
},
},
{
sequelize,
modelName: 'post',
tableName: 'post',
timestamps: true,
}
)- Add associations in
src/models/index.ts:
import { User } from './user.model'
import { Post } from './post.model'
// Define associations
User.hasMany(Post, { foreignKey: 'userId', as: 'posts' })
Post.belongsTo(User, { foreignKey: 'userId', as: 'user' })
export { User, Post }npm run dev- Start development server with hot reloadnpm run build- Build for productionnpm start- Start production servernpm run lint- Run ESLintnpm run lint:fix- Fix ESLint errors