GithubHelp home page GithubHelp logo

ioffice / blunt Goto Github PK

View Code? Open in Web Editor NEW
5.0 16.0 2.0 26 KB

Matter-of-fact typesafe query builder for doobie

License: MIT License

Scala 100.00%
database query typesafe doobie scala cats shapeless

blunt's Introduction

blunt

blunt is a matter-of-fact query builder made with doobie. blunt provides a simple interface to map database schema to case classes, and an easy to use, typesafe way of constructing queries. You get all the principled function programming that doobie provides, with an additional layer of composability and brevity.

Example

import doobie.imports._
import blunt._

val xa = DriverManagerTransactor[IOLite](
  // Set up your doobie transactor
  // ... 
)

//Define case classes to match your schema
case class Post(id: Int, title: String, subtitle: Option[String], text: String)
case class Comment(id: Int, postId: Int, text: String, likes: Int)

// Create a post and insert into db
val qb = QueryBuilder[Post]
val post = Post(-1, "A Good Post", None, "Here's a real good post")
qb
  .insert(post, 'id) // pass in the post and the id column to exclude
  .build
  .run
  .transact(xa)
  .unsafePerformIO

val postId = // Get the post id with doobie and your database's preferred way

// Create a comment and insert into db
QueryBuilder[Comment]
  .insert(Comment(-1, postId, "This post is good", 5), 'id)
  .build.run.transact(xa)
  .unsafePerformIO

// Select the posts that have comments with 
// greater than or equal to 5 likes
qb.select
  .join[Comment]
  .on('id, 'postId)
  .project[Comment]
  .where[>=]('likes, 5)
  .build
  .nel
  .transact(xa)
  .unsafePerformIO
// res1: NonEmptyList[(Post, Comment)] = ...

Custom Table Names

Blunt will attempt to dervive a Queryable instance for you, by using a pluralized version of the case class name as the table name. If this convention doesn't work for you, or the pluralization is incorrect, you can provide your own Queryable instance:

case class Medium(id: Int, name: String)

object Medium {
  implicit val queryable = new Queryable[Medium] {
    val columns = Seq(fr"id", fr"name") // The order of these should match your case class fields
    val table = Fragment.const("Media")
  }
}

blunt's People

Contributors

emptyflash avatar kentongray avatar markrbm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blunt's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.