# Persister

## Overview

In DIGIT, the Persister Service is used to store data in the database by listening to messages from Kafka topics.

It needs a configuration file (in YAML) to understand:

* Which Kafka topic to listen to
* &#x20;What data to extract from the Kafka message
* &#x20;Where and how to insert/update this data in the database

***

## &#x20;Configure Persister

{% stepper %}
{% step %}

### Add YAML Config in MDMS/Config Repo

* Go to your MDMS or the config GitHub repo.
* Use the right branch based on your environment: example: dev, uat, prod, master
* Add a new file named like this: egov-persister/persister-pgr.yml. This file tells the persister how to save the complaints.

#### Sample Persister Configuration For CCRS

```
persister:
  moduleName: PGR

  kafkaTopics:
    - topic: save-pgr-request
      configKey: PGR_SAVE
    - topic: update-pgr-request
      configKey: PGR_UPDATE

  PGR_SAVE:
    - query: INSERT INTO eg_pgr_service (id, tenantid, servicecode, description, status, createdby, createdtime, lastmodifiedby, lastmodifiedtime)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
      basePath: $.service
      jsonMaps:
        - jsonPath: $.service.id
        - jsonPath: $.service.tenantId
        - jsonPath: $.service.serviceCode
        - jsonPath: $.service.description
        - jsonPath: $.service.applicationStatus
        - jsonPath: $.service.auditDetails.createdBy
        - jsonPath: $.service.auditDetails.createdTime
        - jsonPath: $.service.auditDetails.lastModifiedBy
        - jsonPath: $.service.auditDetails.lastModifiedTime

  PGR_UPDATE:
    - query: UPDATE eg_pgr_service SET status = ?, lastmodifiedby = ?, lastmodifiedtime = ? WHERE id = ?
      basePath: $.service
      jsonMaps:
        - jsonPath: $.service.applicationStatus
        - jsonPath: $.service.auditDetails.lastModifiedBy
        - jsonPath: $.service.auditDetails.lastModifiedTime
        - jsonPath: $.service.id

```

How to read this sample persister configuration:

<table><thead><tr><th width="150.1171875">Term</th><th>What it means</th></tr></thead><tbody><tr><td>moduleName</td><td>Logical name (here, PGR)</td></tr><tr><td>topic</td><td>Kafka topic (like save-pgr-request) that triggers DB action</td></tr><tr><td>basePath</td><td>Root path in JSON (e.g., $.service) from which data is picked</td></tr><tr><td>jsonPath</td><td>Specific path in the request JSON to map to a DB column</td></tr><tr><td>query</td><td>SQL query to insert/update data in the database</td></tr></tbody></table>
{% endstep %}

{% step %}

### Restart Persister Service

* After saving the configuration, run this to reload the updated configuration. Command to run the configuration: `kubectl rollout restart deployment <persister-deployment-name> -n <namespace>`
* The service will not read the new configuration file until it's restarted.
  {% endstep %}

{% step %}

### Validate Configuration

Once the service is restarted:

* Submit a  complaint&#x20;
* Check:
  * &#x20;A message is published on Kafka (e.g., save-pgr-request)
  * The data is stored in the DB (check table eg\_pgr\_service)
* If nothing happens:
  * Check that the topic name matches in both the application and the persister YAML
  * Look for logs in the persister pod for errors
    {% endstep %}
    {% endstepper %}

***

### &#x20;Reference

* [Persister Config Samples on GitHub](https://github.com/egovernments/Citizen-Complaint-Resolution-System/tree/master/configs/egov-persister)

***

### &#x20;Summary

* The Persister config ensures that the complaint data is saved automatically when it is created or updated.
* It works behind the scenes, triggered by Kafka.
* You configure it using a simple YAML file to define what to save and where.
* Restart the service after adding or editing the config.
