{
  "name": "ezGPT - Search PostgreSQL JSONB",
  "active": false,
  "nodes": [
    {
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "id": "pgjsonsearch-0",
      "position": [
        0,
        260
      ]
    },
    {
      "name": "Sample Filter",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "return [{ json: {\"vip\":false,\"sku\":\"SKU-002\",\"quantity\":0} }];"
      },
      "id": "pgjsonsearch-1",
      "position": [
        240,
        260
      ]
    },
    {
      "name": "Check Filter",
      "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;\nconst keys = ['vip', 'sku', 'quantity'];\nif (Object.keys(p).length !== 3 || !keys.every(k => Object.hasOwn(p, k)) ||\n    typeof p.vip !== 'boolean' || typeof p.sku !== 'string' || !/^SKU-\\d{3}$/.test(p.sku) ||\n    !Number.isSafeInteger(p.quantity) || p.quantity < 0 || p.quantity > 1000) {\n  throw new Error('Invalid search filter');\n}\nreturn [{ json: { ...p, filter: {\n  customer: { vip: p.vip },\n  items: [{ sku: p.sku, quantity: p.quantity }]\n} } }];"
      },
      "id": "pgjsonsearch-2",
      "position": [
        480,
        260
      ]
    },
    {
      "name": "Find Documents",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "onError": "stopWorkflow",
      "parameters": {
        "resource": "database",
        "operation": "executeQuery",
        "query": "SELECT COALESCE(\n  jsonb_agg(to_jsonb(t) ORDER BY t.record_id), '[]'::jsonb\n) AS matches\nFROM (\n  SELECT record_id, document\n  FROM app.documents\n  WHERE document @> $1::jsonb\n  ORDER BY record_id\n  LIMIT 21\n) AS t;",
        "options": {
          "queryReplacement": "={{ [JSON.stringify($json.filter)] }}"
        }
      },
      "id": "pgjsonsearch-3",
      "position": [
        720,
        260
      ]
    },
    {
      "name": "Check Search",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const p = $('Check Filter').first().json;\nconst items = $input.all();\nif (items.length !== 1 || Object.keys(items[0].json).length !== 1 ||\n    !Array.isArray(items[0].json.matches)) throw new Error('Invalid search envelope');\nconst rows = items[0].json.matches;\nif (rows.length > 20) throw new Error('Too many matches; narrow the filter');\nlet last = '';\nfor (const r of rows) {\n  const d = r?.document;\n  if (!r || Object.keys(r).length !== 2 || typeof r.record_id !== 'string' ||\n      !/^DOC-\\d{3}$/.test(r.record_id) || r.record_id <= last ||\n      !d || typeof d !== 'object' || Array.isArray(d) ||\n      !d.customer || typeof d.customer.name !== 'string' ||\n      !d.customer.name.trim() || d.customer.name.length > 100 ||\n      /[\\u0000-\\u001f\\u007f]/.test(d.customer.name) ||\n      Buffer.from(d.customer.name, 'utf8').toString('utf8') !== d.customer.name ||\n      d.customer.vip !== p.vip || !Array.isArray(d.items) ||\n      !d.items.some(i => i && i.sku === p.sku && i.quantity === p.quantity)) {\n    throw new Error('Returned document does not match filter');\n  }\n  last = r.record_id;\n}\nreturn [{ json: { found: rows.length,\n  filter: { vip: p.vip, sku: p.sku, quantity: p.quantity },\n  matches: rows.map(r => ({ record_id: r.record_id, customer_name: r.document.customer.name })),\n  verified: true, external_send: false } }];"
      },
      "id": "pgjsonsearch-4",
      "position": [
        960,
        260
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Sample Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sample Filter": {
      "main": [
        [
          {
            "node": "Check Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Filter": {
      "main": [
        [
          {
            "node": "Find Documents",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Documents": {
      "main": [
        [
          {
            "node": "Check Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
