Skip to content

Commit

Permalink
fix(GeoWriteBatch): fix update function to use fieldNames
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Nov 10, 2018
1 parent 5716654 commit e27e668
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/GeoDocumentReference.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeoFirestoreTypes } from './GeoFirestoreTypes';
import { encodeSetUpdateDocument } from './utils';
import { encodeSetDocument } from './utils';
import { GeoCollectionReference } from './GeoCollectionReference';
import { GeoDocumentSnapshot } from './GeoDocumentSnapshot';
import { GeoFirestore } from './GeoFirestore';
Expand Down Expand Up @@ -105,7 +105,7 @@ export class GeoDocumentReference {
options?: GeoFirestoreTypes.SetOptions
): Promise<void> {
return (this._document as GeoFirestoreTypes.web.DocumentReference).set(
encodeSetUpdateDocument(data, (options) ? options.customKey : null),
encodeSetDocument(data, (options) ? options.customKey : null),
options
).then(() => null);
}
Expand All @@ -120,7 +120,7 @@ export class GeoDocumentReference {
* @return A Promise resolved once the data has been successfully written to the backend (Note it won't resolve while you're offline).
*/
public update(data: GeoFirestoreTypes.UpdateData, customKey?: string): Promise<void> {
return (this._document as GeoFirestoreTypes.web.DocumentReference).update(encodeSetUpdateDocument(data, customKey)).then(() => null);
return (this._document as GeoFirestoreTypes.web.DocumentReference).update(encodeSetDocument(data, customKey)).then(() => null);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/GeoFirestoreTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export namespace GeoFirestoreTypes {
mergeFields?: Array<string | cloud.FieldPath | web.FieldPath>;
}
export interface SnapshotOptions extends FirestoreTypes.SnapshotOptions { }
export type UpdateData = DocumentData;
export interface UpdateData { [fieldPath: string]: any; }
export type WhereFilterOp = '<' | '<=' | '==' | '>=' | '>' | 'array-contains';
export namespace web {
export interface CollectionReference extends FirestoreTypes.CollectionReference { }
Expand Down
8 changes: 4 additions & 4 deletions src/GeoWriteBatch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GeoFirestoreTypes } from './GeoFirestoreTypes';
import { encodeSetUpdateDocument } from './utils';
import { encodeSetDocument, encodeUpdateDocument } from './utils';
import { GeoDocumentReference } from './GeoDocumentReference';

/**
Expand Down Expand Up @@ -36,7 +36,7 @@ export class GeoWriteBatch {
documentRef['_document'] : documentRef) as GeoFirestoreTypes.web.DocumentReference;
(this._writeBatch as GeoFirestoreTypes.web.WriteBatch).set(
ref,
encodeSetUpdateDocument(data, (options) ? options.customKey : null),
encodeSetDocument(data, (options) ? options.customKey : null),
options
);
return this;
Expand All @@ -54,12 +54,12 @@ export class GeoWriteBatch {
*/
public update(
documentRef: GeoDocumentReference | GeoFirestoreTypes.cloud.DocumentReference | GeoFirestoreTypes.web.DocumentReference,
data: GeoFirestoreTypes.DocumentData,
data: GeoFirestoreTypes.UpdateData,
customKey?: string
): GeoWriteBatch {
const ref = ((documentRef instanceof GeoDocumentReference) ?
documentRef['_document'] : documentRef) as GeoFirestoreTypes.web.DocumentReference;
(this._writeBatch as GeoFirestoreTypes.web.WriteBatch).update(ref, encodeSetUpdateDocument(data, customKey));
(this._writeBatch as GeoFirestoreTypes.web.WriteBatch).update(ref, encodeUpdateDocument(data, customKey));
return this;
}

Expand Down
30 changes: 27 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ export function encodeGeoDocument(
}

/**
* Encodes a Document used by GeoWriteBatch as a GeoDocument.
* Encodes a Document used by GeoWriteBatch.set as a GeoDocument.
*
* @param data The document being set or updated.
* @param data The document being set.
* @param customKey The key of the document to use as the location. Otherwise we default to `coordinates`.
* @return The document encoded as GeoDocument object.
*/
export function encodeSetUpdateDocument(data: GeoFirestoreTypes.DocumentData, customKey?: string): GeoFirestoreTypes.Document {
export function encodeSetDocument(data: GeoFirestoreTypes.DocumentData, customKey?: string): GeoFirestoreTypes.Document {
if (Object.prototype.toString.call(data) === '[object Object]') {
const unparsed: GeoFirestoreTypes.DocumentData = ('d' in data) ? data.d : data;
const locationKey: string = findCoordinatesKey(unparsed, customKey, true);
Expand All @@ -254,6 +254,30 @@ export function encodeSetUpdateDocument(data: GeoFirestoreTypes.DocumentData, cu
}
}

/**
* Encodes a Document used by GeoWriteBatch.update as a GeoDocument.
*
* @param data The document being updated.
* @param customKey The key of the document to use as the location. Otherwise we default to `coordinates`.
* @return The document encoded as GeoDocument object.
*/
export function encodeUpdateDocument(data: GeoFirestoreTypes.UpdateData, customKey?: string): GeoFirestoreTypes.UpdateData {
if (Object.prototype.toString.call(data) === '[object Object]') {
const result = {};
const locationKey: string = findCoordinatesKey(data, customKey, true);
if (locationKey) {
result['l'] = data[locationKey];
result['g'] = encodeGeohash(result['l']);
}
Object.getOwnPropertyNames(data).forEach((prop: string) => {
result['d.' + prop] = data[prop];
});
return result as GeoFirestoreTypes.UpdateData;
} else {
throw new Error('document must be an object');
}
}

/**
* Returns the key of a document that is a GeoPoint.
*
Expand Down
6 changes: 3 additions & 3 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'firebase/firestore';

import {
boundingBoxBits, boundingBoxCoordinates, calculateDistance, decodeGeoDocumentData, decodeGeoQueryDocumentSnapshotData, degreesToRadians,
encodeGeohash, encodeGeoDocument, encodeSetUpdateDocument, findCoordinatesKey, generateGeoQueryDocumentSnapshot, geohashQueries,
GEOHASH_PRECISION, geohashQuery, latitudeBitsForResolution, log2, longitudeBitsForResolution, metersToLongitudeDegrees, toGeoPoint,
validateGeoDocument, validateGeohash, validateLocation, validateQueryCriteria, wrapLongitude
encodeGeohash, encodeGeoDocument, encodeSetDocument, encodeUpdateDocument, findCoordinatesKey, generateGeoQueryDocumentSnapshot,
geohashQueries, GEOHASH_PRECISION, geohashQuery, latitudeBitsForResolution, log2, longitudeBitsForResolution, metersToLongitudeDegrees,
toGeoPoint, validateGeoDocument, validateGeohash, validateLocation, validateQueryCriteria, wrapLongitude
} from '../src/utils';
import {
invalidGeoFirestoreDocuments, invalidGeohashes, invalidLocations, invalidQueryCriterias, validGeoFirestoreDocuments, validGeohashes,
Expand Down

0 comments on commit e27e668

Please sign in to comment.