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

fix: implements a underscore validation to camelcase plugin #1290

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 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) && !out.endsWith('_')) {
out += '_' + char
continue
}
Expand Down
70 changes: 70 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,72 @@ for (const dialect of DIALECTS) {
})
})

it('should respect underscoreBeforeDigits and not add a second underscore in a nested query', async () => {
let db = camelDb.withoutPlugins()

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

db = db.withPlugin(
new CamelCasePlugin({
underscoreBeforeDigits: true,
}),
)

const originalQuery = db.selectFrom('camelPerson').select('addressRow1')

const nestedQuery = db
.selectFrom(originalQuery.as('originalQuery'))
.selectAll()

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

testSql(nestedQuery, 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