You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

import {Model} from '../../../../orm/src/model/Model.ts'
import SessionInterface, {SessionData} from './SessionInterface.ts'
import {Field} from '../../../../orm/src/model/Field.ts'
import {Type} from '../../../../orm/src/db/types.ts'
/**
* Base class for an ORM session model.
* @extends Model<SessionModel>
* @implements SessionInterface
*/
export default class SessionModel extends Model<SessionModel> implements SessionInterface {
protected static populate_key_on_insert: boolean = true
/**
* The JSON serialized session data.
* @type string
*/
@Field(Type.json)
protected data?: string
public get_key(): string {
return String(this.key())
}
public set_key(key: string) {
// @ts-ignore
this[this.key_name()] = key
}
public async persist(): Promise<void> {
await this.save()
}
public get_data(): SessionData {
return this.data ? JSON.parse(this.data) : undefined
}
public set_data(data: SessionData) {
this.data = JSON.stringify(data)
}
public get_attribute(key: string): any {
const data = this.get_data()
if ( data ) return data[key]
}
public set_attribute(key: string, value: any) {
const data = this.get_data()
data[key] = value
this.set_data(data)
}
public async init_session(): Promise<void> {
this.data = JSON.stringify({})
}
}