diff --git a/src/menus/base.menu.ts b/src/menus/base.menu.ts index c8f4523..beb173f 100644 --- a/src/menus/base.menu.ts +++ b/src/menus/base.menu.ts @@ -52,6 +52,8 @@ export abstract class BaseMenu { }, set: (key: string, val: any) => Config.getInstance().session?.set(this.sessionId!, key, val), + remove: (key: string) => + Config.getInstance().session?.remove(this.sessionId!, key), }; } diff --git a/src/sessions/memcache.session.ts b/src/sessions/memcache.session.ts index 5822ad5..a8287fa 100644 --- a/src/sessions/memcache.session.ts +++ b/src/sessions/memcache.session.ts @@ -2,57 +2,62 @@ import { State } from "@src/models"; import { BaseSession } from "./base.session"; export class MemcacheSession extends BaseSession { - private static instance: MemcacheSession; - - private constructor() { - super(); - } - - public static getInstance(): MemcacheSession { - if (!MemcacheSession.instance) { - MemcacheSession.instance = new MemcacheSession(); - } - - return MemcacheSession.instance; - } - - async setState(id: string, state: State): Promise { - this.states[id] = state; - return state; - } - - async getState(id: string): Promise { - return this.states[id]; - } - - clear(id: string): void | State { - const _state = this.states[id]; - delete this.states[id]; - delete this.data[id]; - return _state; - } - - async set(sessionId: string, key: string, value: any) { - if (this.data[sessionId] == null) { - this.data[sessionId] = {}; - } - - this.data[sessionId][key] = value; - } - - async get( - sessionId: string, - key: string, - defaultValue?: T, - ): Promise { - if (this.data[sessionId] == null) { - return defaultValue; - } - - return (this.data[sessionId][key] || defaultValue) as T; - } - - async getAll(sessionId: string): Promise { - return this.data[sessionId] as T; - } + private static instance: MemcacheSession; + + private constructor() { + super(); + } + + public static getInstance(): MemcacheSession { + if (!MemcacheSession.instance) { + MemcacheSession.instance = new MemcacheSession(); + } + + return MemcacheSession.instance; + } + + async setState(id: string, state: State): Promise { + this.states[id] = state; + return state; + } + + async getState(id: string): Promise { + return this.states[id]; + } + + clear(id: string): State { + const _state = this.states[id]; + delete this.states[id]; + delete this.data[id]; + return _state; + } + + async set(sessionId: string, key: string, value: any) { + if (this.data[sessionId] == null) { + this.data[sessionId] = {}; + } + + this.data[sessionId][key] = value; + } + + async remove(sessionId: string, key: string) { + this.data[sessionId] ??= {}; + delete this.data[sessionId][key] + } + + async get( + sessionId: string, + key: string, + defaultValue?: T, + ): Promise { + if (this.data[sessionId] == null) { + return defaultValue; + } + + return (this.data[sessionId][key] || defaultValue) as T; + } + + async getAll(sessionId: string): Promise { + return this.data[sessionId] as T; + } } diff --git a/src/sessions/mysql.session.ts b/src/sessions/mysql.session.ts index 99e02d9..83a9184 100644 --- a/src/sessions/mysql.session.ts +++ b/src/sessions/mysql.session.ts @@ -106,12 +106,12 @@ export class MySQLSession extends BaseSession { return State.fromJSON(JSON.parse(resp[0].state)); } - clear(sessionId: string): void | State { + clear(sessionId: string): State { const _state = this.states[sessionId]; delete this.states[sessionId]; delete this.data[sessionId]; - if (this.config.softDelete == false || this.config.softDelete == null) { + if (this.config.softDelete === false || this.config.softDelete == null) { this.db .query(`DELETE FROM ${this.tableName} WHERE session_id = ?`, [ sessionId, @@ -143,6 +143,16 @@ export class MySQLSession extends BaseSession { ); } + async remove(sessionId: string, key: string): Promise { + this.data[sessionId] ??= {}; + delete this.data[sessionId][key]; + + await this.db.query( + `UPDATE ${this.tableName} SET data = ? WHERE session_id = ? ${this.softDeleteQuery}`, + [JSON.stringify(this.data[sessionId]), sessionId], + ); + } + async get( sessionId: string, key: string, diff --git a/src/sessions/redis.session.ts b/src/sessions/redis.session.ts index 9d1b901..44d94a5 100644 --- a/src/sessions/redis.session.ts +++ b/src/sessions/redis.session.ts @@ -76,7 +76,7 @@ export class RedisSession extends BaseSession { return val == null ? undefined : State.fromJSON(JSON.parse(val)); } - clear(sessionId: string): void | State { + clear(sessionId: string): State { const _state = this.states[sessionId]; delete this.states[sessionId]; delete this.data[sessionId];