I decided to give Cloudflare's D1 a try
Published: 2024-03-22 19:12:27

Recently, I decided to give Cloudflare`s D1 database a try for a personal project. I was looking for a simple, fast, and reliable database that could be used to store and retrieve data. I was also looking for a database that could be easily integrated with my existing infrastructure. Cloudflare D1 seemed to be a good fit for my requirements. 


## What is Cloudflare D1 ?

Cloudflare D1 is a serverless database that is native to the Cloudflare Workers platform. It is build on SQLite and is designed to be fast, reliable, and easy to use. D1 is fully managed by Cloudflare and does not require any setup or maintenance. It is also highly available and scalable, making it suitable for production use cases.


## When to use D1 ? 

You should be using D1 if you are willing to use Cloudflare Workers at the backend of your application, or going fullstack deploying your app using Cloudflare Pages. 

I particularly was willing to go deep on Cloudflare, decided to use D1 for my project, and build my whole backend in Hono.js. For spicy up, I also decided to use Drizzle ORM to interact with the database. 

Earlier in time there was some issues in connecting to databases using Cloudflare Workers because of the issue with connections pooling. But now with D1, this isn't an issue anymore. You can easily connect to the database and perform CRUD operations without plugging in any third-party software such as Prisma Accelarate. 


## Why use D1 ?

D1 is **part of the Cloudflare ecossystem**, is fully integrated with the Workers platform and leverage the whole Cloudflare network to provide a fast and reliable database. 

It is **serverless**, meaning that you don't have to worry about managing the infrastructure or scaling the database, and compared to other serverless databases, such as Azure SQL, AWS Aurora, CockroachDB, and PlanetScale, it is **cheap**. It goes into the pay as you go model, and have a generous free tier. In the free tier alone, you are allowed to read 5,000,000 rows per day, write 100,000 rows per day, and store up to 5GB of data.  

Also, D1 is **easy to use**. It comes with a simple API that allows you to perform queries on the database, and now Drizzle ORM is also a viable option to use. You can use Drizzle to define models and generate migration files and use the Wrangler CLI to run and control the migrations. 

One thing that would be a great addition in the future is the ability to rollback migrations. Unfortunately, this feature is not available yet.

Another drawback is that outside access of the database is still limited. You have to use Cloudflare's dashboard to access the database, interact with the data, and run queries outside a Cloudflare Worker script. This is a limitation that can be a deal breaker for some use cases, but it is not a big deal for me.


## How to use D1 ?

For getting started with D1 you need to make sure that you have a Cloudflare account, and also that Node.js and npm are installed on your machine. You also need to have the Wrangler CLI installed. 

To create a new D1 database, you can use the Wrangler CLI to  login to your Cloudflare account, create a new workers project, and add the D1 database to the project. Once that is done you can start using the database by importing the D1 module in your worker script and performing queries on the database. To spicy things up you can also choose to power your Worker with Hono.js and use Drizzle ORM to interact with the database.


## Conclusion 

Cloudflare D1 is a great database for serverless applications that are built on the Cloudflare Workers platform. It is fast, reliable, and easy to use, and it is fully managed by Cloudflare. It is also highly available and scalable, making it suitable for production use cases.

It worked really well for me, as I was creating [a third-party platform built on top of Cloudflare Workers](https://flaremingo.com/) providing users with a user-friendly interface to create, manage, and deploy workers without the need for coding expertise. 

But the platform is still in beta, so there are some limitations that you should be aware of. For example, you can only access the database through the Cloudflare dashboard, and you cannot run queries outside a Cloudflare Worker script or the dashboard. 

In any case, I am happy with my experience with D1 and would recommend it to anyone who is looking for a serverless database. 

I would recommend D1 to anyone who is looking for a serverless database that is native to the Cloudflare Workers platform.