graphback icon indicating copy to clipboard operation
graphback copied to clipboard

Support of multiple datasources

Open machi1990 opened this issue 4 years ago • 6 comments

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 avatar Sep 07 '20 21:09 machi1990

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

machi1990 avatar Sep 07 '20 21:09 machi1990

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

wtrocki avatar Sep 07 '20 22:09 wtrocki

Fully onboard with this. There was similar issue for the old runtime API - https://github.com/aerogear/graphback/issues/1025

craicoverflow avatar Sep 08 '20 07:09 craicoverflow

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?

craicoverflow avatar Oct 01 '20 16:10 craicoverflow

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 avatar Oct 01 '20 17:10 machi1990

Otherwise, we are fine documenting this option now.

machi1990 avatar Oct 01 '20 17:10 machi1990