-
Notifications
You must be signed in to change notification settings - Fork 7
/
PostgresStore.ts
42 lines (33 loc) · 1.52 KB
/
PostgresStore.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
import Store from './Store.ts'
import { SessionData } from '../Session.ts'
import type postgres from "https://deno.land/x/postgresjs@v3.2.4/mod.js";
// deno-lint-ignore ban-types
type SQLExecutor = postgres.Sql<{}>;
export default class PostgresStore implements Store {
sql: SQLExecutor
tableName: string
constructor(sql: SQLExecutor, tableName = 'sessions') {
this.sql = sql
this.tableName = tableName
}
async initSessionsTable() {
await this.sql`create table if not exists ${this.sql(this.tableName)} (id varchar(21) not null primary key, data varchar)`;
}
async sessionExists(sessionId: string) {
const result = await this.sql`select data from ${this.sql(this.tableName)} where id = ${sessionId}`
return result.length > 0 ? true : false
}
async getSessionById(sessionId: string) {
const result = await this.sql`select data from ${this.sql(this.tableName)} where id = ${sessionId}`
return result.length > 0 ? JSON.parse(result[0].data) as SessionData : null
}
async createSession(sessionId : string, initialData : SessionData) {
await this.sql`insert into ${this.sql(this.tableName)} (id, data) values (${sessionId}, ${JSON.stringify(initialData)})`
}
async deleteSession(sessionId: string) {
await this.sql`delete from ${this.sql(this.tableName)} where id = ${sessionId}`
}
async persistSessionData(sessionId : string, sessionData : SessionData) {
await this.sql`update ${this.sql(this.tableName)} set data = ${JSON.stringify(sessionData)} where id = ${sessionId}`
}
}