K
Kysely11mo ago
elitan

camelCase plugin with a strangely named column

We have a column named cl_companyID (I'm as confused as you) and I have issues with the camelCase plugin because it thinks the name is cl_company_id. Is there a way around this, without renaming the column? I was thinking maybe we could add support with some kind of lookup table with weird names for the plugin to respect.
Solution:
Something like ```ts class MyCamelCasePlugin extends CamelCasePlugin { protected override snakeCase(str: string): string {...
Jump to solution
7 Replies
koskimas
koskimas11mo ago
You can extends the plugin and override the snakeCase and camelCase methods.
elitan
elitan11mo ago
We also have one: cl_LID_c 🥲 "I like snake case but I also like pascal case but also snake case is good"
Solution
koskimas
koskimas11mo ago
Something like
class MyCamelCasePlugin extends CamelCasePlugin {
protected override snakeCase(str: string): string {
if (snakeLookup[str]) {
return snakeLookup[str]
}

return super.snakeCase(str)
}

protected override camelCase(str: string): string {
if (camelLookup[str]) {
return camelLookup[str]
}

return super.camelCase(str)
}
}
class MyCamelCasePlugin extends CamelCasePlugin {
protected override snakeCase(str: string): string {
if (snakeLookup[str]) {
return snakeLookup[str]
}

return super.snakeCase(str)
}

protected override camelCase(str: string): string {
if (camelLookup[str]) {
return camelLookup[str]
}

return super.camelCase(str)
}
}
elitan
elitan11mo ago
Thanks, very helpful! and I only have information about the column name here? not the table
koskimas
koskimas11mo ago
Yeah. Figuring out the table is super hard/impossible
elitan
elitan11mo ago
Wouldn't be surprised if I found the same column named in different variations. Like company_id and company_ID for different tables ok hmm Maybe I should first make sure we don't have that issue Thank you @koskimas Keep up the great work with Kysely ❤️
elitan
elitan11mo ago
got it working btw 🚀
// convert wierd column names
const snakeLookup = {
clCompanyID: 'cl_companyID',
} as Record<string, string>;

class CustomCamelCasePlugin extends CamelCasePlugin {
protected override snakeCase(str: string): string {
if (snakeLookup[str]) {
return snakeLookup[str];
}

return super.snakeCase(str);
}
}
// convert wierd column names
const snakeLookup = {
clCompanyID: 'cl_companyID',
} as Record<string, string>;

class CustomCamelCasePlugin extends CamelCasePlugin {
protected override snakeCase(str: string): string {
if (snakeLookup[str]) {
return snakeLookup[str];
}

return super.snakeCase(str);
}
}
that's it
Want results from more Discord servers?
Add your server
More Posts