Skip to content
This repository has been archived by the owner on Dec 2, 2017. It is now read-only.

Creating a connector

Sergio Xalambrí edited this page Jul 25, 2017 · 2 revisions

The connectors are just async functions which receives all the environment variables and return a new data source connection. Your connection can be whatever it help your interact with data.

async function connector(env) {
  return {
    hello() {
      return 'hello world!'
    }
  }
}

module.exports = connector;

That's a really simple connector, as you can see it receive the environment variables as an argument and it return a object. Then you can use that object inside your models to get that piece of data.

Your connectors can be generic or specific to some server. Eg. you can have a Sequelize connector for any kind of relational database or your can have a PostgreSQL specific connector.

Using multi instances of a single connector

You may want to use the same connector multiple times, maybe you have 2 MongoDB databases or you want to fetch data from many REST APIs. If that's your case you can create a custom connector who work as a proxy for the Grial provided connectors.

This proxy connector receive your own custom environment variables and pass them as the required to the real connector.

exports.pokeapi = function({ POKEAPI_ENDPOINT }) {
  return require('@grial/connector-rest')({ REST_ENDPOINT: POKEAPI_ENDPOINT })
}

exports.swapi = function({ SWAPI_ENDPOINT }) {
  return require('@grial/connector-rest')({ REST_ENDPOINT: SWAPI_ENDPOINT })
}

That way the real connector receive the required variable but you can use a custom one with a any name.

Clone this wiki locally