Skip to content

Commit

Permalink
fix: implements a underscore validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyzzor committed Dec 11, 2024
1 parent 42732f7 commit b9d5ec2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/plugin/camel-case/camel-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function createSnakeCaseMapper({
// If underScoreBeforeDigits is true then, well, insert an underscore
// before digits :). Only the first digit gets an underscore if
// there are multiple.
if (underscoreBeforeDigits && isDigit(char) && !isDigit(prevChar)) {
if (underscoreBeforeDigits && isDigit(char) && !isDigit(prevChar) && !hasUnderscore(out)) {
out += '_' + char
continue
}
Expand Down Expand Up @@ -116,6 +116,10 @@ function isDigit(char: string): boolean {
return char >= '0' && char <= '9'
}

function hasUnderscore(str: string): boolean {
return str.endsWith('_')
}

function memoize(func: StringMapper): StringMapper {
const cache = new Map<string, string>()

Expand Down
87 changes: 87 additions & 0 deletions test/node/src/camel-case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,93 @@ for (const dialect of DIALECTS) {
})
})

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
.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: [],
},
})
})

if (dialect === 'postgres' || dialect === 'mssql') {
it('should convert merge queries', async () => {
const query = camelDb
Expand Down

0 comments on commit b9d5ec2

Please sign in to comment.