Skip to content

Commit

Permalink
feat(session): implement remove method for deleting items from sess…
Browse files Browse the repository at this point in the history
…ion for all drivers
  • Loading branch information
ephrimlawrence committed Jun 28, 2024
1 parent 6d0f783 commit 72f4d13
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 56 deletions.
2 changes: 2 additions & 0 deletions src/menus/base.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}

Expand Down
111 changes: 58 additions & 53 deletions src/sessions/memcache.session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State> {
this.states[id] = state;
return state;
}

async getState(id: string): Promise<State | undefined> {
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<T = unknown>(
sessionId: string,
key: string,
defaultValue?: T,
): Promise<T | undefined> {
if (this.data[sessionId] == null) {
return defaultValue;
}

return (this.data[sessionId][key] || defaultValue) as T;
}

async getAll<T>(sessionId: string): Promise<T | undefined> {
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<State> {
this.states[id] = state;
return state;
}

async getState(id: string): Promise<State | undefined> {
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<T = unknown>(
sessionId: string,
key: string,
defaultValue?: T,
): Promise<T | undefined> {
if (this.data[sessionId] == null) {
return defaultValue;
}

return (this.data[sessionId][key] || defaultValue) as T;
}

async getAll<T>(sessionId: string): Promise<T | undefined> {
return this.data[sessionId] as T;
}
}
14 changes: 12 additions & 2 deletions src/sessions/mysql.session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -143,6 +143,16 @@ export class MySQLSession extends BaseSession {
);
}

async remove(sessionId: string, key: string): Promise<void> {
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<T>(
sessionId: string,
key: string,
Expand Down
2 changes: 1 addition & 1 deletion src/sessions/redis.session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 72f4d13

Please sign in to comment.