How to Use Fastify to Migrate MySQL to HarperDB in an App.

How to Use Fastify to Migrate MySQL to HarperDB in an App.


7 min read

Migrating data from one database to another can be a challenging task, especially when dealing with a large amount of data. In this tutorial, I will show you how to use Fastify to simplify the process and make it easier to migrate your MySQL database to HarperDB in an app.

You will learn how to migrate a web application written in React, MySQL, and Fastify, but using Fastify proxy, we will replace MySQL with HarperDB.

We'll make use of the following tools in this tutorial

1- HarperDB

2- Fastify

3- Node.js

4- MySQL


There are some conditions you should be aware of before proceeding. The tutorial assumes a basic understanding of

  • FastifyJS, MySQL, JavaScript, and TypeScript

  • How to use a package manager like npm or yarn

  • Must have a HarperDB account (Don't worry; it's free)


If you want to get to the source code or the demo faster, here are the links:

What is HarperDB

If you're tired of dealing with inflexible, hard-to-use databases, it's time to try HarperDB!

With its sleek, modern design and powerful feature set, HarperDB is the database management system that will take your data to the next level.

Unlike traditional databases, HarperDB is a NoSQL database, which means it can store and retrieve data in a variety of formats. This makes it perfect for applications that require real-time data processing, such as edge computing, IoT, analytics, and microservices.

But don't let its NoSQL roots fool you - HarperDB is also fully compatible with SQL, which means you can use familiar SQL queries to work with your data. And with its built-in data management and manipulation tools, HarperDB makes it easy to manipulate, analyze, and visualize your data in real-time.

In short, HarperDB is the database that has it all - power, flexibility, and ease of use.

What is Fastify:

Fastify is a popular web framework for Node.js that provides a simple, fast, and low-overhead way to create web applications.

It is built on top of Node.js and uses TypeScript, which makes it easy to develop and maintain.

Fastify is a web framework that will make your heart race! Built for Node.js, Fastify is a lean, mean, performance-focused machine that will help you build APIs that are both lightning-fast and rock solid.

With Fastify, you can build APIs that are a joy to use and a pleasure to maintain. Thanks to its schema-based approach, Fastify lets you define the shape of your data with precision, ensuring that your APIs are always predictable and reliable. And because Fastify is built for speed, you can be confident that your APIs will handle even the heaviest workloads with ease.

But Fastify isn't just about speed and reliability. It's also highly extensible, thanks to its powerful plugin system.

With Fastify, you can easily add new features and functionality to your APIs, allowing you to tailor them to your exact needs.

Fun fact: HarperDB’s Custom Functions feature is an integrated API server powered by Fastify, enabling you to build your entire application in one place.

What is React:

By using Fastify to migrate the data from MySQL to HarperDB, you can take advantage of the benefits of HarperDB without having to rebuild the entire app.

We have a ToDo app that was built with React as the front end, Fastify as an API, and MySQL as a Database.

This ToDo React app runs on localhost, it looks like this:

Project Setup

Once you have the prerequisites installed, follow these steps to migrate your MySQL database to HarperDB using Fastify:

Install the Fastify and HarperDB modules from npm:

We need to set up our local environment first. Make sure to use node v14.17.3 to avoid errors during installation.

npm install fastify

npm install harperdb

Visit the docs for further explanation and installation troubleshooting.

What are the React components in the existing app?

1- Task component (Tasks.jsx)

2- Task List (TaskList.jsx)

3- Task Search (TaskSearch.jsx)

I'll attach the code links for the three components here and explain what they do in the first app.

What we need to migrate

We will now replace the layer of the Fatsify API in our backend that was intricately connected to the MySQL DB with the HarperDB API.

With HarperDB, every single operation that you perform on a table is exposed as a super-convenient API! This means that you don't have to mess around with complex configuration files or install any additional packages - just use the API and let HarperDB handle the rest.

For example, if you want to insert a new record into a table, all you have to do is call the insert API and pass in the relevant data. HarperDB will take care of the rest, automatically handling the details of inserting the data into the table and ensuring that it is stored correctly.

And the best part is that this convenient API-based approach to table operations works for every operation, not just insertions. Whether you want to update, delete, or query data, HarperDB has got you covered with its easy-to-use APIs.

In short, HarperDB makes it simple and straightforward to perform any operation on your tables - just use the API and let HarperDB do the heavy lifting!

Because HarperDB exposes each table operation as an API, no additional configuration or package installation is required. We can use those APIs to access HarperDB over the cloud very easily.

Let's migrate the MySQL database to HarperDB.

How we created an API with HarperDB Custom Functions

Let's first create the schema.

Created table:

The "ToDoApi" API, which will resemble

Let's write our own API endpoints in order to:

  • create a task

  • edit a task

  • delete a task

  • get task

How We Integrated HarperDB in the App:

The process of migrating the data from MySQL to HarperDB using Fastify is simple and straightforward, and it can be easily integrated into the development process for the app.

Step-by-Step Process:

  1. First, install Fastify and the necessary plugins, such as the Fastify MySQL and Fastify HarperDB plugins.

  2. Next, create a new Fastify server and establish a connection to your MySQL database. Use the Fastify MySQL plugin to query the data from your ToDo app and extract it into a format that is compatible with HarperDB.

  3. Then, use Fastify to connect to your HarperDB database and create the necessary tables for your ToDo app. Use the Fastify HarperDB plugin to insert the migrated data into the appropriate tables in HarperDB.

  4. Once the data has been migrated, update your React ToDo app to use the HarperDB database for storing and retrieving data. You can use the Fastify HarperDB plugin to perform queries and manipulate the data in HarperDB from your React app.

  5. Finally, test your migrated React ToDo app to make sure that it is functioning correctly and that all of the data has been migrated successfully.

Overall, migrating a React ToDo app from MySQL to HarpeDB using Fastify is a simple and straightforward process.

We added HarperDB API endpoints and removed MySQL queries from the existing Fastify applications

There aren't many changes required on the frontend and backend and most APIs remain unchanged during migration. With the help of the Fastify proxy, we were able to manage this migration to HarperDB endpoints very smoothly.

More on HarperDB Custom Functions can be found in the documentation here.


Now that we've covered a lot, let me summarise this tutorial for you.

  1. With HarperDB's NoSQL roots, it can store and retrieve data in a variety of formats, making it perfect for applications that require real-time data processing, such as IoT, analytics, and microservices.

  2. Fastify's schema-based approach allows you to define the shape of your data with precision, ensuring that your APIs are always predictable and reliable.

  3. By migrating to HarperDB, you can improve the scalability and performance of your application, as it is built to handle heavy workloads with ease.

  4. Using Fastify to migrate your MySQL database to HarperDB can also reduce the overall maintenance costs of your application.

If you have any questions or suggestions for future tutorials, please leave them in the comments section. If you enjoyed this tutorial, please share it with others.