# Configuring a database connection

## Introduction

To configure a database connection, add a connector shape to your process flow [in the normal way](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape) and select the required `source instance` and `source query` for an existing [database connector](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/building-a-database-connector):

&#x20;![](https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FnxG5AXyjn3uSaU5ldxsn%2Fdb%20connector%20-%20configure%20-%201.png?alt=media\&token=9b3017ae-8085-4643-9903-3f52ab971030)

Having selected a `source instance`, you'll know if you're working with a database connector because the subsequent field requires you to choose a `source query`rather than a `source endpoint`.&#x20;

Generally, database connector settings work on the same principles as 'normal' connectors but there are differences, depending on whether you're using a query that [receives](#receive-queries) or [sends](#send-queries) data.

## Receive queries

When a connector shape is configured with a `receive` type query (i.e. you're receiving data from a database), you'll see settings sections for `variables`, `error handling`, and `response handling`:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fw07B9rWAe5kXfRcYRWsW%2Fdb%20connector%20-%20configure%20-%202%20-%20receive%20b.png?alt=media&#x26;token=17a79c5b-d8d1-49d9-8326-0d6ccc16d9ad" alt="" width="375"><figcaption></figcaption></figure></div>

These options are summarised [below](#receive-options).

### Receive options

| Section           | Setting                    | Summary                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| ----------------- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Variables         | Custom                     | Any query variables defined for the selected query are displayed here. Variables may be mandatory (denoted with an asterisk) so a value must be present at runtime, or optional.                                                                                                                                                                                                                                                                                                                                                                                                                            |
| Error handling    | `Retries`                  | Sets the number of retries that will be attempted if a connection can't be made. You can define a value between `0` and `2`. The default setting is `1`.                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| Response handling | `Save response in payload` | <p>Set this option to <code>on</code> to save the response from the completed operation IN the payload for subsequent processing. </p><p></p><p>This option provides the ability to access the response body via <a href="../../../../../developer-hub/connector-builder/building-your-own-connector/building-a-database-connector/working-with-queries">payload parameters</a>.<br><br>By default, the response is saved in a field named <code>response</code> however,  when the <code>save response in payload</code> option is toggled <code>on</code>, you can specify your preferred field name.</p> |

### Response data

When a process flow runs using a `receive` type query, received data is returned in a payload. You can view this data in logs - 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%2FdQwwBfX3yNRoLHGjXzm0%2Fdb%20connector%20-%20response%20-%20receive.png?alt=media&#x26;token=ed7b48b0-a275-4881-8f28-e74239cbeae8" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
The number of payloads received is determined by [pagination options](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/building-a-database-connector#queries) defined in the query setup, and whether these options are referenced in the associated query.
{% endhint %}

## Send queries

When a connector shape is configured with a `send` type query (i.e. you're sending data to a database), you'll see settings sections for `variables`, `database`, `error handling`, and `response handling`:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FNIR0w6IxP0TsW5A6DNJ3%2Fdb%20connector%20-%20configure%20-%202%20-%20send%20b.png?alt=media&#x26;token=ddb9a62d-d72f-45cf-9afc-3d59601bc2fa" alt="" width="375"><figcaption></figcaption></figure></div>

These options are summarised [below](#send-options).

### Send options

| Section           | Setting                    | Summary                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ----------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Variables         | Custom                     | Any query variables defined for the selected query are displayed here. Variables may be mandatory (denoted with an asterisk) so a value must be present at runtime, or optional.                                                                                                                                                                                                                                                                                          |
| `Database`        | `Override query`           | <p>You can enter your own database query here which will be run instead of the source query already selected. </p><p></p><p>Before using an override query you should ensure that:</p><ul><li>target columns exist in the database </li><li>target columns are configured (in the database) to accept <code>null</code> values.</li></ul>                                                                                                                                 |
|                   | `Items path`               | <p>If your incoming payload contains a single array of items, enter the field name here. In doing so, the process flow loops through each item in the array and performs multiple inserts as one operation. </p><p></p><p>For more information please see <a href="#about-item-paths">About item paths</a>.</p>                                                                                                                                                           |
| Error handling    | `Retries`                  | Sets the number of retries that will be attempted if a connection can't be made. You can define a value between `0` and `2`. The default setting is `1`.                                                                                                                                                                                                                                                                                                                  |
| Response handling | `Save response as payload` | Set this option to `on` to save the response from the completed operation as a payload for subsequent processing.                                                                                                                                                                                                                                                                                                                                                         |
|                   | `Save response in payload` | <p>Set this option to <code>on</code> to save the response from the completed operation IN the payload for subsequent processing. </p><p></p><p>This option saves the response from your database, together with the payload that was sent in.<br><br>By default, the response is saved in a field named <code>response</code> however,  when the <code>save response in payload</code> option is toggled <code>on</code>, you can specify your preferred field name.</p> |

### About item paths

The `items path` field is used to run the associated query for multiple items (i.e. database rows) in a single operation, by looping through all items in a given array:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FfFUg5oU8E5iGCxIbESxb%2Fdb%20connector%20-%20configure%20-%202%20-%20send%20c.png?alt=media&#x26;token=d3b2b060-5148-4986-b64e-0c26e45f2d49" alt="" width="375"><figcaption></figcaption></figure></div>

Here, specify the field name in your payload associated with the array to be targeted.&#x20;

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FfUVdhc1UgMWObOesLjkS%2Ficon%20eye.png?alt=media&#x26;token=c8b6d3a9-b5a9-4cb0-8aeb-00bd39c00b60" alt="" data-size="line"> Example (simple array)</summary>

In the example below, our payload includes one `products` field with items in an array:

```json
{
  "products": [
    {
      "id": 4001,
      "sku": "BAG-001",
      "colour": "Blue",
      "category": "Bags",
      "quantity": 11
    },
    {
      "id": 4002,
      "sku": "BAG-002",
      "colour": "Green",
      "category": "Bags",
      "quantity": 11
    },
    {
      "id": 4003,
      "sku": "BAG-003",
      "colour": "Pink",
      "category": "Bags",
      "quantity": 12
    },
    {
      "id": 4004,
      "sku": "BAG-004",
      "colour": "Red",
      "category": "Bags",
      "quantity": 10
    }
  ]
}

```

To send all these items into our database, the `items path` should be set to:

`products`

</details>

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FfUVdhc1UgMWObOesLjkS%2Ficon%20eye.png?alt=media&#x26;token=c8b6d3a9-b5a9-4cb0-8aeb-00bd39c00b60" alt="" data-size="line"> Example (nested arrays)</summary>

In the example below, our payload includes a top-level `products` array with nested `accessories` and `footwear` arrays:

```json
{
  "products": [
    {
      "accessories": [
        {
          "id": 4,
          "sku": "BAG-003",
          "colour": "Green",
          "category": "Bags",
          "quantity": 11
        },
        {
          "id": 5,
          "sku": "BAG-004",
          "colour": "Pink",
          "category": "Bags",
          "quantity": 12
        } 
      ]
    },
    {
      "footwear": [
        {
          "id": 6,
          "sku": "SHOE-001",
          "colour": "Black",
          "category": "Shoes",
          "quantity": 20
        },
        {
          "id": 7,
          "sku": "SHOE-002",
          "colour": "White",
          "category": "Shoes",
          "quantity": 15
        },
        {
          "id": 8,
          "sku": "BOOT-001",
          "colour": "Brown",
          "category": "Boots",
          "quantity": 10
        }
      ]
    }
  ]
}

```

To send all `accessories` items into our database, the `items path` should be set to:

`products.0.accessories` &#x20;

To send all `footwear` items into our database, the `items path` should be set to:

`products.1.footwear`&#x20;

</details>

The `items path` field can't be used to target individual payload items. For this, you'd need an alternative approach - for example, you might choose to `override query` and provide a more specific query to target the required item.

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FfUVdhc1UgMWObOesLjkS%2Ficon%20eye.png?alt=media&#x26;token=c8b6d3a9-b5a9-4cb0-8aeb-00bd39c00b60" alt="" data-size="line"> Example (target specific item)</summary>

In the example below, our payload includes a top-level `products` array with nested `accessories` and `footwear` arrays:

```json
{
  "products": [
    {
      "accessories": [
        {
          "id": 4,
          "sku": "BAG-003",
          "colour": "Green",
          "category": "Bags",
          "quantity": 11
        },
        {
          "id": 5,
          "sku": "BAG-004",
          "colour": "Pink",
          "category": "Bags",
          "quantity": 12
        } 
      ]
    },
    {
      "footwear": [
        {
          "id": 6,
          "sku": "SHOE-001",
          "colour": "Black",
          "category": "Shoes",
          "quantity": 20
        },
        {
          "id": 7,
          "sku": "SHOE-002",
          "colour": "White",
          "category": "Shoes",
          "quantity": 15
        },
        {
          "id": 8,
          "sku": "BOOT-001",
          "colour": "Brown",
          "category": "Boots",
          "quantity": 10
        }
      ]
    }
  ]
}

```

To insert the FIRST item in the `footwear` array, we'd leave the `items path` field blank and enter the following `override query`:

{% code overflow="wrap" %}

```sql
INSERT INTO products (id, sku, colour, category, quantity) VALUES (:products.1.footwear.0.id, :products.1.footwear.0.sku, :products.1.footwear.0.colour, :products.1.footwear.0.category, :products.1.footwear.0.quantity)
```

{% endcode %}

</details>

### Response data

When a process flow runs using a `send` type query, the default behaviour is for the response from your database to be returned as the payload. You can view this in logs - 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%2F1acKDAKtImUrNPDKYiuX%2Fdb%20connector%20-%20response%20-%20send.png?alt=media&#x26;token=14c963e3-85e5-4ec2-9e84-24e0b3935fd7" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
If you want to see the data that was passed in, toggle the [save response in payload](#send-options) option to `on`.
{% endhint %}

## Related pages

* [Building a database connector](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/building-a-database-connector)
* [Working with queries](https://doc.wearepatchworks.com/product-documentation/developer-hub/connector-builder/building-your-own-connector/building-a-database-connector/working-with-queries)
