{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "b63fac57-e9f7-4fb0-b5ce-fc8b97194f78",
      "name": "When clicking \u2018Execute workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -672,
        160
      ],
      "parameters": {},
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "8e0849a9-a399-42fe-8ae8-36a326f8f69e",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "position": [
        -464,
        160
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "297de599-7abe-4184-ab2f-6c082fa376be",
              "name": "base_url",
              "type": "string",
              "value": "https://app.nocodb.com"
            },
            {
              "id": "7c7bf021-4423-4b64-9a33-a5a3031ca4bf",
              "name": "source_tables",
              "type": "array",
              "value": "[\"table1\",\"table2\"]"
            },
            {
              "id": "57fa4746-0a1b-4b61-b4b1-9c7462113cf6",
              "name": "source_base",
              "type": "string",
              "value": "source_base"
            },
            {
              "id": "d1c410e5-22d8-4dcc-a859-c615ab5fef17",
              "name": "destination_base",
              "type": "string",
              "value": "destination_base"
            },
            {
              "id": "27d5d8a6-ff34-46cc-9c0f-9815a31e1c50",
              "name": "copy_rows",
              "type": "boolean",
              "value": true
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "05ea7fd0-0ca6-4b5c-8428-2811dcdf655f",
      "name": "Create NocoDB Table",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        368,
        256
      ],
      "parameters": {
        "url": "={{ $(\"Config\").first().json.base_url }}/api/v3/meta/bases/{{$(\"Config\").first().json.destination_base}}/tables",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"title\": \"{{ $json.title }} - Copy {{$now.toString().slice(0,19).split('T').join(' ')}}\",\n  \"table_name\": \"{{ $json.title }} - Copy {{$now.toString().slice(0,19).split('T').join(' ')}}\",\n  \"fields\": {{ JSON.stringify($json\n.fields\n.filter(f=>f.type!==\"Links\"&&f.type!==\"ID\"&&f.type!==\"ForeignKey\"&&f.type!==\"LinkToAnotherRecord\"&&f.type!==\"Lookup\")\n.map(f=>({title: f.title, type: f.type, options: f.options})))\n}}\n}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "nocoDbApiToken"
      },
      "credentials": {
        "nocoDbApiToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "5a6c0673-0e08-4310-8cf7-3fb16c5c2ca5",
      "name": "Create Tables",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -48,
        160
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "f3ea3975-1c1d-4997-9f85-7ad4aa910341",
      "name": "Prepare fields data for Insert",
      "type": "n8n-nodes-base.set",
      "position": [
        1216,
        256
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "873256a1-8179-4356-98e8-81a1d088c062",
              "name": "records",
              "type": "array",
              "value": "={{ $json.records.map(r=>r.fields) }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "4714dfe6-b1dd-4af6-9eb1-ded88af05e09",
      "name": "Copy rows to new tables",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        768,
        144
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "dbfb3da6-c7a9-4c62-a247-002112722c31",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -720,
        -112
      ],
      "parameters": {
        "color": 3,
        "width": 400,
        "height": 576,
        "content": "## Config\nFill out this config. You need to provide:\n1. NocoDB Base URL - You might leave default value if you are not self hosting\n2. Source Tables - Tables you want to copy\n3. Source Base - Base where previously mentioned table lives\n4. Destination Base - Where to copy tables\n5. Copy Rows - If you wish to copy data as well"
      },
      "typeVersion": 1
    },
    {
      "id": "4077153e-e66f-434a-958b-67015b3070d1",
      "name": "Fetch tables records",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        960,
        256
      ],
      "parameters": {
        "url": "={{ $(\"Config\").first().json.base_url }}/api/v3/data/{{$(\"Config\").first().json.source_base}}/{{$(\"Config\").first().json.source_tables[$runIndex]}}/records",
        "options": {},
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "={{ $('Create Tables').all()[$runIndex].json.fields.filter(f=>!['ID'].includes(f.type)).map(f=>f.title) }}"
            }
          ]
        },
        "nodeCredentialType": "nocoDbApiToken"
      },
      "credentials": {
        "nocoDbApiToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "cea6094e-b752-44bf-a839-d1d61b76fb4e",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -304,
        -112
      ],
      "parameters": {
        "color": 7,
        "width": 832,
        "height": 576,
        "content": "## Step 1: Fetch information about tables and create copies\nWe use custom HTTP requests in order to access `/meta` endpoints. Learn more about NocoDB meta APIs [here](https://nocodb.com/apis/v3/meta)"
      },
      "typeVersion": 1
    },
    {
      "id": "7cb5d5d4-80bc-4e6f-a583-be071aa8e41b",
      "name": "Get Source Table Metadata",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        160,
        256
      ],
      "parameters": {
        "url": "={{ $('Config').first().json.base_url }}/api/v3/meta/bases/{{ $('Config').first().json.source_base }}/tables/{{ $json.source_tables}}",
        "options": {},
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "nocoDbApiToken"
      },
      "credentials": {
        "nocoDbApiToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "d53778c9-a560-4ee1-aad0-7d0bdc956b24",
      "name": "Split tables",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        -256,
        160
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "source_tables"
      },
      "typeVersion": 1
    },
    {
      "id": "179c06fe-ed44-4955-a713-f2a4b55675d1",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        -112
      ],
      "parameters": {
        "color": 7,
        "width": 1136,
        "height": 576,
        "content": "## Step 2: Copy data over from old tables to the new ones\nHere, we use custom HTTP request node so we can programatically select which fields we want to copy. We are using `/data` APIs about which you can learn more  [here](https://nocodb.com/apis/v3/data). For inserting data we could use standard N8N NocoDB nodes but it would require inserting each record separately, instead we use once again HTTP node to bulk upload all fields"
      },
      "typeVersion": 1
    },
    {
      "id": "3932f8a4-04ef-4868-884e-ecabbb7d3c7c",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1232,
        -384
      ],
      "parameters": {
        "width": 496,
        "height": 848,
        "content": "## Clone NocoDB Tables with data over to another base\n### How it works?\nThis workflow uses NocoDB API to first fetch all relevant table metadata to later use it in custom HTTP request to copy it's structure . Next, it fetches data from original tables and puts it in newly created tables.\n\n### IMPORTANT\n**Workflow omits all relation fields to reduce complexity. These fields won't be carried over: `Links`, `Lookup`, `LinkToAnotherRecord`, `ForeignKey` and `ID`**\n\n### Setup steps\n1. Fill out Config node\n2. Connect your NocoDB credentials\nYou are all set\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nNeed help? Contact us at [developers@sailingbyte.com](mailto:developers@sailingbyte.com) or visit [sailingbyte.com](https://sailingbyte.com)!\n\nHappy hacking!"
      },
      "typeVersion": 1
    },
    {
      "id": "2f86432a-9775-4f04-ae08-60caed87684d",
      "name": "Fetch tables records1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1488,
        256
      ],
      "parameters": {
        "url": "={{ $(\"Config\").first().json.base_url }}/api/v3/data/{{$(\"Config\").first().json.source_base}}/{{$(\"Config\").first().json.source_tables[$runIndex]}}/records",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "={{ $json.records }}"
            }
          ]
        },
        "queryParameters": {
          "parameters": []
        },
        "nodeCredentialType": "nocoDbApiToken"
      },
      "credentials": {
        "nocoDbApiToken": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "fe121775-9a96-431d-81b2-b1adc9899114",
      "name": "Copy data over?",
      "type": "n8n-nodes-base.switch",
      "position": [
        592,
        144
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 3,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "c281894c-e016-4706-b15b-82630da1981e",
                    "operator": {
                      "type": "boolean",
                      "operation": "true",
                      "singleValue": true
                    },
                    "leftValue": "={{ $('Config').item.json.copy_rows }}",
                    "rightValue": false
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.4
    }
  ],
  "connections": {
    "Config": {
      "main": [
        [
          {
            "node": "Split tables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split tables": {
      "main": [
        [
          {
            "node": "Create Tables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Tables": {
      "main": [
        [
          {
            "node": "Copy data over?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Source Table Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Copy data over?": {
      "main": [
        [
          {
            "node": "Copy rows to new tables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create NocoDB Table": {
      "main": [
        [
          {
            "node": "Create Tables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch tables records": {
      "main": [
        [
          {
            "node": "Prepare fields data for Insert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch tables records1": {
      "main": [
        [
          {
            "node": "Copy rows to new tables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Copy rows to new tables": {
      "main": [
        [],
        [
          {
            "node": "Fetch tables records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Source Table Metadata": {
      "main": [
        [
          {
            "node": "Create NocoDB Table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare fields data for Insert": {
      "main": [
        [
          {
            "node": "Fetch tables records1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}