drizzle-team / drizzle-kysely

69 stars 1 forks source link

Feat: Add support for camelCase types #2

Open MariuzM opened 3 months ago

MariuzM commented 3 months ago

When i use Kysely + plugins: [new CamelCasePlugin()] i get types that are camelCase, but when i also want to use Kyselify types are only snake_case.

Any chance to have support for camelCase when using with plugins: [new CamelCasePlugin()]?

import type { Kyselify } from 'drizzle-orm/kysely'
import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely'
import { Pool } from 'pg'

import type { UsersTable } from '-db/index.tables'

export interface DB {
    users: Kyselify<typeof UsersTable>
}

export const db = new Kysely<DB>({
    dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL }) }),
    plugins: [new CamelCasePlugin()],
})

export const main = async () => {
    const t1 = await db.selectFrom('users').select(['']).execute()
    t1[0]?.accountType === 'host'
    t1[0]?.console.log('🚀 ~ t1:', t1)

    db.insertInto('users')
        .values({
            accountType: 'host',
        })
        .execute()
}
image
mjbergman92 commented 3 months ago

Out of pure curiosity, I have been attempting to define a way to provide Kysely with aliases for column names, and also possibly for table names.

I have drizzle setup for a postgres database and I am using kysely for a mssql database, and I'm now using dremio to be able to do joins between the two databases and using Kysely to build the queries for dremio.

I'm able to construct the postgres tables using Kyselify, but not with the drizzle aliases. For clarity, I want the names in code to remain the same as when I'm using drizzle.

TLDR: A custom plugin for Kysely to provide aliases is in store. If I make headway on one, I will share it with the community.

EDIT: I took the time to look into making a plugin. The benefits of Kysely seem to also be its weakness in this case. The closest you can get is creating a transformer for ReferenceNode; however, the original table name of aliased tables in not included in the ReferenceNode. That's the missing link here is somehow gaining the context for aliased tables which would then allow you to search a provided alias map to the plugin for the table and column name.