Skip to content

Commit

Permalink
feat(session: postgres): implement remove method for deleting items…
Browse files Browse the repository at this point in the history
… from session
  • Loading branch information
ephrimlawrence committed Jun 28, 2024
1 parent b170644 commit 1f4b9b6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
67 changes: 42 additions & 25 deletions src/sessions/base.session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,46 @@ import { State } from "@src/models";
import { SessionOptions } from "@src/types";

export abstract class BaseSession {
protected readonly states: { [sessionId: string]: State } = {};
protected readonly data: { [sessionId: string]: Record<string, any> } = {};

// TODO: change this to a proper configuration based on the session type
async configure(options?: SessionOptions): Promise<void> {
// throw new Error("Method not implemented.");
}

abstract setState(id: string, state: State): Promise<State>;

abstract getState(id: string): Promise<State | undefined>;

abstract clear(id: string): void | State;

// TODO: add delete for data

abstract set(sessionId: string, key: string, value: any): Promise<void>;

abstract get<T>(
sessionId: string,
key: string,
defaultValue?: T,
): Promise<T | undefined>;

abstract getAll<T = unknown>(sessionId: string): Promise<T | undefined>;
protected readonly states: { [sessionId: string]: State } = {};
protected readonly data: { [sessionId: string]: Record<string, any> } = {};

// TODO: change this to a proper configuration based on the session type
async configure(options?: SessionOptions): Promise<void> {
// throw new Error("Method not implemented.");
}

abstract setState(id: string, state: State): Promise<State>;

abstract getState(id: string): Promise<State | undefined>;

abstract clear(id: string): State;

// TODO: add delete for data

/**
* Set a key value pair in the session
*
* @param {string} sessionId The session ID, must be unique for each user
* @param {string} key The key to store the value
* @param {any} value The value to store
*/
abstract set(sessionId: string, key: string, value: any): Promise<void>;

/**
* Remove a key value pair from the session
*
* @param {string} sessionId The session ID, must be unique for each user
* @param {string} key The key to remove along with its value from the session
*/
remove(sessionId: string, key: string): Promise<void> {
throw new Error("Method not implemented for the session driver. Feel free to create a pull request to implement this method!");
}

abstract get<T>(
sessionId: string,
key: string,
defaultValue?: T,
): Promise<T | undefined>;

abstract getAll<T = unknown>(sessionId: string): Promise<T | undefined>;
}
18 changes: 16 additions & 2 deletions src/sessions/postgresql.session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ export class PostgresSession extends BaseSession {
return val == null ? undefined : State.fromJSON(JSON.parse(val.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
.none("DELETE FROM $1~.$2~ WHERE session_id = $3", [
this.config.schema,
Expand Down Expand Up @@ -156,6 +156,20 @@ export class PostgresSession extends BaseSession {
return val;
}

async remove(sessionId: string, key: string): Promise<void> {
const val = await this.db.one(
`UPDATE $1~.$2~ SET data = data - '{$3}', updated_at = $4 WHERE session_id = $5 ${this.softDeleteQuery} RETURNING *`,
[
this.config.schema,
this.config.tableName,
key,
new Date().toISOString(),
sessionId,
],
);
return val;
}

async get<T>(
sessionId: string,
key: string,
Expand Down
4 changes: 1 addition & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Request, Response } from "./request";
import { Validation, ValidationResponse } from "./validation.type";

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
// biome-ignore lint/complexity/noBannedTypes: <explanation>
export interface Type<T = any> extends Function {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
new(...args: any[]): T;
}

export type Session = {
get: <T>(key: string, defaultValue?: any) => Promise<T | undefined>;
getAll: <T>() => Promise<T | undefined> | undefined;
set: (key: string, val: any) => Promise<void> | undefined;
remove: (key: string) => Promise<void> | undefined;
};

export type FormInput = {
Expand Down

0 comments on commit 1f4b9b6

Please sign in to comment.