Skip to main content
Module

x/kysely_deno_postgres_dialect/mod.ts>kysely.CamelCasePlugin

Kysely dialect for PostgreSQL using the deno-postgres client.
Go to Latest
class kysely.CamelCasePlugin
implements KyselyPlugin
import { kysely } from "https://deno.land/x/kysely_deno_postgres_dialect@v0.27.0/mod.ts";
const { CamelCasePlugin } = kysely;

A plugin that converts snake_case identifiers in the database into camelCase in the javascript side.

For example let's assume we have a table called person_table with columns first_name and last_name in the database. When using CamelCasePlugin we would setup Kysely like this:

interface Person {
  firstName: string
  lastName: string
}

interface Database {
  personTable: Person
}

const db = new Kysely<Database>({
  dialect: new PostgresDialect({
    database: 'kysely_test',
    host: 'localhost',
  }),
  plugins: [
    new CamelCasePlugin()
  ]
})

const person = await db.selectFrom('personTable')
  .where('firstName', '=', 'Arnold')
  .select(['firstName', 'lastName'])
  .executeTakeFirst()

// generated sql:
// select first_name, last_name from person_table where first_name = $1

if (person) {
  console.log(person.firstName)
}

As you can see from the example, everything needs to be defined in camelCase in the typescript code: table names, columns, schemas, everything. When using the CamelCasePlugin Kysely works as if the database was defined in camelCase.

There are various options you can give to the plugin to modify the way identifiers are converted. See CamelCasePluginOptions. If those options are not enough, you can override this plugin's snakeCase and camelCase methods to make the conversion exactly the way you like:

class MyCamelCasePlugin extends CamelCasePlugin {
  protected override snakeCase(str: string): string {
    return mySnakeCase(str)
  }

  protected override camelCase(str: string): string {
    return myCamelCase(str)
  }
}

Constructors

new
CamelCasePlugin(opt?: CamelCasePluginOptions)

Methods

protected
camelCase(str: string): string
protected
mapRow(row: UnknownRow): UnknownRow
protected
snakeCase(str: string): string

This is called for each query before it is executed. You can modify the query by transforming its OperationNode tree provided in PluginTransformQueryArgs.node | args.node and returning the transformed tree. You'd usually want to use an OperationNodeTransformer for this.

If you need to pass some query-related data between this method and transformResult you can use a WeakMap with PluginTransformQueryArgs.queryId | args.queryId as the key:

const plugin = {
  data: new WeakMap<QueryId, SomeData>(),

  transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
    this.data.set(args.queryId, something)
    return args.node
  },

  transformResult(args: PluginTransformResultArgs): QueryResult<UnknownRow> {
    const data = this.data.get(args.queryId)
    return args.result
  }
}

You should use a WeakMap instead of a Map or some other strong references because transformQuery is not always matched by a call to transformResult which would leave orphaned items in the map and cause a memory leak.

This method is called for each query after it has been executed. The result of the query can be accessed through PluginTransformResultArgs.result | args.result. You can modify the result and return the modifier result.