Giter Club home page Giter Club logo

Comments (9)

koskimas avatar koskimas commented on May 11, 2024

It's not possible to make that work.

from kysely.

michael-land avatar michael-land commented on May 11, 2024

this is a bug, idk why it got closed.

from kysely.

michael-land avatar michael-land commented on May 11, 2024
create table company
(
    id               integer generated by default as identity
        constraint company_pk
            primary key,
    amount_spend_12m int
); 

you can try this ddl with camelcase plugin

There are two way to type this table.

interface Company {
  amountSpend12m: string
}

const rows = db
.selectFrom('company')
.select('amountSpend12m')
.compile()

// compiles to `select "amount_spend12m" from "company"`;
// this query produce error because amount_spend12m is not defined 
interface Company {
  amountSpend_12m: string
}
const rows = db
.selectFrom('company')
.select('amountSpend_12m')
.compile()

// compiles to `select "amount_spend_12m" from "company"`;
// the result actual shape is { amountSpend12m: number } but kysely infer it as { amountSpend_12m: number } 

How should we type such table? I already rename the column to something else without numbers, but this should be a bug in my opinion

from kysely.

koskimas avatar koskimas commented on May 11, 2024

The camel case plugin simply runs all identifiers (column names, table names etc.) through a toSnakeCase function and then the returned column names through a toCamelCase function. When kysely receives a column named amount_spend_12m from the db, how could it possibly know that it's supposed to transform it to a mixture of cameCase and snake_case amountSpend_12m?

We don't actually know which selects map to which returned columns. Figuring that out would be super difficult.

You can override the CamelCasePlugin:

 class MyCamelCasePlugin extends CamelCasePlugin {
   protected override camelCase(str: string): string {
     if (str === 'amount_spend_12m') {
       return 'amountSpend_12m'
     }
     return super.camelCase(str)
   }
 }

but it's not possible to make the default work the way you want. For example, how would Kysely know what to do when you run selectFrom('company').selectAll().limit(1).execute()?

from kysely.

michael-land avatar michael-land commented on May 11, 2024

if the column name is amount_spend_12m, how should user define it in typescript interface?

from kysely.

koskimas avatar koskimas commented on May 11, 2024

amountSpend12m

from kysely.

michael-land avatar michael-land commented on May 11, 2024

it doesn't work. kysely transform it to amount_spend12m

from kysely.

koskimas avatar koskimas commented on May 11, 2024

See the documentation https://koskimas.github.io/kysely/interfaces/CamelCasePluginOptions.html#underscoreBeforeDigits

from kysely.

koskimas avatar koskimas commented on May 11, 2024

And you're welcome for the free help once again!

from kysely.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.