File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments