Skip to content

Commit 280bc39

Browse files
authored
Revert "Always construct DocumentSnapshot for event.data.previous for Firestore functions" (#150)
This reverts commit f0ac982.
1 parent c038290 commit 280bc39

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

spec/providers/firestore.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ describe('Firestore Functions', () => {
145145
let testFunction = firestore.document('path').onCreate((event) => {
146146
expect(event.data.data()).to.deep.equal({key1: true, key2: 123});
147147
expect(event.data.get('key1')).to.equal(true);
148-
expect(event.data.previous).to.not.equal(null);
149-
expect(event.data.previous.exists).to.be.false;
148+
expect(event.data.previous).to.equal(null);
150149
});
151150
let data = constructEvent({}, createValue());
152151
return testFunction(data);
@@ -165,7 +164,8 @@ describe('Firestore Functions', () => {
165164

166165
it('constructs appropriate fields and getters for event.data on "document.delete" events', () => {
167166
let testFunction = firestore.document('path').onDelete((event) => {
168-
expect(event.data.exists).to.equal(false);
167+
expect(event.data.data).to.throw(Error);
168+
expect(() => {return event.data.get('key1');}).to.throw(Error);
169169
expect(event.data.previous.data()).to.deep.equal({key1: false, key2: 111});
170170
expect(event.data.previous.get('key1')).to.equal(false);
171171
});
@@ -330,7 +330,7 @@ describe('Firestore Functions', () => {
330330
let snapshot = firestore.dataConstructor({
331331
data: { value: raw },
332332
});
333-
expect(snapshot.data()).to.deep.equal({'binaryVal': new Buffer('foobar')});
333+
expect(snapshot.data()).to.deep.equal({'binaryVal': 'Zm9vYmFy'});
334334
});
335335
});
336336

@@ -387,6 +387,8 @@ describe('Firestore Functions', () => {
387387
});
388388
expect(snapshot.exists).to.be.false;
389389
expect(snapshot.ref.path).to.equal('collection/123');
390+
expect(snapshot.data).to.throw(Error);
391+
expect(() => {return snapshot.get('key1');}).to.throw(Error);
390392
});
391393

392394
it('constructs existent DocumentSnapshot with empty data when all fields of document deleted', () => {

src/providers/firestore.ts

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,73 @@ function isDeltaDocumentSnapshot(data: any): data is DeltaDocumentSnapshot {
8484
return 'exists' in data;
8585
};
8686

87-
function getValueProto(event, valueFieldName) {
87+
function getValueProto(event) {
8888
let data = event.data;
89-
if (_.isEmpty(_.get(data, valueFieldName))) {
89+
if (_.isEmpty(_.get(data, 'value'))) {
9090
// Firestore#snapshot_ takes resource string instead of proto for a non-existent snapshot
9191
return event.resource;
9292
}
9393
let proto = {
94-
fields: _.get(data, [valueFieldName, 'fields'], {}),
95-
createTime: dateToTimestampProto(_.get(data, [valueFieldName, 'createTime'])),
96-
updateTime: dateToTimestampProto(_.get(data, [valueFieldName, 'updateTime'])),
97-
name: _.get(data, [valueFieldName, 'name'], event.resource),
94+
fields: convertToFieldsProto(_.get(data, 'value.fields', {})),
95+
createTime: dateToTimestampProto(_.get(data, 'value.createTime')),
96+
updateTime: dateToTimestampProto(_.get(data, 'value.updateTime')),
97+
name: _.get(data, 'value.name', event.resource),
9898
};
9999
return proto;
100100
};
101101

102+
function getOldValueProto(event) {
103+
let data = event.data;
104+
let proto = {
105+
fields: convertToFieldsProto(_.get(data, 'oldValue.fields', {})),
106+
createTime: dateToTimestampProto(_.get(data, 'oldValue.createTime')),
107+
updateTime: dateToTimestampProto(_.get(data, 'oldValue.updateTime')),
108+
name: _.get(data, 'oldValue.name', event.resource),
109+
};
110+
return proto;
111+
};
112+
113+
function convertToFieldsProto(fields): object {
114+
if (!fields) {
115+
return {};
116+
}
117+
function convertHelper(data) {
118+
let result;
119+
_.forEach(data, (value: any, valueType: string) => {
120+
let dataPart;
121+
if (valueType === 'arrayValue') {
122+
let array = _.get(value, 'values', []);
123+
dataPart = {
124+
arrayValue: {
125+
values: _.map(array, (elem) => {
126+
return convertHelper(elem);
127+
}),
128+
},
129+
};
130+
} else if (valueType === 'mapValue') {
131+
let map = _.get(value, 'fields', {});
132+
dataPart = {
133+
mapValue: {
134+
fields: _.mapValues(map, (val) => {
135+
return convertHelper(val);
136+
}),
137+
},
138+
};
139+
} else if (valueType === 'timestampValue') {
140+
dataPart = {timestampValue: dateToTimestampProto(value)};
141+
} else {
142+
dataPart = data;
143+
}
144+
result = _.merge({}, dataPart, {valueType: valueType});
145+
});
146+
return result;
147+
}
148+
149+
return _.mapValues(fields, (data: object) => {
150+
return convertHelper(data);
151+
});
152+
};
153+
102154
/** @internal */
103155
export function dataConstructor(raw: Event<any>) {
104156
if (isDeltaDocumentSnapshot(raw.data)) {
@@ -107,14 +159,17 @@ export function dataConstructor(raw: Event<any>) {
107159
if (!firestoreInstance) {
108160
firestoreInstance = firebase.firestore(apps().admin);
109161
}
110-
let valueProto = getValueProto(raw, 'value');
162+
let valueProto = getValueProto(raw);
111163
let readTime = dateToTimestampProto(_.get(raw.data, 'value.readTime'));
112-
let snapshot = firestoreInstance.snapshot_(valueProto, readTime, 'json') as DeltaDocumentSnapshot;
164+
let snapshot = firestoreInstance.snapshot_(valueProto, readTime) as DeltaDocumentSnapshot;
113165
Object.defineProperty(snapshot, 'previous', {
114166
get: () => {
115-
let oldValueProto = getValueProto(raw, 'oldValue');
116-
let oldReadTime = dateToTimestampProto(_.get(raw.data, 'oldValue.readTime'));
117-
return firestoreInstance.snapshot_(oldValueProto, oldReadTime, 'json') as DeltaDocumentSnapshot;
167+
if (_.isEmpty(_.get(raw, 'data.oldValue', {}))) {
168+
return null;
169+
}
170+
let oldValueProto = getOldValueProto(raw);
171+
let oldReadTime = dateToTimestampProto(_.get(raw.data, 'value.oldValue.readTime'));
172+
return firestoreInstance.snapshot_(oldValueProto, oldReadTime) as DeltaDocumentSnapshot;
118173
},
119174
});
120175
return snapshot;

0 commit comments

Comments
 (0)