denodrivers / postgres

PostgreSQL driver for Deno

Home Page:https://denodrivers.github.io/postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fields are case-insensitive

cryptogohan opened this issue · comments

Didn't find an issue so I thought I'd open this one to discuss.

Using the fields prop on the queryObject method the returned fields are case-insensitive. Not only is JavaScript case sensitive, camelCase is the default. That means if I have an interface like so:

type Person = {
	firstName: string;
	age: number;
}

I can't actually use the, otherwise very useful!, fields functionality. Is there a way we could support mapping column names to object properties? It'd would save a lot of boilerplate.

Curious to hear what other think, library works great!

Hmmm perhaps good to add what I considered, something like this:

type Person = {
	firstName: string;
	age: number;
};

const result = await client.queryObject({
	text: "SELECT * FROM people",
	fields: ["first_name", "age"],
});
const people = result.rows.map(rawPerson => ({
	firstName: rawPerson.first_name,
	age: rawPerson.age,
});

Or I'm considering using this helper:

const rowToObject = <A>(row: unknown[], fields: string[]): A =>
  fields.reduce((obj, fieldName, index) => {
    (obj as Record<string, unknown>)[fieldName] = row[index];
    return obj;
  }, {} as A);

Which you could then use like so: rowToObject(row, ["firstName", "age"]). It'd be nice in my use-case to improve this situation (:

Closed by #287

@Soremwar nice to see the update! this improves things somewhat. However, the default for fields in most SQL databases is snake_case. That still means I need to write the above boilerplate to go from my database column names to JS objects.

I was more thinking of an option that says "please convert snake_case to camelCase" which is rather straightforward but does save a lot of boilerplate I imagine.

Postgres will attempt to convert to lowercase your fields when it can, however there is an easy way to work around that

SELECT MY_FIELD FROM MY_TABLE will result in { my_field: something }
SELECT "myField" FROM MY_TABLE will result in { myField: something }

It's all a matter of playing with the SQL syntax, and with this patch you can do that using the fields parameter too!

@Soremwar I understand. What I'm saying is I'd like to follow the convention on the JavaScript-side, camelCase, and I'd like to follow the convention on the SQL-side snake_case but perhaps that's not strictly for the driver to provide. Happy to use a different module.

Wait so what you want is this?

const query_result = client.queryObject<{ myField: string }>({
      text: "SELECT MY_FIELD FROM MY_TABLE",
      fields: ["myField"],
});

This will have MY_FIELD casted to myField on the master version of deno-postgres

ooooh, I must've overlooked this in the docs or code, yea that's perfect @Soremwar , thanks!

@cryptogohan https://deno-postgres.com/#/?id=object-query

A lot of people don't know about this docs, so I'm just linking them here