Non hardcoded strings in sql()

This:

import { sql } from 'drizzle-orm';
import { check, int, sqliteTable, text } from 'drizzle-orm/sqlite-core';


const SOME_KIND_ENUM  = ["win", "sbst", "tbst", "chop"] as const;

export const someTable = sqliteTable("some_table", {
  id: int().primaryKey({ autoIncrement: true }),
  kind: text({ enum: SOME_KIND_ENUM }).notNull(),
}, (table) => [
  check(
            "is_valid",
      sql`${table.kind} IN ('${SOME_KIND_ENUM.join(`', '`)}')`
        ),
]);


produces:

CREATE TABLE `some_table` (
    `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    `kind` text NOT NULL,
    CONSTRAINT "is_valid" CHECK("some_table"."kind" IN ('?'))
);


I would really prefer to have one source of truth for this enum. This way I have one place from where I can drive the type and all the other stuff.
Was this page helpful?