- Introduced a messaging module on the backend using NestJS, including repository, service, controller, DTOs, and WebSocket Gateway. - Developed a frontend messaging page with conversation management, real-time message handling, and chat UI. - Implemented `MessageService` for API integrations and `SocketProvider` for real-time WebSocket updates. - Enhanced database schema to support conversations, participants, and messages with Drizzle ORM.
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import {
|
|
index,
|
|
pgTable,
|
|
primaryKey,
|
|
text,
|
|
timestamp,
|
|
uuid,
|
|
} from "drizzle-orm/pg-core";
|
|
import { users } from "./users";
|
|
|
|
export const conversations = pgTable("conversations", {
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
createdAt: timestamp("created_at", { withTimezone: true })
|
|
.notNull()
|
|
.defaultNow(),
|
|
updatedAt: timestamp("updated_at", { withTimezone: true })
|
|
.notNull()
|
|
.defaultNow(),
|
|
});
|
|
|
|
export const conversationParticipants = pgTable(
|
|
"conversation_participants",
|
|
{
|
|
conversationId: uuid("conversation_id")
|
|
.notNull()
|
|
.references(() => conversations.id, { onDelete: "cascade" }),
|
|
userId: uuid("user_id")
|
|
.notNull()
|
|
.references(() => users.uuid, { onDelete: "cascade" }),
|
|
joinedAt: timestamp("joined_at", { withTimezone: true })
|
|
.notNull()
|
|
.defaultNow(),
|
|
},
|
|
(t) => ({
|
|
pk: primaryKey({ columns: [t.conversationId, t.userId] }),
|
|
}),
|
|
);
|
|
|
|
export const messages = pgTable(
|
|
"messages",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
conversationId: uuid("conversation_id")
|
|
.notNull()
|
|
.references(() => conversations.id, { onDelete: "cascade" }),
|
|
senderId: uuid("sender_id")
|
|
.notNull()
|
|
.references(() => users.uuid, { onDelete: "cascade" }),
|
|
text: text("text").notNull(),
|
|
createdAt: timestamp("created_at", { withTimezone: true })
|
|
.notNull()
|
|
.defaultNow(),
|
|
readAt: timestamp("read_at", { withTimezone: true }),
|
|
},
|
|
(table) => ({
|
|
conversationIdIdx: index("messages_conversation_id_idx").on(
|
|
table.conversationId,
|
|
),
|
|
senderIdIdx: index("messages_sender_id_idx").on(table.senderId),
|
|
}),
|
|
);
|
|
|
|
export type ConversationInDb = typeof conversations.$inferSelect;
|
|
export type NewConversationInDb = typeof conversations.$inferInsert;
|
|
export type MessageInDb = typeof messages.$inferSelect;
|
|
export type NewMessageInDb = typeof messages.$inferInsert;
|