Import other modules into monorepo
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-06-01 20:59:40 -05:00
parent 26d54033af
commit 9be9c44a32
138 changed files with 11544 additions and 139 deletions

View File

@@ -0,0 +1,20 @@
import {Model} from "../model/Model";
import {Field} from "../model/Field";
import {FieldType} from "../types";
/**
* A model instance which stores records from the ORMCache driver.
*/
export class CacheModel extends Model<CacheModel> {
protected static table = 'caches'; // FIXME allow configuring
protected static key = 'cache_key';
@Field(FieldType.varchar, 'cache_key')
public cacheKey!: string;
@Field(FieldType.text, 'cache_value')
public cacheValue!: string;
@Field(FieldType.timestamp, 'cache_expires')
public cacheExpires?: Date;
}

View File

@@ -0,0 +1,45 @@
import {Container} from "../../di"
import {Cache} from "../../util"
import {CacheModel} from "./CacheModel"
/**
* A cache driver whose records are stored in a database table using the CacheModel.
*/
export class ORMCache extends Cache {
public async fetch(key: string): Promise<string | undefined> {
const model = await CacheModel.query<CacheModel>()
.where(CacheModel.qualifyKey(), '=', key)
.where(CacheModel.propertyToColumn('cacheExpires'), '>', new Date())
.first()
if ( model ) {
return model.cacheValue
}
}
public async put(key: string, value: string, expires?: Date): Promise<void> {
let model = await CacheModel.findByKey<CacheModel>(key)
if ( !model ) {
model = <CacheModel> Container.getContainer().make(CacheModel)
}
model.cacheKey = key
model.cacheValue = value
model.cacheExpires = expires
await model.save()
}
public async has(key: string): Promise<boolean> {
return CacheModel.query()
.where(CacheModel.qualifyKey(), '=', key)
.where(CacheModel.propertyToColumn('cacheExpires'), '>', new Date())
.exists()
}
public async drop(key: string): Promise<void> {
await CacheModel.query()
.where(CacheModel.qualifyKey(), '=', key)
.delete()
}
}

View File

@@ -0,0 +1,68 @@
import {SessionModel} from "./SessionModel"
import {Container} from "../../di"
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from "../../http/session/Session";
/**
* An implementation of the Session driver whose records are stored in a database table.
*/
export class ORMSession extends Session {
protected key?: string
protected data?: SessionData
protected session?: SessionModel
public getKey(): string {
if ( !this.key ) {
throw new NoSessionKeyError()
}
return this.key
}
public setKey(key: string): void {
this.key = key
}
public async load() {
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
}
}
public async persist() {
if ( !this.key ) throw new NoSessionKeyError()
if ( !this.data || !this.session ) throw new SessionNotLoadedError()
this.session.uuid = this.key
this.session.json = JSON.stringify(this.data)
await this.session.save()
}
public getData() {
if ( !this.data ) throw new SessionNotLoadedError()
return this.data
}
public setData(data: SessionData) {
this.data = data
}
public get(key: string, fallback?: any): any {
if ( !this.data ) throw new SessionNotLoadedError()
return this.data[key] ?? fallback
}
public set(key: string, value: any) {
if ( !this.data ) throw new SessionNotLoadedError()
this.data[key] = value
}
}

View File

@@ -0,0 +1,17 @@
import {Model} from "../model/Model";
import {Field} from "../model/Field";
import {FieldType} from "../types";
/**
* Model used to fetch & store sessions from the ORMSession driver.
*/
export class SessionModel extends Model<SessionModel> {
protected static table = 'sessions'; // FIXME allow configuring
protected static key = 'session_uuid';
@Field(FieldType.varchar, 'session_uuid')
public uuid!: string;
@Field(FieldType.json, 'session_data')
public json!: any;
}