Skip to content

Reverse transform of column names when inserting or updating #16

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

Closed
davojan opened this issue Jan 17, 2020 · 9 comments
Closed

Reverse transform of column names when inserting or updating #16

davojan opened this issue Jan 17, 2020 · 9 comments
Labels
enhancement New feature or request

Comments

@davojan
Copy link

davojan commented Jan 17, 2020

Transforming column names in both ways would greatly improved ergonomics of the library. Consider this example:

const sql = postgres({
  transform: {
    column: postgres.toCamel,
  },
}) 
const entity = {
  appliedAt: new Date,  // db column name is "applied_at"
}
// error: column "appliedat" of relation "some_table" does not exist
sql`INSERT INTO some_table ${sql(entity)}`

Transform option should work both ways, I think. May be configured like this:

{
  transform: {
    column: {
      fromDb: postgres.toCamel,
      toDb: postgres.toSnake,
    },
  },
}
@davojan davojan changed the title Reverse transform of column names where inserting or updating Reverse transform of column names when inserting or updating Jan 17, 2020
@porsager
Copy link
Owner

Hi @davojan ..

Yes I have considered an option for that.. Good to track in this issue - thanks ;)

@porsager
Copy link
Owner

I like the non breaking way you suggested of supplying an object with { to, from }

@porsager porsager added the enhancement New feature or request label Jan 17, 2020
@michael-land
Copy link

It would be useful!

@michael-land
Copy link

michael-land commented Jul 3, 2020

Should we also transform column name for sql(obj, ...columns_names) ?

user can use sql(customer, 'firstName') instead of sql(customer, 'first_name')

it also useful for typescript I guess, we can restrict column name to keyof T

@iby
Copy link
Contributor

iby commented Jun 29, 2021

@porsager Hey! Need this one badly. Has anyone worked on this? Are you open towards the PR? P.S. Also love the idea with { to, from }.

@basaran
Copy link

basaran commented Aug 13, 2021

this could be extremely useful.

@porsager
Copy link
Owner

@iby and @basaran Would you mind trying out how #212 works for you?

For eg. camel casing use:

const sql = postgres({
  transform: {
    column: { to: postgres.fromCamel, from: postgres.toCamel }
  }
})

@basaran
Copy link

basaran commented Aug 13, 2021

Hello,

Thank you, I think it's fine:

CREATE TABLE IF NOT EXISTS public."camelTable"
(
    snake_to_camel text COLLATE pg_catalog."default"
)
async function dooo() {
    const importantInformation = {
        snakeToCamel: "aaaa",
    };

    const importantColumns = ["snakeToCamel"];

    const a_insert = await sql`
      INSERT INTO "camelTable" ${sql(importantInformation)}
    `;

    const b_select = await sql`
      SELECT ${sql(importantColumns)} FROM "camelTable" 
    `;

    console.log(a_insert);
    console.log(b_select);
}
INSERT INTO "camelTable" ("snake_to_camel") values ($1)
[{"type":0,"value":"aaaa","raw":"aaaa"}]
SELECT "snake_to_camel" FROM "camelTable"
[
  count: 1,
  command: 'INSERT',
  state: { status: 'I', pid: 1634864, secret: 1829386482 }
]
[
  { snakeToCamel: 'aaaa' },
  { snakeToCamel: 'aaaa' },
  columns: [ { name: 'snakeToCamel', parser: undefined, type: 25 } ],
]

@porsager
Copy link
Owner

Fixed with #212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants