Files
claudemesh/packages/db/src/schema/chat.ts
Alejandro Gutiérrez 8ce8b04e75 fix(db): rename pgSchema exports to prevent barrel collision
chat/image/mesh modules all exported a generic `const schema`
binding. When packages/db/src/schema/index.ts did `export * from
"./chat"` + `export * from "./image"` + `export * from "./mesh"`,
TypeScript's ambiguous-re-export rule silently dropped the colliding
bindings — drizzle-kit's introspection could not find the pgSchema
instances, so CREATE SCHEMA statements were never emitted. The
migration worked on the prior dev DB only because chat/image already
existed from an earlier turbostarter run; a fresh clone would fail.

pdf.ts already used `pdfSchema` (unique name). Applied the same
pattern everywhere:
- chat.ts:  `export const chatSchema = pgSchema("chat")`
- image.ts: `export const imageSchema = pgSchema("image")`
- mesh.ts:  `export const meshSchema = pgSchema("mesh")`

Also added `CREATE EXTENSION IF NOT EXISTS vector` at the top of the
migration (pgvector is used by pdf.embedding — the generated
migration assumed it was pre-enabled).

Verified end-to-end against a fresh pgvector/pgvector:pg17 container:
`pnpm drizzle-kit migrate` applies cleanly from scratch, all 7 mesh.*
tables + chat/image/pdf/mesh schemas created correctly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 22:02:09 +01:00

77 lines
2.4 KiB
TypeScript

import { relations } from "drizzle-orm";
import { integer, jsonb, pgSchema, timestamp, text } from "drizzle-orm/pg-core";
import { generateId } from "@turbostarter/shared/utils";
import { createInsertSchema, createSelectSchema } from "../utils/drizzle-zod";
import { user } from "./auth";
// Uniquely-named pgSchema export (not `schema`) so drizzle-kit can
// introspect it through the `export * from "./chat"` barrel. See
// mesh.ts for the full rationale.
export const chatSchema = pgSchema("chat");
export const messageRoleEnum = chatSchema.enum("role", [
"system",
"assistant",
"user",
]);
export const chat = chatSchema.table("chat", {
id: text().primaryKey().notNull().$defaultFn(generateId),
name: text(),
userId: text()
.references(() => user.id, {
onDelete: "cascade",
onUpdate: "cascade",
})
.notNull(),
createdAt: timestamp().defaultNow(),
});
export const message = chatSchema.table("message", {
id: text().primaryKey().notNull().$defaultFn(generateId),
chatId: text()
.references(() => chat.id, { onDelete: "cascade", onUpdate: "cascade" })
.notNull(),
role: messageRoleEnum().notNull(),
createdAt: timestamp().defaultNow(),
});
export const messageRelations = relations(message, ({ many }) => ({
part: many(part),
}));
export const part = chatSchema.table("part", {
id: text().primaryKey().notNull().$defaultFn(generateId),
messageId: text()
.references(() => message.id, { onDelete: "cascade", onUpdate: "cascade" })
.notNull(),
type: text().notNull(),
order: integer().notNull(),
details: jsonb().notNull(),
createdAt: timestamp().defaultNow(),
});
export const partRelations = relations(part, ({ one }) => ({
message: one(message, {
fields: [part.messageId],
references: [message.id],
}),
}));
export const selectChatSchema = createSelectSchema(chat);
export const insertChatSchema = createInsertSchema(chat);
export const selectMessageSchema = createSelectSchema(message);
export const insertMessageSchema = createInsertSchema(message);
export const selectPartSchema = createSelectSchema(part);
export const insertPartSchema = createInsertSchema(part);
export type SelectChat = typeof chat.$inferSelect;
export type InsertChat = typeof chat.$inferInsert;
export type SelectMessage = typeof message.$inferSelect;
export type InsertMessage = typeof message.$inferInsert;
export type SelectPart = typeof part.$inferSelect;
export type InsertPart = typeof part.$inferInsert;