Scala Slick Strategies For Composing Compiled Queries of various shapes -
i using postgresql 9.6, slick 3.2 , have slick-pg wired up.
i'm implementing graphql backend in scala , need implement queries resolve graphql cursors of various shapes. shapes of input enumerable. of inputs influencing query be:
- a user selectable ordering.
- various filtering criteria (full-text-search, date-ranges etc).
- selecting between backward or forward traversal.
my current simple cursors have backward , forward hardcoded compiled queries. , both have same shape , using standard slick abstractions factor out commonality reuse (value classes ops, inheritance hierarchy "entity" tables , code generation). there still lot of boilerplate , won't scale more dynamic shapes unless list them out.
here excerpt class uses pattern:
private [shard] class schemaalltypecursor(shard: slickshard) extends cursorspec[typemodel] { import shard.postgresprofile.api._ private val forwardqc = compiled { (tenantidurls: rep[list[string]], low: rep[long], take: constcolumn[long]) => queries.listtypesbytenantids(tenantidurls).forwardcursorbyid(low, take) } private val backwardqc = compiled { (tenantidurls: rep[list[string]], high: rep[long], take: constcolumn[long]) => queries.listtypesbytenantids(tenantidurls).backwardcursorbyid(high, take) } def executor(tenantidurls: list[string])(c: cursorrequest)(implicit req: req[_]): observable[typemodel] = { implicit val ec = req.ctx.ec observable fromfuture { shard.db.run { if (c.forward) forwardqc(tenantidurls, c.base, c.take).result else backwardqc(tenantidurls, c.base, c.take).result } flatmap materializealltypes(req.ctx, tenantidurls) } flatmap observable.fromiterable } }
the overall question how compiled queries use-case without mountains of boilerplate ? insights i'd gain is:
- what abstractions have composing parts of queries ? in example above
backwardcursorbyid
,forwardcursorbyid
parts of query. i'm hoping able build compiled functions parts , cache them somehow.... - how use record type in compiled functions ? (it hinted in slick documentation possible, can't find examples).
- is there way caching compiled queries without listing them out ? -- afaik. if compile function in executor function above repeatedly compiled on each function invocation of executor function.
i open using postgres specific types supported slick-pg .
Comments
Post a Comment