© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
30 replies
rphlmr ⚡

Custom SQL function (json_agg & json_build_object)

Hello there.

I'm new to Drizzle and I love it 😍.

I have successfully made a
jsonAgg
jsonAgg
helper.

function jsonAgg<T extends PgTable<TableConfig>>(table: T) {
  return sql<InferModel<T>[]>`json_agg(${table})`;
}
function jsonAgg<T extends PgTable<TableConfig>>(table: T) {
  return sql<InferModel<T>[]>`json_agg(${table})`;
}


Now I have trouble to make a
jsonAggBuildObject
jsonAggBuildObject
😅 that should produce something like this:

sql`json_agg(json_build_object('key1', ${table.col1}, 'key2', ${table.col2}, ...))`
sql`json_agg(json_build_object('key1', ${table.col1}, 'key2', ${table.col2}, ...))`


I try something but it end with an error
error: could not determine data type of parameter $1
error: could not determine data type of parameter $1


function jsonAggBuildObject<T extends Record<string, AnyColumn>>(shape: T) {
  const shapeString = Object.entries(shape)
    .map(([key, value]) => {
      return `'${key}', ${value}`;
    })
    .join(",");

  return sql<
    InferColumnsDataTypes<T>[]
  >`json_agg(json_build_object(${shapeString}))`;
}
function jsonAggBuildObject<T extends Record<string, AnyColumn>>(shape: T) {
  const shapeString = Object.entries(shape)
    .map(([key, value]) => {
      return `'${key}', ${value}`;
    })
    .join(",");

  return sql<
    InferColumnsDataTypes<T>[]
  >`json_agg(json_build_object(${shapeString}))`;
}


Any idea ?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Custom postgres JSON_AGG function help
Drizzle TeamDTDrizzle Team / help
15mo ago
limiting json_agg
Drizzle TeamDTDrizzle Team / help
2y ago
Postgres json_agg
Drizzle TeamDTDrizzle Team / help
3y ago
Using json_agg does not format correctly
Drizzle TeamDTDrizzle Team / help
3y ago