{
  "name": "ezGPT - PostgreSQL literal text search",
  "active": false,
  "nodes": [
    {
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "id": "pgtextsearch-0",
      "position": [
        0,
        260
      ]
    },
    {
      "name": "Sample Search",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return [{json:{\"query\":\"кабель\"}}];"
      },
      "id": "pgtextsearch-1",
      "position": [
        240,
        260
      ]
    },
    {
      "name": "Check Search",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one search item');\nconst p = items[0].json;\nif (Object.keys(p).length !== 1 || typeof p.query !== 'string' ||\n    !p.query.trim() || p.query !== p.query.trim() || p.query.length > 40 ||\n    /[\\u0000-\\u001f\\u007f]/.test(p.query) ||\n    Buffer.from(p.query,'utf8').toString('utf8') !== p.query) {\n  throw new Error('Invalid search text');\n}\nreturn [{json:{query:p.query}}];"
      },
      "id": "pgtextsearch-2",
      "position": [
        480,
        260
      ]
    },
    {
      "name": "Search Products",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "onError": "stopWorkflow",
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "WITH found AS (\n  SELECT sku, product_name, price_kopecks\n  FROM app.products\n  WHERE product_name ILIKE '%' ||\n    replace(replace(replace($1, '^', '^^'), '%', '^%'), '_', '^_')\n    || '%' ESCAPE '^'\n  ORDER BY sku\n  LIMIT 21\n)\nSELECT count(*)::integer AS found_count,\n       COALESCE(jsonb_agg(to_jsonb(f) ORDER BY f.sku), '[]'::jsonb) AS rows\nFROM found f;",
        "options": {
          "queryReplacement": "={{ [$json.query] }}"
        }
      },
      "id": "pgtextsearch-3",
      "position": [
        720,
        260
      ]
    },
    {
      "name": "Check Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error('Expected one search envelope');\nconst d = items[0].json;\nif (Object.keys(d).length !== 2 || !Number.isSafeInteger(d.found_count) ||\n    d.found_count < 0 || d.found_count > 20 || !Array.isArray(d.rows) ||\n    d.rows.length !== d.found_count) throw new Error('Invalid search result or limit exceeded');\nlet last = '';\nfor (const r of d.rows) {\n  if (!r || Object.keys(r).length !== 3 || typeof r.sku !== 'string' ||\n      !/^SKU-\\d{3}$/.test(r.sku) || r.sku <= last ||\n      typeof r.product_name !== 'string' || !r.product_name.trim() || r.product_name.length > 100 ||\n      /[\\u0000-\\u001f\\u007f]/.test(r.product_name) ||\n      Buffer.from(r.product_name,'utf8').toString('utf8') !== r.product_name ||\n      !Number.isSafeInteger(r.price_kopecks) || r.price_kopecks < 0 || r.price_kopecks > 100000000) {\n    throw new Error('Invalid or duplicated product');\n  }\n  last = r.sku;\n}\nreturn [{json:{query:$('Check Search').first().json.query,\n  found:d.found_count, products:d.rows, verified:true, external_send:false}}];"
      },
      "id": "pgtextsearch-4",
      "position": [
        960,
        260
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Search": {
      "main": [
        [
          {
            "node": "Check Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Search": {
      "main": [
        [
          {
            "node": "Search Products",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Products": {
      "main": [
        [
          {
            "node": "Check Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
