core
core copied to clipboard
Link multiple projects to the same D1 database?
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!
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?
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!
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.
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
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?
A database: true will be needed at first, but right now not from the NuxtHub admin to edit the bindings sorry
@atinux, amazing. Thank you so much, that's perfect! Shall I close this issue as completed?
Let's keep it open :)
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.
Just saw this issue. So right now only one database is supported and the name can not be changed?
If I have a separate Worker, should I do the setup there to use the database from the Nuxt Hub project?
Just saw this issue. So right now only one database is supported and the name can not be changed?
If I have a separate Worker, should I do the setup there to use the database from the Nuxt Hub project?
If you have a separate Workers project which isn't on NuxtHub, you'll be able to bind the same NuxtHub D1 database to it.