{"id":21593519,"url":"https://github.com/tamimattafi/android-room-compound","last_synced_at":"2026-05-12T04:32:15.227Z","repository":{"id":43638414,"uuid":"510142549","full_name":"tamimattafi/android-room-compound","owner":"tamimattafi","description":"A light-weight code generation library that helps reducing boilerplate code when working with relations in Room","archived":false,"fork":false,"pushed_at":"2023-01-28T19:09:04.000Z","size":214,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-24T16:49:22.335Z","etag":null,"topics":["android","android-library","compound","kotlin","ksp","relationships","room-database","sqlite"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tamimattafi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-03T21:48:36.000Z","updated_at":"2023-06-11T13:15:56.000Z","dependencies_parsed_at":"2023-02-15T18:31:18.309Z","dependency_job_id":null,"html_url":"https://github.com/tamimattafi/android-room-compound","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamimattafi%2Fandroid-room-compound","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamimattafi%2Fandroid-room-compound/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamimattafi%2Fandroid-room-compound/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tamimattafi%2Fandroid-room-compound/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tamimattafi","download_url":"https://codeload.github.com/tamimattafi/android-room-compound/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244206460,"owners_count":20416085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","compound","kotlin","ksp","relationships","room-database","sqlite"],"created_at":"2024-11-24T17:13:24.498Z","updated_at":"2026-05-12T04:32:15.178Z","avatar_url":"https://github.com/tamimattafi.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# android-room-compound\nA light-weight code generation library that helps reducing boilerplate code when working with relations in Room\n\n## Definition\nA **Compound** is a simple data class holding two or more entities, or even other compounds, and defining a relationship between them.\n\nFor example, we have a `User` model in our domain, that contains some other models such as `Avatar`, `Banner`... etc\n\nThe equivalent of User in our data layer will be `UserCompound`, containing an embedded `UserEntity`, and defining a relationship with `AvatarEntity` and `BannerEntity`.\n\n\u003e Compound is not a very common name for objects, but still better than the traditional naming used amoung android developers when working with relations in room, such as `UserWithAvatarAndBanner`. Extending our user will be so painful the more we add more relations.\n\n## Installation\n### build.gradle (project level)\nThis library is available on `mavenCentral`, so make sure to add this repository:\n```kotlin\nbuildscript {\n    repositories {\n        mavenCentral()\n    }\n}\n```\n\nAlso, we need to add `ksp` plugin, since it is used for annotation processing:\n```kotlin\nplugins {\n    id(\"com.google.devtools.ksp\") version \"$ksp_version\"\n}\n```\n\u003e Check build.gradle of this library for the compatible version of ksp [here](https://github.com/tamimattafi/android-room-compound/blob/main/build.gradle)\n\n### build.gradle (module level)\nIn our gradle script of our library or application, we need to apply `ksp` plugin:\n```kotlin\nplugins {\n    id(\"com.android.library\")\n    id(\"com.google.devtools.ksp\")\n}\n```\n\nThen we add the required dependencies:\n```kotlin\ndependencies {\n    implementation(\"com.attafitamim.room:compound-annotations:$version\")\n    ksp(\"com.attafitamim.room:compound-processor:$version\")\n}\n```\n\u003e Check release notes for the latest version at [here](https://github.com/tamimattafi/android-room-compound/releases)\n\n## Usage\nThe usage is very simple, just create a `Compound` data class with the required entities and other compounds, then annotate it with `@Compound` annotation:\n```kotlin\n@Compound\ndata class MainCompound(\n    @Embedded\n    val mainEntity: MainEntity,\n\n    @Relation(\n        parentColumn = \"name\",\n        entityColumn = \"name\",\n        associateBy = Junction(\n            value = MainSecondJunction::class,\n            parentColumn = \"mainId\",\n            entityColumn = \"secondId\"\n        )\n    )\n    val secondaryCompounds: List\u003cSecondCompound\u003e?,\n\n    @Relation(\n        parentColumn = \"name\",\n        entityColumn = \"name\",\n        associateBy = Junction(\n            value = MainSecondJunction::class,\n            parentColumn = \"mainId\",\n            entityColumn = \"secondId\"\n        )\n    )\n    val secondaryEntities: List\u003cSecondEntity\u003e?,\n\n    @Relation(\n        parentColumn = \"name\",\n        entityColumn = \"name\"\n    )\n    val secondaryCompound: SecondCompound?\n)\n```\n\nAfter a successful build, a dao will be generated, we need to add it to our `ApplicationDatabase`:\n```kotlin\n@Database(\n    entities = [\n        MainEntity::class,\n        MainSecondJunction::class,\n        SecondEntity::class,\n        SecondThirdJunction::class,\n        ThirdEntity::class,\n        ForthEntity::class\n    ],\n    version = 1,\n    exportSchema = false\n)\nabstract class MainDatabase : RoomDatabase() {\n    abstract val mainCompoundDao: IMainCompoundDao\n    abstract val secondCompoundDao: ISecondCompoundDao\n    abstract val thirdCompoundDao: IThirdCompoundDao\n    abstract val mainCompound: MainCompound\n}\n```\n\u003e Note that since room allows us to implement other daos, we can actually have `IMainDao` implement `IMainCompoundDao` so we can add more methods, and still have our generated ones.\n\nUsing this dao would look like this:\n```kotlin\n        val secondEntity = SecondEntity(\"mainEntity\", \"1/2/3\")\n        val forthEntity = ForthEntity(\"forthEntity\", \"1/2/3/4\")\n        val thirdEntity = ThirdEntity(\"thirdEntity\", \"1/2/3\")\n        val thirdCompound = ThirdCompound(thirdEntity, listOf(secondEntity), listOf(forthEntity))\n\n        val secondCompound = SecondCompound(\n            secondEntity,\n            listOf(thirdCompound),\n            listOf(thirdEntity),\n            thirdCompound,\n            forthEntity\n        )\n\n        val mainEntity = MainEntity(\"mainEntity\", \"1/2/3\")\n        val mainCompound = MainCompound(mainEntity, listOf(secondCompound), listOf(secondEntity), secondCompound)\n        database.mainCompoundDao.insertOrUpdate(mainCompound)\n```\n\n\u003e Please check the sample app for more information [here](https://github.com/tamimattafi/android-room-compound/tree/main/sample)\n\n## Settings\nYou can customize the generated code by defining these rules in your `build.gradle` (module level)\n```kotlin\nksp {\n    // Dao's methods will be suspended\n    arg(\"suspendDao\", \"true\")\n    // Add \"I\" as a prefix to dao's interface\n    arg(\"useDaoPrefix\", \"true\")\n    // Add \"Dao\" postfix to dao's interface\n    arg(\"useDaoPostfix\", \"true\")\n}\n```\n\u003e More settings will be supported in the future\n\n## Licence\nApache License 2.0\nA permissive license whose main conditions require preservation of copyright and license notices. Contributors provide an express grant of patent rights. Licensed works, modifications, and larger works may be distributed under different terms and without source code.\n\nMore information [here](https://github.com/tamimattafi/android-room-compound/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftamimattafi%2Fandroid-room-compound","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftamimattafi%2Fandroid-room-compound","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftamimattafi%2Fandroid-room-compound/lists"}