| Metadata | Value |
|---|---|
| Status | Active |
| Version | 1.1.0 |
| Last Updated | 2026-09-10 |
| Author | Sangeetha Grantha Team |
| Document Type | Design reference |
[!NOTE] Design/reference material: this page may include proposals or earlier implementation assumptions. Use current feature map for implemented behavior and current operating steps.
Implementation Date: 2025-01-27
This feature optimizes the application’s Data Access Layer (DAL) by leveraging modern features of the Kotlin Exposed ORM framework. The primary goals are to reduce database round-trips, improve transaction efficiency, and eliminate anti-patterns like “Delete-Insert” for collection updates.
✅ All optimizations have been successfully implemented across all repositories.
The previous implementation of the DAL suffered from several inefficiencies:
KrithiSections) involved deleting all existing records and re-inserting the new set. This caused:
created_at timestamps).create and update required two separate database queries: one to perform the action and another to SELECT the resulting entity to return to the caller. This doubled the network latency for these critical operations.Collection management has been refactored to implement a smart diffing algorithm:
batchInsert, update, and deleteWhere operations.Implemented in:
KrithiRepository.saveSections() - Smart diffing for krithi sectionsKrithiRepository.saveLyricVariantSections() - Smart diffing for lyric variant sectionsKrithiRepository.update() - Smart diffing for raga associationsKrithiRepository.updateTags() - Smart diffing for tag associationsBenefits:
All repositories now utilize RETURNING clause capabilities to perform writes and reads in a single query.
Implementation Pattern:
insert { ... }.resultedValues to retrieve the generated ID and default values immediately.Table.updateReturning() to apply changes and retrieve the updated record in one atomic operation.Repositories Optimized:
create(), update()create(), update(), createLyricVariant(), updateLyricVariant()createImport(), reviewImport()create(), update()create(), update()create(), update()create(), update()create(), update()createVariant(), updateVariant(), createRow(), updateRow()Code Pattern Example:
// Create with RETURNING
suspend fun create(...): EntityDto = DatabaseFactory.dbQuery {
EntityTable.insert {
// ... field assignments
}
.resultedValues
?.single()
?.toEntityDto()
?: error("Failed to insert entity")
}
// Update with RETURNING
suspend fun update(id: Uuid, ...): EntityDto? = DatabaseFactory.dbQuery {
EntityTable
.updateReturning(
where = { EntityTable.id eq id.toJavaUuid() }
) {
// ... field updates
}
.singleOrNull()
?.toEntityDto()
}
updateReturning and resultedValues)RETURNING clause)created_at timestamps on sub-resources// Two queries: INSERT + SELECT
KrithisTable.insert { ... }
KrithisTable.selectAll().where { ... }.single()
// Single query: INSERT with RETURNING
KrithisTable.insert { ... }
.resultedValues
?.single()
?.toKrithiDto()
// Two queries: UPDATE + SELECT
KrithisTable.update { ... }
KrithisTable.selectAll().where { ... }.singleOrNull()
// Single query: UPDATE with RETURNING
KrithisTable.updateReturning(where = { ... }) { ... }
.singleOrNull()
?.toKrithiDto()