function convertUnsupportedDataTypes()

in bigquery-firestore-export/functions/src/helper.ts [197:241]


function convertUnsupportedDataTypes(row: any): any {
  if (row === null || typeof row !== 'object') {
    return row;
  }

  const result = {...row};

  /**
   * Checks if a value is a BigQuery datetime-related type
   */
  function shouldConvertToTimestamp(
    value: any
  ): value is
    | BigQueryTimestamp
    | BigQueryDate
    | BigQueryTime
    | BigQueryDatetime {
    return (
      value instanceof BigQueryTimestamp ||
      value instanceof BigQueryDate ||
      value instanceof BigQueryTime ||
      value instanceof BigQueryDatetime
    );
  }

  for (const [key, value] of Object.entries(row)) {
    if (value === null || typeof value !== 'object') continue;

    if (shouldConvertToTimestamp(value)) {
      // Convert all BigQuery datetime types to Firestore Timestamp
      result[key] = admin.firestore.Timestamp.fromDate(new Date(value.value));
    } else if (value instanceof Date) {
      result[key] = admin.firestore.Timestamp.fromDate(value);
    } else if (value instanceof Buffer) {
      result[key] = new Uint8Array(value);
    } else if (value instanceof Geography) {
      result[key] = value.value;
    } else if (typeof value === 'object') {
      // Recursively convert nested objects
      result[key] = convertUnsupportedDataTypes(value);
    }
  }

  return result;
}