@@ -25,33 +25,59 @@ import {
25
25
IBytes
26
26
} from './common' ;
27
27
28
- export { SetOptions , DocumentData , GetOptions , WhereFilterOp } ;
28
+ export { SetOptions , DocumentData , GetOptions , WhereFilterOp } ;
29
29
30
- import { deserialize , firebase , FirebaseApp , FirebaseError , serialize } from '@nativescript/firebase-core' ;
30
+ import { deserialize , firebase , FirebaseApp , FirebaseError , serialize } from '@nativescript/firebase-core' ;
31
31
32
32
33
33
let defaultFirestore : Firestore ;
34
34
35
35
const fb = firebase ( ) ;
36
36
Object . defineProperty ( fb , 'firestore' , {
37
- value : ( app ?: FirebaseApp ) => {
38
- if ( ! app ) {
39
- if ( ! defaultFirestore ) {
40
- defaultFirestore = new Firestore ( ) ;
41
- }
42
- return defaultFirestore ;
43
- }
44
-
45
- return new Firestore ( app ) ;
46
- } ,
47
- writable : false ,
37
+ value : ( app ?: FirebaseApp ) => {
38
+ if ( ! app ) {
39
+ if ( ! defaultFirestore ) {
40
+ defaultFirestore = new Firestore ( ) ;
41
+ }
42
+ return defaultFirestore ;
43
+ }
44
+
45
+ return new Firestore ( app ) ;
46
+ } ,
47
+ writable : false ,
48
48
} ) ;
49
49
50
50
function deserializeField ( value ) {
51
+
51
52
if ( typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ) {
52
53
return value ;
53
54
}
54
55
56
+ if ( value instanceof java . lang . Short ) {
57
+ return value . shortValue ( ) ;
58
+ }
59
+
60
+ if ( value instanceof java . lang . Integer ) {
61
+ return value . intValue ( ) ;
62
+ }
63
+
64
+ if ( value instanceof java . lang . Long ) {
65
+ return value . longValue ( ) ;
66
+ }
67
+
68
+ if ( value instanceof java . lang . Float ) {
69
+ return value . floatValue ( ) ;
70
+ }
71
+
72
+ if ( value instanceof java . lang . Double ) {
73
+ return value . doubleValue ( ) ;
74
+ }
75
+
76
+ if ( value instanceof java . lang . Boolean ) {
77
+ return value . booleanValue ( ) ;
78
+ }
79
+
80
+
55
81
if ( value instanceof com . google . firebase . Timestamp ) {
56
82
return Timestamp . fromNative ( value ) ;
57
83
}
@@ -76,10 +102,6 @@ function deserializeField(value) {
76
102
return CollectionReference . fromNative ( value ) ;
77
103
}
78
104
79
- if ( ! value ) {
80
- return null ;
81
- }
82
-
83
105
if ( value instanceof java . util . List ) {
84
106
const array = [ ] ;
85
107
const size = value . size ( ) ;
@@ -104,6 +126,11 @@ function deserializeField(value) {
104
126
return Bytes . fromNative ( value ) ;
105
127
}
106
128
129
+ if ( ! value ) {
130
+ return null ;
131
+ }
132
+
133
+
107
134
return value ;
108
135
}
109
136
@@ -139,12 +166,10 @@ function serializeItems(value) {
139
166
}
140
167
}
141
168
142
-
143
169
if ( typeof value === 'boolean' ) {
144
170
return java . lang . Boolean . valueOf ( value ) ;
145
171
}
146
172
147
-
148
173
if ( value instanceof Timestamp ) {
149
174
return value . native ;
150
175
}
@@ -188,7 +213,7 @@ function serializeItems(value) {
188
213
}
189
214
190
215
if ( value instanceof Bytes ) {
191
- return value . native ;
216
+ return value . native ;
192
217
}
193
218
194
219
return value ;
@@ -323,6 +348,13 @@ export class SnapshotMetadata implements ISnapshotMetadata {
323
348
return this . native . hasPendingWrites ( ) ;
324
349
}
325
350
351
+ toJSON ( ) {
352
+ return {
353
+ fromCache : this . fromCache ,
354
+ hasPendingWrites : this . hasPendingWrites
355
+ }
356
+ }
357
+
326
358
327
359
get native ( ) {
328
360
return this . #native;
@@ -373,6 +405,16 @@ export class DocumentSnapshot<T extends DocumentData = DocumentData> implements
373
405
}
374
406
}
375
407
408
+ toJSON ( ) {
409
+ return {
410
+ exists : this . exists ,
411
+ id : this . id ,
412
+ metadata : this . metadata ,
413
+ ref : this . ref ,
414
+ data : this . data
415
+ }
416
+ }
417
+
376
418
get native ( ) {
377
419
return this . #native;
378
420
}
@@ -417,6 +459,16 @@ export class DocumentChange implements IDocumentChange {
417
459
}
418
460
}
419
461
462
+ toJSON ( ) {
463
+ return {
464
+ doc : this . doc ,
465
+ newIndex : this . newIndex ,
466
+ oldIndex : this . oldIndex ,
467
+ type : this . type ,
468
+ }
469
+ }
470
+
471
+
420
472
get native ( ) {
421
473
return this . #native;
422
474
}
@@ -426,7 +478,7 @@ export class DocumentChange implements IDocumentChange {
426
478
}
427
479
}
428
480
429
- export class Query < T extends DocumentData = DocumentData > implements IQuery < T > {
481
+ export class Query < T extends DocumentData = DocumentData > implements IQuery < T > {
430
482
#native: com . google . firebase . firestore . Query ;
431
483
432
484
static fromNative ( query : com . google . firebase . firestore . Query ) : Query {
@@ -538,7 +590,7 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery <T>
538
590
539
591
} else if ( argsCount === 4 ) {
540
592
541
- listener = this . native . addSnapshotListener ( com . google . firebase . firestore . MetadataChanges . INCLUDE , new com . google . firebase . firestore . EventListener < com . google . firebase . firestore . QuerySnapshot > ( {
593
+ listener = this . native . addSnapshotListener ( com . google . firebase . firestore . MetadataChanges . INCLUDE , new com . google . firebase . firestore . EventListener < com . google . firebase . firestore . QuerySnapshot > ( {
542
594
onEvent ( ss , error : com . google . firebase . firestore . FirebaseFirestoreException ) {
543
595
if ( error ) {
544
596
onError ?.( FirebaseError . fromNative ( error ) ) ;
@@ -614,9 +666,9 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery <T>
614
666
case 'array-contains' :
615
667
query = this . native . whereArrayContains (
616
668
fieldPath . native ,
617
- value . map ( ( val ) => {
669
+ Array . isArray ( value ) ? value . map ( ( val ) => {
618
670
return val ?. native || val ;
619
- } )
671
+ } ) : value
620
672
) ;
621
673
break ;
622
674
case 'array-contains-any' :
@@ -671,9 +723,9 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery <T>
671
723
case 'array-contains' :
672
724
query = this . native . whereArrayContains (
673
725
fieldPath as any ,
674
- value . map ( ( val ) => {
726
+ Array . isArray ( value ) ? value . map ( ( val ) => {
675
727
return val ?. native || val ;
676
- } )
728
+ } ) : value
677
729
) ;
678
730
break ;
679
731
case 'array-contains-any' :
@@ -817,6 +869,14 @@ export class QuerySnapshot implements IQuerySnapshot {
817
869
}
818
870
}
819
871
872
+ toJSON ( ) {
873
+ return {
874
+ docs : this . docs ,
875
+ empty : this . empty ,
876
+ metadata : this . metadata ,
877
+ size : this . size
878
+ }
879
+ }
820
880
821
881
get native ( ) {
822
882
return this . #native;
@@ -872,6 +932,14 @@ export class CollectionReference<T extends DocumentData = DocumentData> extends
872
932
return DocumentReference . fromNative ( this . native . document ( documentPath || '/' ) ) ;
873
933
}
874
934
935
+ toJSON ( ) {
936
+ return {
937
+ id : this . id ,
938
+ path : this . path ,
939
+ parent : this . parent
940
+ }
941
+ }
942
+
875
943
get native ( ) {
876
944
return this . #native;
877
945
}
@@ -1131,6 +1199,14 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
1131
1199
} ) ;
1132
1200
}
1133
1201
1202
+ toJSON ( ) {
1203
+ return {
1204
+ id : this . id ,
1205
+ path : this . path ,
1206
+ parent : this . parent
1207
+ }
1208
+ }
1209
+
1134
1210
get native ( ) {
1135
1211
return this . #native;
1136
1212
}
@@ -1169,6 +1245,12 @@ export class FieldPath implements IFieldPath {
1169
1245
documentId ( ) : FieldPath {
1170
1246
return FieldPath . fromNative ( com . google . firebase . firestore . FieldPath . documentId ( ) ) ;
1171
1247
}
1248
+
1249
+ toJSON ( ) {
1250
+ return {
1251
+ documentId : this . documentId
1252
+ }
1253
+ }
1172
1254
}
1173
1255
1174
1256
export class FieldValue implements IFieldValue {
@@ -1247,7 +1329,7 @@ export class GeoPoint implements IGeoPoint {
1247
1329
return this . native ;
1248
1330
}
1249
1331
1250
- toString ( ) {
1332
+ toJSON ( ) {
1251
1333
return {
1252
1334
latitude : this . latitude ,
1253
1335
longitude : this . longitude
@@ -1289,7 +1371,7 @@ export class Timestamp implements ITimestamp {
1289
1371
return this . native ;
1290
1372
}
1291
1373
1292
- toString ( ) {
1374
+ toJSON ( ) {
1293
1375
return {
1294
1376
nanoseconds : this . nanoseconds ,
1295
1377
seconds : this . seconds
@@ -1445,6 +1527,16 @@ export class Settings implements ISettings {
1445
1527
this . #builder. setSslEnabled ( value ) ;
1446
1528
}
1447
1529
1530
+ toJSON ( ) {
1531
+ return {
1532
+ cacheSizeBytes : this . cacheSizeBytes ,
1533
+ host : this . host ,
1534
+ ignoreUndefinedProperties : this . ignoreUndefinedProperties ,
1535
+ persistence : this . persistence ,
1536
+ ssl : this . ssl
1537
+ }
1538
+ }
1539
+
1448
1540
get android ( ) {
1449
1541
return this . native ;
1450
1542
}
@@ -1467,9 +1559,9 @@ export class Bytes implements IBytes {
1467
1559
}
1468
1560
1469
1561
static fromBase64String ( base64 ) {
1470
- if ( typeof base64 === 'string' ) {
1562
+ if ( typeof base64 === 'string' ) {
1471
1563
let b64 = base64 ;
1472
- if ( base64 . startsWith ( 'data:' ) ) {
1564
+ if ( base64 . startsWith ( 'data:' ) ) {
1473
1565
b64 = base64 . split ( "," ) [ 1 ] ;
1474
1566
}
1475
1567
const data = new java . lang . String ( b64 ) . getBytes ( 'UTF-8' ) ;
@@ -1493,31 +1585,31 @@ export class Bytes implements IBytes {
1493
1585
1494
1586
#base64: string
1495
1587
toBase64 ( ) : string {
1496
- if ( ! this . #base64) {
1588
+ if ( ! this . #base64) {
1497
1589
const data = this . native . toBytes ( ) ;
1498
1590
this . #base64 = android . util . Base64 . encodeToString ( data , android . util . Base64 . NO_WRAP ) ;
1499
1591
}
1500
-
1592
+
1501
1593
return this . #base64;
1502
1594
}
1503
1595
1504
1596
#native_buffer;
1505
1597
#buffer;
1506
1598
toUint8Array ( ) : Uint8Array {
1507
- if ( ! this . #native_buffer) {
1599
+ if ( ! this . #native_buffer) {
1508
1600
this . #native_buffer = java . nio . ByteBuffer . wrap ( this . native . toBytes ( ) )
1509
1601
}
1510
- if ( ! this . #buffer) {
1602
+ if ( ! this . #buffer) {
1511
1603
this . #buffer = ( < any > ArrayBuffer ) . from ( this . #native_buffer)
1512
1604
}
1513
1605
return new Uint8Array ( this . #buffer) ;
1514
1606
}
1515
1607
1516
- get native ( ) {
1608
+ get native ( ) {
1517
1609
return this . #native;
1518
1610
}
1519
1611
1520
- get android ( ) {
1612
+ get android ( ) {
1521
1613
return this . native ;
1522
1614
}
1523
1615
}
@@ -1530,7 +1622,7 @@ export class Firestore implements IFirestore {
1530
1622
if ( app ) {
1531
1623
this . #native = com . google . firebase . firestore . FirebaseFirestore . getInstance ( app . native ) ;
1532
1624
} else {
1533
- if ( defaultFirestore ) {
1625
+ if ( defaultFirestore ) {
1534
1626
return defaultFirestore ;
1535
1627
}
1536
1628
defaultFirestore = this ;
0 commit comments