Skip to content

Commit 950e0ab

Browse files
committed
fix(firestore): return primitives
feat: logging now prints accessor values
1 parent 4b3770d commit 950e0ab

File tree

2 files changed

+263
-104
lines changed

2 files changed

+263
-104
lines changed

packages/firebase-firestore/index.android.ts

Lines changed: 129 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,59 @@ import {
2525
IBytes
2626
} from './common';
2727

28-
export {SetOptions, DocumentData, GetOptions, WhereFilterOp};
28+
export { SetOptions, DocumentData, GetOptions, WhereFilterOp };
2929

30-
import {deserialize, firebase, FirebaseApp, FirebaseError, serialize} from '@nativescript/firebase-core';
30+
import { deserialize, firebase, FirebaseApp, FirebaseError, serialize } from '@nativescript/firebase-core';
3131

3232

3333
let defaultFirestore: Firestore;
3434

3535
const fb = firebase();
3636
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,
4848
});
4949

5050
function deserializeField(value) {
51+
5152
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
5253
return value;
5354
}
5455

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+
5581
if (value instanceof com.google.firebase.Timestamp) {
5682
return Timestamp.fromNative(value);
5783
}
@@ -76,10 +102,6 @@ function deserializeField(value) {
76102
return CollectionReference.fromNative(value);
77103
}
78104

79-
if (!value) {
80-
return null;
81-
}
82-
83105
if (value instanceof java.util.List) {
84106
const array = [];
85107
const size = value.size();
@@ -104,6 +126,11 @@ function deserializeField(value) {
104126
return Bytes.fromNative(value);
105127
}
106128

129+
if (!value) {
130+
return null;
131+
}
132+
133+
107134
return value;
108135
}
109136

@@ -139,12 +166,10 @@ function serializeItems(value) {
139166
}
140167
}
141168

142-
143169
if (typeof value === 'boolean') {
144170
return java.lang.Boolean.valueOf(value);
145171
}
146172

147-
148173
if (value instanceof Timestamp) {
149174
return value.native;
150175
}
@@ -188,7 +213,7 @@ function serializeItems(value) {
188213
}
189214

190215
if (value instanceof Bytes) {
191-
return value.native;
216+
return value.native;
192217
}
193218

194219
return value;
@@ -323,6 +348,13 @@ export class SnapshotMetadata implements ISnapshotMetadata {
323348
return this.native.hasPendingWrites();
324349
}
325350

351+
toJSON() {
352+
return {
353+
fromCache: this.fromCache,
354+
hasPendingWrites: this.hasPendingWrites
355+
}
356+
}
357+
326358

327359
get native() {
328360
return this.#native;
@@ -373,6 +405,16 @@ export class DocumentSnapshot<T extends DocumentData = DocumentData> implements
373405
}
374406
}
375407

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+
376418
get native() {
377419
return this.#native;
378420
}
@@ -417,6 +459,16 @@ export class DocumentChange implements IDocumentChange {
417459
}
418460
}
419461

462+
toJSON() {
463+
return {
464+
doc: this.doc,
465+
newIndex: this.newIndex,
466+
oldIndex: this.oldIndex,
467+
type: this.type,
468+
}
469+
}
470+
471+
420472
get native() {
421473
return this.#native;
422474
}
@@ -426,7 +478,7 @@ export class DocumentChange implements IDocumentChange {
426478
}
427479
}
428480

429-
export class Query<T extends DocumentData = DocumentData> implements IQuery <T> {
481+
export class Query<T extends DocumentData = DocumentData> implements IQuery<T> {
430482
#native: com.google.firebase.firestore.Query;
431483

432484
static fromNative(query: com.google.firebase.firestore.Query): Query {
@@ -538,7 +590,7 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery <T>
538590

539591
} else if (argsCount === 4) {
540592

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>({
542594
onEvent(ss, error: com.google.firebase.firestore.FirebaseFirestoreException) {
543595
if (error) {
544596
onError?.(FirebaseError.fromNative(error));
@@ -614,9 +666,9 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery <T>
614666
case 'array-contains':
615667
query = this.native.whereArrayContains(
616668
fieldPath.native,
617-
value.map((val) => {
669+
Array.isArray(value) ? value.map((val) => {
618670
return val?.native || val;
619-
})
671+
}) : value
620672
);
621673
break;
622674
case 'array-contains-any':
@@ -671,9 +723,9 @@ export class Query<T extends DocumentData = DocumentData> implements IQuery <T>
671723
case 'array-contains':
672724
query = this.native.whereArrayContains(
673725
fieldPath as any,
674-
value.map((val) => {
726+
Array.isArray(value) ? value.map((val) => {
675727
return val?.native || val;
676-
})
728+
}) : value
677729
);
678730
break;
679731
case 'array-contains-any':
@@ -817,6 +869,14 @@ export class QuerySnapshot implements IQuerySnapshot {
817869
}
818870
}
819871

872+
toJSON() {
873+
return {
874+
docs: this.docs,
875+
empty: this.empty,
876+
metadata: this.metadata,
877+
size: this.size
878+
}
879+
}
820880

821881
get native() {
822882
return this.#native;
@@ -872,6 +932,14 @@ export class CollectionReference<T extends DocumentData = DocumentData> extends
872932
return DocumentReference.fromNative(this.native.document(documentPath || '/'));
873933
}
874934

935+
toJSON() {
936+
return {
937+
id: this.id,
938+
path: this.path,
939+
parent: this.parent
940+
}
941+
}
942+
875943
get native() {
876944
return this.#native;
877945
}
@@ -1131,6 +1199,14 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
11311199
});
11321200
}
11331201

1202+
toJSON() {
1203+
return {
1204+
id: this.id,
1205+
path: this.path,
1206+
parent: this.parent
1207+
}
1208+
}
1209+
11341210
get native() {
11351211
return this.#native;
11361212
}
@@ -1169,6 +1245,12 @@ export class FieldPath implements IFieldPath {
11691245
documentId(): FieldPath {
11701246
return FieldPath.fromNative(com.google.firebase.firestore.FieldPath.documentId());
11711247
}
1248+
1249+
toJSON() {
1250+
return {
1251+
documentId: this.documentId
1252+
}
1253+
}
11721254
}
11731255

11741256
export class FieldValue implements IFieldValue {
@@ -1247,7 +1329,7 @@ export class GeoPoint implements IGeoPoint {
12471329
return this.native;
12481330
}
12491331

1250-
toString() {
1332+
toJSON() {
12511333
return {
12521334
latitude: this.latitude,
12531335
longitude: this.longitude
@@ -1289,7 +1371,7 @@ export class Timestamp implements ITimestamp {
12891371
return this.native;
12901372
}
12911373

1292-
toString() {
1374+
toJSON() {
12931375
return {
12941376
nanoseconds: this.nanoseconds,
12951377
seconds: this.seconds
@@ -1445,6 +1527,16 @@ export class Settings implements ISettings {
14451527
this.#builder.setSslEnabled(value);
14461528
}
14471529

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+
14481540
get android() {
14491541
return this.native;
14501542
}
@@ -1467,9 +1559,9 @@ export class Bytes implements IBytes {
14671559
}
14681560

14691561
static fromBase64String(base64) {
1470-
if(typeof base64 === 'string'){
1562+
if (typeof base64 === 'string') {
14711563
let b64 = base64;
1472-
if(base64.startsWith('data:')){
1564+
if (base64.startsWith('data:')) {
14731565
b64 = base64.split(",")[1];
14741566
}
14751567
const data = new java.lang.String(b64).getBytes('UTF-8');
@@ -1493,31 +1585,31 @@ export class Bytes implements IBytes {
14931585

14941586
#base64: string
14951587
toBase64(): string {
1496-
if(!this.#base64){
1588+
if (!this.#base64) {
14971589
const data = this.native.toBytes();
14981590
this.#base64 = android.util.Base64.encodeToString(data, android.util.Base64.NO_WRAP);
14991591
}
1500-
1592+
15011593
return this.#base64;
15021594
}
15031595

15041596
#native_buffer;
15051597
#buffer;
15061598
toUint8Array(): Uint8Array {
1507-
if(!this.#native_buffer){
1599+
if (!this.#native_buffer) {
15081600
this.#native_buffer = java.nio.ByteBuffer.wrap(this.native.toBytes())
15091601
}
1510-
if(!this.#buffer){
1602+
if (!this.#buffer) {
15111603
this.#buffer = (<any>ArrayBuffer).from(this.#native_buffer)
15121604
}
15131605
return new Uint8Array(this.#buffer);
15141606
}
15151607

1516-
get native(){
1608+
get native() {
15171609
return this.#native;
15181610
}
15191611

1520-
get android(){
1612+
get android() {
15211613
return this.native;
15221614
}
15231615
}
@@ -1530,7 +1622,7 @@ export class Firestore implements IFirestore {
15301622
if (app) {
15311623
this.#native = com.google.firebase.firestore.FirebaseFirestore.getInstance(app.native);
15321624
} else {
1533-
if(defaultFirestore){
1625+
if (defaultFirestore) {
15341626
return defaultFirestore;
15351627
}
15361628
defaultFirestore = this;

0 commit comments

Comments
 (0)