gajus / slonik-utilities

Utilities for manipulating data in PostgreSQL database using Slonik.
Other
32 stars 8 forks source link

Make snake case normalization optional or configurable #10

Open aklotos opened 3 years ago

aklotos commented 3 years ago

In my use case I have a column in a table with a perfectly valid name _id. When building and executing an UPDATE query using utility with WHERE clause with condition on _id the column name is converted to "id" after normalization. The problem comes from the lodash#snakeCase implementation used in normalization logic which removes the leading underscores in the column names.

Example:

const { update } = require('slonik-utilities');
// ...
const whereCondition = { '_test': 1 };
await update(connection, 'test_table', { test2: 42 }, whereCondition);

Expected:

UPDATE "test_table"
SET "test_2" = $1
WHERE "_test" = $2

Actual:

UPDATE "test_table"
SET "test_2" = $1
WHERE "test" = $2
nholik commented 1 year ago

Not sure if there is any appetite to allow an option for this, but I've put in a proposed PR if there is any opening to support a custom format in cases where snake_case is not, for whatever reason, being used on the target database.