Repository: prisma/prisma
File: packages/cli/src/utils/checkpoint.ts (TypeScript)

Code before:
 * Tries to read some data from the Prisma Schema
 * if an error occurs it will silently fail and return undefined values
 */
export async function tryToReadDataFromSchema(schemaPath?: string, schemaPathFromConfig?: string) {
  let schemaProvider: string | undefined
  let schemaPreviewFeatures: string[] | undefined
  let schemaGeneratorsProviders: string[] | undefined

  try {
    const schemaContext = await loadSchemaContext({
      schemaPathFromArg: schemaPath,
      schemaPathFromConfig,
      printLoadMessage: false,
    })

    if (schemaContext.datasources.length > 0) {
      schemaProvider = schemaContext.datasources[0].provider
    }

    // Example 'prisma-client-js'
    schemaGeneratorsProviders = schemaContext.generators
      // Check that value is defined
      .filter((generator) => generator && generator.provider)
      .map((generator) => parseEnvValue(generator.provider))

    const clientGeneratorProviders = new Set(['prisma-client', 'prisma-client-js'])
    const previewFeatures = schemaContext.generators
      .filter((generator) => {
        const provider = generator?.provider ? parseEnvValue(generator.provider) : undefined
        return provider && clientGeneratorProviders.has(provider)
      })
      .flatMap((generator) => generator.previewFeatures ?? [])

    if (previewFeatures.length > 0) {
      const seen = new Set<string>()
      schemaPreviewFeatures = []
      for (const feature of previewFeatures) {
        if (!seen.has(feature)) {
          seen.add(feature)
          schemaPreviewFeatures.push(feature)
        }
      }
    }
  } catch (e) {
    debug(
      'Error from tryToReadDataFromSchema() while processing the schema. This is not a fatal error. It will continue without the processed data.',
    )
    debug(e)
  }

  return {

Review comment (suggestion):
@copilot make `clientGeneratorProviders` an array instead of `Set`, linear search is faster in an array

Code after:
 * Tries to read some data from the Prisma Schema
 * if an error occurs it will silently fail and return undefined values
 */
export async function tryToReadDataFromSchema(schemaPath?: string, schemaPathFromConfig?: string) {
  let schemaProvider: string | undefined
  let schemaPreviewFeatures: string[] | undefined
  let schemaGeneratorsProviders: string[] | undefined

  try {
    const schemaContext = await loadSchemaContext({
      schemaPathFromArg: schemaPath,
      schemaPathFromConfig,
      printLoadMessage: false,
    })

    if (schemaContext.datasources.length > 0) {
      schemaProvider = schemaContext.datasources[0].provider
    }

    // Example 'prisma-client-js'
    schemaGeneratorsProviders = schemaContext.generators
      // Check that value is defined
      .filter((generator) => generator && generator.provider)
      .map((generator) => parseEnvValue(generator.provider))

    const clientGeneratorProviders = ['prisma-client', 'prisma-client-js']
    const previewFeatures = schemaContext.generators
      .filter((generator) => {
        const provider = generator?.provider ? parseEnvValue(generator.provider) : undefined
        return provider !== undefined && clientGeneratorProviders.includes(provider)
      })
      .flatMap((generator) => generator.previewFeatures ?? [])

    if (previewFeatures.length > 0) {
      schemaPreviewFeatures = Array.from(new Set(previewFeatures))
    }
  } catch (e) {
    debug(
      'Error from tryToReadDataFromSchema() while processing the schema. This is not a fatal error. It will continue without the processed data.',
    )
    debug(e)
  }

  return {
    schemaProvider,
    schemaPreviewFeatures,
    schemaGeneratorsProviders,
  }
}

/*