2021-06-03 03:36:25 +00:00
|
|
|
import {SessionModel} from './SessionModel'
|
|
|
|
import {Container} from '../../di'
|
|
|
|
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from '../../http/session/Session'
|
2021-06-02 01:59:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An implementation of the Session driver whose records are stored in a database table.
|
|
|
|
*/
|
|
|
|
export class ORMSession extends Session {
|
|
|
|
protected key?: string
|
2021-06-03 03:36:25 +00:00
|
|
|
|
2021-06-02 01:59:40 +00:00
|
|
|
protected data?: SessionData
|
2021-06-03 03:36:25 +00:00
|
|
|
|
2021-06-02 01:59:40 +00:00
|
|
|
protected session?: SessionModel
|
|
|
|
|
|
|
|
public getKey(): string {
|
|
|
|
if ( !this.key ) {
|
|
|
|
throw new NoSessionKeyError()
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.key
|
|
|
|
}
|
|
|
|
|
|
|
|
public setKey(key: string): void {
|
|
|
|
this.key = key
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public async load(): Promise<void> {
|
2021-06-02 01:59:40 +00:00
|
|
|
if ( !this.key ) {
|
|
|
|
throw new NoSessionKeyError()
|
|
|
|
}
|
|
|
|
|
|
|
|
const session = <SessionModel> await SessionModel.findByKey(this.key)
|
|
|
|
if ( session ) {
|
|
|
|
this.session = session
|
|
|
|
this.data = this.session.json
|
|
|
|
} else {
|
|
|
|
this.session = <SessionModel> Container.getContainer().make(SessionModel)
|
|
|
|
this.session.uuid = this.key
|
|
|
|
this.data = {} as SessionData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public async persist(): Promise<void> {
|
|
|
|
if ( !this.key ) {
|
|
|
|
throw new NoSessionKeyError()
|
|
|
|
}
|
|
|
|
if ( !this.data || !this.session ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
|
|
|
this.session.uuid = this.key
|
|
|
|
this.session.json = JSON.stringify(this.data)
|
|
|
|
await this.session.save()
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public getData(): SessionData {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
return this.data
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public setData(data: SessionData): void {
|
2021-06-02 01:59:40 +00:00
|
|
|
this.data = data
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public get(key: string, fallback?: unknown): any {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
2021-07-17 17:49:07 +00:00
|
|
|
|
2021-06-02 01:59:40 +00:00
|
|
|
return this.data[key] ?? fallback
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
public set(key: string, value: unknown): void {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
2021-07-17 17:49:07 +00:00
|
|
|
|
2021-06-02 01:59:40 +00:00
|
|
|
this.data[key] = value
|
|
|
|
}
|
2021-07-17 17:49:07 +00:00
|
|
|
|
|
|
|
public forget(key: string): void {
|
|
|
|
if ( !this.data ) {
|
|
|
|
throw new SessionNotLoadedError()
|
|
|
|
}
|
|
|
|
|
|
|
|
delete this.data[key]
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|