The registered_patients map reduce view is redundant. It is almost an exact copy of reports_by_subject — both index data_record reports by their subject's patient/place shortcode.
Maintaining a second, near-identical index over every report is wasteful. The view can be removed and its consumers repointed at reports_by_subject.
reports_by_subject is a strict superset of registered_patients: it emits the same patient_id/place_id keys (plus case_id, patient_uuid, and place_uuid) for the same data_record reports. Every consumer of registered_patients already post-filters the returned docs through registrationUtils.isValidRegistration, which discards error docs, non-registrations, and unconfigured forms — so the extra docs surfaced by reports_by_subject are trimmed back to exactly what registered_patients would have returned.
Two differences need to be handled when switching:
- Key coercion:
registered_patients emits String(id) keys; reports_by_subject emits the raw value. To preserve matching for any numeric patient_id/place_id, reports_by_subject should coerce its emitted keys with String().
- Duplicate emits:
reports_by_subject emits a key from both the top-level property and doc.fields, so a single report can be returned more than once per key. Consumers must de-duplicate the returned docs by _id (getReportsBySubject in transitions already does this).
View: medic-client/registered_patients
Definition
- Database: medic-client (synced to offline clients)
- Map keys:
String(doc.patient_id), String(doc.place_id) (falling back to doc.fields.patient_id / doc.fields.place_id)
- Map values: (none)
- Reduce: None
Usages
Note: removing the view (and the String() change to reports_by_subject) triggers a one-time view reindex on upgrade.
The
registered_patientsmap reduce view is redundant. It is almost an exact copy ofreports_by_subject— both indexdata_recordreports by their subject's patient/place shortcode.Maintaining a second, near-identical index over every report is wasteful. The view can be removed and its consumers repointed at
reports_by_subject.reports_by_subjectis a strict superset ofregistered_patients: it emits the samepatient_id/place_idkeys (pluscase_id,patient_uuid, andplace_uuid) for the samedata_recordreports. Every consumer ofregistered_patientsalready post-filters the returned docs throughregistrationUtils.isValidRegistration, which discards error docs, non-registrations, and unconfigured forms — so the extra docs surfaced byreports_by_subjectare trimmed back to exactly whatregistered_patientswould have returned.Two differences need to be handled when switching:
registered_patientsemitsString(id)keys;reports_by_subjectemits the raw value. To preserve matching for any numericpatient_id/place_id,reports_by_subjectshould coerce its emitted keys withString().reports_by_subjectemits a key from both the top-level property anddoc.fields, so a single report can be returned more than once per key. Consumers must de-duplicate the returned docs by_id(getReportsBySubjectin transitions already does this).View: medic-client/registered_patients
Definition
String(doc.patient_id),String(doc.place_id)(falling back todoc.fields.patient_id/doc.fields.place_id)Usages
shared-libs/transitions/src/lib/utils.js:165{ key/keys, include_docs: true }getRegistrations— fetches registration reports for one or more shortcodes, then filters to valid registrations.api/src/services/export/message-mapper.js:122{ keys: patientIds, include_docs: true }admin/src/js/services/message-queue.js:135{ keys: shortcodes, include_docs: true }webapp/src/ts/services/format-data-record.service.ts:41{ key: shortcode, include_docs: true }Note: removing the view (and the
String()change toreports_by_subject) triggers a one-time view reindex on upgrade.