Question: Using Jooq with jsonb_agg on postgreSQL without code generator?

Question

Using Jooq with jsonb_agg on postgreSQL without code generator?

Answers 1
Added at 2017-11-08 17:11
Tags
Question

I would like to use Jooq with jsonb_agg in order to create jsonb objects I can read as a stream. Is there a way to do this without having Jooq look at the database?

I know I can have Jooq look at the database and decide what aggregates it finds but is there a way to use this without going through that introspection process? I have been through the docs 4 times and don't see an answer.

Answers to

Using Jooq with jsonb_agg on postgreSQL without code generator?

nr: #1 dodano: 2017-11-09 07:11

So the answer is buried in the DSL documentation, regarding defining fields.

All of the examples before use some variant of:

create.select(field("example1"), field("example2")).from(table(a)...

However you can define your own fields or even inline function definitions.

A field definition could be something like:

Field<Object> example1 = field("example1");

Or if you want to define a type:

Field<String> example2 = field("example2", String.class);

This becomes important when you want to define more complex fields, such as:

Field<String> jsonAry = function("jsonb_agg", String.class, example2);

Jooq really shines at allowing things to be composed like this. You can then:

Fiend<String> fullJson = function("jsonb_build_object", String.class, example1, example2);

Then if you groupBy(example1) you get a nice jsonb_agg object out.

Source Show
◀ Wstecz