Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local D1 DB persists to wrong directory not picked up by vite #76

Closed
KaiSpencer opened this issue Feb 20, 2024 · 6 comments
Closed

Local D1 DB persists to wrong directory not picked up by vite #76

KaiSpencer opened this issue Feb 20, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@KaiSpencer
Copy link

What version of HonoX are you using?

0.1.3

What steps can reproduce the bug?

Clone the examples repository https://github.com/yusukebe/honox-examples/tree/main/projects/blog
cd to projects/blog
run npx wrangler d1 execute hono-blog-demo --local --file=./blog.sql
run yarn dev

What is the expected behavior?

Local DB is created and when you launch the app it works with seeded db from blog.sql

What do you see instead?

D1_ERROR: no such table: articles

Additional information

the local db is being created in .wrangler, the vite dev plugin is looking in .mf

If you run build and then wrangler pages dev ./dist this works, as the wrangler command shims the database in .wrangler and is available to honox

It seems there needs to be something tweaked with the miniflare setup to pickup local d1 correctly

@KaiSpencer KaiSpencer added the bug Something isn't working label Feb 20, 2024
@shimahi
Copy link

shimahi commented Feb 20, 2024

Hi.

If you write d1Databases and d1Persist entries in Vite config as the sample repo, the DB schema at startup is made at ./.mf/d1/{D1_Binding_Name}/db.sqlite

// vite.config.ts

import pages from '@hono/vite-cloudflare-pages'
import pagesPlugin from '@hono/vite-dev-server/cloudflare-pages'
import honox from 'honox/vite'
import client from 'honox/vite/client'
import { defineConfig } from 'vite'

export default defineConfig(({ mode }) => {
  if (mode === 'client') {
    return {
      plugins: [client()],
    }
  }
  return {
    plugins: [
      honox({
        devServer: {
          plugins: [
            pagesPlugin({
              d1Databases: ['DB'],
              d1Persist: true,
            }),
          ],
        },
      }),
      pages(),
    ],
  }
})

Optimizations for these migrate, seed, and uses vary as they depend on the query builder everyone uses, but it would be nice if this specification is clearly stated in the README or in a future Doc.

@KaiSpencer
Copy link
Author

I do not think it is unreasonable to expect the wrangler commands to manipulate D1 to work with honox, especially given the deploy command is using the wrangler CLI too.

@bruceharrison1984
Copy link
Contributor

It works fine you just need to configure HonoX correctly. I am currently using it within a project. d1persist can also be a string to configure a custom location.

You just have to change the directories to match wherever you are pointing wrangler. In my case, I put .wrangler in the root of a mono-repo.

I agree that more documentation is needed around this.

const serversidePlugins = [
  tsconfigPaths(),
  honox({
    devServer: {
      cf: {
        d1Databases: { TORCH_DATABASE: 'TORCH_DATABASE' },
        d1Persist: '../../.wrangler/state/v3/d1',
        r2Buckets: ['TORCH_R2'],
        r2Persist: '../../.wrangler/state/v3/r2',
        kvNamespaces: ['TORCH_AUTH'],
        kvPersist: '../../.wrangler/state/v3/kv',
      },
    },
  }),
  pages(),
];

@KaiSpencer
Copy link
Author

Thanks for your patience guys!

I have managed to get this working with using wrangler d1 execute with the following configuration

Key changes:
d1Databases - explicitly state ID of database as per wrangler.toml
d1Persist - set to directory as wrangler d1 execute runs

    honox({
      devServer: {
        plugins: [
          pagesPlugin({
            d1Databases: { DB: "c1ec051a-3e19-4c89-92fa-bdc2b3f69e1c" },
            d1Persist: "./.wrangler/state/v3/d1",
          }),
        ],
      },
    }),

@yusukebe
Copy link
Member

Hi @KaiSpencer

You can use the new API in Wranlger to make it more easy. See here: #39 (comment)

@KaiSpencer
Copy link
Author

Hi @KaiSpencer

You can use the new API in Wranlger to make it more easy. See here: #39 (comment)

Thank you so much @yusukebe

Works perfectly, much simpler too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants