2021-04-27 09:16:44 +02:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
import NotFoundError from '../error/notfound-error';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { ISession, ISessionStore } from '../types/stores/session-store';
|
2021-11-02 15:13:46 +01:00
|
|
|
import { addDays } from 'date-fns';
|
2023-01-30 09:02:44 +01:00
|
|
|
import { Db } from './db';
|
2021-04-27 09:16:44 +02:00
|
|
|
|
|
|
|
const TABLE = 'unleash_session';
|
|
|
|
|
|
|
|
interface ISessionRow {
|
|
|
|
sid: string;
|
|
|
|
sess: string;
|
|
|
|
created_at: Date;
|
|
|
|
expired?: Date;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export default class SessionStore implements ISessionStore {
|
2021-04-27 09:16:44 +02:00
|
|
|
private logger: Logger;
|
|
|
|
|
|
|
|
private eventBus: EventEmitter;
|
|
|
|
|
2023-01-30 09:02:44 +01:00
|
|
|
private db: Db;
|
2021-04-27 09:16:44 +02:00
|
|
|
|
2023-01-30 09:02:44 +01:00
|
|
|
constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
|
2021-04-27 09:16:44 +02:00
|
|
|
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[]> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const rows = await this.db<ISessionRow>(TABLE).whereRaw(
|
|
|
|
"(sess -> 'user' ->> 'id')::int = ?",
|
|
|
|
[userId],
|
|
|
|
);
|
2021-04-27 09:16:44 +02:00
|
|
|
if (rows && rows.length > 0) {
|
|
|
|
return rows.map(this.rowToSession);
|
|
|
|
}
|
|
|
|
throw new NotFoundError(
|
|
|
|
`Could not find sessions for user with id ${userId}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async get(sid: string): Promise<ISession> {
|
2021-04-27 09:16:44 +02:00
|
|
|
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)
|
2021-08-12 15:04:37 +02:00
|
|
|
.whereRaw("(sess -> 'user' ->> 'id')::int = ?", [userId])
|
2021-04-27 09:16:44 +02:00
|
|
|
.del();
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async delete(sid: string): Promise<void> {
|
|
|
|
await this.db<ISessionRow>(TABLE).where('sid', '=', sid).del();
|
2021-04-27 09:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async insertSession(data: Omit<ISession, 'createdAt'>): Promise<ISession> {
|
|
|
|
const row = await this.db<ISessionRow>(TABLE)
|
|
|
|
.insert({
|
|
|
|
sid: data.sid,
|
|
|
|
sess: JSON.stringify(data.sess),
|
2021-11-02 15:13:46 +01:00
|
|
|
expired: data.expired || addDays(Date.now(), 1),
|
2021-04-27 09:16:44 +02:00
|
|
|
})
|
|
|
|
.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();
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-27 09:16:44 +02:00
|
|
|
private rowToSession(row: ISessionRow): ISession {
|
|
|
|
return {
|
|
|
|
sid: row.sid,
|
|
|
|
sess: row.sess,
|
|
|
|
createdAt: row.created_at,
|
|
|
|
expired: row.expired,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SessionStore;
|