jsonGroupObject function

Expression<String> jsonGroupObject(
  1. Map<Expression<String>, Expression<Object>> values
)

Returns a JSON object consisting of the keys and values from the provided values map.

As an example, consider this example schema to store emails:

class Emails extends Table {
  TextColumn get subject => text()();
  TextColumn get body => text()();
  IntColumn get folder => integer().references(Folders, #id)();
}

class Folders extends Table {
  IntColumn get id => integer()();
  TextColumn get title => text()();
}

Now, say you wanted to write a query finding the subject and body of every email in every folder. The resulting value might look like this:

 {
   "Group array example": "Hey there, aren't you aware that email is dead?",
   "Re: Group array example": "It's just an example okay?"
 }

Again, the starting point is formed by a query joining the tables:

final query = select(folders)
  .join([innerJoin(emails, emails.folder.equalsExp(folders.id))]);

Now, a group by clause and jsonGroupObject can be used to collapse all joined rows from the emails table into a single value:

final subjectAndBody = jsonGroupObject({emails.subject: emails.body});
query
  ..groupBy([folders.id])
  ..addColumns([subjectAndBody]);

Running this query would return one row for each folder, where row.read(subjectAndBody) is a textual JSON representation of a Map<String, String>.

Implementation

Expression<String> jsonGroupObject(Map<Expression<String>, Expression> values) {
  return FunctionCallExpression('json_group_object', _groupObjectArgs(values));
}