Skip to content

Commit

Permalink
fix: implements a underscore validation to camelcase plugin (#1290)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Vieira  <[email protected]>
Co-authored-by: Igal Klebanov <[email protected]>
  • Loading branch information
3 people authored Jan 4, 2025
1 parent 541c935 commit 383e1a5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
7 changes: 6 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,12 @@ 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) &&
!out.endsWith('_')
) {
out += '_' + char
continue
}
Expand Down
46 changes: 46 additions & 0 deletions test/node/src/camel-case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ for (const dialect of DIALECTS) {
preferences: {
disable_emails: boolean
}
addressRow1: string
}

interface CamelDatabase {
Expand All @@ -52,6 +53,7 @@ for (const dialect of DIALECTS) {
'preferences',
dialect === 'mssql' ? 'varchar(8000)' : 'json',
)
.addColumn('addressRow1', 'varchar(255)')
.execute()
})

Expand All @@ -63,11 +65,13 @@ for (const dialect of DIALECTS) {
firstName: 'Jennifer',
lastName: 'Aniston',
preferences: json({ disable_emails: true }),
addressRow1: '123 Main St',
},
{
firstName: 'Arnold',
lastName: 'Schwarzenegger',
preferences: json({ disable_emails: true }),
addressRow1: '123 Main St',
},
])
.execute()
Expand Down Expand Up @@ -309,6 +313,48 @@ for (const dialect of DIALECTS) {
})
})

it.only('should respect `underscoreBeforeDigits` and not add a second underscore in a nested query', async () => {
const db = camelDb
.withoutPlugins()
.withPlugin(new CamelCasePlugin({ underscoreBeforeDigits: true }))

const query = db
.selectFrom(
db
.selectFrom('camelPerson')
.select('addressRow1')
.as('originalQuery'),
)
.selectAll()

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

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

0 comments on commit 383e1a5

Please sign in to comment.