Skip to content

Commit c4ae70f

Browse files
committed
fix(firestore): fieldPath as key
1 parent 7f6da0b commit c4ae70f

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

packages/firebase-firestore/index.android.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,12 @@ export class FieldPath implements IFieldPath {
11701170
return this.native;
11711171
}
11721172

1173-
documentId(): FieldPath {
1173+
static documentId(): FieldPath {
11741174
return FieldPath.fromNative(com.google.firebase.firestore.FieldPath.documentId());
11751175
}
11761176

1177-
toJSON() {
1178-
return {
1179-
documentId: this.documentId,
1180-
};
1177+
toString() {
1178+
return this.native.toString().replace(/([`])/g, '');
11811179
}
11821180
}
11831181

packages/firebase-firestore/index.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export interface ICollectionReference<T extends DocumentData = DocumentData> {
6161
doc(documentPath?: undefined | string): IDocumentReference<T>;
6262
}
6363

64-
export interface IFieldPath {
65-
documentId(): IFieldPath;
66-
}
64+
export interface IFieldPath {}
6765

6866
export interface IFieldValue {}
6967

@@ -406,9 +404,10 @@ export declare class DocumentReference<T extends DocumentData = DocumentData> im
406404
}
407405

408406
export declare class FieldPath implements IFieldPath {
407+
constructor(fieldNames: string[]);
409408
constructor(...fieldNames: string[]);
410409

411-
documentId(): FieldPath;
410+
static documentId(): FieldPath;
412411

413412
readonly android: any;
414413
readonly ios: any;

packages/firebase-firestore/index.ios.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ const main_queue = dispatch_get_current_queue();
88

99
import { firebase, FirebaseApp, FirebaseError, serialize } from '@nativescript/firebase-core';
1010

11+
import '@nativescript/core';
12+
1113
let defaultFirestore: Firestore;
1214

15+
declare var FieldPathExt;
16+
1317
const fb = firebase();
1418
Object.defineProperty(fb, 'firestore', {
1519
value: (app?: FirebaseApp) => {
@@ -141,18 +145,25 @@ function serializeItems(value) {
141145
}
142146

143147
function createDictionary(dataOrField: any, fieldPathValue?: any, moreFieldsAndValues?: any) {
144-
// TODO: Correctly handle FieldPaths used as keys or passed in the dataOrField and
145-
// moreFieldsAndValues params e.g. new FieldPath(['test', '123']) will currently result in an
146-
// object like { "<FIRFieldPath: 0x283f94640>": "value" }
147-
const data = {}
148-
const assignKeyValue = (key, val) => data[key instanceof FieldPath ? key.native : key] = serializeItems(val);
148+
const data = NSMutableDictionary.alloc().init();
149+
150+
const assignKeyValue = (key, val) => {
151+
// check the map for weak object
152+
if (typeof key === 'string' && key.includes('FIRFieldPath')) {
153+
const ref = fp_refs.get(key)?.get?.();
154+
if (ref) {
155+
data.setObjectForKey(serializeItems(val), ref);
156+
return;
157+
}
158+
}
159+
data.setObjectForKey(serializeItems(val), key instanceof FieldPath ? key.native : key);
160+
};
149161
if (!fieldPathValue && !moreFieldsAndValues) {
150162
Object.entries(dataOrField).forEach((item) => assignKeyValue(item[0], item[1]));
151-
} else {
163+
} else {
152164
assignKeyValue(dataOrField, fieldPathValue);
153165
if (Array.isArray(moreFieldsAndValues)) {
154-
Object.entries(Object.fromEntries(moreFieldsAndValues))
155-
.forEach(([key, value]) => assignKeyValue(key, value));
166+
Object.entries(Object.fromEntries(moreFieldsAndValues)).forEach(([key, value]) => assignKeyValue(key, value));
156167
}
157168
}
158169

@@ -993,19 +1004,27 @@ export class DocumentReference<T extends DocumentData = DocumentData> implements
9931004
}
9941005
}
9951006

1007+
const fp_refs = new Map<string, WeakRef<FIRFieldPath>>();
1008+
9961009
export class FieldPath implements IFieldPath {
9971010
_native: FIRFieldPath;
9981011

9991012
constructor(fieldNames: string[], native = false) {
10001013
if (!native) {
10011014
this._native = FIRFieldPath.alloc().initWithFields(fieldNames);
1015+
this._addToMap();
10021016
}
10031017
}
10041018

1019+
private _addToMap() {
1020+
fp_refs.set(this.native.description, new WeakRef(this._native));
1021+
}
1022+
10051023
static fromNative(field: FIRFieldPath) {
10061024
if (field instanceof FIRFieldPath) {
10071025
const path = new FieldPath([], true);
10081026
path._native = field;
1027+
path._addToMap();
10091028
return path;
10101029
}
10111030
return null;
@@ -1019,14 +1038,12 @@ export class FieldPath implements IFieldPath {
10191038
return this.native;
10201039
}
10211040

1022-
documentId(): FieldPath {
1041+
static documentId(): FieldPath {
10231042
return FieldPath.fromNative(FIRFieldPath.documentID());
10241043
}
10251044

1026-
toJSON() {
1027-
return {
1028-
documentId: this.documentId,
1029-
};
1045+
toString() {
1046+
return this.native.description;
10301047
}
10311048
}
10321049

0 commit comments

Comments
 (0)