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

Code before:
    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]

    // Convert array buffers to arrays of bytes.
    // Base64 would've been more efficient but would collide with the existing
    // logic that treats string values of type Bytes as raw UTF-8 bytes that was
    // implemented for other adapters.
    if (value instanceof ArrayBuffer) {
      result[i] = Buffer.from(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):
likewise, let's not use `Uint8Array` and not `Buffer` unless absolutely necessary.

Even better, maybe we shouldn't even handle this case on this level, it should be the `DataMapper` that converts arbitrary `ArrayBuffer`s to `Uint8Array`s if necessary at all.

Code after:
    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
    }

    result[i] = value
  }