{"id":19422541,"url":"https://github.com/scalalandio/ocdquery","last_synced_at":"2025-04-24T15:32:20.997Z","repository":{"id":135126875,"uuid":"197773915","full_name":"scalalandio/ocdquery","owner":"scalalandio","description":"Over-Complicated Database Query using higher-kinded data","archived":false,"fork":false,"pushed_at":"2020-10-30T15:31:54.000Z","size":311,"stargazers_count":35,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T07:12:51.682Z","etag":null,"topics":["crud","database","doobie","higher-kinded-types","scala"],"latest_commit_sha":null,"homepage":"https://scalalandio.github.io/ocdquery/","language":"Scala","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/scalalandio.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-19T13:03:37.000Z","updated_at":"2024-10-28T16:35:22.000Z","dependencies_parsed_at":"2023-03-15T14:45:12.226Z","dependency_job_id":null,"html_url":"https://github.com/scalalandio/ocdquery","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalalandio%2Focdquery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalalandio%2Focdquery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalalandio%2Focdquery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scalalandio%2Focdquery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scalalandio","download_url":"https://codeload.github.com/scalalandio/ocdquery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250654473,"owners_count":21465887,"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":["crud","database","doobie","higher-kinded-types","scala"],"created_at":"2024-11-10T13:34:07.001Z","updated_at":"2025-04-24T15:32:20.989Z","avatar_url":"https://github.com/scalalandio.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Over-Complicated Database Query (OCD Query)\n\n[![Build Status](https://travis-ci.org/scalalandio/ocdquery.svg?branch=master)](https://travis-ci.org/scalalandio/ocdquery)\n[![Maven Central](https://img.shields.io/maven-central/v/io.scalaland/ocdquery-core_2.12.svg)](http://search.maven.org/#search%7Cga%7C1%7Cocdquery)\n[![License](http://img.shields.io/:license-Apache%202-green.svg)](http://www.apache.org/licenses/LICENSE-2.0.txt)\n\n[Doobie](https://github.com/tpolecat/doobie/) queries generated using higher-kinded data.\n\nSee [complete documentation at GH pages](https://scalalandio.github.io/ocdquery/)\n\n## Instalation\n\n1. add in your sbt:\n```scala\nlibraryDependencies += \"io.scalaland\" %% \"ocdquery-core\" % \"0.5.0\"\n```\n(and maybe some optics library like [Quicklens](https://github.com/softwaremill/quicklens)\nor [Monocle](https://github.com/julien-truffaut/Monocle))\n\n2. create higher-kinded data representation:\n\n```scala\nimport java.time.LocalDate\nimport java.util.UUID\n\nfinal case class TicketF[F[_], C[_]](\n  id:      C[UUID],\n  name:    F[String],\n  surname: F[String],\n  from:    F[String],\n  to:      F[String],\n  date:    F[LocalDate]\n)\n```\n\n3. create a repository:\n\n```scala\nimport cats.Id\nimport com.softwaremill.quicklens._\nimport doobie.h2.implicits._\nimport io.scalaland.ocdquery._\n\n// only have to do it once!\nval TicketRepo: Repo.EntityRepo[TicketF] = {\n  // I have no idea why shapeless cannot find this Generic on its own :/\n  // if you figure it out, please PR!!!\n  implicit val ticketRead: doobie.Read[Repo.ForEntity[TicketF]#Entity] =\n    QuasiAuto.read(shapeless.Generic[TicketF[Id, Id]])\n    \n  Repo.forEntity[TicketF](\n    \"tickets\".tableName,\n    // I suggest using quicklens or monocle's extension methods\n    // as they are more reliable than .copy\n    DefaultColumnNames.forEntity[TicketF].modify(_.from).setTo(\"from_\".columnName)\n  )\n}\n```\n\n4. generate queries\n\n```scala\n// build these in you services with type safety!\n\nTicketRepo.insert(\n  // no need to pass \"empty\" fields like \"id = Unit\"!\n  Create.fromTuple((\"John\", \"Smith\", \"London\", \"Paris\", LocalDate.now))\n).run\n\nimport io.scalaland.ocdquery.sql._ // common filter syntax like `=`, `\u003c\u003e`\n\nTicketRepo.update.withFilter { columns =\u003e\n  (columns.name `=` \"John\") and (columns.surname `=` \"Smith\")\n}(\n  TicketRepo.emptyUpdate.modify(_.data).setTo(LocalDate.now)\n).run\n\nTicketRepo.fetch.withSort(_.name, Sort.Ascending).withLimit(5) {\n _.from `=` \"London\"\n}.to[List]\n\nTicketRepo.delete(_.id `=` deletedId).run\n```\n\n5. perform even joins returning tuples of entities:\n\n```scala\nval joiner = TicketRepo\n  .join(TicketRepo).on(_._1.id, _._2.id) // after .join() we have a tuple!\n  .join(TicketRepo).on(_._2.id, _._3.id) // and now triple!\n  .fetch.withSort(_._1.name, Sort.Ascending).withLimit(5) { columns =\u003e\n    columns._1.name `=` \"John\"\n  }.to[List] // ConnectionIO[(Entity, Entity, Entity)]\n```\n\n## Limitations\n\n* Library assumes that `EntityF` is flat, and automatic generation of Doobie queries is done in a way which doesn't\n  allow you to use JOINs, nested SELECTs etc. If you need them you can use utilities from `RepoMeta` to write your own\n  query, while delegating some of the work to `RepoMeta` (see how `Repo` does it!). \n* Using `EntityF` everywhere is definitely not convenient. Also it doesn't let you\n  define default values like e.g. `None`/`Skipped` for optional fields. So use them\n  internally, as entities to work with your database and separate them from\n  entities exposed in your API/published language. You can use [chimney](https://github.com/scalalandio/chimney)\n  for turning public instances to and from internal instances,\n* types sometimes confuse compiler, so while it can derive something like `shapeless.Generic[TicketF[Id, Id]]`,\n  it has issues finding `Generic.Aux`, so Doobie sometimes get's confused - `QuasiAuto` let you provide\n  the right values explicitly, so that the derivation is not blocked by such silly issue. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscalalandio%2Focdquery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscalalandio%2Focdquery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscalalandio%2Focdquery/lists"}