17
17
18
18
import * as firestore from '@firebase/firestore-types' ;
19
19
20
+ import * as api from '../protos/firestore_proto_api' ;
21
+
20
22
import { FirebaseApp } from '@firebase/app-types' ;
21
23
import { FirebaseService , _FirebaseApp } from '@firebase/app-types/private' ;
22
24
import { DatabaseId , DatabaseInfo } from '../core/database_info' ;
@@ -42,12 +44,6 @@ import { IndexedDbPersistence } from '../local/indexeddb_persistence';
42
44
import { LruParams } from '../local/lru_garbage_collector' ;
43
45
import { Document , MaybeDocument , NoDocument } from '../model/document' ;
44
46
import { DocumentKey } from '../model/document_key' ;
45
- import {
46
- ArrayValue ,
47
- FieldValue ,
48
- RefValue ,
49
- ServerTimestampValue
50
- } from '../model/field_value' ;
51
47
import { DeleteMutation , Mutation , Precondition } from '../model/mutation' ;
52
48
import { FieldPath , ResourcePath } from '../model/path' ;
53
49
import { PlatformSupport } from '../platform/platform' ;
@@ -106,6 +102,8 @@ import {
106
102
import { UserDataWriter } from './user_data_writer' ;
107
103
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types' ;
108
104
import { Provider } from '@firebase/component' ;
105
+ import { isServerTimestamp } from '../model/server_timestamps' ;
106
+ import { JsonProtoSerializer } from '../remote/serializer' ;
109
107
110
108
// settings() defaults:
111
109
const DEFAULT_HOST = 'firestore.googleapis.com' ;
@@ -550,7 +548,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
550
548
return value ;
551
549
}
552
550
} ;
553
- return new UserDataReader ( preConverter ) ;
551
+ return new UserDataReader (
552
+ new JsonProtoSerializer ( databaseId , {
553
+ useProto3Json : PlatformSupport . getPlatform ( ) . usesProto3Json
554
+ } ) ,
555
+ preConverter
556
+ ) ;
554
557
}
555
558
556
559
private static databaseIdFromApp ( app : FirebaseApp ) : DatabaseId {
@@ -1352,14 +1355,20 @@ export interface SnapshotOptions extends firestore.SnapshotOptions {}
1352
1355
1353
1356
export class DocumentSnapshot < T = firestore . DocumentData >
1354
1357
implements firestore . DocumentSnapshot < T > {
1358
+ private readonly _serializer : JsonProtoSerializer ;
1359
+
1355
1360
constructor (
1356
1361
private _firestore : Firestore ,
1357
1362
private _key : DocumentKey ,
1358
1363
public _document : Document | null ,
1359
1364
private _fromCache : boolean ,
1360
1365
private _hasPendingWrites : boolean ,
1361
1366
private readonly _converter ?: firestore . FirestoreDataConverter < T >
1362
- ) { }
1367
+ ) {
1368
+ this . _serializer = new JsonProtoSerializer ( this . _firestore . _databaseId , {
1369
+ useProto3Json : PlatformSupport . getPlatform ( ) . usesProto3Json
1370
+ } ) ;
1371
+ }
1363
1372
1364
1373
data ( options ?: firestore . SnapshotOptions ) : T | undefined {
1365
1374
validateBetweenNumberOfArgs ( 'DocumentSnapshot.data' , arguments , 0 , 1 ) ;
@@ -1381,11 +1390,13 @@ export class DocumentSnapshot<T = firestore.DocumentData>
1381
1390
} else {
1382
1391
const userDataWriter = new UserDataWriter (
1383
1392
this . _firestore ,
1393
+ this . _serializer ,
1384
1394
this . _firestore . _areTimestampsInSnapshotsEnabled ( ) ,
1385
1395
options . serverTimestamps ,
1386
1396
/* converter= */ undefined
1387
1397
) ;
1388
- return userDataWriter . convertValue ( this . _document . data ( ) ) as T ;
1398
+ const proto = this . _document . data ( ) . toProto ( ) ;
1399
+ return userDataWriter . convertValue ( proto ) as T ;
1389
1400
}
1390
1401
}
1391
1402
}
@@ -1403,6 +1414,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
1403
1414
if ( value !== null ) {
1404
1415
const userDataWriter = new UserDataWriter (
1405
1416
this . _firestore ,
1417
+ this . _serializer ,
1406
1418
this . _firestore . _areTimestampsInSnapshotsEnabled ( ) ,
1407
1419
options . serverTimestamps ,
1408
1420
this . _converter
@@ -1490,7 +1502,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1490
1502
] ;
1491
1503
validateStringEnum ( 'Query.where' , whereFilterOpEnums , 2 , opStr ) ;
1492
1504
1493
- let fieldValue : FieldValue ;
1505
+ let fieldValue : api . Value ;
1494
1506
const fieldPath = fieldPathFromArgument ( 'Query.where' , field ) ;
1495
1507
const operator = Operator . fromString ( opStr ) ;
1496
1508
if ( fieldPath . isKeyField ( ) ) {
@@ -1505,11 +1517,11 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1505
1517
) ;
1506
1518
} else if ( operator === Operator . IN ) {
1507
1519
this . validateDisjunctiveFilterElements ( value , operator ) ;
1508
- const referenceList : FieldValue [ ] = [ ] ;
1509
- for ( const arrayValue of value as FieldValue [ ] ) {
1520
+ const referenceList : api . Value [ ] = [ ] ;
1521
+ for ( const arrayValue of value as api . Value [ ] ) {
1510
1522
referenceList . push ( this . parseDocumentIdValue ( arrayValue ) ) ;
1511
1523
}
1512
- fieldValue = new ArrayValue ( referenceList ) ;
1524
+ fieldValue = { arrayValue : { values : referenceList } } ;
1513
1525
} else {
1514
1526
fieldValue = this . parseDocumentIdValue ( value ) ;
1515
1527
}
@@ -1715,7 +1727,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1715
1727
`${ methodName } ().`
1716
1728
) ;
1717
1729
}
1718
- return this . boundFromDocument ( methodName , snap . _document ! , before ) ;
1730
+ return this . boundFromDocument ( snap . _document ! , before ) ;
1719
1731
} else {
1720
1732
const allFields = [ docOrField ] . concat ( fields ) ;
1721
1733
return this . boundFromFields ( methodName , allFields , before ) ;
@@ -1733,12 +1745,8 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1733
1745
* of the query or if any of the fields in the order by are an uncommitted
1734
1746
* server timestamp.
1735
1747
*/
1736
- private boundFromDocument (
1737
- methodName : string ,
1738
- doc : Document ,
1739
- before : boolean
1740
- ) : Bound {
1741
- const components : FieldValue [ ] = [ ] ;
1748
+ private boundFromDocument ( doc : Document , before : boolean ) : Bound {
1749
+ const components : api . Value [ ] = [ ] ;
1742
1750
1743
1751
// Because people expect to continue/end a query at the exact document
1744
1752
// provided, we need to use the implicit sort order rather than the explicit
@@ -1749,10 +1757,12 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1749
1757
// results.
1750
1758
for ( const orderBy of this . _query . orderBy ) {
1751
1759
if ( orderBy . field . isKeyField ( ) ) {
1752
- components . push ( new RefValue ( this . firestore . _databaseId , doc . key ) ) ;
1760
+ components . push ( {
1761
+ referenceValue : doc . key . toName ( this . firestore . _databaseId )
1762
+ } ) ;
1753
1763
} else {
1754
1764
const value = doc . field ( orderBy . field ) ;
1755
- if ( value instanceof ServerTimestampValue ) {
1765
+ if ( isServerTimestamp ( value ) ) {
1756
1766
throw new FirestoreError (
1757
1767
Code . INVALID_ARGUMENT ,
1758
1768
'Invalid query. You are trying to start or end a query using a ' +
@@ -1796,7 +1806,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1796
1806
) ;
1797
1807
}
1798
1808
1799
- const components : FieldValue [ ] = [ ] ;
1809
+ const components : api . Value [ ] = [ ] ;
1800
1810
for ( let i = 0 ; i < values . length ; i ++ ) {
1801
1811
const rawValue = values [ i ] ;
1802
1812
const orderByComponent = orderBy [ i ] ;
@@ -1830,7 +1840,9 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
1830
1840
) ;
1831
1841
}
1832
1842
const key = new DocumentKey ( path ) ;
1833
- components . push ( new RefValue ( this . firestore . _databaseId , key ) ) ;
1843
+ components . push ( {
1844
+ referenceValue : key . toName ( this . firestore . _databaseId )
1845
+ } ) ;
1834
1846
} else {
1835
1847
const wrapped = this . firestore . _dataReader . parseQueryValue (
1836
1848
methodName ,
@@ -2029,7 +2041,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
2029
2041
* appropriate errors if the value is anything other than a DocumentReference
2030
2042
* or String, or if the string is malformed.
2031
2043
*/
2032
- private parseDocumentIdValue ( documentIdValue : unknown ) : RefValue {
2044
+ private parseDocumentIdValue ( documentIdValue : unknown ) : api . Value {
2033
2045
if ( typeof documentIdValue === 'string' ) {
2034
2046
if ( documentIdValue === '' ) {
2035
2047
throw new FirestoreError (
@@ -2060,10 +2072,12 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
2060
2072
`but '${ path } ' is not because it has an odd number of segments (${ path . length } ).`
2061
2073
) ;
2062
2074
}
2063
- return new RefValue ( this . firestore . _databaseId , new DocumentKey ( path ) ) ;
2075
+ return {
2076
+ referenceValue : new DocumentKey ( path ) . toName ( this . firestore . _databaseId )
2077
+ } ;
2064
2078
} else if ( documentIdValue instanceof DocumentReference ) {
2065
2079
const ref = documentIdValue as DocumentReference < T > ;
2066
- return new RefValue ( this . firestore . _databaseId , ref . _key ) ;
2080
+ return { referenceValue : ref . _key . toName ( this . firestore . _databaseId ) } ;
2067
2081
} else {
2068
2082
throw new FirestoreError (
2069
2083
Code . INVALID_ARGUMENT ,
0 commit comments