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

[CamelCasePlugin] underscore might added twice when underscoreBeforeDigits is set to true globally #1281

Open
bakasmarius opened this issue Nov 26, 2024 · 0 comments
Labels
bug Something isn't working built-in plugin Related to a built-in plugin

Comments

@bakasmarius
Copy link

Given:
I have a PostgreSQL database that has some columns with underscores before the digits, i.e.:
image
and I have configured Kysely DB to use CamelCasePlugin
and I have set CamelCasePlugin options to use { underscoreBeforeDigits: true } (so that working with columns that have an underscore + digit, such as address_row_1, would not cause any errors)

When:
I select from a subquery

Then:
Actual behavior: addressRow1 becomes address_row__1 in a final SQL, thus resulting in an error, because such a column does not exist (I think because createSnakeCaseMapper executes conversion twice)
Expected behavior: addressRow1 becomes address_row_1 in a final SQL resulting in no errors

Steps to reproduce:
In Kysely repository, in camel-case.test.ts, add this test, run it and see that it's failing:

camel-case.test.ts
 it('should respect underscoreBeforeDigits and not add a second underscore in a nested query', async () => {
   interface CamelTestDatabase {
     camelTest: CamelTest
   }

   interface CamelTest {
     id: Generated<number>
     firstName: string
     lastName: string
     addressRow1: string
     addressRow2: string
   }

   let dbForMigration = new Kysely<CamelTestDatabase>({
     ...ctx.config,
     plugins: [new CamelCasePlugin()],
   })

   await dbForMigration.schema.dropTable('test_camel').ifExists().execute()
   await createTableWithId(dbForMigration.schema, dialect, 'test_camel')
     .addColumn('first_name', 'varchar(255)')
     .addColumn('last_name', 'varchar(255)')
     .addColumn('address_row_1', 'varchar(255)')
     .addColumn('address_row_2', 'varchar(255)')
     .execute()

   let db = new Kysely<CamelTestDatabase>({
     ...ctx.config,
     plugins: [new CamelCasePlugin({ underscoreBeforeDigits: true })],
   })

   if (dialect === 'mssql' || dialect === 'sqlite') {
     db = db.withPlugin(new ParseJSONResultsPlugin())
   }

   const originalQuery = db.selectFrom('camelTest').select('addressRow1')
   const nestedQuery = db
     // this is a workaround, uncomment it for the test to pass:
     // .withoutPlugins()
     // .withPlugin(new CamelCasePlugin({ underscoreBeforeDigits: false }))
     .selectFrom(originalQuery.as('originalQuery'))
     .selectAll()

   testSql(originalQuery, dialect, {
     postgres: {
       sql: [`select "address_row_1" from "camel_test"`],
       parameters: [],
     },
     mysql: {
       sql: ['select `address_row_1` from `camel_test`'],
       parameters: [],
     },
     mssql: {
       sql: [`select "address_row_1" from "camel_test"`],
       parameters: [],
     },
     sqlite: {
       sql: [`select "address_row_1" from "camel_test"`],
       parameters: [],
     },
   })

   testSql(nestedQuery, dialect, {
     postgres: {
       sql: [
         `select * from (select "address_row_1" from "camel_test") as "original_query"`,
       ],
       parameters: [],
     },
     mysql: {
       sql: [
         'select * from (select `address_row_1` from `camel_test`) as `original_query`',
       ],
       parameters: [],
     },
     mssql: {
       sql: [
         `select * from (select "address_row_1" from "camel_test") as "original_query"`,
       ],
       parameters: [],
     },
     sqlite: {
       sql: [
         `select * from (select "address_row_1" from "camel_test") as "original_query"`,
       ],
       parameters: [],
     },
   })
 })

As a workaround, these lines could be uncommented to pass the test and get the desired output/behavior, but that doesn't really feel right to me because CamelCasePlugin gets instantiated several times instead of one:

     // .withoutPlugins()
     // .withPlugin(new CamelCasePlugin({ underscoreBeforeDigits: false }))

So my question is: is this really a bug(when SelectQueryBuilder selects from another SelectQueryBuilder), or the workaround I found is the actual way to go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working built-in plugin Related to a built-in plugin
Projects
None yet
Development

No branches or pull requests

3 participants