Repository: prisma/prisma
File: packages/adapter-better-sqlite3/src/conversion.ts (TypeScript)

Code before:
}

function inferObjectType(value: {}): ColumnType {
  if (value instanceof ArrayBuffer) {
    return ColumnTypeEnum.Bytes
  }
  throw new UnexpectedTypeError(value)
}

class UnexpectedTypeError extends Error {
  name = 'UnexpectedTypeError'
  constructor(value: unknown) {
    const type = typeof value
    const repr = type === 'object' ? JSON.stringify(value) : String(value)
    super(`unexpected value of type ${type}: ${repr}`)
  }
}

export function mapRow(row: Row, columnTypes: ColumnType[]): ResultValue[] {
  const result: ResultValue[] = []

  for (let i = 0; i < row.length; i++) {
    const value = row[i]

    if (value instanceof ArrayBuffer) {
      result[i] = new Uint8Array(value)
      continue
    }

    // If an integer is required and the current number isn't one,
    // discard the fractional part.
    if (
      typeof value === 'number' &&
      (columnTypes[i] === ColumnTypeEnum.Int32 || columnTypes[i] === ColumnTypeEnum.Int64) &&
      !Number.isInteger(value)
    ) {
      result[i] = Math.trunc(value)
      continue
    }

    // Decode DateTime values saved as numeric timestamps which is the
    // format used by the native quaint sqlite connector.
    if (['number', 'bigint'].includes(typeof value) && columnTypes[i] === ColumnTypeEnum.DateTime) {
      result[i] = new Date(Number(value)).toISOString()
      continue
    }

    // Convert bigint to string as we can only use JSON-encodable types here.
    if (typeof value === 'bigint') {
      result[i] = value.toString()
      continue

Review comment (suggestion):
`better-sqlite3` returns `Buffer` for `BLOB` affinity according to https://github.com/WiseLibs/better-sqlite3/blob/6209be238d6a1b181f516e4e636986604b0f62e1/docs/api.md?plain=1#L663.

`Buffer` is a subclass of `Uint8Array`.
`ArrayBuffer` is not an ancestor of `Uint8Array`.

This code seems to be unreachable.

Code after:
}

function inferObjectType(value: {}): ColumnType {
  if (value instanceof ArrayBuffer) {
    return ColumnTypeEnum.Bytes
  }
  throw new UnexpectedTypeError(value)
}

class UnexpectedTypeError extends Error {
  name = 'UnexpectedTypeError'
  constructor(value: unknown) {
    const type = typeof value
    const repr = type === 'object' ? JSON.stringify(value) : String(value)
    super(`unexpected value of type ${type}: ${repr}`)
  }
}

export function mapRow(row: Row, columnTypes: ColumnType[]): ResultValue[] {
  const result: ResultValue[] = []

  for (let i = 0; i < row.length; i++) {
    const value = row[i]

    // If an integer is required and the current number isn't one,
    // discard the fractional part.
    if (
      typeof value === 'number' &&
      (columnTypes[i] === ColumnTypeEnum.Int32 || columnTypes[i] === ColumnTypeEnum.Int64) &&
      !Number.isInteger(value)
    ) {
      result[i] = Math.trunc(value)
      continue
    }

    // Decode DateTime values saved as numeric timestamps which is the
    // format used by the native quaint sqlite connector.
    if (['number', 'bigint'].includes(typeof value) && columnTypes[i] === ColumnTypeEnum.DateTime) {
      result[i] = new Date(Number(value)).toISOString()
      continue
    }

    // Convert bigint to string as we can only use JSON-encodable types here.
    if (typeof value === 'bigint') {
      result[i] = value.toString()
      continue
    }

    result[i] = value
  }