Skip to content

Commit fd2f8b3

Browse files
committed
chore: add initial DAO/DTO interfaces
Prep for Firebase v10/AngularFire v16 migration
1 parent f9fd161 commit fd2f8b3

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { FieldValue, Timestamp, WithFieldValue } from '@firebase/firestore';
2+
import { toTimestamp } from '@app/data/models/dto/temporal';
3+
import { Temporal } from 'temporal-polyfill';
4+
5+
export const setToDto = <T>(set: FieldValue | WithFieldValue<Set<T>>): FieldValue | T[] => {
6+
if (set instanceof FieldValue) return set;
7+
if (set instanceof Set) return [...set];
8+
9+
throw new Error('WithFieldValue of a Set is not supported');
10+
};
11+
12+
export const temporalInstantToDto = (instant: FieldValue | WithFieldValue<Temporal.Instant>): FieldValue | Timestamp => {
13+
if (instant instanceof FieldValue) return instant;
14+
if (instant instanceof Temporal.Instant) return toTimestamp(instant);
15+
16+
throw new Error('WithFieldValue of a Temporal.Instant is not supported');
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Temporal } from 'temporal-polyfill';
2+
3+
export { HasId } from '../common/id';
4+
5+
export interface WithTimestampMetadata {
6+
/** Date when this document was last modified at. */
7+
lastModified: Temporal.Instant;
8+
/** Date when this document was created. */
9+
createdAt: Temporal.Instant;
10+
}
11+
12+
export interface WithTimestampMetadataJson {
13+
/** Date when this document was last modified at, in ISO8601 form. */
14+
lastModified: string;
15+
/** Date when this document was created, in ISO8601 form. */
16+
createdAt: string;
17+
}

src/app/data/models/dao/parsers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Temporal } from 'temporal-polyfill';
2+
import { Timestamp } from '@firebase/firestore';
3+
4+
export const toBoolean = (value: unknown): boolean => {
5+
if (typeof value === 'boolean') return value;
6+
if (typeof value === 'string') return value === 'true';
7+
return Boolean(value);
8+
}
9+
10+
export const toInstantOrNull = (value: unknown): Temporal.Instant | null => {
11+
if (value instanceof Temporal.Instant) return value;
12+
if (value instanceof Timestamp) return Temporal.Instant.fromEpochMilliseconds(value.toMillis());
13+
if (typeof value === 'string') return Temporal.Instant.from(value);
14+
if (typeof value === 'number') return Temporal.Instant.fromEpochMilliseconds(value);
15+
return null;
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Temporal } from 'temporal-polyfill';
2+
import { Timestamp } from '@firebase/firestore';
3+
4+
/** Converts the specified `instant` to its Firestore {@link Timestamp} equivalent. */
5+
export const toTimestamp = (instant: Temporal.Instant): Timestamp => Timestamp.fromMillis(instant.epochMilliseconds);

0 commit comments

Comments
 (0)