parent
2fd3c6c22b
commit
81906b02bc
@ -0,0 +1,20 @@
|
|||||||
|
import {Model} from '../../orm/src/model/Model.ts'
|
||||||
|
import {Field} from '../../orm/src/model/Field.ts'
|
||||||
|
import {Type} from '../../orm/src/db/types.ts'
|
||||||
|
|
||||||
|
export default class LoginAttemptModel extends Model<LoginAttemptModel> {
|
||||||
|
protected static table = 'daton_login_attempts'
|
||||||
|
protected static key = 'daton_login_attempt_id'
|
||||||
|
|
||||||
|
protected static readonly CREATED_AT = 'attempt_date'
|
||||||
|
protected static readonly UPDATED_AT = null
|
||||||
|
|
||||||
|
@Field(Type.int)
|
||||||
|
protected daton_login_attempt_id?: number
|
||||||
|
|
||||||
|
@Field(Type.int)
|
||||||
|
protected user_id!: number
|
||||||
|
|
||||||
|
@Field(Type.timestamp)
|
||||||
|
protected attempt_date!: Date
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
import {Model} from '../../orm/src/model/Model.ts'
|
||||||
|
import {Type} from '../../orm/src/db/types.ts'
|
||||||
|
import {Field} from '../../orm/src/model/Field.ts'
|
||||||
|
import LoginAttemptModel from './LoginAttempt.model.ts'
|
||||||
|
import {Relation} from '../../orm/src/model/relation/decorators.ts'
|
||||||
|
|
||||||
|
export default class UserModel extends Model<UserModel> {
|
||||||
|
protected static table = 'daton_users'
|
||||||
|
protected static key = 'user_id'
|
||||||
|
|
||||||
|
protected static readonly CREATED_AT = 'created_at'
|
||||||
|
protected static readonly UPDATED_AT = 'updated_at'
|
||||||
|
|
||||||
|
@Field(Type.int)
|
||||||
|
protected user_id?: number
|
||||||
|
|
||||||
|
@Field(Type.varchar)
|
||||||
|
protected first_name!: String
|
||||||
|
|
||||||
|
@Field(Type.varchar)
|
||||||
|
protected last_name!: String
|
||||||
|
|
||||||
|
@Field(Type.bool)
|
||||||
|
protected active!: Boolean
|
||||||
|
|
||||||
|
@Relation()
|
||||||
|
public login_attempts() {
|
||||||
|
return this.has_many(LoginAttemptModel)
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,11 @@
|
|||||||
import {QueryRow} from '../../../db/types.ts'
|
import {QueryRow} from '../../../db/types.ts'
|
||||||
|
import ConnectionExecutable from '../ConnectionExecutable.ts'
|
||||||
|
import {Model} from '../../../model/Model.ts'
|
||||||
|
import {Collection} from '../../../../../lib/src/collection/Collection.ts'
|
||||||
|
|
||||||
export default abstract class ResultOperator<T> {
|
export default abstract class ResultOperator<T> {
|
||||||
|
public async process_eager_loads<T2>(query: ConnectionExecutable<T2>, results: Collection<T>): Promise<void> { }
|
||||||
|
|
||||||
abstract inflate_row(row: QueryRow): T
|
abstract inflate_row(row: QueryRow): T
|
||||||
abstract deflate_row(item: T): QueryRow
|
abstract deflate_row(item: T): QueryRow
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
import {Select} from '../../builder/type/Select.ts'
|
||||||
|
|
||||||
|
export class ModelSelect<T> extends Select<T> {
|
||||||
|
protected _withs: string[] = []
|
||||||
|
|
||||||
|
public with(related: string) {
|
||||||
|
this._withs.push(related)
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
public get eager_relations(): string[] {
|
||||||
|
return [...this._withs]
|
||||||
|
}
|
||||||
|
|
||||||
|
clone(): ModelSelect<T> {
|
||||||
|
const select = super.clone() as ModelSelect<T>
|
||||||
|
select._withs = this._withs
|
||||||
|
return select
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,30 @@
|
|||||||
import {Model} from '../Model.ts'
|
import {Model} from '../Model.ts'
|
||||||
import {HasOneOrMany} from './HasOneOrMany.ts'
|
import {HasOneOrMany} from './HasOneOrMany.ts'
|
||||||
|
import {Collection} from "../../../../lib/src/collection/Collection.ts";
|
||||||
|
import {Logging} from "../../../../lib/src/service/logging/Logging.ts";
|
||||||
|
|
||||||
export class HasOne<T extends Model<T>, T2 extends Model<T2>> extends HasOneOrMany<T, T2> {
|
export class HasOne<T extends Model<T>, T2 extends Model<T2>> extends HasOneOrMany<T, T2> {
|
||||||
|
protected _value?: T2
|
||||||
|
protected _loaded = false
|
||||||
|
|
||||||
public async get(): Promise<T2 | undefined> {
|
public async get(): Promise<T2 | undefined> {
|
||||||
return this.fetch().first()
|
return this.fetch().first()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public set_value(related: Collection<T2>) {
|
||||||
|
this._value = related.first()
|
||||||
|
this._loaded = true
|
||||||
|
|
||||||
|
if ( related.length > 1 ) {
|
||||||
|
this.make(Logging).warn(`HasOne relation result contained more than one value for ${this.qualified_local_key} -> ${this.qualified_local_key}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public get_value(): T2 | undefined {
|
||||||
|
return this._value
|
||||||
|
}
|
||||||
|
|
||||||
|
public is_loaded(): boolean {
|
||||||
|
return this._loaded
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue