Skip to content

Commit

Permalink
refactor: use g instead of .g for geofirestore data
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jun 9, 2020
1 parent e2ad5e5 commit 9444cae
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
2 changes: 1 addition & 1 deletion firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ service cloud.firestore {
&& request.resource.data.d.count is number;
}
match /tests/{key} {
allow read, write: if false;
allow read, write: if true;
}
}
}
5 changes: 2 additions & 3 deletions src/GeoDocumentSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ export class GeoDocumentSnapshot {
| GeoFirestoreTypes.web.FieldPath,
options?: GeoFirestoreTypes.SnapshotOptions
): any {
const path = 'd.' + fieldPath;
return this._isWeb && options
? (this._snapshot as GeoFirestoreTypes.web.DocumentSnapshot).get(
path,
fieldPath,
options
)
: this._snapshot.get(path);
: this._snapshot.get(fieldPath);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/GeoFirestoreTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@types/node';

export namespace GeoFirestoreTypes {
export interface GeoDocumentData extends DocumentData {
'.g'?: {
g?: {
coordinates: web.GeoPoint | cloud.GeoPoint;
geohash: string;
};
Expand Down
2 changes: 1 addition & 1 deletion src/GeoQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class GeoQuery {
const query: string[] = this._stringToQuery(toQueryStr);
// Create the Firebase query
return this._query
.orderBy('g')
.orderBy('g.geohash')
.startAt(query[0])
.endAt(query[1]) as GeoFirestoreTypes.web.Query;
});
Expand Down
26 changes: 12 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function decodeGeoQueryDocumentSnapshotData(
): {data: () => GeoFirestoreTypes.GeoDocumentData; distance: number} {
if (validateGeoDocument(data, true)) {
const distance = center
? calculateDistance(data['.g'].coordinates, center)
? calculateDistance(data.g.coordinates, center)
: null;
return {data: () => data, distance};
}
Expand Down Expand Up @@ -233,7 +233,7 @@ export function encodeGeoDocument(
): GeoFirestoreTypes.GeoDocumentData {
validateLocation(coordinates);
validateGeohash(geohash);
document['.g'] = {
document.g = {
coordinates,
geohash,
};
Expand Down Expand Up @@ -294,16 +294,14 @@ export function encodeUpdateDocument(
customKey?: string
): GeoFirestoreTypes.UpdateData {
if (Object.prototype.toString.call(data) === '[object Object]') {
const result: any = {};
const location = findCoordinates(data, customKey, true);
if (location) {
result['l'] = location;
result['g'] = encodeGeohash(result['l']);
const coordinates = findCoordinates(data, customKey, true);
if (coordinates) {
data.g = {
coordinates,
geohash: encodeGeohash(coordinates),
};
}
Object.getOwnPropertyNames(data).forEach((prop: string) => {
result['d.' + prop] = data[prop];
});
return result as GeoFirestoreTypes.UpdateData;
return data;
} else {
throw new Error('document must be an object');
}
Expand Down Expand Up @@ -535,11 +533,11 @@ export function validateGeoDocument(

if (!documentData) {
error = 'no document found';
} else if ('.g' in documentData) {
error = !validateGeohash(documentData['.g'].geohash, true)
} else if ('g' in documentData) {
error = !validateGeohash(documentData.g.geohash, true)
? 'invalid geohash on object'
: null;
error = !validateLocation(documentData['.g'].coordinates, true)
error = !validateLocation(documentData.g.coordinates, true)
? 'invalid location on object'
: error;
} else {
Expand Down

0 comments on commit 9444cae

Please sign in to comment.