* Executes this QB and returns the raw results, mapped to the property names (unless disabled via last parameter). * Use `method` to specify what kind of result you want to get (array/single/meta).
(method?: 'all' | 'get' | 'run', options?: ExecuteOptions | boolean)
| 2450 | * Use `method` to specify what kind of result you want to get (array/single/meta). |
| 2451 | */ |
| 2452 | async execute<U = any>(method?: 'all' | 'get' | 'run', options?: ExecuteOptions | boolean): Promise<U> { |
| 2453 | options = typeof options === 'boolean' ? { mapResults: options } : (options ?? {}); |
| 2454 | options.mergeResults ??= true; |
| 2455 | options.mapResults ??= true; |
| 2456 | const isRunType = [QueryType.INSERT, QueryType.UPDATE, QueryType.DELETE, QueryType.TRUNCATE].includes(this.type); |
| 2457 | method ??= isRunType ? 'run' : 'all'; |
| 2458 | |
| 2459 | if (!this.connectionType && (isRunType || this.context)) { |
| 2460 | this.connectionType = 'write'; |
| 2461 | } |
| 2462 | |
| 2463 | if (!this.#state.finalized && method === 'get' && this.type === QueryType.SELECT) { |
| 2464 | this.limit(1); |
| 2465 | } |
| 2466 | |
| 2467 | const query = this.toQuery(); |
| 2468 | const cached = await this.em?.tryCache<Entity, U>(this.mainAlias.entityName, this.#state.cache, [ |
| 2469 | 'qb.execute', |
| 2470 | query.sql, |
| 2471 | query.params, |
| 2472 | method, |
| 2473 | ]); |
| 2474 | |
| 2475 | if (cached?.data !== undefined) { |
| 2476 | return cached.data as unknown as U; |
| 2477 | } |
| 2478 | |
| 2479 | const loggerContext = { id: this.em?.id, ...this.loggerContext, ...this.#abortOptions }; |
| 2480 | const res = await this.getConnection().execute(query.sql, query.params, method, this.context, loggerContext); |
| 2481 | const meta = this.mainAlias.meta; |
| 2482 | |
| 2483 | if (!options.mapResults || !meta) { |
| 2484 | await this.em?.storeCache(this.#state.cache, cached!, res); |
| 2485 | return res as unknown as U; |
| 2486 | } |
| 2487 | |
| 2488 | if (method === 'run') { |
| 2489 | return res as U; |
| 2490 | } |
| 2491 | |
| 2492 | const joinedProps = this.driver.joinedProps(meta, this.#state.populate); |
| 2493 | let mapped: EntityData<Entity>[]; |
| 2494 | |
| 2495 | if (Array.isArray(res)) { |
| 2496 | const map: Dictionary = {}; |
| 2497 | mapped = res.map(r => this.driver.mapResult<Entity>(r as Entity, meta, this.#state.populate, this as any, map)!); |
| 2498 | |
| 2499 | if (options.mergeResults && joinedProps.length > 0) { |
| 2500 | mapped = this.driver.mergeJoinedResult(mapped, this.mainAlias.meta, joinedProps); |
| 2501 | } |
| 2502 | } else { |
| 2503 | mapped = [this.driver.mapResult<Entity>(res, meta, joinedProps, this as any)!]; |
| 2504 | } |
| 2505 | |
| 2506 | if (method === 'get') { |
| 2507 | await this.em?.storeCache(this.#state.cache, cached!, mapped[0]); |
| 2508 | return mapped[0] as U; |
| 2509 | } |
nothing calls this directly
no test coverage detected