|
|
|
import ResultOperator from '../builder/type/result/ResultOperator.ts'
|
|
|
|
import {Model} from './Model.ts'
|
|
|
|
import {Injectable} from '../../../di/src/decorator/Injection.ts'
|
|
|
|
import {QueryRow} from '../db/types.ts'
|
|
|
|
import Instantiable from '../../../di/src/type/Instantiable.ts'
|
|
|
|
import {make} from '../../../di/src/global.ts'
|
|
|
|
import ConnectionExecutable from '../builder/type/ConnectionExecutable.ts'
|
|
|
|
import {Collection} from '../../../lib/src/collection/Collection.ts'
|
|
|
|
import {ModelSelect} from './query/ModelSelect.ts'
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export default class ModelResultOperator<T extends Model<T>> extends ResultOperator<T> {
|
|
|
|
constructor(
|
|
|
|
protected ModelClass: Instantiable<T>,
|
|
|
|
) {
|
|
|
|
super()
|
|
|
|
}
|
|
|
|
|
|
|
|
inflate_row(row: QueryRow): T {
|
|
|
|
return make(this.ModelClass).assume_from_source(row)
|
|
|
|
}
|
|
|
|
|
|
|
|
deflate_row(item: T): QueryRow {
|
|
|
|
return item.to_row()
|
|
|
|
}
|
|
|
|
|
|
|
|
public async process_eager_loads<T2>(query: ConnectionExecutable<T2>, results: Collection<T>) {
|
|
|
|
if ( query instanceof ModelSelect ) {
|
|
|
|
const eagers = query.eager_relations
|
|
|
|
const model = new this.ModelClass()
|
|
|
|
|
|
|
|
for ( const rel_name of eagers ) {
|
|
|
|
const relation = model.get_relation(rel_name)
|
|
|
|
const select = await relation.build_eager_query(query, results)
|
|
|
|
const all_related = await select.results()
|
|
|
|
results.each(result => {
|
|
|
|
const result_relation = result.get_relation(rel_name)
|
|
|
|
const result_related = result_relation.match_results(all_related as any)
|
|
|
|
result_relation.set_value(result_related as any)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|