kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
9.8k stars 250 forks source link

[FEATURE REQUEST]: Camel case plugin - option to disable transformations on selected columns or data types #961

Open snax4a opened 2 months ago

snax4a commented 2 months ago

Hello,

The CamelCasePlugin options allow disabling conversion to camel case on all nested objects https://kysely-org.github.io/kysely-apidoc/interfaces/CamelCasePluginOptions.html#maintainNestedObjectKeys, however i don't find this quite useful as for example we might want to keep conversion on nested objects e.q. included by jsonArrayFrom, jsonObjectFrom helpers but we might not want to convert some jsonb column data.

For example we could have a custom_fields jsonb column where object keys are actual values (in this case, related entity ids)

{
   "field_9npl6g7wqazcdgjz":{
      "name":"Name 1",
      "type":"TYPE_1",
   },
   "field_mvj5nwggpmyxwbj6":{
      "name":"Name 2",
      "type":"TYPE_2"
   }
}

I would want to be able to disable conversion for this selected column or data type e.q. jsonb either on the pluguin level, or ideally on the query level if it would be possible.

new CamelCasePlugin({ skipDataTypes: ["json", "jsonb"] })
// or
new CamelCasePlugin({ skipColumns: ["customFields"] })
db.selectFrom("Table")
  .select(["col1", "col2"])
  .skipCamelCase(["col2"])
  .executeTakeFirst();
koskimas commented 2 months ago

Kysely doesn't know about data types. We don't know something's json or jsonb. Skipping by column name would be possible, but only if you don't alias the column.