DEV Community

Cover image for Functional Programming with fp-ts in Node.js
Piyush Chauhan
Piyush Chauhan

Posted on

Functional Programming with fp-ts in Node.js

Introduction

Functional programming (FP) has gained popularity for its composability, testability, and robustness. In the JavaScript ecosystem, libraries like fp-ts bring powerful FP concepts to TypeScript, allowing you to write cleaner and more reliable code.

This article explores fp-ts concepts like Option, Either, Task, Reader, and ReaderTaskEither. We’ll build a basic CRUD app using fp-ts, pg (PostgreSQL client), and Express.js to see how these abstractions shine in real-world applications.


Key Concepts

Before diving into the app, let’s briefly discuss the main concepts:

  1. Option: Models the presence or absence of a value (Some or None).
  2. Either: Represents computations that can succeed (Right) or fail (Left).
  3. Task: Represents lazy asynchronous computations.
  4. Reader: Injects dependencies into computations.
  5. ReaderTaskEither: Combines Reader, Task, and Either for async operations with dependencies and error handling.

Setting Up the Project

Initialize the Project

mkdir fp-ts-crud && cd fp-ts-crud
npm init -y
npm install express pg fp-ts io-ts
npm install --save-dev typescript @types/express ts-node-dev jest @types/jest ts-jest
Enter fullscreen mode Exit fullscreen mode

Setup TypeScript

Create a tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"]
}
Enter fullscreen mode Exit fullscreen mode

Project Structure

src/
  index.ts        # Entry point
  db.ts           # Database setup
  models/         # Data models and validation
  services/       # Business logic
  controllers/    # CRUD operations
  utils/          # fp-ts utilities
  errors/         # Custom error classes
Enter fullscreen mode Exit fullscreen mode

Implementing the CRUD App

Database Setup (db.ts)

import { Pool } from 'pg';

export const pool = new Pool({
  user: 'postgres',
  host: 'localhost',
  database: 'fp_ts_crud',
  password: 'password',
  port: 5432,
});
Enter fullscreen mode Exit fullscreen mode

Define Models and Validation (models/User.ts)

import * as t from 'io-ts';
import { isRight } from 'fp-ts/Either';

export const User = t.type({
  id: t.number,
  name: t.string,
  email: t.string,
});

export const validateUser = (data: unknown): t.TypeOf<typeof User> | null => {
  const result = User.decode(data);
  return isRight(result) ? result.right : null;
};
Enter fullscreen mode Exit fullscreen mode

Custom Error Handling (errors/AppError.ts)

export class AppError extends Error {
  constructor(public statusCode: number, public code: string, public message: string) {
    super(message);
    this.name = 'AppError';
  }
}

export const createAppError = (statusCode: number, code: string, message: string): AppError => {
  return new AppError(statusCode, code, message);
};
Enter fullscreen mode Exit fullscreen mode

Service Layer (services/UserService.ts)

import { pool } from '../db';
import { ReaderTaskEither, right, left } from 'fp-ts/ReaderTaskEither';
import { pipe } from 'fp-ts/function';
import { createAppError, AppError } from '../errors/AppError';
import { validateUser } from '../models/User';

type Dependencies = { db: typeof pool };
type User = { name: string; email: string };

export const createUser = (
  user: User
): ReaderTaskEither<Dependencies, AppError, string> => (deps) => async () => {
  // Validate the incoming user data
  const validatedUser = validateUser(user);

  if (!validatedUser) {
    return left(createAppError(400, 'INVALID_USER_DATA', 'Invalid user data provided'));
  }

  try {
    const result = await deps.db.query(
      'INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id',
      [validatedUser.name, validatedUser.email]
    );
    return right(`User created with ID: ${result.rows[0].id}`);
  } catch (error) {
    return left(createAppError(500, 'USER_CREATION_FAILED', 'Failed to create user'));
  }
};

export const getUser = (
  id: number
): ReaderTaskEither<Dependencies, AppError, { id: number; name: string; email: string }> => (deps) => async () => {
  try {
    const result = await deps.db.query('SELECT * FROM users WHERE id = $1', [id]);
    return result.rows[0]
      ? right(result.rows[0])
      : left(createAppError(404, 'USER_NOT_FOUND', 'User not found'));
  } catch {
    return left(createAppError(500, 'USER_FETCH_FAILED', 'Failed to fetch user'));
  }
};
Enter fullscreen mode Exit fullscreen mode

CRUD Operations (controllers/UserController.ts)

import { pipe } from 'fp-ts/function';
import { createUser, getUser } from '../services/UserService';
import { pool } from '../db';
import { AppError } from '../errors/AppError';

const errorHandler = (err: unknown, res: express.Response): void => {
  if (err instanceof AppError) {
    res.status(err.statusCode).json({ error: { code: err.code, message: err.message } });
  } else {
    res.status(500).json({ error: { code: 'UNKNOWN_ERROR', message: 'An unexpected error occurred' } });
  }
};

export const createUserHandler = (req: express.Request, res: express.Response): void => {
  pipe(
    createUser(req.body),
    (task) => task({ db: pool }),
    (promise) =>
      promise.then((result) =>
        result._tag === 'Left'
          ? errorHandler(result.left, res)
          : res.json({ message: result.right })
      )
  );
};

export const getUserHandler = (req: express.Request, res: express.Response): void => {
  pipe(
    getUser(parseInt(req.params.id, 10)),
    (task) => task({ db: pool }),
    (promise) =>
      promise.then((result) =>
        result._tag === 'Left'
          ? errorHandler(result.left, res)
          : res.json(result.right)
      )
  );
};
Enter fullscreen mode Exit fullscreen mode

Express API (index.ts)

import express from 'express';
import { createUserHandler, getUserHandler } from './controllers/UserController';

const app = express();
app.use(express.json());

// Routes
app.post('/users', createUserHandler);
app.get('/users/:id', getUserHandler);

// Start Server
app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
Enter fullscreen mode Exit fullscreen mode

Running the App with Docker and Docker Compose

Dockerfile

# Stage 1: Build
FROM node:22 AS builder
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Run
FROM node:22
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm install --production
CMD ["node", "dist/index.js"]
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

version: '3.8'
services:
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
      POSTGRES_DB: fp_ts_crud
    ports:
      - "5432:5432"
    volumes:
      - db_data:/var/lib/postgresql/data
volumes:
  db_data:
Enter fullscreen mode Exit fullscreen mode

Run the App - Development Mode

# Start the database
docker-compose up -d

# Run the app
npx ts-node-dev src/index.ts
Enter fullscreen mode Exit fullscreen mode

Run the App - Production Mode

# Build the docker image
docker build -t fp-ts-crud-app .

# Start the database
docker-compose up -d

# Run the container
docker run -p 3000:3000 fp-ts-crud-app
Enter fullscreen mode Exit fullscreen mode

Writing Tests

Setup Jest

Update package.json scripts:

"scripts": {
  "test": "jest",
  "test:watch": "jest --watch"
}
Enter fullscreen mode Exit fullscreen mode

Example Test (__tests__/UserService.test.ts)

import { createUser, getUser } from '../services/UserService';
import { pool } from '../db';

jest.mock('../db', () => ({
  pool: {
    query: jest.fn(),
  },
}));

describe('UserService', () => {
  afterEach(() => {
    jest.clearAllMocks();
  });

  it('should create a user', async () => {
    (pool.query as jest.Mock).mockResolvedValueOnce({ rows: [{ id: 1 }] });

    const result = await createUser({ name: 'Alice', email: 'alice@example.com' })({ db: pool })();

    expect(result._tag).toBe('Right');
    if (result._tag === 'Right') {
      expect(result.right).toBe('User created with ID: 1');
    }
  });

  it('should return error if user not found', async () => {
    (pool.query as jest.Mock).mockResolvedValueOnce({ rows: [] });

    const result = await getUser(1)({ db: pool })();

    expect(result._tag).toBe('Left');
    if (result._tag === 'Left') {
      expect(result.left.message).toBe('User not found');
    }
  });

  it('should return error for invalid user data during creation', async () => {
    const invalidData = { name: 123, email: 'invalid-email' };
    const result = await createUser(invalidData)({ db: pool })();
    expect(result._tag).toBe('Left');

    if (result._tag === 'Left') {
      expect(result.left.message).toBe('Invalid user data provided');
    }
  });
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

By leveraging fp-ts, Docker, and robust error handling, we built a functional, scalable, and maintainable Node.js CRUD application. Using functional programming patterns makes your code more predictable and reliable, especially when handling asynchronous workflows.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay