Build a Staff Directory Application
In this tutorial, you will learn how to use D1 to build a staff directory. This application will allow users to access information about an organization's employees and give admins the ability to add new employees directly within the app. To do this, you will first need to set up a D1 database to manage data seamlessly, then you will develop and deploy your application using the HonoX Framework ↗ and Cloudflare Pages.
Before moving forward with this tutorial, make sure you have the following:
- A Cloudflare account, if you do not have one, sign up ↗ before continuing.
- A recent version of npm ↗ installed.
If you do not want to go through with the setup now, view the completed code ↗ on GitHub.
In this tutorial, you will use HonoX ↗, a meta-framework for creating full-stack websites and Web APIs to build your application. To use HonoX in your project, run the hono-create
command.
To get started, run the following command:
During the setup process, you will be asked to provide a name for your project directory and to choose a template. When making your selection, choose the x-basic
template.
Once your project is set up, you can see a list of generated files as below. This is a typical project structure for a HonoX application:
The project includes directories for app code, routes, and server setup, alongside configuration files for package management, TypeScript, and Vite.
To create a database for your project, use the Cloudflare CLI tool, Wrangler, which supports the wrangler d1
command for D1 database operations. Create a new database named staff-directory
with the following command:
After creating your database, you will need to set up a binding in the Wrangler configuration file to integrate your database with your application.
This binding enables your application to interact with Cloudflare resources such as D1 databases, KV namespaces, and R2 buckets. To configure this, create a wrangler.toml
file in your project's root directory and input the basic setup information:
Next, add the database binding details to your wrangler.toml
file. This involves specifying a binding name (in this case, DB
), which will be used to reference the database within your application, along with the database_name
and database_id
provided when you created the database:
You have now configured your application to access and interact with your D1 database, either through the command line or directly within your codebase.
You will also need to make adjustments to your Vite config file in vite.config.js
. Add the following config settings to ensure that Vite is properly set up to work with Cloudflare bindings in local environment:
To interact with your D1 database, you can directly issue SQL commands using the wrangler d1 execute
command:
The command above allows you to run queries or operations directly from the command line.
For operations such as initial data seeding or batch processing, you can pass a SQL file with your commands. To do this, create a schema.sql
file in the root directory of your project and insert your SQL queries into this file:
The above queries will create three tables: Locations
, Departments
, and Employees
. To populate these tables with initial data, use the INSERT INTO
command. After preparing your schema file with these commands, you can apply it to the D1 database. Do this by using the --file
flag to specify the schema file for execution:
To execute the schema locally and seed data into your local directory, pass the --local
flag to the above command.
After setting up your D1 database and configuring the wrangler.toml
file as outlined in previous steps, your database is accessible in your code through the DB
binding. This allows you to directly interact with the database by preparing and executing SQL statements. In the following step, you will learn how to use this binding to perform common database operations such as retrieving data and inserting new records.
For a complete list of all the queries used in the application, refer to the db.ts ↗ file in the codebase.
The application uses hono/jsx
for rendering. You can set up a Renderer in app/routes/_renderer.tsx
using the JSX-rendered middleware, serving as the entry point for your application:
Add the bindings defined earlier in global.d.ts
file where the global type definitions for TypeScript is defined ensuring type consistency across your application:
This application uses Tailwind CSS ↗ for styling. To use Tailwind CSS, refer to the TailwindCSS documentation ↗, or follow the steps provided on GitHub ↗.
To display a list of employees, invoke the findAllEmployees
function from your db.ts
file and call that within the routes/index.tsx
file. The createRoute()
function present in the file serves as a helper function for defining routes that handle different HTTP methods like GET
, POST
, PUT
, or DELETE
.
The existing code within the file includes a placeholder that uses the Counter component. You should replace this section with the following code block:
The code snippet demonstrates how to import the findAllEmployees
, findAllLocations
, and findAllDepartments
functions from the db.ts
file, and how to use the binding c.env.DB
to invoke these functions. With these, you can retrieve and display the fetched data on the page.
Use the export POST
route to create a new employee through the /admin
page:
During the process of creating a new employee, the image uploaded can be stored in an R2 bucket prior to being added to the database.
To store an image in an R2 bucket:
- Create an R2 bucket.
- Upload the image to this bucket.
- Obtain a public URL for the image from the bucket. This URL is then saved in your database, linking to the image stored in the R2 bucket.
Use the wrangler r2 bucket create
command to create a bucket:
Once the bucket is created, add the R2 bucket binding to your wrangler.toml
file:
Pass the R2 binding to the global.d.ts
file:
To store the uploaded image in the R2 bucket, you can use the put()
method provided by R2. This method allows you to upload the image file to your bucket:
Refer to GitHub ↗ for the full codebase.
With your application ready for deployment, you can use Wrangler to build and deploy your project to the Cloudflare Network. Ensure you are logged in to your Cloudflare account by running the wrangler whoami
command. If you are not logged in, Wrangler will prompt you to login by creating an API key that you can use to make authenticated requests automatically from your computer.
After successful login, confirm that your wrangler.toml
file is configured similarly to the code block below:
Run wrangler deploy
to deploy your project to Cloudflare. After deployment you can test your application is working by accessing the deployed URL provided for you. Your browser should display your application with the base frontend you created. If you do not have any data populated in your database, go to the /admin
page to add a new employee, and this should return a new employee in your home page.
In this tutorial, you built a staff directory application where users can view all employees within an organization. Refer to the Staff directory repository ↗ for the full source code.