Skip to content

Improve DX around column default values and common raw SQL usage. #1162

@igalklebanov

Description

@igalklebanov

Hey 👋

Temporal columns with a default value being current date and time are very common.
Forcing users to do:

import { sql } from 'kysely'

.addColumn('created_at', 'datetime', (cb) => cb.defaultTo(sql`CURRENT_TIMESTAMP`)) // MySQL / SQLite / PostgreSQL / MSSQL
// or
.addColumn('created_at', 'timestampz', (cb) => cb.defaultTo(sql`now()`)) // PostgreSQL

everywhere, is not ideal.

These also come up in issues/discord more often than desired.
There are also other defaultTo use cases that use raw SQL and are quite common.

Worth considering, but not the only way probably:

  1. deprecate defaultTo.
  2. introduce defaultLit for literals (strings, numbers, booleans, etc.).
  3. introduce defaultRaw that accepts any raw SQL string, but has autocompletion for the common stuff like CURRENT_TIMESTAMP, NOW(), gen_random_uuid(), etc. no longer have to import sql template tag. Better DX.

Another approach, given there's not a lot of these:

  1. introduce defaultToCurrentTimestamp().
  2. introduce defaultToNow().

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiRelated to library's APIenhancementNew feature or requestmssqlRelated to MS SQL Server (MSSQL)mysqlRelated to MySQLoracleRelated to OraclepostgresRelated to PostgreSQLsqliteRelated to sqlite

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions