ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabaseโ€ข3y agoโ€ข
44 replies
Sam Hulick

Using complex JSON functions

We're using AWS Aurora + knex at the moment, and we've got some knex helpers like this:
/**
 * Takes an object and returns a Postgres json_build_object()
 * @param obj A map of any shape
 * @param alias Column alias
 * @returns json_build_object() SQL code
 */
export function jsonBuildObjectSql(
  obj: Record<string, unknown>,
  alias?: string,
  checkFieldForNull?: string
): string {
  return (
    (checkFieldForNull
      ? `case when ${checkFieldForNull} is not null then `
      : '') +
    'json_build_object(' +
    Object.entries(obj)
      .map(pair => [`'${pair[0]}'`, pair[1]])
      .join(', ') +
    ')' +
    (checkFieldForNull ? ' else null end' : '') +
    (alias ? ` as ${alias}` : '')
  );
}
/**
 * Takes an object and returns a Postgres json_build_object()
 * @param obj A map of any shape
 * @param alias Column alias
 * @returns json_build_object() SQL code
 */
export function jsonBuildObjectSql(
  obj: Record<string, unknown>,
  alias?: string,
  checkFieldForNull?: string
): string {
  return (
    (checkFieldForNull
      ? `case when ${checkFieldForNull} is not null then `
      : '') +
    'json_build_object(' +
    Object.entries(obj)
      .map(pair => [`'${pair[0]}'`, pair[1]])
      .join(', ') +
    ')' +
    (checkFieldForNull ? ' else null end' : '') +
    (alias ? ` as ${alias}` : '')
  );
}

I'd love to move to Supabase, but I'm not sure if the engine is capable of this or not (strictly speaking of
@supabase/supabase-js
@supabase/supabase-js
). I'd rather not use actual DB connections, as all of our back-end is in Lambda functions, so the API approach is much neater
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

supabase functions deploy and deno.json
SupabaseSSupabase / help-and-questions
4mo ago
JSON parsing issues with Supabase Edge Functions
SupabaseSSupabase / help-and-questions
4y ago
Filter using json isnt working
SupabaseSSupabase / help-and-questions
4y ago
Using import maps in Deno Edge Functions
SupabaseSSupabase / help-and-questions
4y ago