Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camel case to snake case name conversion #204

Open
joshiain opened this issue Aug 9, 2021 · 12 comments
Open

Camel case to snake case name conversion #204

joshiain opened this issue Aug 9, 2021 · 12 comments
Labels
enhancement New feature or request postgrest-patch-needed Requires a change on PostgREST

Comments

@joshiain
Copy link

joshiain commented Aug 9, 2021

Feature request

Is your feature request related to a problem? Please describe.

postgres column naming conventions suggest using snake_case. Javascript variable naming conventions typically use camelCase. This leads to a few issues when trying to interact with the postgres database using the postgrest-js api.

I either have to use snake_case for my object keys in my javascript code, but this is then inconsistent with the rest of my code.

I could also use camelCase for my postgres column names, but then this creates a lot of friction with queries as casing is ignored without "".

Or I could automatically convert the keys from camelCase to snake_case before sending any data, and from snake_case to camelCase when receiving data.

Describe the solution you'd like

A config setting, or option that will automatically convert object keys from camelCase to snake_case when sending data.

Having a table with columns: user_name and date_added. The following would work:

const data = [{userName: "abc123", dateAdded: "2021-01-01"}]
await postgrest.select('table').insert(data)

And similarly when querying for data, the returned object would have the column names converted from snake_case to camelCase.

Describe alternatives you've considered

pg-promise, knex.js, and objection.js provide similar utils to help with this issue.

Additional context

Add any other context or screenshots about the feature request here.

@wiesson
Copy link

wiesson commented Jan 18, 2022

This would be soooo helpful :)

@steve-chavez
Copy link
Member

Renaming columns on reads is possible, but not on writes(PostgREST/postgrest#1773) - once the latter is solved postgrest-js could autogenerate aliases to solve this.

@steve-chavez steve-chavez added the postgrest-patch-needed Requires a change on PostgREST label Feb 7, 2022
@wiesson
Copy link

wiesson commented Feb 7, 2022

I ended up with converting my db to the camel case format and accepted that I have to live with quotes forever

@steve-chavez
Copy link
Member

Another option for this: supabase/supabase#7136

@irohitb
Copy link

irohitb commented Sep 20, 2023

If this helps anyone, I have been using ts-case-convert and it works like magic.

@devhandler
Copy link

devhandler commented Oct 24, 2023

it would be so much easier if postgres can simply ignore casing without quotes, treating select camelcasecol from tbl the same as select camelCaseCol from tbl. then everything is solved.

not sure why do they still throw errors when you reference camelCase col in all lower case letters. the reverse actually works. you can reference lower case letters of column names by using uppercase letters

@weeksie
Copy link

weeksie commented Nov 16, 2023

This is extremely frustrating. It's a very simple move to add a conversion. Knex does it without sweating. The "accepted" option is basically to break encapsulation so that you can tell you have a data object b/c its properties are in snake case. I appreciate "opinionated" frameworks as much as the next guy but a typescript/javascript framework (of all things) should have this affordance.

@wiesson
Copy link

wiesson commented Nov 16, 2023

Offtopic: I'm also hoping that at some point, it will be added to postgrest. It feels like a feature were I would apply for a job, fix that one thing and leave 🙈

@alvgaona
Copy link

+1

@isaachinman
Copy link

Can anyone here elaborate as to how you are using ts-case-convert to automatically wrap methods?

@joryphillips
Copy link

joryphillips commented Mar 13, 2024

@isaachinman you probably figured this out a while ago, but this is more or less it. You'd want to do the reverse for write operations.

import { objectToCamel } from "ts-case-convert";

export async function getThingById(id: string) {
  const tableQuery = supabase
    .from("my_cool_table")
    .select("*")
    .eq("id", id);

  const { data, error } = await tableQuery;
  if (error) throw error;

  return objectToCamel(data);
}

@veloware
Copy link

veloware commented Sep 13, 2024

If this helps anyone, I have been using ts-case-convert and it works like magic.

Ive gave this a go, and it works, but I'm curious if you ran into any complexities regarding inferring the TS types? as in -

normally you could do -

const myQuery = supabase.from("xxx")...

type MyQueryResponse = QueryData<typeof myQuery>;

and then MyQueryResponse would have all of the snake_case properties, but im curious if you know how to infer types after the objectToCamel conversion has happened?

EDIT

I've quickly found the answer, in ts-case-convert there is a already types for that, e.g. -

type MyQueryResponse = ObjectToCamel<QueryData<typeof myQuery>>;

MyQueryResponse now has camelCase property names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request postgrest-patch-needed Requires a change on PostgREST
Projects
None yet
Development

No branches or pull requests

10 participants