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 * @implements SessionInterface */ export default class SessionModel extends Model 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 { 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 { this.data = JSON.stringify({}) } }