๐Ÿ”— Connecting Node.js + Prisma + MySQL: PlanetScale vs TiDB Serverless (Free Alternative)

When building scalable backends with Node.js, using a MySQL-compatible managed database makes development easier, especially when paired with Prisma ORM.

In this guide, weโ€™ll:

  • โœ… Compare PlanetScale (paid) and TiDB Serverless (free)

  • โœ… Set up a MySQL database on TiDB

  • โœ… Connect it to your Node.js + Prisma app

  • โœ… Migrate your schema and test a basic query


๐ŸŒ Choosing the Right Database

๐Ÿฆ Option 1: PlanetScale (Best for Production, Paid Plans)

Pros:

  • Built on Vitess (same tech used by YouTube)

  • Branching, non-blocking schema changes

  • High-performance and scalable

Cons:

  • No longer offers free plans as of mid-2024

  • May require billing info and subscriptions for production use

๐Ÿ’ก Option 2: TiDB Serverless (Free & Scalable)

Pros:

  • 100% free tier with generous usage

  • Fully managed, auto-scaling

  • MySQL-compatible

  • Easy integration with Prisma

Cons:

  • Requires SSL for connections (but Prisma supports it)

๐Ÿš€ Project Setup: Node.js + Prisma + MySQL (TiDB)

We'll use TiDB Serverless with Prisma ORM in a Node.js project.


๐Ÿงฑ Step-by-Step Integration Guide

โœ… 1. Create a TiDB Account

  • Visit https://tidbcloud.com

  • Sign up using Google/GitHub/email

  • Create a Serverless cluster:

    • Choose region (us-west-2 is good)

    • Set database name (e.g., test)

    • Note down the connection string


โœ… 2. Create a Node.js Project

# 1. Create a new directory for the project
mkdir prisma-mysql-demo

# 2. Navigate into the project directory
cd prisma-mysql-demo

# 3. Initialize a new Node.js project with default settings
npm init -y  # Creates a package.json file with default values

# 4. Install Prisma CLI (for migrations etc.) and Prisma Client (to query the database)
npm install prisma @prisma/client
# prisma: development tool for migrations and schema handling
# @prisma/client: the runtime client used to interact with the DB

โœ… 3. Initialize Prisma

npx prisma init

This creates:

  • .env file (for DB URL)

  • prisma/schema.prisma (for schema definition)


โœ… 4. Update .env with Your TiDB MySQL Connection String

Replace your .env file with the following (example):

DATABASE_URL="mysql://USERNAME:PASSWORD@HOST:PORT/DATABASE?sslaccept=strict"

Example from TiDB:

DATABASE_URL="mysql://4Xq2uafiudouqfsftrGg.root:QxTQVigouwdfofqeTohxHKd@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test?sslaccept=strict"

โš ๏ธ Important: ?sslaccept=strict is required for TiDB secure connection


โœ… 5. Define Your Prisma Schema

Edit prisma/schema.prisma:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model Activity {
  id          Int      @id @default(autoincrement())
  title       String
  description String
  date        DateTime
  imageUrl    String
  createdAt   DateTime @default(now())
}

โœ… 6. Push Schema to TiDB

Run the following:

npx prisma generate
npx prisma migrate dev --name init

This will create the Activity table in your TiDB Serverless database.


โœ… 7. Create a Sample Server (server.js)

const express = require('express');
const { PrismaClient } = require('@prisma/client');
const app = express();
const prisma = new PrismaClient();

app.use(express.json());

app.post('/activity', async (req, res) => {
  const { title, description, date, imageUrl } = req.body;
  const activity = await prisma.activity.create({
    data: { title, description, date: new Date(date), imageUrl },
  });
  res.json(activity);
});

app.get('/activity', async (req, res) => {
  const activities = await prisma.activity.findMany();
  res.json(activities);
});

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

โœ… 8. Run Your Server

node server.js

Test your endpoints using Postman or cURL.


๐ŸŽฏ Summary

FeaturePlanetScaleTiDB Serverless
PricingPaidโœ… Free Tier
SSL RequiredOptionalโœ… Required
Prisma Supportโœ… Yesโœ… Yes
Schema Migrationsโœ… Supportedโœ… Supported

โœ… Final Thoughts

If you're building a startup MVP, portfolio project, or a college app, TiDB Serverless is a perfect, no-cost MySQL backend with Prisma.

Once your app grows, you can always migrate to PlanetScale or another production-grade provider.

0
Subscribe to my newsletter

Read articles from Prathamesh Pichkate directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prathamesh Pichkate
Prathamesh Pichkate

MERN STACK DEVELOPER