import { DEFAULT_PRIMARY_ID , ModelConstructorInterface , ModelInterface , QueryBuilderInterface , Relation , RelationTypes , } from "../models/model.interface.js" ; import { Model } from "../models/model.js" ; import { QueryBuilder } from "../models/query-builder.js" ; import { Connector } from "../connection/connector.js" ; import { RelationInterface } from "./relation.interface.js" ; export abstract class Relations extends QueryBuilder implements RelationInterface , QueryBuilderInterface { /**
     * IDBDatabase
     */ public abstract db : IDBDatabase ; /**
     * Connection instance
     */ public abstract connector : Connector ; param parentResults param relationResults param relation public abstract bindResults ( parentResults , relationResults , relation ) ; param results public abstract fetch ( results ) ; param model param relation public filteredModel ( model , relation ) { model = this . bindBuilder ( model ) ; const builder = new QueryBuilder ( ) ; if ( relation . func ) { const query = relation . func ( builder ) ; model . builder = model . builder . concat ( query . builder ) ; model . with ( builder . relations ) ; } return model ; } public bindBuilder ( model ) { model . builder = model . builder . concat ( this . builder ) ; model . with ( this . relations ) ; return model ; } param relation public getRelationModel ( relation ) { let model ; if ( typeof relation . model === "string" ) { const tableSchema = this . connector . migrationSchema . tables . find ( table => table . name === relation . model ) ; if ( ! tableSchema ) { throw new Error ( 'Table schema not found' ) ; } model = new Model ( this . db , tableSchema , this . connector ) ; } else { model = < ModelInterface > relation . model ; } return model ; } param model param relation public getLocalKey ( model , relation ) { const primaryKey = model . table . primary || DEFAULT_PRIMARY_ID ; return relation . localKey ? relation . localKey : primaryKey ; } param model param relation public getAttributeName ( model , relation ) { return relation . attributeName || model . table . name ; } }