Skip to content

Instantly share code, notes, and snippets.

@mattfysh
Created March 18, 2024 12:09
Show Gist options
  • Save mattfysh/109ba8203dfd38688148ecbb78f77a62 to your computer and use it in GitHub Desktop.
Save mattfysh/109ba8203dfd38688148ecbb78f77a62 to your computer and use it in GitHub Desktop.
declaring drizzle types for a table defined elsewhere
import {
SQLiteTableWithColumns,
SQLiteTextBuilderInitial,
SQLiteTextJsonBuilderInitial,
SQLiteIntegerBuilderInitial,
SQLiteColumnBuilderBase,
} from 'drizzle-orm/sqlite-core'
import type { BuildColumns, HasDefault, NotNull } from 'drizzle-orm'
type Text<
TName extends string,
TEnum extends [string, ...string[]] = [string, ...string[]],
> = SQLiteTextBuilderInitial<TName, TEnum>
type Json<TName extends string> = SQLiteTextJsonBuilderInitial<TName>
type Table<
TName extends string,
T extends Record<string, SQLiteColumnBuilderBase>,
> = SQLiteTableWithColumns<{
name: TName
schema: undefined
columns: BuildColumns<TName, T, 'sqlite'>
dialect: 'sqlite'
}>
// -----------
type Articles = Table<
'articles',
{
id: NotNull<Text<'id'>>
title: NotNull<Text<'title'>>
description: Text<'description'>
meta: Json<'meta'>
status: HasDefault<
NotNull<
Text<
'status',
['FOO', 'BAR', 'BAZ']
>
>
>
createdAt: HasDefault<NotNull<SQLiteIntegerBuilderInitial<'created_at'>>>
updatedAt: HasDefault<NotNull<SQLiteIntegerBuilderInitial<'updated_at'>>>
}
>
export type DbSchema = {
articles: Articles
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment