# Building a database connector

## Introduction

Building a database connector with the Connector Builder differs slightly from building standard connectors - instead of defining API *endpoints*, we define database *queries*. As such, you'll notice some UI variations from our standard [Connector Builder guidelines](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector).

## Supported database types

Currently, the following database types are supported:

* [MySQL](https://dev.mysql.com/)
* [PostgreSQL](https://www.postgresql.org/)&#x20;
* [Microsoft SQL](https://learn.microsoft.com/en-us/sql/t-sql/queries/queries?view=sql-server-ver16)

## Security

* Database connectors are authenticated with `username` and `password` over SSL (if available, otherwise with no SSL).&#x20;
* We recommend ensuring that the accounts used to authenticate instances of this connector are associated with 'least privilege' permissions (so if you only need to read data, ensure that read-only permissions are applied).
* Please ensure that any Patchworks variables used in queries are escaped, where appropriate - variable values are substituted at run time as-is; they are not interpreted, sanitised, or escaped automatically. For more information please see [Working with queries](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/working-with-queries#variable-placement-and-escaping).

## Database connector setup

Once you've chosen to [add a new connector and launched the Connector Builder](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/accessing-the-connector-builder), a *basic details* form is displayed. For a database connector, you must set the `system type` field to `database`. This prompts a `database type` field below - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FHnf5CH1Iee736Ov5yVNz%2Fdb%20connector%201.png?alt=media&#x26;token=11409515-8b9f-42d1-a7f6-659726357a18" alt="" width="375"><figcaption></figcaption></figure></div>

When you go on to choose a `database type` and save this page, you'll step through a setup wizard to complete your connector setup:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F7RLxzB0m4fwBhZ4geYkP%2Fdb%20connector%20-%20queries%20-%2012.png?alt=media&#x26;token=720beb3c-f15e-49de-ba45-0bdf84ba8e45" alt="" width="375"><figcaption></figcaption></figure></div>

This is very similar to the [endpoint setup wizard](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/4-endpoints/adding-a-new-endpoint), but there are some notable differences. These are detailed below:

* [Settings](#basic-settings)
* [Variables](#variables)
* [Authentication](#authentication)
* [Queries](#queries)

### Settings

As noted above, to build a database connector you must choose to set the `system type` field to `database`. This prompts a `database type` field:&#x20;

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th><th data-type="content-ref"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><p>As noted above, to build a database connector, you must set the <code>system type</code> field to <code>database</code>. This prompts a <code>database type</code> field below - for example:<br></p><p><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FHnf5CH1Iee736Ov5yVNz%2Fdb%20connector%201.png?alt=media&#x26;token=11409515-8b9f-42d1-a7f6-659726357a18" alt="" data-size="original"></p></td><td></td><td></td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FafB7k12bogWMwmBrjGLu%2F1%20small%20blue.png?alt=media&#x26;token=6f5aa9a1-73c6-42b7-9fc3-24799a803987">1 small blue.png</a></td></tr><tr><td>Select your required database type from the dropdown field:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FzZahwr4sj7GhSA5Q2Kci%2Fdb%20connector%203.png?alt=media&#x26;token=10cac5f1-cf13-46ea-9668-75d87fff171a" alt=""></td><td>Complete other settings <a href="1-basic-details">as normal</a> and save your changes. As soon as you save this page, the database connector setup wizard is launched.</td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F7UUvGwieWZkxCy8RmLvM%2F2%20small%20blue.png?alt=media&#x26;token=0dca41a6-b86b-4186-bf7e-70505983f437">2 small blue.png</a></td></tr></tbody></table>

{% hint style="info" %}
For more information on the *basic details* page please see our [general Connector Builder section](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/1-basic-details).
{% endhint %}

### Variables

Variables are added and used [as normal](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/2-connector-variables) - i.e. any variables defined here are available when you move on to set up [authentication](#authentication) for your connector:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FH0DkyDn8QyQjofC8kbnd%2Fdb%20connector%204.png?alt=media&#x26;token=77aa9bb8-f7b1-4f56-8ee7-2d7731ba2699" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
For more information on the *variables* page please see our [general Connector Builder section](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/2-connector-variables).
{% endhint %}

### Authentication

An authentication method must be defined [as normal](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/3-authentication-methods) however, for a database connector the [DB user pass](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/3-authentication-methods/supported-authentication-types/db-user-pass-authentication) authentication method should be used:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FqPmPQadY7DIJsYTqpXyx%2Fdb%20connector%205.png?alt=media&#x26;token=f8dde0b2-3a2a-489f-a6aa-66b12a1d419a" alt=""><figcaption></figcaption></figure>

Having confirmed this selection, the default setup is displayed:

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F7LRJQl91vLdXYIcRPgXH%2Fdb%20auth%20method.png?alt=media&#x26;token=88f61db2-2570-4883-8690-06c957ae870f" alt=""><figcaption></figcaption></figure>

Values for all required authentication variables shown here must be provided when a user adds an instance of this connector. With the default (recommended) setup this means that the following values are expected:

* username
* password
* port
* host
* database name

Unless you have a specific reason for changing this setup, you can just save this tab and continue.

{% hint style="info" %}
Details for this authentication method are available on our [DB user pass](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/3-authentication-methods/supported-authentication-types/db-user-pass-authentication) page. For more information on the *authentication* page please see our [general Connector Builder section](https://doc.wearepatchworks.com/product-documentation/release-information/core-release-notes/2023-12-21-release-notes-core).&#x20;
{% endhint %}

### Queries

The queries tab is only displayed when you add a `database` type connector. This is where you define queries that can be used in process flows (via the [connector shape](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape)):

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FssuQ9R73uwNPF0VAUfaZ%2Fdb%20connector%20-%20queries%20-%201.png?alt=media&#x26;token=87b5e18e-d5e0-402a-9166-946a19acbb71" alt=""><figcaption></figcaption></figure>

The process of adding a *query* is very similar to that of adding [endpoints](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/4-endpoints) - having chosen to `add a new system query`, you step through a series of pages to provide a name and a direction; define any required variables, add the query itself, and set pagination options.&#x20;

If you're familiar with adding *endpoints*, you will recognise some of these options however, the steps below show specific setup requirements for *queries*.

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th><th data-type="content-ref"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td>Add a <code>name</code> for this query and choose the type of <code>entity</code> that you're querying:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FhRrGGZTvjroT9YqmB95w%2Fdb%20connector%20-%20queries%20-%203.png?alt=media&#x26;token=79bf5ed0-51d2-49e1-b32a-069335d3e02b" alt=""></td><td><p>The <code>name</code> entered here is displayed to users when they configure a <a href="../../../process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape">connector shape</a> to use this connector and are looking for a query to apply. Click the <code>create</code> button to confirm and access query configuration options.</p><p></p><p>For general information about these settings, please see our main <a href="4-endpoints/adding-a-new-endpoint">Connector Builder section</a> - the same principles apply to both <em>endpoints</em> and <em>queries</em>. </p></td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FUT4wuaGv11XQum3dxtn9%2F1%20small%20blue.png?alt=media&#x26;token=14d5190e-8e3d-404b-bbc3-7f18a0cfc948">1 small blue.png</a></td></tr><tr><td>General details are added in the top panel, with query configuration in the tabs below:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fc8HyxhSXFMmWw9Xm53n0%2Fdb%20connector%20-%20queries%20-%204.png?alt=media&#x26;token=94e157e3-0c32-4b11-be23-fa81ebab4a08" alt=""></td><td></td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F7UUvGwieWZkxCy8RmLvM%2F2%20small%20blue.png?alt=media&#x26;token=0dca41a6-b86b-4186-bf7e-70505983f437">2 small blue.png</a></td></tr><tr><td>In the top panel, set the direction of this query as appropriate - i.e. does it modify data (<code>send</code>) or retrieve data (<code>receive</code>)?</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F8gvTIsmuYWGIXNfjgTAq%2Fdb%20connector%20-%20queries%20-%205.png?alt=media&#x26;token=2f85f9f6-68d4-4360-9c61-6bc06dbbb591" alt=""></td><td>For general information about these settings, please see our main <a href="4-endpoints/adding-a-new-endpoint">Connector Builder section</a> - the same principles apply to both <em>endpoints</em> and <em>queries</em>. </td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FUZfGixxqRrXhfbce5JTy%2F3%20small%20blue.png?alt=media&#x26;token=89d89290-ce20-4118-ab9d-a1de16f4a805">3 small blue.png</a></td></tr><tr><td>Move down to the <code>authentication</code> tab and toggle the <code>DB user pass</code> authentication method <code>on</code> for this query:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FoWnoFFXRGA8RzRTVC5WT%2Fdb%20connector%20-%20queries%20-%206.png?alt=media&#x26;token=5230d549-767e-48df-a68f-7bdfe9e68b1e" alt=""></td><td>For general information about these settings, please see our main <a href="4-endpoints/endpoint-options/authentication">Connector Builder section</a> - the same principles apply to both <em>endpoints</em> and <em>queries</em>. </td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FNB1XoDP8TSQOGfWTDusp%2F4%20small%20blue.png?alt=media&#x26;token=095d8b02-5ec0-4fe4-9d77-e3274f2998af">4 small blue.png</a></td></tr><tr><td>If you want to add variables for inclusion in this query (so values can be provided at runtime and injected into queries), select the  <code>query variables</code> tab and add the required variables: </td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F6Ur9DmP5ksPOeFqiaDYC%2Fdb%20connector%20-%20queries%20-%207.png?alt=media&#x26;token=f98cf746-a696-45de-8619-b273a4b3ad92" alt=""></td><td>For examples of how variables might be used, see <a href="working-with-queries#using-variables">Working with database queries</a>. For general information about these settings, please see our main <a href="4-endpoints/endpoint-options/endpoint-variables">Connector Builder section</a> - the same principles apply to both <em>endpoints</em> and <em>queries</em>. </td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FHNgGcuuzjcqSHJWDdShx%2F5%20small%20blue.png?alt=media&#x26;token=beabd868-cf27-493c-8a19-70d5406e3d1e">5 small blue.png</a></td></tr><tr><td>Select the <code>query</code> tab to add your database query - for example:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fe0LTDBNwNI4ZkN3onNQF%2Fdb%20connector%20-%20queries%20-%208.png?alt=media&#x26;token=44e95d16-5ce4-41f1-9585-5d580df24b1a" alt=""></td><td>This is where you define your query using the required syntax for your database (in this case, it's <em>MySQL</em>). Syntax highlighting is included (verification is not currently available - any issues with queries will be <a href="#error-handling">reported at runtime</a>).<br><br>Standard syntax is used for the database type however, there are some Patchworks conventions to consider - please see <a href="building-a-database-connector/working-with-queries">Working with database queries</a> for more information.</td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FtkkdfxulpeRb6u0Bm8ad%2F6%20small%20blue.png?alt=media&#x26;token=9af7efe1-09b6-4f99-9ad0-3238562681db">6 small blue.png</a></td></tr><tr><td>If your query includes parameters for pagination (<code>LIMIT</code> and <code>OFFSET</code>), select the <code>pagination</code> tab to define the required values to be passed in - for example:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FlJMJZIuV4Y42gIL2F8Oo%2Fdb%20connector%20-%20queries%20-%209.png?alt=media&#x26;token=37f4df8d-4e05-48c8-a539-905fbee8a6dc" alt=""></td><td><code>Limit offset</code> is the most commonly used pagination method for databases. For general information on this topic please see our main <a href="4-endpoints/endpoint-options/pagination">Connector Builder section</a> - the same principles apply to both <em>endpoints</em> and <em>queries</em>. </td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FoqQf4tdpXjNlbcD21aib%2F7%20small%20blue.png?alt=media&#x26;token=a553b64f-0c56-43b5-9108-214936a06284">7 small blue.png</a></td></tr><tr><td>Having created all your required queries, you can go on to add instances of this connector (<a href="../../../process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape/configuring-a-database-connection">for use in process flows</a>) in the <a href="../../../connectors-and-instances/working-with-instances/adding-an-instance">normal way</a>:</td><td><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FMPSNR08IYGEBcUkh66hG%2Fdb%20connector%20-%20queries%20-%2010.png?alt=media&#x26;token=b6d0a561-ac4a-4beb-94c2-68690753ae63" alt=""></td><td><p>When prompted, choose the <code>DB user pass</code> authentication method, then enter your credentials and database details:</p><p></p><p><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FMsJsrc3YSjoKL0zdEIF0%2Fdb%20connector%20-%20queries%20-%2011.png?alt=media&#x26;token=fefe9f15-a888-4236-9eb3-dce34571f83f" alt=""> </p></td><td></td><td><a href="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FjlWpjvQTaoWon5L4ROLI%2F8%20small%20blue.png?alt=media&#x26;token=6770f561-6328-4fae-bc52-97fa21fa3b4f">8 small blue.png</a></td></tr></tbody></table>

## Error handling

Any issues found with queries at runtime (e.g. syntax errors) are reported from the database (verbatim) in [run logs](https://doc.wearepatchworks.com/product-documentation/process-flows/error-reporting-and-exception-handling/run-logs-and-queue).&#x20;

## Related pages

* [Working with database queries](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/building-a-database-connector/working-with-queries)
* [Configuring a database connection](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape/configuring-a-database-connection)
