{
  "name": "Loop Over Items: paginate with Reset",
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Start at Page 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start at Page 1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Page": {
      "main": [
        [
          {
            "node": "Has More Pages?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has More Pages?": {
      "main": [
        [
          {
            "node": "Next Page",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Collect All Pages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Next Page": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Fetch Page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "nodes": [
    {
      "id": "9df06f2b-d4b7-4001-8f27-2ad47f3bce9e",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        48
      ],
      "parameters": {}
    },
    {
      "id": "4d0ab13d-4912-4d04-b5cf-ab58e97ac455",
      "name": "Start at Page 1",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        48
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "return [{ json: { page: 1 } }];"
      }
    },
    {
      "id": "c2a56838-640c-4d6f-97f5-946f66168b49",
      "name": "Fetch Page",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        672,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// Stands in for an API with 3 pages of 2 records each\nconst page = $input.first().json.page;\nreturn [{ json: { page, records: [page * 10 + 1, page * 10 + 2], hasMore: page < 3, runIndex: $runIndex } }];"
      }
    },
    {
      "id": "1aab6499-a41a-4831-8734-95ca5480850d",
      "name": "Has More Pages?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        896,
        0
      ],
      "parameters": {
        "conditions": {
          "combinator": "and",
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{ $json.hasMore }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ]
        }
      }
    },
    {
      "id": "5f1f5ff1-1057-47f7-81d2-e789404632de",
      "name": "Next Page",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "return [{ json: { page: $input.first().json.page + 1 } }];"
      }
    },
    {
      "id": "f9620dcf-ab69-401f-8402-7a1ee436cf10",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        448,
        48
      ],
      "parameters": {
        "batchSize": 1,
        "options": {
          "reset": true
        }
      }
    },
    {
      "id": "af010698-0dfc-4cb0-93c1-af34272866a3",
      "name": "Collect All Pages",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        192
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const records = [];\nlet pages = 0;\nfor (let run = 0; ; run++) {\n  let items;\n  try { items = $('Fetch Page').all(0, run); } catch (e) { break; }\n  if (!items || items.length === 0) break;\n  pages++;\n  for (const i of items) records.push(...i.json.records);\n}\nreturn [{ json: { pages, records } }];"
      }
    }
  ],
  "settings": {
    "executionOrder": "v1"
  }
}
