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

Support more than one DB connection on different architectural layers #67

Open
serhii-chernenko opened this issue Nov 10, 2024 · 1 comment
Labels
enhancement New feature or request PR welcome

Comments

@serhii-chernenko
Copy link

serhii-chernenko commented Nov 10, 2024

I want to separate logic between Nuxt 3 Layers and different DB connections:

  • For ecommerce logic such as customers, orders, payments, etc.
  • And for CMS logic such as pages, blocks, etc.

I want to have a few DB connections. Is it possible? If not, can I expect this feature will be delivered soon?

I see it's possible for SQL databases for Nitro OOTB. I want to have the same for MongoDB:
https://nitro.build/guide/database#configuration

I also checked your repo and docs, I see you support more than one defineMongooseConnection:
https://nuxt-mongoose.nuxt.space/docs/api/utils#definemongooseconnection
But I suspect it doesn't support different models for different connections. I guess, all the used records will be just duplicated between the connections.

I expect separation by different connections and models dirs.

// nuxt.config.ts
export default defineNuxtConfig({
  mongoose: [
  {
    uri: 'process.env.MONGODB_CMS_URI',
    options: {},
    modelsDir: 'models/cms',
    devtools: true,
  },
  {
    uri: 'process.env.MONGODB_ECOMMERCE_URI',
    options: {},
    modelsDir: 'models/ecommerce',
    devtools: true,
  }
 ],
})

Usage:

  • page.scheme.ts in models/cms directory has to be automatically prefixed like CmsPage:
// layers/cms/server/api/pages/index.get.ts
export default defineEventHandler(async (event) => {
  try {
    return await CmsPage.find({})
  }
  catch (error) {
    return error
  }
})
  • user.scheme.ts in models/ecommerce directory has to be automatically prefixed like EcommerceUser:
// layers/ecommerce/server/api/users/index.get.ts
export default defineEventHandler(async (event) => {
  try {
    return await EcommerceUser.find({})
  }
  catch (error) {
    return error
  }
})

Just keep in mind that the folders have to be in different layers, just merged in result application, like

root                        # Project root directory
|
|—— layers                  # Custom Nuxt Layers for core functionalities
|   |—— core                # Core layer with core utilities and composables
|   |—— cms                 # Content Management System functionality
|   |—— ecommerce           # Ecommerce functionality
|   
|—— apps                    # Nuxt 3 applications for different platform parts
    |—— storefront          # Storefront application for public user interactions
    |—— admin               # Admin application for management

More about layers here:

Let me know please, if you don't plan to support the described feature, I'd try to implement it for my own project. But it'd be nice if you advise me on what to pay my attention.

It also could solve the previous Issues:

@serhii-chernenko serhii-chernenko changed the title Support more than one DB connection Support more than one DB connection on different architectural layers Nov 10, 2024
@arashsheyda
Copy link
Owner

this is great idea to have! but it definitely needs work to be done... I've been quite busy lately but I hope I can find some time in the next month to some issue tirage.

@arashsheyda arashsheyda added enhancement New feature or request PR welcome labels Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request PR welcome
Projects
None yet
Development

No branches or pull requests

2 participants