Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Support of multiple datasources #2011

Open
machi1990 opened this issue Sep 7, 2020 · 6 comments
Open

Support of multiple datasources #2011

machi1990 opened this issue Sep 7, 2020 · 6 comments
Labels
enhancement New feature or request Hacktoberfest

Comments

@machi1990
Copy link
Contributor

Is your feature request related to a problem? Please describe.

Being able to attach certain models in one database (e.g a Postgres schema, database), and other in a different database e.g mongodb database.

Describe the solution you'd like

  • The database should be identifiable by name when setting up the API
  • The same name can be used when declaring the business model in the GraphQL schema

Describe alternatives you've considered

To achieve this, users have to setup two separate or more APIs, one for each database.

@machi1990 machi1990 added enhancement New feature or request triage labels Sep 7, 2020
@machi1990
Copy link
Contributor Author

/cc @craicoverflow, @wtrocki
Automatically generated comment to notify maintainers

@wtrocki
Copy link
Contributor

wtrocki commented Sep 7, 2020

Totally possible now thru data providers. Based on model we can pick different data provider etc.

@craicoverflow
Copy link

Fully onboard with this. There was similar issue for the old runtime API - #1025

@craicoverflow
Copy link

This can be achieved simple enough from the user code:

import { KnexDBDataProvider } from '@graphback/runtime-knex';
import { MongoDBDataProvider } from '@graphback/runtime-mongo';

const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, {
  dataProviderCreator: (model: ModelDefinition) => {
    if (['Note', 'Comment'].includes(model.graphqlType.name)) {
      return new KnexDBDataProvider(model, Knex({...}));
    } else {
      return new MongoDBDataProvider(model, db);
    }
  }
});

Is there a need to create a helper for this when it is so easily achieved?

@machi1990
Copy link
Contributor Author

This can be achieved simple enough from the user code:

import { KnexDBDataProvider } from '@graphback/runtime-knex';
import { MongoDBDataProvider } from '@graphback/runtime-mongo';

const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, {
  dataProviderCreator: (model: ModelDefinition) => {
    if (['Note', 'Comment'].includes(model.graphqlType.name)) {
      return new KnexDBDataProvider(model, Knex({...}));
    } else {
      return new MongoDBDataProvider(model, db);
    }
  }
});

Is there a need to create a helper for this when it is so easily achieved?

Right, this should have been also mentioned as an alternative and viable solution. I was thinking more of a declarative way to achieving this.

i.e a db name is declared on model, and auto-magically the "helper" will handle which database provider to instantiate.

@machi1990
Copy link
Contributor Author

Otherwise, we are fine documenting this option now.

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

No branches or pull requests

3 participants