nuxt-hub / core

Build full-stack applications with Nuxt on CloudFlare, with zero configuration.
https://hub.nuxt.com
Apache License 2.0
800 stars 35 forks source link

Support multiple bindings (databases/kv/vectorize) #179

Open RihanArfan opened 1 week ago

RihanArfan commented 1 week ago

Is your feature request related to a problem? Please describe. Right now NuxtHub adds a single binding for whatever feature is enabled in the config with a hardcoded binding name.

Describe the solution you'd like It would be nice if an array could be specified alongside the boolean to allow multiple bindings, and access them by specifying them within the hubX() util.

export default defineNuxtConfig({
  hub: {
    kv: [
      {
        binding: "CONFIG",
        id: "abcdefg"
      },
      {
        binding: "ANOTHER",
        id: "foobar"
      }
    ],
    database: true
  },
})

const anotherKeys = await hubKV("ANOTHER").keys()
const keys = await hubKV().keys() // would use the default binding

Describe alternatives you've considered N/A

Additional context Cloudflare support multiple bindings and for more complex applications, several may be beneficial.

Atinux commented 1 week ago

Hey @RihanArfan

We indeed imagined the ability to switch between bindings but I did not want to make it too complex to start with, it's very rare the need for multiple databases / KV / R2 and also it may also complexify the admin.

But if we have to work on it, I would see it this way:

export default defineNuxtConfig({
  hub: {
    database: {
      // alias: BINDING
      default: 'DB', // always added
      users: 'USERS'
    }
  },
})

Then you can do:

const db =  hubDatabase() // default
const usersDb = hubDatabase('users')

We should not have to specify any ID as NuxtHub should take care of this for us.

nicogenz commented 1 week ago

Hey @Atinux,

Your proposal looks really good to me. I just wanted to add some notes because I think there are definitely use cases where it makes sense to have multiple D1 instances, for example. Since D1 has a maximum limit of 10 GB (see D1 Limit Documentation), it might be useful to split data into multiple databases. Another use case could be to use one database for each customer in an application.

Atinux commented 5 days ago

@nicogenz the main issue for having a use case as "one database for each user" is that you cannot add new database at runtime and you actually need to rebuild your website for the bindings to take effect.