Skip to content

An AI-powered search engine with a generative UI

feat: optimize database schema to match Vercel's AI SDK v5 implementation

Yoshiki Miura341e2d6673b7
diff --git a/drizzle/0003_optimize_schema_types.sql b/drizzle/0003_optimize_schema_types.sql
new file mode 100644
index 00000000..bcae5ce1
--- /dev/null
+++ b/drizzle/0003_optimize_schema_types.sql
@@ -0,0 +1,15 @@
+-- Optimize schema to match Vercel's implementation
+-- Note: These changes affect column types only, not the actual data
+
+-- Update varchar length constraints
+ALTER TABLE chats ALTER COLUMN id TYPE varchar(191);
+ALTER TABLE chats ALTER COLUMN user_id TYPE varchar(255);
+
+ALTER TABLE messages ALTER COLUMN id TYPE varchar(191);
+ALTER TABLE messages ALTER COLUMN chat_id TYPE varchar(191);
+
+ALTER TABLE parts ALTER COLUMN id TYPE varchar(191);
+ALTER TABLE parts ALTER COLUMN message_id TYPE varchar(191);
+
+-- Note: Timestamp columns remain unchanged as PostgreSQL handles them appropriately
+-- The mode change from 'string' to 'date' is handled at the application level
\ No newline at end of file
diff --git a/lib/actions/__tests__/chat-db.test.ts b/lib/actions/__tests__/chat-db.test.ts
index d98335f8..42db9547 100644
--- a/lib/actions/__tests__/chat-db.test.ts
+++ b/lib/actions/__tests__/chat-db.test.ts
@@ -55,7 +55,7 @@ const testChatData: DBChat = {
id: commonChatId,
userId: commonUserId,
title: 'Test Chat Title',
- createdAt: now,
+ createdAt: new Date(now),
visibility: 'private'
}
@@ -63,7 +63,7 @@ const testMessageData: DBMessage = {
id: commonMessageId,
chatId: commonChatId,
role: 'user',
- createdAt: now
+ createdAt: new Date(now)
}
describe('Chat Actions - clearChats', () => {
diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.ts
index affc4ca2..6a94c4c3 100644
--- a/lib/actions/chat-db.ts
+++ b/lib/actions/chat-db.ts
@@ -344,8 +344,10 @@ export async function deleteTrailingMessages(
return { error: 'Pivot message not found' }
}
- // The createdAt field from DBMessage is expected to be a string (ISO 8601 format)
- const pivotTimestamp = pivotMessage.createdAt
+ // The createdAt field from DBMessage is now a Date object
+ const pivotTimestamp = pivotMessage.createdAt instanceof Date
+ ? pivotMessage.createdAt.toISOString()
+ : pivotMessage.createdAt
// 3. Call the database function to delete messages after the pivot message
const deleteResult = await chatDb.deleteMessagesByChatIdAfterTimestamp(
diff --git a/lib/db/chat.ts b/lib/db/chat.ts
index 60df92e7..38eac81d 100644
--- a/lib/db/chat.ts
+++ b/lib/db/chat.ts
@@ -193,7 +193,7 @@ export async function deleteMessagesByChatIdAfterTimestamp(
.where(
and(
eq(messages.chatId, chatId),
- gte(messages.createdAt, timestamp) // Use ISO string directly
+ gte(messages.createdAt, new Date(timestamp)) // Convert string to Date
)
)
diff --git a/lib/db/schema.ts b/lib/db/schema.ts
index c95c5174..643164f2 100644
--- a/lib/db/schema.ts
+++ b/lib/db/schema.ts
@@ -18,14 +18,14 @@ export const generateId = () => createId()
export const chats = pgTable(
'chats',
{
- id: varchar('id')
+ id: varchar('id', { length: 191 })
.primaryKey()
.$defaultFn(() => generateId()),
- createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
+ createdAt: timestamp('created_at')
.notNull()
.defaultNow(),
title: text('title').notNull(),
- userId: varchar('user_id').notNull(),
+ userId: varchar('user_id', { length: 255 }).notNull(),
visibility: varchar('visibility', {
length: 256,
enum: ['public', 'private']
@@ -46,14 +46,14 @@ export type Chat = InferSelectModel<typeof chats>
export const messages = pgTable(
'messages',
{
- id: varchar('id')
+ id: varchar('id', { length: 191 })
.primaryKey()
.$defaultFn(() => generateId()),
- chatId: varchar('chat_id')
+ chatId: varchar('chat_id', { length: 191 })
.notNull()
.references(() => chats.id, { onDelete: 'cascade' }),
role: varchar('role', { length: 256 }).notNull(),
- createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
+ createdAt: timestamp('created_at')
.notNull()
.defaultNow()
},
@@ -73,10 +73,10 @@ export type Message = InferSelectModel<typeof messages>
export const parts = pgTable(
'parts',
{
- id: varchar('id')
+ id: varchar('id', { length: 191 })
.primaryKey()
.$defaultFn(() => generateId()),
- messageId: varchar('message_id')
+ messageId: varchar('message_id', { length: 191 })
.notNull()
.references(() => messages.id, { onDelete: 'cascade' }),
order: integer('order').notNull(),
@@ -143,7 +143,7 @@ export const parts = pgTable(
// Provider metadata
providerMetadata: json('provider_metadata').$type<Record<string, any>>(),
- createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })
+ createdAt: timestamp('created_at')
.notNull()
.defaultNow()
},
diff --git a/lib/types/message-persistence.ts b/lib/types/message-persistence.ts
index caacf37c..9857065f 100644
--- a/lib/types/message-persistence.ts
+++ b/lib/types/message-persistence.ts
@@ -49,7 +49,7 @@ export type DBMessage = {
id: string
chatId: string
role: string
- createdAt: string
+ createdAt: Date | string
}
// Extended UIMessage type (persistence support)
diff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.ts
index 28d1af13..653921c2 100644
--- a/lib/utils/message-mapping.ts
+++ b/lib/utils/message-mapping.ts
@@ -65,8 +65,8 @@ export function mapUIMessagePartsToDBParts(
messageParts: UIMessagePart[],
messageId: string
): DBMessagePart[] {
- return messageParts
- .map((part, index) => {
+ const mappedParts = messageParts
+ .map((part, index): DBMessagePart | null => {
const basePart = {
messageId,
order: index,
@@ -195,8 +195,11 @@ export function mapUIMessagePartsToDBParts(
}
}
})
- .filter((part): part is DBMessagePart => part !== null) // Filter out null values
- .map((part, index) => ({ ...part, order: index })) // Re-index after filtering
+
+ // Filter out null values and re-index
+ return mappedParts
+ .filter((part): part is DBMessagePart => part !== null)
+ .map((part, index) => ({ ...part, order: index }))
}
/**
@@ -396,7 +399,7 @@ export function buildUIMessageFromDB(
dbMessage: {
id: string
role: string
- createdAt?: string
+ createdAt?: Date | string
},
dbParts: DBMessagePartSelect[]
): UIMessage {
@@ -405,7 +408,7 @@ export function buildUIMessageFromDB(
role: dbMessage.role as 'user' | 'assistant',
parts: dbParts.map(mapDBPartToUIMessagePart) as UIMessage['parts'],
metadata: dbMessage.createdAt
- ? { createdAt: new Date(dbMessage.createdAt) }
+ ? { createdAt: dbMessage.createdAt instanceof Date ? dbMessage.createdAt : new Date(dbMessage.createdAt) }
: undefined
}
}