{
  "id": "redis-opcode-example",
  "title": "Using the operation code",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/redis-opcode-example/",
  "summary": "",
  "tags": [
    "docs",
    "integrate",
    "rs",
    "rdi"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "page_type": "content",
  "content_hash": "49a3e55e2ffcf90cbe727ad93a81f40be304fa32f583fde900b4cc02983f489f",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The operation code (`opcode`) is a metadata field that indicates the type of operation that generated the change in the source database. It can be useful for tracking changes and understanding the context of the data being processed.\n\nThe opcode is only available in the [full row format](), and can be accessed in the `transform` and `output` sections of the job file.\n\nIt has one of the following values:\n\n- r - Read (applies to only snapshots)\n- c - Create\n- u - Update\n- d - Delete\n- t = Truncate (PostgreSQL specific)\n- m = Message (PostgreSQL specific)\n\n\nYou can add the value of the operation code to the output, and also use it in a conditional expression to modify the behavior of the job. The following examples demonstrate the different use-cases."
    },
    {
      "id": "adding-the-operation-code-to-the-output",
      "title": "Adding the operation code to the output",
      "role": "content",
      "text": "Use the `add_field` transformation to add a new field that contains the value of the `opcode` field from the source data. Note that the fields must be prefixed with `after` to be included in the output.\n\n\n[code example]"
    },
    {
      "id": "filtering-operation-by-output-code",
      "title": "Filtering operation by output code.",
      "role": "content",
      "text": "In some cases you may want to ignore certain operations (for example, you may not be interested in deletions). Use the `filter` transformation to filter out any operations you don't need to process.\n\n[code example]"
    },
    {
      "id": "modifying-the-output-based-on-the-operation-code",
      "title": "Modifying the output based on the operation code",
      "role": "content",
      "text": "The previous example filters out specific operations, but you can also modify the output based on the operation code. For example, you can add a new field that tracks the status of the record based on the operation code.\n\nNote that when a source record is deleted, you must modify the value of the `opcode` field if you want to prevent the corresponding record in the target database from being removed automatically.\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "adding-the-operation-code-to-the-output-ex0",
      "language": "yaml",
      "code": "name: Add operation code to employee\nsource:\n  schema: public\n  table: employee\n  row_format: full\n\ntransform:\n  # add the operation code to the data\n  - uses: add_field\n    with:\n      field: after.operation_code\n      expression: opcode\n      language: jmespath",
      "section_id": "adding-the-operation-code-to-the-output"
    },
    {
      "id": "filtering-operation-by-output-code-ex0",
      "language": "yaml",
      "code": "name: Filter out delete operations\nsource:\n  schema: public\n  table: employee\n  row_format: full\n\ntransform:\n  - uses: filter\n    with:\n      expression: opcode != 'd'\n      language: jmespath",
      "section_id": "filtering-operation-by-output-code"
    },
    {
      "id": "modifying-the-output-based-on-the-operation-code-ex0",
      "language": "yaml",
      "code": "name: Track status based on operation code\nsource:\n  schema: public\n  table: employee\n  row_format: full\n\ntransform:\n  - uses: add_field\n    with:\n      fields:\n        # Here you set the value of the field based on the value of the opcode field\n        - field: after.status\n          expression: opcode == 'd' && 'inactive' || 'active'\n          language: jmespath\n\n        # You have to change the value of the opcode field to prevent deletion\n        - field: opcode\n          expression: opcode == 'd' && 'u' || opcode\n          language: jmespath",
      "section_id": "modifying-the-output-based-on-the-operation-code"
    }
  ]
}
