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

feat: add support for extra bindings (Hyperdrive) #245

Merged
merged 6 commits into from
Aug 27, 2024
Merged

Conversation

atinux
Copy link
Contributor

@atinux atinux commented Aug 27, 2024

Resolves #208

We can specify extra bindings that will be added when deploying the project (without having to use a wrangler.toml)

So far I want to experience using bindings.hyperdrive before thinking of a hyperdrive key as the remote storage might be tricky to handle.

Example:

export default defineNuxtConfig({
  hub: {
    bindings: {
      hyperdrive: {
        // tip: use an environment variable here instead
        POSTGRES: '08f7bc805d1d409aac17e72af502abd0'
      }
    }
  }
})

Then in your API route:

import type { Hyperdrive } from '@cloudflare/workers-types'
import postgres from 'postgres'

export default eventHandler(async (event) => {
  // Check if hyperdrive is available (only on Cloudflare)
  const hyperdrive = process.env.POSTGRES as Hyperdrive | undefined
  // Fallback to local Postgres DB URL
  const dbURL = hyperdrive?.connectionString || process.env.NUXT_POSTGRES_URL

  const sql = postgres(dbURL, {
    ssl: import.meta.dev ? 'require' : false // hyperdrive should not use ssl for now
  })

  const products = await sql`SELECT * FROM products`

  event.waitUntil(sql.end())
  return products
})

I plan to have a top-level hub.hyperdrive property and support hubHyperdrive() but so far it seems that the platform proxy from wrangler does not support hyperdrive right now, so I will open another PR once this will be possible.

Copy link

pkg-pr-new bot commented Aug 27, 2024

commit: 65887f9

pnpm add https://pkg.pr.new/nuxt-hub/core/@nuxthub/core@245

Open in Stackblitz

Copy link

cloudflare-workers-and-pages bot commented Aug 27, 2024

Deploying nuxthub-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 65887f9
Status: ✅  Deploy successful!
Preview URL: https://1774ce49.nuxthub-module.pages.dev
Branch Preview URL: https://feat-hyperdrive.nuxthub-module.pages.dev

View logs

@atinux atinux merged commit 6e548e5 into main Aug 27, 2024
6 checks passed
@atinux atinux deleted the feat/hyperdrive branch August 27, 2024 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Cloudflare Hyperdrive connection in Nuxt-hub
1 participant