json_arrayagg(json_array ...

db.query.team.findFirst({
where: eq(t.slug, params.slug),
with: {
members: true,
},
});
db.query.team.findFirst({
where: eq(t.slug, params.slug),
with: {
members: true,
},
});
this SQL is
{
sql: 'select `id`, `slug`, `name`, `subtitle`, `avatar_url`, `created_at`, `updated_at`, coalesce((select json_arrayagg(json_array(`id`, `team_id`, `user_id`, `role`, `created_at`, `updated_at`)) from `team_member` `team_members` where `team_members`.`team_id` = `team`.`id`), json_array()) as `members` from `team` where `team`.`slug` = ? limit ?',
params: [ 'melodyclue-team', 1 ]
}
{
sql: 'select `id`, `slug`, `name`, `subtitle`, `avatar_url`, `created_at`, `updated_at`, coalesce((select json_arrayagg(json_array(`id`, `team_id`, `user_id`, `role`, `created_at`, `updated_at`)) from `team_member` `team_members` where `team_members`.`team_id` = `team`.`id`), json_array()) as `members` from `team` where `team`.`slug` = ? limit ?',
params: [ 'melodyclue-team', 1 ]
}
but
const sq = db
.select({
members: sql`json_arrayagg(json_array(${teamMember.id},${teamMember.userId},${teamMember.role},${teamMember.createdAt},${teamMember.updatedAt}))`,
})
.from(teamMember)
.where(eq(teamMember.teamId, t.id));

const data = await db
.select({
id: t.id,
name: t.name,
subtitle: t.subtitle,
slug: t.slug,
avatarUrl: t.avatarUrl,
createdAt: t.createdAt,
updatedAt: t.updatedAt,
members: sql`coalesce(${sq}, json_array())`.as("members"),
})
.from(t)
.where(eq(t.slug, params.slug));
const sq = db
.select({
members: sql`json_arrayagg(json_array(${teamMember.id},${teamMember.userId},${teamMember.role},${teamMember.createdAt},${teamMember.updatedAt}))`,
})
.from(teamMember)
.where(eq(teamMember.teamId, t.id));

const data = await db
.select({
id: t.id,
name: t.name,
subtitle: t.subtitle,
slug: t.slug,
avatarUrl: t.avatarUrl,
createdAt: t.createdAt,
updatedAt: t.updatedAt,
members: sql`coalesce(${sq}, json_array())`.as("members"),
})
.from(t)
.where(eq(t.slug, params.slug));
this is not one I want. Is this drizzle-related issue?
1 Reply
melodyclue
melodyclueOP11mo ago
I think json_object is right one, not json_array
Want results from more Discord servers?
Add your server