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

Link multiple projects to the same D1 database? #248

Open
howardah opened this issue Aug 28, 2024 · 9 comments
Open

Link multiple projects to the same D1 database? #248

howardah opened this issue Aug 28, 2024 · 9 comments
Labels
enhancement New feature or request

Comments

@howardah
Copy link

Hi, I'm just now exploring NuxtHub and am super impressed with all the work that you all have done.

I have one question for the moment however. I have two projects that I am building that will ultimately want to use the same database. One is a sort of admin app where users will edit information that will appear on the other app. For multiple reasons, that I'll not go into just here, I think it makes sense that the applications should be separated into two projects. So I'm wondering, if there's a way to specify the D1 database id so that the two applications can have access to the same data?

I tried creating a wrangler.toml file at the root of my project with the database name (per the wrangler configuration instructions) and that didn't seem to do the trick:

[[d1_databases]]
binding = "DB"
database_name = "[MY_DB_NAME]"
database_id = "[MY_BD_ID]"

Is this possible at the moment and/or are there any existing workarounds?

Thank you so much in advance!

Copy link
Contributor

atinux commented Aug 28, 2024

Thank you for your kind words!

This is a very good use case and right now this is not possible and I don't have a direct workaround but this pushed me to also work on #179 as well as this is related too.

I guess on top of supporting multiple databases like this:

export default defineNuxtConfig({
  hub: {
    database: {
      default: true,
      users: true
    }
  },
})

I could support adding an existing database with:

export default defineNuxtConfig({
  hub: {
    database: {
      default: 'your-d1-id'
    },
    // or even simplified
    database: 'your-d1-id'
  },
})

What do you think?

@howardah
Copy link
Author

I think that that would be a perfect solution! I'll keep an eye on this and have just subscribed to notifications from #179 to keep up with any updates there as well. Thank you again for your help and quick response!

@atinux atinux changed the title Question: Link multiple projects to the same D1 database? Link multiple projects to the same D1 database? Aug 30, 2024
@atinux atinux added the enhancement New feature or request label Aug 30, 2024
Copy link

Overall, it would be good if such functionality applied not only to the database, but also if different projects within one account could use the same R2 bucket or the same KV namespace.

And in the long-term perspective – to add S3 methods for interacting with the R2 bucket from other accounts.

@atinux
Copy link
Contributor

atinux commented Oct 21, 2024

Sorry for being so long about it @howardah

So far it is possible only using the CF dashboard once you deployed your second project:

  • Open the project on CF Pages
  • Go to the settings -> bindings
  • Update the DB binding to use the one from the first project

@sparrow-chik-chrk
Copy link

Sorry for being so long about it @howardah

So far it is possible only using the CF dashboard once you deployed your second project:

  • Open the project on CF Pages
  • Go to the settings -> bindings
  • Update the DB binding to use the one from the first project

@atinux Will a database also be needed in Nuxt Hub Admin, and will it be possible to edit it from there?

Copy link
Contributor

atinux commented Oct 21, 2024

A database: true will be needed at first, but right now not from the NuxtHub admin to edit the bindings sorry

@howardah
Copy link
Author

@atinux, amazing. Thank you so much, that's perfect! Shall I close this issue as completed?

Copy link
Contributor

atinux commented Oct 23, 2024

Let's keep it open :)

@bovium
Copy link

bovium commented Oct 26, 2024

I totally agree that this would be a great feature for several use cases.

@sparrow-chik-chrk It is Important to ensure that only one of the projects should own the migrations of the database. When you design your applications. It could end up being a very complicated database if both projects change the database schema.

@atinux I don't know if it could be possible to fail the deploy if there already exist another project using migrations against the same database id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants