-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathsession-store.ts
136 lines (114 loc) · 3.99 KB
/
session-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type EventEmitter from 'events';
import type { Logger, LogProvider } from '../logger';
import NotFoundError from '../error/notfound-error';
import type { ISession, ISessionStore } from '../types/stores/session-store';
import { addDays } from 'date-fns';
import type { Db } from './db';
const TABLE = 'unleash_session';
interface ISessionRow {
sid: string;
sess: string;
created_at: Date;
expired?: Date;
}
export default class SessionStore implements ISessionStore {
private logger: Logger;
private eventBus: EventEmitter;
private db: Db;
constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
this.db = db;
this.eventBus = eventBus;
this.logger = getLogger('lib/db/session-store.ts');
}
async getActiveSessions(): Promise<ISession[]> {
const rows = await this.db<ISessionRow>(TABLE)
.whereNull('expired')
.orWhere('expired', '>', new Date())
.orderBy('created_at', 'desc');
return rows.map(this.rowToSession);
}
async getSessionsForUser(userId: number): Promise<ISession[]> {
const rows = await this.db<ISessionRow>(TABLE).whereRaw(
"(sess -> 'user' ->> 'id')::int = ?",
[userId],
);
if (rows && rows.length > 0) {
return rows.map(this.rowToSession);
}
return [];
}
async get(sid: string): Promise<ISession> {
const row = await this.db<ISessionRow>(TABLE)
.where('sid', '=', sid)
.first();
if (row) {
return this.rowToSession(row);
}
throw new NotFoundError(`Could not find session with sid ${sid}`);
}
async deleteSessionsForUser(userId: number): Promise<void> {
await this.db<ISessionRow>(TABLE)
.whereRaw("(sess -> 'user' ->> 'id')::int = ?", [userId])
.del();
}
async delete(sid: string): Promise<void> {
await this.db<ISessionRow>(TABLE).where('sid', '=', sid).del();
}
async insertSession(data: Omit<ISession, 'createdAt'>): Promise<ISession> {
const row = await this.db<ISessionRow>(TABLE)
.insert({
sid: data.sid,
sess: JSON.stringify(data.sess),
expired: data.expired || addDays(Date.now(), 1),
})
.returning<ISessionRow>(['sid', 'sess', 'created_at', 'expired']);
if (row) {
return this.rowToSession(row);
}
throw new Error('Could not insert session');
}
async deleteAll(): Promise<void> {
await this.db(TABLE).del();
}
destroy(): void {}
async exists(sid: string): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE sid = ?) AS present`,
[sid],
);
const { present } = result.rows[0];
return present;
}
async getAll(): Promise<ISession[]> {
const rows = await this.db<ISessionRow>(TABLE);
return rows.map(this.rowToSession);
}
private rowToSession(row: ISessionRow): ISession {
return {
sid: row.sid,
sess: row.sess,
createdAt: row.created_at,
expired: row.expired,
};
}
async getSessionsCount(): Promise<{ userId: number; count: number }[]> {
const rows = await this.db(TABLE)
.select(this.db.raw("sess->'user'->>'id' AS user_id"))
.count('* as count')
.groupBy('user_id');
return rows.map((row) => ({
userId: Number(row.user_id),
count: Number(row.count),
}));
}
async getMaxSessionsCount(): Promise<number> {
const result = await this.db(TABLE)
.select(this.db.raw("sess->'user'->>'id' AS user_id"))
.count('* as count')
.groupBy('user_id')
.orderBy('count', 'desc')
.first();
return result ? Number(result.count) : 0;
}
}
module.exports = SessionStore;