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.
46 lines
1.3 KiB
46 lines
1.3 KiB
import {Model} from "../Model.ts";
|
|
import {Relation} from "./Relation.ts";
|
|
import ConnectionExecutable from "../../builder/type/ConnectionExecutable.ts";
|
|
import {WhereBuilder} from "../../builder/type/WhereBuilder.ts";
|
|
|
|
export abstract class HasOneOrMany<T extends Model<T>, T2 extends Model<T2>> extends Relation<T, T2> {
|
|
constructor(
|
|
protected parent: T,
|
|
public readonly related: T2,
|
|
protected foreign_key_spec?: string,
|
|
protected local_key_spec?: string,
|
|
) { super(parent, related) }
|
|
|
|
public get foreign_key() {
|
|
return this.foreign_key_spec || this.parent.key_name()
|
|
}
|
|
|
|
public get local_key() {
|
|
return this.local_key_spec || this.foreign_key
|
|
}
|
|
|
|
public get qualified_foreign_key() {
|
|
return this.related.qualify_column(this.foreign_key)
|
|
}
|
|
|
|
public get qualified_local_key() {
|
|
return this.related.qualify_column(this.local_key)
|
|
}
|
|
|
|
protected get parent_value() {
|
|
// @ts-ignore
|
|
return this.parent[this.local_key]
|
|
}
|
|
|
|
public query(): ConnectionExecutable<T2> {
|
|
return this.builder().select('*')
|
|
}
|
|
|
|
public scope_query(where: WhereBuilder) {
|
|
where.where(where => {
|
|
where.where(this.qualified_foreign_key, '=', this.parent_value)
|
|
.whereRaw(this.qualified_foreign_key, 'IS NOT', 'NULL')
|
|
})
|
|
}
|
|
}
|