Created
March 18, 2024 12:09
-
-
Save mattfysh/109ba8203dfd38688148ecbb78f77a62 to your computer and use it in GitHub Desktop.
declaring drizzle types for a table defined elsewhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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