Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’3 min read
πŸ”— Connecting Node.js + Prisma + MySQL: PlanetScale vs TiDB Serverless (Free Alternative)
P

MERN STACK DEVELOPER

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.