Repository: prisma/prisma
File: packages/client/src/runtime/core/engines/client/ClientEngine.ts (TypeScript)

Code before:

  async request<T>(
    query: JsonQuery,
    { interactiveTransaction, customDataProxyFetch }: RequestOptions<unknown>,
  ): Promise<{ data: T }> {
    debug(`sending request`)
    const queryStr = JSON.stringify(query)

    const { executor, queryCompiler } = await this.#ensureStarted().catch((err) => {
      throw this.#transformRequestError(err, queryStr)
    })

    let queryPlan: QueryPlanNode
    if (isRawQuery(query)) {
      queryPlan = compileRawQuery(query)
    } else {
      try {
        queryPlan = this.#withLocalPanicHandler(() =>
          this.#withCompileSpan({
            queries: [query],
            execute: () => queryCompiler.compile(queryStr) as QueryPlanNode,
          }),
        )
      } catch (error) {
        throw this.#transformCompileError(error)
      }
    }

    try {
      debug(`query plan created`, queryPlan)

      // TODO: ORM-508 - Implement query plan caching by replacing all scalar values in the query with params automatically.
      const placeholderValues = {}
      const result = await executor.execute({
        plan: queryPlan,
        model: query.modelName,
        operation: query.action,
        placeholderValues,
        transaction: interactiveTransaction,
        batchIndex: undefined,
        customFetch: customDataProxyFetch?.(globalThis.fetch),
        queryInfo: {
          type: 'single',
          modelName: query.modelName,
          action: query.action,
          query: query.query,
        },
      })

      debug(`query plan executed`)


Review comment (refactor):
nitpicky

can we extract this into a function so we could `const queryPlan = funcNameTBD(query, queryStr)` and also ditch the `if-else`?

Code after:

  async request<T>(
    query: JsonQuery,
    { interactiveTransaction, customDataProxyFetch }: RequestOptions<unknown>,
  ): Promise<{ data: T }> {
    debug(`sending request`)

    const { executor, queryCompiler } = await this.#ensureStarted().catch((err) => {
      throw this.#transformRequestError(err, JSON.stringify(query))
    })
    const plan = this.#compileQuery(query, queryCompiler)

    try {
      debug(`query plan created`, plan)

      // TODO: ORM-508 - Implement query plan caching by replacing all scalar values in the query with params automatically.
      const placeholderValues = {}
      const result = await executor.execute({
        plan,
        model: query.modelName,
        operation: query.action,
        placeholderValues,
        transaction: interactiveTransaction,
        batchIndex: undefined,
        customFetch: customDataProxyFetch?.(globalThis.fetch),
        queryInfo: {
          type: 'single',
          modelName: query.modelName,
          action: query.action,
          query: query.query,
        },
      })

      debug(`query plan executed`)

      return { data: { [query.action]: result } as T }
    } catch (e: any) {
      throw this.#transformRequestError(e, JSON.stringify(query))
    }
  }

  async requestBatch<T>(
    queries: JsonQuery[],
    { transaction, customDataProxyFetch }: RequestBatchOptions<unknown>,
  ): Promise<BatchQueryEngineResult<T>[]> {
    if (queries.length === 0) {
      return []
    }

    const firstAction = queries[0].action
    const firstModelName = queries[0].modelName