https://github.com/mellowagain/honc-d1
refactoring test for d1 migrations with the honc stack
https://github.com/mellowagain/honc-d1
Last synced: about 1 year ago
JSON representation
refactoring test for d1 migrations with the honc stack
- Host: GitHub
- URL: https://github.com/mellowagain/honc-d1
- Owner: mellowagain
- Created: 2025-02-13T12:00:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-13T12:01:07.000Z (over 1 year ago)
- Last Synced: 2025-05-14T21:55:47.968Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## πͺΏ HONC
This is a project created with the `create-honc-app` template.
Learn more about the HONC stack on the [website](https://honc.dev) or the main [repo](https://github.com/fiberplane/create-honc-app).
### Getting started
[D1](https://developers.cloudflare.com/d1/) is Cloudflare's serverless SQL database. Running HONC with a D1 database involves two key steps: first, setting up the project locally, and second, deploying it in production. You can spin up your D1 database locally using Wrangler. If you're planning to deploy your application for production use, ensure that you have created a D1 instance in your Cloudflare account.
### Project structure
```#
βββ src
β βββ index.ts # Hono app entry point
β βββ db
β βββ schema.ts # Database schema
βββ .dev.vars.example # Example .dev.vars file
βββ .prod.vars.example # Example .prod.vars file
βββ seed.ts # Optional script to seed the db
βββ drizzle.config.ts # Drizzle configuration
βββ package.json
βββ tsconfig.json # TypeScript configuration
βββ wrangler.toml # Cloudflare Workers configuration
```
### Commands for local development
Run the migrations and (optionally) seed the database:
```sh
# this is a convenience script that runs db:touch, db:generate, db:migrate, and db:seed
npm run db:setup
```
Run the development server:
```sh
npm run dev
```
As you iterate on the database schema, you'll need to generate a new migration file and apply it like so:
```sh
npm run db:generate
npm run db:migrate
```
### Commands for deployment
Before deploying your worker to Cloudflare, ensure that you have a running D1 instance on Cloudflare to connect your worker to.
You can create a D1 instance by navigating to the `Workers & Pages` section and selecting `D1 SQL Database.`
Alternatively, you can create a D1 instance using the CLI:
```sh
npx wrangler d1 create
```
After creating the database, update the `wrangler.toml` file with the database id.
```toml
[[d1_databases]]
binding = "DB"
database_name = "honc-d1-database"
database_id = ""
migrations_dir = "drizzle/migrations"
```
Include the following information in a `.prod.vars` file:
```sh
CLOUDFLARE_D1_TOKEN="" # An API token with D1 edit permissions. You can create API tokens from your Cloudflare profile
CLOUDFLARE_ACCOUNT_ID="" # Find your Account id on the Workers & Pages overview (upper right)
CLOUDFLARE_DATABASE_ID="" # Find the database ID under workers & pages under D1 SQL Database and by selecting the created database
```
If you havenβt generated the latest migration files yet, run:
```shell
npm run db:generate
```
Afterwards, run the migration script for production:
```shell
npm run db:migrate:prod
```
Change the name of the project in `wrangler.toml` to something appropriate for your project:
```toml
name = "my-d1-project"
```
Finally, deploy your worker
```shell
npm run deploy
```