Skip to content

Commit 0de414d

Browse files
committed
feat (user counert): add active_sessions table for counter
- Add active_sessions schema with session tracking - Add index on last_activity for performance optimization - Prepare database structure for live online user counter Table structure: - session_id (text, PK): unique session identifier - last_activity (timestamp): tracks user's last activity time Migration file: drizzle/0002_clean_martin_li.sql Related to: (SP: 3) [Frontend + Backend] Track all active users and display live online counter on Home page
1 parent d7c63a3 commit 0de414d

5 files changed

Lines changed: 2736 additions & 1 deletion

File tree

frontend/db/schema/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export * from './users';
55
export * from './points';
66
export * from './shop';
77
export * from './emailVerificationTokens';
8-
export * from "./passwordResetTokens";
8+
export * from './passwordResetTokens';
9+
export * from './sessions';

frontend/db/schema/sessions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { pgTable, text, timestamp, index } from 'drizzle-orm/pg-core';
2+
3+
export const activeSessions = pgTable(
4+
'active_sessions',
5+
{
6+
sessionId: text('session_id').primaryKey(),
7+
lastActivity: timestamp('last_activity').notNull().defaultNow(),
8+
},
9+
(table) => ({
10+
lastActivityIdx: index('active_sessions_last_activity_idx').on(table.lastActivity),
11+
})
12+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE "active_sessions" (
2+
"session_id" text PRIMARY KEY NOT NULL,
3+
"last_activity" timestamp DEFAULT now() NOT NULL
4+
);
5+
--> statement-breakpoint
6+
CREATE INDEX "active_sessions_last_activity_idx" ON "active_sessions" USING btree ("last_activity");

0 commit comments

Comments
 (0)