Skip to content

Instantly share code, notes, and snippets.

@JeanMeijer
Last active May 2, 2025 13:16
Show Gist options
  • Save JeanMeijer/1dff9ddb2c1734ba3b9e347322ceaea0 to your computer and use it in GitHub Desktop.
Save JeanMeijer/1dff9ddb2c1734ba3b9e347322ceaea0 to your computer and use it in GitHub Desktop.
Type-safe messages with Vercel AI SDK and Drizzle
import { pgTable, json, text, timestamp } from "drizzle-orm/pg-core";
import type { Message, Attachment } from "ai";
export const message = pgTable("message", {
id: text("id").primaryKey(),
chatId: text("chatId")
.notNull()
.references(() => chat.id),
// infers: "data" | "user" | "system" | "assistant"
role: text("role", { enum: ["data", "user", "system", "assistant"]}).notNull(),
// infers: (TextUIPart | ReasoningUIPart | .... )[] | undefined
parts: json("parts").notNull().$type<Message["parts"]>(),
content: text("content").notNull(),
attachments: json("attachments").notNull().$type<Attachment[]>(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
});
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment