2025-09-21 11:02:31 -07:00
|
|
|
import type { InferSelectModel } from "drizzle-orm";
|
2024-10-30 16:01:24 +05:30
|
|
|
import {
|
2025-09-21 11:02:31 -07:00
|
|
|
boolean,
|
|
|
|
|
foreignKey,
|
2025-09-21 12:03:29 +01:00
|
|
|
json,
|
2025-09-21 11:02:31 -07:00
|
|
|
pgTable,
|
2025-09-21 12:03:29 +01:00
|
|
|
primaryKey,
|
2025-09-21 11:02:31 -07:00
|
|
|
text,
|
|
|
|
|
timestamp,
|
|
|
|
|
uuid,
|
|
|
|
|
varchar,
|
|
|
|
|
} from "drizzle-orm/pg-core";
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
export const user = pgTable("User", {
|
|
|
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
|
|
|
email: varchar("email", { length: 64 }).notNull(),
|
|
|
|
|
password: varchar("password", { length: 64 }),
|
2026-03-13 23:18:01 +00:00
|
|
|
name: text("name"),
|
|
|
|
|
emailVerified: boolean("emailVerified").notNull().default(false),
|
|
|
|
|
image: text("image"),
|
|
|
|
|
isAnonymous: boolean("isAnonymous").notNull().default(false),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull().defaultNow(),
|
|
|
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
2024-10-11 18:00:22 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type User = InferSelectModel<typeof user>;
|
|
|
|
|
|
2026-03-13 23:18:01 +00:00
|
|
|
export const session = pgTable("Session", {
|
|
|
|
|
id: text("id").primaryKey(),
|
|
|
|
|
expiresAt: timestamp("expiresAt").notNull(),
|
|
|
|
|
token: text("token").notNull().unique(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull().defaultNow(),
|
|
|
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
|
|
|
|
ipAddress: text("ipAddress"),
|
|
|
|
|
userAgent: text("userAgent"),
|
|
|
|
|
userId: uuid("userId")
|
|
|
|
|
.notNull()
|
|
|
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type Session = InferSelectModel<typeof session>;
|
|
|
|
|
|
|
|
|
|
export const account = pgTable("Account", {
|
|
|
|
|
id: text("id").primaryKey(),
|
|
|
|
|
accountId: text("accountId").notNull(),
|
|
|
|
|
providerId: text("providerId").notNull(),
|
|
|
|
|
userId: uuid("userId")
|
|
|
|
|
.notNull()
|
|
|
|
|
.references(() => user.id, { onDelete: "cascade" }),
|
|
|
|
|
accessToken: text("accessToken"),
|
|
|
|
|
refreshToken: text("refreshToken"),
|
|
|
|
|
idToken: text("idToken"),
|
|
|
|
|
accessTokenExpiresAt: timestamp("accessTokenExpiresAt"),
|
|
|
|
|
refreshTokenExpiresAt: timestamp("refreshTokenExpiresAt"),
|
|
|
|
|
scope: text("scope"),
|
|
|
|
|
password: text("password"),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull().defaultNow(),
|
|
|
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type Account = InferSelectModel<typeof account>;
|
|
|
|
|
|
|
|
|
|
export const verification = pgTable("Verification", {
|
|
|
|
|
id: text("id").primaryKey(),
|
|
|
|
|
identifier: text("identifier").notNull(),
|
|
|
|
|
value: text("value").notNull(),
|
|
|
|
|
expiresAt: timestamp("expiresAt").notNull(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull().defaultNow(),
|
|
|
|
|
updatedAt: timestamp("updatedAt").notNull().defaultNow(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type Verification = InferSelectModel<typeof verification>;
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
export const chat = pgTable("Chat", {
|
|
|
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull(),
|
|
|
|
|
title: text("title").notNull(),
|
|
|
|
|
userId: uuid("userId")
|
2024-10-11 18:00:22 +05:30
|
|
|
.notNull()
|
|
|
|
|
.references(() => user.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
visibility: varchar("visibility", { enum: ["public", "private"] })
|
2024-12-06 13:36:56 +03:00
|
|
|
.notNull()
|
2025-09-21 11:02:31 -07:00
|
|
|
.default("private"),
|
2024-10-11 18:00:22 +05:30
|
|
|
});
|
|
|
|
|
|
2024-11-05 17:15:51 +03:00
|
|
|
export type Chat = InferSelectModel<typeof chat>;
|
|
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
// DEPRECATED: The following schema is deprecated and will be removed in the future.
|
2026-02-23 17:24:17 -08:00
|
|
|
// Read the migration guide at https://chatbot.dev/docs/migration-guides/message-parts
|
2025-09-21 11:02:31 -07:00
|
|
|
export const messageDeprecated = pgTable("Message", {
|
|
|
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
|
|
|
chatId: uuid("chatId")
|
2024-11-05 17:15:51 +03:00
|
|
|
.notNull()
|
|
|
|
|
.references(() => chat.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
role: varchar("role").notNull(),
|
|
|
|
|
content: json("content").notNull(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull(),
|
2024-11-05 17:15:51 +03:00
|
|
|
});
|
|
|
|
|
|
2025-03-16 18:42:29 -07:00
|
|
|
export type MessageDeprecated = InferSelectModel<typeof messageDeprecated>;
|
2024-11-05 17:15:51 +03:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
export const message = pgTable("Message_v2", {
|
|
|
|
|
id: uuid("id").primaryKey().notNull().defaultRandom(),
|
|
|
|
|
chatId: uuid("chatId")
|
2025-03-16 18:42:29 -07:00
|
|
|
.notNull()
|
|
|
|
|
.references(() => chat.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
role: varchar("role").notNull(),
|
|
|
|
|
parts: json("parts").notNull(),
|
|
|
|
|
attachments: json("attachments").notNull(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull(),
|
2025-03-16 18:42:29 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type DBMessage = InferSelectModel<typeof message>;
|
|
|
|
|
|
|
|
|
|
// DEPRECATED: The following schema is deprecated and will be removed in the future.
|
2026-02-23 17:24:17 -08:00
|
|
|
// Read the migration guide at https://chatbot.dev/docs/migration-guides/message-parts
|
2025-03-16 18:42:29 -07:00
|
|
|
export const voteDeprecated = pgTable(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Vote",
|
2025-03-16 18:42:29 -07:00
|
|
|
{
|
2025-09-21 11:02:31 -07:00
|
|
|
chatId: uuid("chatId")
|
2025-03-16 18:42:29 -07:00
|
|
|
.notNull()
|
|
|
|
|
.references(() => chat.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
messageId: uuid("messageId")
|
2025-03-16 18:42:29 -07:00
|
|
|
.notNull()
|
|
|
|
|
.references(() => messageDeprecated.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
isUpvoted: boolean("isUpvoted").notNull(),
|
2025-03-16 18:42:29 -07:00
|
|
|
},
|
|
|
|
|
(table) => {
|
|
|
|
|
return {
|
|
|
|
|
pk: primaryKey({ columns: [table.chatId, table.messageId] }),
|
|
|
|
|
};
|
2025-09-21 11:02:31 -07:00
|
|
|
}
|
2025-03-16 18:42:29 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type VoteDeprecated = InferSelectModel<typeof voteDeprecated>;
|
|
|
|
|
|
|
|
|
|
export const vote = pgTable(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Vote_v2",
|
2024-11-05 17:15:51 +03:00
|
|
|
{
|
2025-09-21 11:02:31 -07:00
|
|
|
chatId: uuid("chatId")
|
2024-11-05 17:15:51 +03:00
|
|
|
.notNull()
|
|
|
|
|
.references(() => chat.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
messageId: uuid("messageId")
|
2024-11-05 17:15:51 +03:00
|
|
|
.notNull()
|
|
|
|
|
.references(() => message.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
isUpvoted: boolean("isUpvoted").notNull(),
|
2024-11-05 17:15:51 +03:00
|
|
|
},
|
|
|
|
|
(table) => {
|
|
|
|
|
return {
|
|
|
|
|
pk: primaryKey({ columns: [table.chatId, table.messageId] }),
|
|
|
|
|
};
|
2025-09-21 11:02:31 -07:00
|
|
|
}
|
2024-11-05 17:15:51 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type Vote = InferSelectModel<typeof vote>;
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
export const document = pgTable(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Document",
|
2024-10-30 16:01:24 +05:30
|
|
|
{
|
2025-09-21 11:02:31 -07:00
|
|
|
id: uuid("id").notNull().defaultRandom(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull(),
|
|
|
|
|
title: text("title").notNull(),
|
|
|
|
|
content: text("content"),
|
|
|
|
|
kind: varchar("text", { enum: ["text", "code", "image", "sheet"] })
|
2025-02-06 17:18:26 +03:00
|
|
|
.notNull()
|
2025-09-21 11:02:31 -07:00
|
|
|
.default("text"),
|
|
|
|
|
userId: uuid("userId")
|
2024-10-30 16:01:24 +05:30
|
|
|
.notNull()
|
|
|
|
|
.references(() => user.id),
|
|
|
|
|
},
|
|
|
|
|
(table) => {
|
|
|
|
|
return {
|
|
|
|
|
pk: primaryKey({ columns: [table.id, table.createdAt] }),
|
|
|
|
|
};
|
2025-09-21 11:02:31 -07:00
|
|
|
}
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type Document = InferSelectModel<typeof document>;
|
|
|
|
|
|
2024-11-06 20:24:18 +03:00
|
|
|
export const suggestion = pgTable(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Suggestion",
|
2024-10-30 16:01:24 +05:30
|
|
|
{
|
2025-09-21 11:02:31 -07:00
|
|
|
id: uuid("id").notNull().defaultRandom(),
|
|
|
|
|
documentId: uuid("documentId").notNull(),
|
|
|
|
|
documentCreatedAt: timestamp("documentCreatedAt").notNull(),
|
|
|
|
|
originalText: text("originalText").notNull(),
|
|
|
|
|
suggestedText: text("suggestedText").notNull(),
|
|
|
|
|
description: text("description"),
|
|
|
|
|
isResolved: boolean("isResolved").notNull().default(false),
|
|
|
|
|
userId: uuid("userId")
|
2024-10-30 16:01:24 +05:30
|
|
|
.notNull()
|
|
|
|
|
.references(() => user.id),
|
2025-09-21 11:02:31 -07:00
|
|
|
createdAt: timestamp("createdAt").notNull(),
|
2024-10-30 16:01:24 +05:30
|
|
|
},
|
|
|
|
|
(table) => ({
|
|
|
|
|
pk: primaryKey({ columns: [table.id] }),
|
|
|
|
|
documentRef: foreignKey({
|
|
|
|
|
columns: [table.documentId, table.documentCreatedAt],
|
|
|
|
|
foreignColumns: [document.id, document.createdAt],
|
|
|
|
|
}),
|
2025-09-21 11:02:31 -07:00
|
|
|
})
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
|
2024-11-06 20:24:18 +03:00
|
|
|
export type Suggestion = InferSelectModel<typeof suggestion>;
|
2025-05-01 12:36:52 -07:00
|
|
|
|
|
|
|
|
export const stream = pgTable(
|
2025-09-21 11:02:31 -07:00
|
|
|
"Stream",
|
2025-05-01 12:36:52 -07:00
|
|
|
{
|
2025-09-21 11:02:31 -07:00
|
|
|
id: uuid("id").notNull().defaultRandom(),
|
|
|
|
|
chatId: uuid("chatId").notNull(),
|
|
|
|
|
createdAt: timestamp("createdAt").notNull(),
|
2025-05-01 12:36:52 -07:00
|
|
|
},
|
|
|
|
|
(table) => ({
|
|
|
|
|
pk: primaryKey({ columns: [table.id] }),
|
|
|
|
|
chatRef: foreignKey({
|
|
|
|
|
columns: [table.chatId],
|
|
|
|
|
foreignColumns: [chat.id],
|
|
|
|
|
}),
|
2025-09-21 11:02:31 -07:00
|
|
|
})
|
2025-05-01 12:36:52 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type Stream = InferSelectModel<typeof stream>;
|