Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 9x 9x 9x 9x 9x 9x 12x 9x 12x 12x 9x 9x 9x 9x 9x 12x 12x 12x 9x 4x 4x 9x 4x 4x 9x 9x 9x 9x 11x 11x 11x 11x 11x 3x 3x 18x 18x 3x | import { type Constructor, Model } from "@decaf-ts/decorator-validation"; import type { Executor, RawExecutor } from "../interfaces"; import type { FromSelector, GroupBySelector, OrderBySelector, SelectSelector, } from "./selectors"; import { Condition } from "./Condition"; import { findPrimaryKey, InternalError } from "@decaf-ts/db-decorators"; import { final } from "../utils/decorators"; import type { CountOption, DistinctOption, LimitOption, MaxOption, MinOption, OffsetOption, OrderAndGroupOption, SelectOption, WhereOption, } from "./options"; import { Paginatable } from "../interfaces/Paginatable"; import { Paginator } from "./Paginator"; import { Adapter } from "../persistence"; import { QueryError } from "./errors"; export abstract class Statement<Q, M extends Model, R> implements Executor<R>, RawExecutor<Q>, Paginatable<M, R, Q> { protected readonly selectSelector?: SelectSelector<M>[]; protected distinctSelector?: SelectSelector<M>; protected maxSelector?: SelectSelector<M>; protected minSelector?: SelectSelector<M>; protected countSelector?: SelectSelector<M>; protected fromSelector!: Constructor<M>; protected whereCondition?: Condition<M>; protected orderBySelector?: OrderBySelector<M>; protected groupBySelector?: GroupBySelector<M>; protected limitSelector?: number; protected offsetSelector?: number; protected constructor(protected adapter: Adapter<any, Q, any, any>) {} select< // eslint-disable-next-line @typescript-eslint/no-unused-vars S extends readonly SelectSelector<M>[], >(): SelectOption<M, M[]>; select<S extends readonly SelectSelector<M>[]>( selector: readonly [...S] ): SelectOption<M, Pick<M, S[number]>[]>; @final() select<S extends readonly SelectSelector<M>[]>( selector?: readonly [...S] ): SelectOption<M, M[]> | SelectOption<M, Pick<M, S[number]>[]> { Object.defineProperty(this, "selectSelector", { value: selector, writable: false, }); return this as SelectOption<M, M[]> | SelectOption<M, Pick<M, S[number]>[]>; } @final() distinct<S extends SelectSelector<M>>( selector: S ): DistinctOption<M, M[S][]> { this.distinctSelector = selector; return this as DistinctOption<M, M[S][]>; } @final() max<S extends SelectSelector<M>>(selector: S): MaxOption<M, M[S]> { this.maxSelector = selector; return this as MaxOption<M, M[S]>; } @final() min<S extends SelectSelector<M>>(selector: S): MinOption<M, M[S]> { this.minSelector = selector; return this as MinOption<M, M[S]>; } @final() count<S extends SelectSelector<M>>(selector?: S): CountOption<M, number> { this.countSelector = selector; return this as CountOption<M, number>; } @final() public from(selector: FromSelector<M>): WhereOption<M, R> { this.fromSelector = ( typeof selector === "string" ? Model.get(selector) : selector ) as Constructor<M>; Iif (!this.fromSelector) throw new QueryError(`Could not find selector model: ${selector}`); return this; } @final() public where(condition: Condition<M>): OrderAndGroupOption<M, R> { this.whereCondition = condition; return this; } @final() public orderBy( selector: OrderBySelector<M> ): LimitOption<M, R> & OffsetOption<R> { this.orderBySelector = selector; return this; } @final() public groupBy(selector: GroupBySelector<M>): LimitOption<M, R> { this.groupBySelector = selector; return this; } @final() public limit(value: number): OffsetOption<R> { this.limitSelector = value; return this; } @final() public offset(value: number): Executor<R> { this.offsetSelector = value; return this; } @final() async execute(): Promise<R> { try { const query: Q = this.build(); return (await this.raw(query)) as R; } catch (e: unknown) { throw new InternalError(e as Error); } } async raw<R>(rawInput: Q): Promise<R> { const results = await this.adapter.raw<R>(rawInput); if (!this.selectSelector) return results; const pkAttr = findPrimaryKey( new (this.fromSelector as Constructor<M>)() ).id; const processor = function recordProcessor( this: Statement<Q, M, R>, r: any ) { const id = r[pkAttr]; return this.adapter.revert( r, this.fromSelector as Constructor<any>, pkAttr, id ) as any; }.bind(this as any); if (Array.isArray(results)) return results.map(processor) as R; return processor(results) as R; } protected abstract build(): Q; protected abstract parseCondition(condition: Condition<M>): Q; abstract paginate(size: number): Promise<Paginator<M, R, Q>>; } |