start relations (has one or many; has one; has many)
This commit is contained in:
@@ -6,7 +6,7 @@ import {Connection} from '../../db/Connection.ts'
|
||||
import {ResultCollection} from './result/ResultCollection.ts'
|
||||
import {ResultIterable} from './result/ResultIterable.ts'
|
||||
import ResultOperator from './result/ResultOperator.ts'
|
||||
import {Collection} from '../../../../lib/src/collection/Collection.ts'
|
||||
import {collect, Collection} from '../../../../lib/src/collection/Collection.ts'
|
||||
import NoTargetOperatorError from '../../error/NoTargetOperatorError.ts'
|
||||
|
||||
export default abstract class ConnectionExecutable<T> {
|
||||
@@ -21,12 +21,17 @@ export default abstract class ConnectionExecutable<T> {
|
||||
throw new Error('Unable to execute database item: no target connection.')
|
||||
}
|
||||
|
||||
if ( !this.__target_operator ) throw new NoTargetOperatorError()
|
||||
|
||||
const query = `SELECT * FROM (${this.sql(0)}) AS target_query OFFSET ${i} LIMIT 1`
|
||||
const result = await this.__target_connection.query(query)
|
||||
const row = result.rows.first()
|
||||
if ( row ) return this.__target_operator.inflate_row(row)
|
||||
if ( row ) {
|
||||
if ( !this.__target_operator ) throw new NoTargetOperatorError()
|
||||
|
||||
const inflated = this.__target_operator.inflate_row(row)
|
||||
await this.__target_operator.process_eager_loads(this, collect([inflated]))
|
||||
|
||||
return inflated
|
||||
}
|
||||
}
|
||||
|
||||
async get_range(start: number, end: number): Promise<Collection<T>> {
|
||||
@@ -36,10 +41,15 @@ export default abstract class ConnectionExecutable<T> {
|
||||
|
||||
const query = `SELECT * FROM (${this.sql(0)}) AS target_query OFFSET ${start} LIMIT ${(end - start) + 1}`
|
||||
const result = await this.__target_connection.query(query)
|
||||
return result.rows.map(row => {
|
||||
const inflated = result.rows.map(row => {
|
||||
if ( !this.__target_operator ) throw new NoTargetOperatorError()
|
||||
return this.__target_operator.inflate_row(row)
|
||||
})
|
||||
|
||||
if ( !this.__target_operator ) throw new NoTargetOperatorError()
|
||||
await this.__target_operator.process_eager_loads(this, inflated)
|
||||
|
||||
return inflated
|
||||
}
|
||||
|
||||
iterator(): ResultIterable<T> {
|
||||
|
||||
@@ -20,6 +20,7 @@ import {FullOuterJoin} from './join/FullOuterJoin.ts'
|
||||
import {HavingBuilder} from './HavingBuilder.ts'
|
||||
import ConnectionExecutable from './ConnectionExecutable.ts'
|
||||
import {Scope} from '../Scope.ts'
|
||||
import {isInstantiable} from "../../../../di/src/type/Instantiable.ts";
|
||||
|
||||
export type WhereBuilderFunction = (group: WhereBuilder) => any
|
||||
export type HavingBuilderFunction = (group: HavingBuilder) => any
|
||||
@@ -78,6 +79,37 @@ export class Select<T> extends ConnectionExecutable<T> {
|
||||
return this
|
||||
}
|
||||
|
||||
clear_fields() {
|
||||
this._fields = []
|
||||
return this
|
||||
}
|
||||
|
||||
clone(): Select<T> {
|
||||
const constructor = this.constructor as typeof Select
|
||||
if ( !isInstantiable<Select<T>>(constructor) ) {
|
||||
throw new TypeError(`Parent constructor is not instantiable.`)
|
||||
}
|
||||
|
||||
const select = new constructor()
|
||||
|
||||
select._fields = this._fields
|
||||
select._source = this._source
|
||||
select._wheres = this._wheres
|
||||
select._scopes = this._scopes
|
||||
select._havings = this._havings
|
||||
select._limit = this._limit
|
||||
select._offset = this._offset
|
||||
select._joins = this._joins
|
||||
select._distinct = this._distinct
|
||||
select._group_by = this._group_by
|
||||
select._order = this._order
|
||||
|
||||
select.__target_connection = this.__target_connection
|
||||
select.__target_operator = this.__target_operator
|
||||
|
||||
return select
|
||||
}
|
||||
|
||||
group_by(...groupings: string[]) {
|
||||
this._group_by = groupings
|
||||
return this
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
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> {
|
||||
public async process_eager_loads<T2>(query: ConnectionExecutable<T2>, results: Collection<T>): Promise<void> { }
|
||||
|
||||
abstract inflate_row(row: QueryRow): T
|
||||
abstract deflate_row(item: T): QueryRow
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user