Chris Travers
Chris Travers

Reputation: 26464

Using Jooq with jsonb_agg on postgreSQL without code generator?

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.

Upvotes: 1

Views: 782

Answers (1)

Chris Travers
Chris Travers

Reputation: 26464

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.

Upvotes: 3

Related Questions