{
  "id": "BMMFT2vYdGSt1cjr",
  "name": "Global Key-Value Store Template",
  "tags": [],
  "nodes": [
    {
      "id": "f259758f-2df3-4a49-ac03-0dff107d0fb0",
      "name": "Create Globals table",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        -272,
        368
      ],
      "parameters": {
        "columns": {
          "column": [
            {
              "name": "key"
            },
            {
              "name": "value"
            }
          ]
        },
        "options": {},
        "resource": "table",
        "operation": "create",
        "tableName": "Globals"
      },
      "typeVersion": 1.1
    },
    {
      "id": "33cf6930-8ebe-4942-abe7-a1f26596ff38",
      "name": "Stop and Error",
      "type": "n8n-nodes-base.stopAndError",
      "position": [
        -272,
        208
      ],
      "parameters": {
        "errorMessage": "Unknown error happened while retrieving Globals table"
      },
      "typeVersion": 1
    },
    {
      "id": "414bec77-24c2-4465-8f7c-996ca981da0e",
      "name": "If not the expected error",
      "type": "n8n-nodes-base.if",
      "position": [
        -496,
        288
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "0abb66cd-a718-43a0-94c5-ed607ec669e5",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.error }}",
              "rightValue": "Data table with name \"Globals\" not found"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "da34d122-c0df-41a1-9334-20869f9b64c5",
      "name": "Global found",
      "type": "n8n-nodes-base.if",
      "position": [
        -496,
        736
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "564f82db-938f-4ef7-abe5-5b2430f5dbd3",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "35df67d3-343f-4c49-9f55-d2b916d6cc76",
      "name": "When clicking \u2018Execute workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -976,
        480
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "50db41c6-8d8d-456c-9271-5a41025e6a15",
      "name": "Get Global \"your_variable_name\"",
      "type": "n8n-nodes-base.dataTable",
      "onError": "continueErrorOutput",
      "position": [
        -752,
        480
      ],
      "parameters": {
        "limit": 1,
        "filters": {
          "conditions": [
            {
              "keyName": "=key",
              "keyValue": "your_variable_name",
              "condition": "=eq"
            }
          ]
        },
        "matchType": "allConditions",
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "name",
          "value": "Globals"
        }
      },
      "typeVersion": 1.1,
      "alwaysOutputData": true
    },
    {
      "id": "380bd9e8-ccf9-452b-83e3-ed21cb0eb407",
      "name": "Set default value",
      "type": "n8n-nodes-base.set",
      "position": [
        -272,
        848
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "3aba0dd8-38b1-4355-bd2f-54e8194bcd80",
              "name": "your_variable_name",
              "type": "string",
              "value": "=value goes here"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "6f19c478-7657-4ea7-a5c6-a57aa486d974",
      "name": "Format value",
      "type": "n8n-nodes-base.set",
      "position": [
        -272,
        624
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "dd1cd738-335c-47e4-b6b2-cf02cf86691c",
              "name": "your_variable_name",
              "type": "string",
              "value": "={{ $json.value }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "8306af44-bc85-4c08-80a5-7c6804d865a0",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        416
      ],
      "parameters": {
        "color": 7,
        "width": 224,
        "height": 240,
        "content": "## 1. Get variable"
      },
      "typeVersion": 1
    },
    {
      "id": "26411923-8894-4eb0-b77c-fe17ed208bb2",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -560,
        144
      ],
      "parameters": {
        "color": 7,
        "width": 480,
        "height": 384,
        "content": "## Fallback: Create table if it does not exist\n_Needs to run first if previous node\nfailed - hence it is placed above_"
      },
      "typeVersion": 1
    },
    {
      "id": "2474eeff-0bc5-4ab1-828c-b48c2f565cf9",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -560,
        560
      ],
      "parameters": {
        "color": 7,
        "width": 480,
        "height": 448,
        "content": "## 2. Define default value if needed"
      },
      "typeVersion": 1
    },
    {
      "id": "7325803f-d5d0-4597-87de-7d039e50d3ce",
      "name": "Do something",
      "type": "n8n-nodes-base.noOp",
      "position": [
        16,
        736
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "26fe591d-01fd-4566-9140-034349e1683d",
      "name": "Upsert Global \"your_variable_name\"",
      "type": "n8n-nodes-base.dataTable",
      "position": [
        272,
        736
      ],
      "parameters": {
        "columns": {
          "value": {
            "key": "your_variable_name",
            "value": "={{ $('Do something').item.json.your_variable_name }}"
          },
          "schema": [
            {
              "id": "key",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "key",
              "defaultMatch": false
            },
            {
              "id": "value",
              "type": "string",
              "display": true,
              "removed": false,
              "readOnly": false,
              "required": false,
              "displayName": "value",
              "defaultMatch": false
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "filters": {
          "conditions": [
            {
              "keyName": "=key",
              "keyValue": "=your_variable_name",
              "condition": "=eq"
            }
          ]
        },
        "options": {},
        "operation": "upsert",
        "dataTableId": {
          "__rl": true,
          "mode": "name",
          "value": "Globals"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "89dd60b8-25e3-411b-9463-1f883cd08f13",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        208,
        656
      ],
      "parameters": {
        "color": 7,
        "width": 224,
        "height": 256,
        "content": "## 4. Save variable"
      },
      "typeVersion": 1
    },
    {
      "id": "858ad53d-9f1c-4c0b-99f9-ed070c5ba994",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1680,
        256
      ],
      "parameters": {
        "width": 576,
        "height": 560,
        "content": "## How it works\nA data table is used as a key value store having the two columns \u201ckey\u201d and \u201cvalue\u201d.\n_It is referenced by name, which makes it easier to migrate this workflow to other instances._\n1. At the beginning a variable is retrieved by it\u2019s key\n2. If the variable / key does not exist yet, a default value is set, otherwise the stored value is being returned\n3. The variable can now be used and updated (see customization below)\n4. The variable is being created/updated in the database for the next workflow run\nIf the variable did not exist before it get\u2019s created\n \nFallback: If the table doesn\u2019t exist, it automatically gets created\n\n## Setup\n- [ ] Replace the placeholder **your_variable_name** in the _Data Table_ and _Set_ nodes\n\n## Customization\n- Delete the trigger node and place this snippet into your existing workflow\n- Replace the _Do something_ placeholder node in step 3 with your own logic that consumes and later updates the variable - just make sure the mapping is later valid in step 4\n- This setup also works with using multiple variables simultaneously which can be achieved with a slight modification"
      },
      "typeVersion": 1
    },
    {
      "id": "bcbac038-fa72-42fe-8479-f3edc1da50aa",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        656
      ],
      "parameters": {
        "color": 7,
        "width": 224,
        "height": 256,
        "content": "## 3. Use variable"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "214d9937-cb52-4b44-9ebd-d71ba51d1257",
  "connections": {
    "Do something": {
      "main": [
        [
          {
            "node": "Upsert Global \"your_variable_name\"",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format value": {
      "main": [
        [
          {
            "node": "Do something",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Global found": {
      "main": [
        [
          {
            "node": "Format value",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set default value",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set default value": {
      "main": [
        [
          {
            "node": "Do something",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Globals table": {
      "main": [
        []
      ]
    },
    "If not the expected error": {
      "main": [
        [
          {
            "node": "Stop and Error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Globals table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Global \"your_variable_name\"": {
      "main": [
        [
          {
            "node": "Global found",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If not the expected error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Get Global \"your_variable_name\"",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}