All Projects → scalalandio → ocdquery

scalalandio / ocdquery

Licence: Apache-2.0 license
Over-Complicated Database Query using higher-kinded data

Programming Languages

scala
5932 projects
shell
77523 projects

Projects that are alternatives of or similar to ocdquery

react-native-firebase-firestore-crud-example
React Native Firebase Tutorial: Build CRUD Firestore App
Stars: ✭ 24 (-29.41%)
Mutual labels:  crud
spring-boot-mongodb-react-java-crud
Spring Boot, MongoDB and React.js CRUD Java Web Application Example
Stars: ✭ 33 (-2.94%)
Mutual labels:  crud
mvc
PHP MVC boilerplate with user authentication, basic security and MySQL CRUD operations.
Stars: ✭ 28 (-17.65%)
Mutual labels:  crud
boiler-plate-commands
Package to generate automatic cruds for Laravel BoilerPlate Apps
Stars: ✭ 13 (-61.76%)
Mutual labels:  crud
SimplePHP
A small query builder project designed to assist daily routines and speed up the process of communicating with the database.
Stars: ✭ 14 (-58.82%)
Mutual labels:  crud
nine-cards-backend
An Open Source Android Launcher built with Scala on Android
Stars: ✭ 61 (+79.41%)
Mutual labels:  doobie
okta-spring-boot-react-crud-example
Simple CRUD with React and Spring Boot 2.0
Stars: ✭ 214 (+529.41%)
Mutual labels:  crud
C-Sharp-Learning-Journey
Some of the projects i made when starting to learn c#, winfroms and wpf
Stars: ✭ 95 (+179.41%)
Mutual labels:  crud
dapper-repositories
CRUD for Dapper
Stars: ✭ 523 (+1438.24%)
Mutual labels:  crud
laravel-realworld-example-app
Exemplary RealWorld backend API built with Laravel PHP framework.
Stars: ✭ 34 (+0%)
Mutual labels:  crud
now-cms
No description or website provided.
Stars: ✭ 15 (-55.88%)
Mutual labels:  crud
crudlfap
MIRROR of yourlabs.io/oss/crudlfap
Stars: ✭ 20 (-41.18%)
Mutual labels:  crud
rust-realworld-example-app
Example Rust codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld API spec.
Stars: ✭ 19 (-44.12%)
Mutual labels:  crud
ionic4-angular8-crud-mobileapps-example
Ionic 4 Angular 8 Tutorial: Learn to Build CRUD Mobile Apps
Stars: ✭ 20 (-41.18%)
Mutual labels:  crud
azure-cosmos-dotnet-repository
Wraps the .NET SDK for Azure Cosmos DB abstracting away the complexity, exposing a simple CRUD-based repository pattern
Stars: ✭ 190 (+458.82%)
Mutual labels:  crud
ios-grpc-note-crud-app
Swift iOS gRPC Client Note Taking App
Stars: ✭ 16 (-52.94%)
Mutual labels:  crud
mongo-crud
CRUD API built with MongoDB and Express
Stars: ✭ 18 (-47.06%)
Mutual labels:  crud
vue-crud-ui
Single file Vue.js script that adds a UI to the PHP-CRUD-API project
Stars: ✭ 47 (+38.24%)
Mutual labels:  crud
NodeExpressCRUD
Node, Express, Mongoose and MongoDB CRUD Web Application
Stars: ✭ 45 (+32.35%)
Mutual labels:  crud
todo-list
A practical web application built with Node.js, Express, and MySQL for you to readily record, view, and manage your tasks with an account: Create, view, edit, delete, filter, and sort expenses are as easy as pie 🥧
Stars: ✭ 18 (-47.06%)
Mutual labels:  crud

Over-Complicated Database Query (OCD Query)

Build Status Maven Central License

Doobie queries generated using higher-kinded data.

See complete documentation at GH pages

Instalation

  1. add in your sbt:
libraryDependencies += "io.scalaland" %% "ocdquery-core" % "0.5.0"

(and maybe some optics library like Quicklens or Monocle)

  1. create higher-kinded data representation:
import java.time.LocalDate
import java.util.UUID

final case class TicketF[F[_], C[_]](
  id:      C[UUID],
  name:    F[String],
  surname: F[String],
  from:    F[String],
  to:      F[String],
  date:    F[LocalDate]
)
  1. create a repository:
import cats.Id
import com.softwaremill.quicklens._
import doobie.h2.implicits._
import io.scalaland.ocdquery._

// only have to do it once!
val TicketRepo: Repo.EntityRepo[TicketF] = {
  // I have no idea why shapeless cannot find this Generic on its own :/
  // if you figure it out, please PR!!!
  implicit val ticketRead: doobie.Read[Repo.ForEntity[TicketF]#Entity] =
    QuasiAuto.read(shapeless.Generic[TicketF[Id, Id]])
    
  Repo.forEntity[TicketF](
    "tickets".tableName,
    // I suggest using quicklens or monocle's extension methods
    // as they are more reliable than .copy
    DefaultColumnNames.forEntity[TicketF].modify(_.from).setTo("from_".columnName)
  )
}
  1. generate queries
// build these in you services with type safety!

TicketRepo.insert(
  // no need to pass "empty" fields like "id = Unit"!
  Create.fromTuple(("John", "Smith", "London", "Paris", LocalDate.now))
).run

import io.scalaland.ocdquery.sql._ // common filter syntax like `=`, `<>`

TicketRepo.update.withFilter { columns =>
  (columns.name `=` "John") and (columns.surname `=` "Smith")
}(
  TicketRepo.emptyUpdate.modify(_.data).setTo(LocalDate.now)
).run

TicketRepo.fetch.withSort(_.name, Sort.Ascending).withLimit(5) {
 _.from `=` "London"
}.to[List]

TicketRepo.delete(_.id `=` deletedId).run
  1. perform even joins returning tuples of entities:
val joiner = TicketRepo
  .join(TicketRepo).on(_._1.id, _._2.id) // after .join() we have a tuple!
  .join(TicketRepo).on(_._2.id, _._3.id) // and now triple!
  .fetch.withSort(_._1.name, Sort.Ascending).withLimit(5) { columns =>
    columns._1.name `=` "John"
  }.to[List] // ConnectionIO[(Entity, Entity, Entity)]

Limitations

  • Library assumes that EntityF is flat, and automatic generation of Doobie queries is done in a way which doesn't allow you to use JOINs, nested SELECTs etc. If you need them you can use utilities from RepoMeta to write your own query, while delegating some of the work to RepoMeta (see how Repo does it!).
  • Using EntityF everywhere is definitely not convenient. Also it doesn't let you define default values like e.g. None/Skipped for optional fields. So use them internally, as entities to work with your database and separate them from entities exposed in your API/published language. You can use chimney for turning public instances to and from internal instances,
  • types sometimes confuse compiler, so while it can derive something like shapeless.Generic[TicketF[Id, Id]], it has issues finding Generic.Aux, so Doobie sometimes get's confused - QuasiAuto let you provide the right values explicitly, so that the derivation is not blocked by such silly issue.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].