Create belongs_to_one and belongs_to_many inverse helpers
This commit is contained in:
parent
81906b02bc
commit
bd1c221e36
@ -29,6 +29,10 @@ console.log(sel.clone())
|
|||||||
const gm = await sel.results().first()
|
const gm = await sel.results().first()
|
||||||
|
|
||||||
console.log(gm)
|
console.log(gm)
|
||||||
console.log(await gm.login_attempts())
|
|
||||||
|
const la = (await gm.login_attempts()).first()
|
||||||
|
console.log(la)
|
||||||
|
|
||||||
|
console.log(await la.user())
|
||||||
|
|
||||||
// console.log(await las.fetchRelated())
|
// console.log(await las.fetchRelated())
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import {Model} from '../../orm/src/model/Model.ts'
|
import {Model} from '../../orm/src/model/Model.ts'
|
||||||
import {Field} from '../../orm/src/model/Field.ts'
|
import {Field} from '../../orm/src/model/Field.ts'
|
||||||
import {Type} from '../../orm/src/db/types.ts'
|
import {Type} from '../../orm/src/db/types.ts'
|
||||||
|
import UserModel from "./User.model.ts";
|
||||||
|
import {Relation} from "../../orm/src/model/relation/decorators.ts";
|
||||||
|
|
||||||
export default class LoginAttemptModel extends Model<LoginAttemptModel> {
|
export default class LoginAttemptModel extends Model<LoginAttemptModel> {
|
||||||
protected static table = 'daton_login_attempts'
|
protected static table = 'daton_login_attempts'
|
||||||
@ -17,4 +19,9 @@ export default class LoginAttemptModel extends Model<LoginAttemptModel> {
|
|||||||
|
|
||||||
@Field(Type.timestamp)
|
@Field(Type.timestamp)
|
||||||
protected attempt_date!: Date
|
protected attempt_date!: Date
|
||||||
|
|
||||||
|
@Relation()
|
||||||
|
public user() {
|
||||||
|
return this.belongs_to_one(UserModel, 'login_attempts')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ import Instantiable from '../../../di/src/type/Instantiable.ts'
|
|||||||
import {HasMany} from './relation/HasMany.ts'
|
import {HasMany} from './relation/HasMany.ts'
|
||||||
import {ModelSelect} from "./query/ModelSelect.ts";
|
import {ModelSelect} from "./query/ModelSelect.ts";
|
||||||
import {Relation} from "./relation/Relation.ts";
|
import {Relation} from "./relation/Relation.ts";
|
||||||
|
import {HasOneOrMany} from "./relation/HasOneOrMany.ts";
|
||||||
|
|
||||||
// TODO separate read/write connections
|
// TODO separate read/write connections
|
||||||
// TODO manual dirty flags
|
// TODO manual dirty flags
|
||||||
@ -979,6 +980,38 @@ abstract class Model<T extends Model<T>> extends Builder<T> implements Rehydrata
|
|||||||
return new HasMany(this as any, new related() as any, foreign_key, local_key)
|
return new HasMany(this as any, new related() as any, foreign_key, local_key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the inverse of a has one or many relation.
|
||||||
|
* @param {typeof Model} related
|
||||||
|
* @param {string} relation_name - the name of the relation function on the inverse
|
||||||
|
*/
|
||||||
|
public belongs_to_one<T extends Model<T>>(related: Instantiable<T>, relation_name: string) {
|
||||||
|
const related_inst = new related()
|
||||||
|
const relation = related_inst.get_relation(relation_name)
|
||||||
|
|
||||||
|
if ( !(relation instanceof HasOneOrMany) ) {
|
||||||
|
throw new TypeError(`Cannot create belongs to one relation. Inverse relation must be has one or many.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HasOne(this as any, related_inst, relation.local_key, relation.foreign_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the inverse of a has one or many relation.
|
||||||
|
* @param {typeof Model} related
|
||||||
|
* @param {string} relation_name - the name of the relation function on the inverse
|
||||||
|
*/
|
||||||
|
public belongs_to_many<T extends Model<T>>(related: Instantiable<T>, relation_name: string) {
|
||||||
|
const related_inst = new related()
|
||||||
|
const relation = related_inst.get_relation(relation_name)
|
||||||
|
|
||||||
|
if ( !(relation instanceof HasOneOrMany) ) {
|
||||||
|
throw new TypeError(`Cannot create belongs to one relation. Inverse relation must be has one or many.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HasMany(this as any, related_inst, relation.local_key, relation.foreign_key)
|
||||||
|
}
|
||||||
|
|
||||||
public get_relation<T2 extends Model<T2>>(name: string): Relation<T, T2> {
|
public get_relation<T2 extends Model<T2>>(name: string): Relation<T, T2> {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const rel: any = this[name]()
|
const rel: any = this[name]()
|
||||||
|
Loading…
Reference in New Issue
Block a user