= How to develop a Custom Connection Factory

If you have developed a custom translator to a source that is not already defined in the Teiid or trying to write a translator to data source, to make translator work, you need a Connection to that source. Teiid library requires that Connection be provided in a particular interface. Below shows an example on how build Connection and ConnectionFacory that can be used with the corresponding Translator.

* Define a connection interface to your Data Source, this can be anything. For example, to a MongoDB this can be `DB` class. This is the go between class that Translator understands to communicate with the souce

[source, java]
----
public interface MyConnection {
    void executeTask();
}
----

* Using the above interface to access the data source, define a concrete class that extends `org.teiid.spring.data.BaseConnection`, to define the connection class.

[source, java]
----
public class CustomConnection extends BaseConnection implements MyConnection {

    @Override
    public void close() throws Exception {
        // release any resources.
    }

    @Override
    public void executeTask() {
        // take some action with with source
    }
} 
----

* Using the above connection class, provide a factory class that can be used to create many connections. This class must extend `org.teiid.spring.data.BaseConnectionFactory` class.

[source, java]
----
public class CustomConnectionFactory extends BaseConnectionFactory<CustomConnection> {
    @Override
    public CustomConnection getConnection() throws Exception {
        return new CustomConnection();
    }
}
----

In this factory class if you need to pass in configuration like URL, username and password define those properties and initialize the class with those properties. If Spring Boot already provides templates you can @AutoWire those object in here. For ex: see MongoDBConnectionFactory as an example.

* The connection classes are now complete. Follow below to create instance of the data source connection factory and make use of them in your application.

[source,java]
----
@Configuration
public class DataSources {
    @Bean
    public CustomConnectionFactory accounts() {
        return new CustomConnectionFactory();
    }
}
----

This creates a bean with name `accounts` in the application, which can be referenced via a SERVER in ddl:

----
CREATE SERVER accounts FOREIGN DATA WRAPPER customTranslator;
----

see link:CustomTranslator.adoc[Custom Translator] on how write and deploy a custom translator.


=== House Keeping Tasks

When a new data source is added, please follow below tasks to make it functional across other projects

* Add example project under "samples" directory show casing how the data source is configured, if possible make it as executable test
* Write a Readme document with example 
* Add a .mustache file in the data source project to auto-generate the Datasource file when using `vdb-codegen-plugin` by other projects.
* Add a test to test .mustache generation in the `vdb-codegen-test` project under `DataSourceGenerationTest.java` class.
* Add pom dependency in the `spring-data-util` project so that the configuration about data source is shared with Operator and VSCode plugin automatically
* if possible add an example in the Teiid Operator code in the form of a YAML
* Test using it on the OpenShift using the YAML created
 
