Skip to content

Commit 18a9b40

Browse files
authored
Add support for geopoints. Add tests for date and ref (#105)
1 parent 8f2def7 commit 18a9b40

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- '8'
4-
- '10'
5-
- '12'
3+
- '8'
4+
- '10'
5+
- '12'

scripts/publish-container/cloudbuild.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ steps:
22
- name: 'gcr.io/cloud-builders/docker'
33
args: ['build', '-t', 'gcr.io/$PROJECT_ID/fft-package-builder', '.']
44
images: ['gcr.io/$PROJECT_ID/fft-package-builder']
5-

scripts/publish/cloudbuild.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ steps:
7878
# Set up the Twitter credentials.
7979
- name: 'gcr.io/$PROJECT_ID/fft-package-builder'
8080
entrypoint: 'cp'
81-
args:
82-
['-v', 'twitter.json', '${_REPOSITORY_NAME}/scripts/twitter.json']
81+
args: ['-v', 'twitter.json', '${_REPOSITORY_NAME}/scripts/twitter.json']
8382

8483
# Set up the npm credentials.
8584
- name: 'gcr.io/$PROJECT_ID/fft-package-builder'
@@ -112,4 +111,3 @@ substitutions:
112111
_KEY_NAME: 'publish'
113112
_REPOSITORY_ORG: 'firebase'
114113
_REPOSITORY_NAME: 'firebase-functions-test'
115-

spec/providers/firestore.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import * as firebase from 'firebase-admin';
23
import fft = require('../../src/index');
34

45
describe('providers/firestore', () => {
@@ -29,4 +30,47 @@ describe('providers/firestore', () => {
2930
expect(snapshot.data()).to.deep.equal(undefined);
3031
expect(snapshot.id).to.equal('doc-id');
3132
});
33+
34+
it('should allow geopoints with makeDocumentSnapshot', () => {
35+
const test = fft();
36+
37+
const hq = new firebase.firestore.GeoPoint(47.6703, 122.1971);
38+
const snapshot = test.firestore.makeDocumentSnapshot(
39+
{ geopoint: hq },
40+
'collection/doc-id'
41+
);
42+
43+
expect(snapshot.data()).to.deep.equal({ geopoint: hq });
44+
});
45+
46+
it('should allow timestmaps with makeDocumentSnapshot', () => {
47+
const test = fft();
48+
49+
const time = new Date();
50+
const snapshot = test.firestore.makeDocumentSnapshot(
51+
{ time },
52+
'collection/doc-id'
53+
);
54+
55+
expect(snapshot.data().time).to.be.instanceof(firebase.firestore.Timestamp);
56+
expect(snapshot.data().time.toDate()).to.deep.equal(time);
57+
});
58+
59+
it('should allow references with makeDocumentSnapshot', () => {
60+
const test = fft();
61+
firebase.initializeApp({
62+
projectId: 'not-a-project',
63+
});
64+
65+
const ref = firebase.firestore().doc('collection/doc-id');
66+
const snapshot = test.firestore.makeDocumentSnapshot(
67+
{ ref },
68+
'collection/doc-id'
69+
);
70+
71+
expect(snapshot.data().ref).to.be.instanceOf(
72+
firebase.firestore.DocumentReference
73+
);
74+
expect(snapshot.data().ref.toString()).to.equal(ref.toString());
75+
});
3276
});

src/main.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222

2323
import { has, merge, random, get } from 'lodash';
2424

25-
import { CloudFunction, EventContext, Change, https, config } from 'firebase-functions';
25+
import {
26+
CloudFunction,
27+
EventContext,
28+
Change,
29+
https,
30+
config,
31+
} from 'firebase-functions';
2632

2733
/** Fields of the event context that can be overridden/customized. */
2834
export type EventContextOptions = {

src/providers/firestore.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ export function objectToValueProto(data: object) {
173173
};
174174
}
175175
if (val === null) {
176+
// TODO: Look this up. This is a google.protobuf.NulLValue,
177+
// and everything in google.protobuf has a customized JSON encoder.
178+
// OTOH, Firestore's generated .d.ts files don't take this into
179+
// account and have the default proto layout.
176180
return {
177181
nullValue: 'NULL_VALUE',
178182
};
@@ -199,6 +203,14 @@ export function objectToValueProto(data: object) {
199203
timestampValue: val.toDate().toISOString(),
200204
};
201205
}
206+
if (val instanceof firestore.GeoPoint) {
207+
return {
208+
geoPointValue: {
209+
latitude: val.latitude,
210+
longitude: val.longitude,
211+
},
212+
};
213+
}
202214
if (isPlainObject(val)) {
203215
return {
204216
mapValue: {

0 commit comments

Comments
 (0)