lemnisk8
lemnisk8
KKysely
Created by lemnisk8 on 6/13/2023 in #help
Using jsonArrayFrom with mariadb.
Thanks @koskimas!
22 replies
KKysely
Created by lemnisk8 on 6/13/2023 in #help
Using jsonArrayFrom with mariadb.
I got it to work but I don't know how 🙂 Can you please help me fix it...? I'm still trying to figure it out.
22 replies
KKysely
Created by lemnisk8 on 6/13/2023 in #help
Using jsonArrayFrom with mariadb.
https://kyse.link/?p=s&i=Y46rOpewvBx9hrsblIEz Can you please check and suggest if this is the right approach and/or any corrections/pointer to doing this in a better way?
22 replies
KKysely
Created by lemnisk8 on 6/13/2023 in #help
Using jsonArrayFrom with mariadb.
This is my attempt to typescripting this.
function jsonArrayFrom<O>(
expr: SelectQueryBuilder<any, any, O>
): SelectQueryBuilder<any, any, O> {
const selection = sql`coalesce(json_arrayagg(json_object(${sql.join(
getJsonObjectArgs(expr.toOperationNode())
)})), '[]') `;
return expr.clearSelect().select(selection);
}

function getJsonObjectArgs(node: SelectQueryNode): RawBuilder<unknown>[] {
return node.selections!.flatMap(({ selection: s }) => {
if (ReferenceNode.is(s) && ColumnNode.is(s.column)) {
return [
sql.lit(s.column.column.name),
sql.id(s.table.table.identifier.name, s.column.column.name),
];
} else if (ColumnNode.is(s)) {
return [sql.lit(s.column.name), sql.ref(s.column.name)];
} else if (AliasNode.is(s) && IdentifierNode.is(s.alias)) {
if (ReferenceNode.is(s.node) && ColumnNode.is(s.node.column)) {
return [
sql.lit(s.alias.name),
sql.id(s.node.table.table.identifier.name, s.node.column.column.name),
];
} else if (ColumnNode.is(s.node)) {
return [sql.lit(s.alias.name), sql.ref(s.node.column.name)];
} else {
return [sql.lit(s.alias.name), sql.ref(s.alias.name)];
}
} else {
throw new Error(
"MySQL jsonArrayFrom and jsonObjectFrom functions can only handle explicit selections due to limitations of the json_object function. selectAll() is not allowed in the subquery."
);
}
});
}
function jsonArrayFrom<O>(
expr: SelectQueryBuilder<any, any, O>
): SelectQueryBuilder<any, any, O> {
const selection = sql`coalesce(json_arrayagg(json_object(${sql.join(
getJsonObjectArgs(expr.toOperationNode())
)})), '[]') `;
return expr.clearSelect().select(selection);
}

function getJsonObjectArgs(node: SelectQueryNode): RawBuilder<unknown>[] {
return node.selections!.flatMap(({ selection: s }) => {
if (ReferenceNode.is(s) && ColumnNode.is(s.column)) {
return [
sql.lit(s.column.column.name),
sql.id(s.table.table.identifier.name, s.column.column.name),
];
} else if (ColumnNode.is(s)) {
return [sql.lit(s.column.name), sql.ref(s.column.name)];
} else if (AliasNode.is(s) && IdentifierNode.is(s.alias)) {
if (ReferenceNode.is(s.node) && ColumnNode.is(s.node.column)) {
return [
sql.lit(s.alias.name),
sql.id(s.node.table.table.identifier.name, s.node.column.column.name),
];
} else if (ColumnNode.is(s.node)) {
return [sql.lit(s.alias.name), sql.ref(s.node.column.name)];
} else {
return [sql.lit(s.alias.name), sql.ref(s.alias.name)];
}
} else {
throw new Error(
"MySQL jsonArrayFrom and jsonObjectFrom functions can only handle explicit selections due to limitations of the json_object function. selectAll() is not allowed in the subquery."
);
}
});
}
22 replies
KKysely
Created by lemnisk8 on 6/13/2023 in #help
Using jsonArrayFrom with mariadb.
Thanks @koskimas @Igal !!! Mariadb now has support for json_arrayagg (since v10.5.0) I am trying to recreate this SQL using jsonArrayFrom
SELECT
p.id,
(
SELECT
COALESCE(
json_arrayagg(
json_object ('pet_id', t.id, 'name', t.name)
),
'[]'
)
FROM
pet t
WHERE
t.owner_id = p.id
) AS pets
FROM
person p
SELECT
p.id,
(
SELECT
COALESCE(
json_arrayagg(
json_object ('pet_id', t.id, 'name', t.name)
),
'[]'
)
FROM
pet t
WHERE
t.owner_id = p.id
) AS pets
FROM
person p
22 replies