Skip to content

Commit

Permalink
fix(firestore-bigquery-export): extract serialized timestamps correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cabljac committed Nov 28, 2023
1 parent 37d7376 commit 2071cfb
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class Partitioning {

private isValidPartitionTypeDate(value) {
/* Check if valid timestamp value from sdk */
if (value instanceof firebase.firestore.Timestamp) return true;
// if (value instanceof firebase.firestore.Timestamp) return true;
if (isTimestampLike(value)) return true;

/* Check if valid date/timstemap, expedted result from production */
if (value && value.toDate && value.toDate()) return true;
Expand Down Expand Up @@ -210,6 +211,15 @@ export class Partitioning {

if (this.isValidPartitionTypeDate(fieldValue)) {
/* Return converted console value */
if (isTimestampLike(fieldValue)) {
const convertedTimestampFieldValue = convertToTimestamp(fieldValue);
return {
[fieldName]: this.convertDateValue(
convertedTimestampFieldValue.toDate()
),
};
}

if (fieldValue.toDate) {
return { [fieldName]: this.convertDateValue(fieldValue.toDate()) };
}
Expand Down Expand Up @@ -309,3 +319,27 @@ export class Partitioning {
}
}
}

type TimestampLike = {
_seconds: number;
_nanoseconds: number;
};

const isTimestampLike = (value: any): value is TimestampLike => {
if (value instanceof firebase.firestore.Timestamp) return true;
return (
typeof value === "object" &&
value !== null &&
"_seconds" in value &&
typeof value["_seconds"] === "number" &&
"_nanoseconds" in value &&
typeof value["_nanoseconds"] === "number"
);
};

const convertToTimestamp = (
value: TimestampLike
): firebase.firestore.Timestamp => {
if (value instanceof firebase.firestore.Timestamp) return value;
return new firebase.firestore.Timestamp(value._seconds, value._nanoseconds);
};

0 comments on commit 2071cfb

Please sign in to comment.