Skip to content

Commit f0ac982

Browse files
authored
Modify event.data.previous to always exist for Firestore functions (#136)
1 parent ad28ffa commit f0ac982

File tree

2 files changed

+15
-72
lines changed

2 files changed

+15
-72
lines changed

spec/providers/firestore.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ 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.equal(null);
148+
expect(event.data.previous).to.not.equal(null);
149+
expect(event.data.previous.exists).to.be.false;
149150
});
150151
let data = constructEvent({}, createValue());
151152
return testFunction(data);
@@ -164,8 +165,7 @@ describe('Firestore Functions', () => {
164165

165166
it('constructs appropriate fields and getters for event.data on "document.delete" events', () => {
166167
let testFunction = firestore.document('path').onDelete((event) => {
167-
expect(event.data.data).to.throw(Error);
168-
expect(() => {return event.data.get('key1');}).to.throw(Error);
168+
expect(event.data.exists).to.equal(false);
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': 'Zm9vYmFy'});
333+
expect(snapshot.data()).to.deep.equal({'binaryVal': new Buffer('foobar')});
334334
});
335335
});
336336

@@ -387,8 +387,6 @@ 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);
392390
});
393391

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

src/providers/firestore.ts

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

87-
function getValueProto(event) {
87+
function getValueProto(event, valueFieldName) {
8888
let data = event.data;
89-
if (_.isEmpty(_.get(data, 'value'))) {
89+
if (_.isEmpty(_.get(data, valueFieldName))) {
9090
// Firestore#snapshot_ takes resource string instead of proto for a non-existent snapshot
9191
return event.resource;
9292
}
9393
let proto = {
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),
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),
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-
154102
/** @internal */
155103
export function dataConstructor(raw: Event<any>) {
156104
if (isDeltaDocumentSnapshot(raw.data)) {
@@ -159,17 +107,14 @@ export function dataConstructor(raw: Event<any>) {
159107
if (!firestoreInstance) {
160108
firestoreInstance = firebase.firestore(apps().admin);
161109
}
162-
let valueProto = getValueProto(raw);
110+
let valueProto = getValueProto(raw, 'value');
163111
let readTime = dateToTimestampProto(_.get(raw.data, 'value.readTime'));
164-
let snapshot = firestoreInstance.snapshot_(valueProto, readTime) as DeltaDocumentSnapshot;
112+
let snapshot = firestoreInstance.snapshot_(valueProto, readTime, 'json') as DeltaDocumentSnapshot;
165113
Object.defineProperty(snapshot, 'previous', {
166114
get: () => {
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;
115+
let oldValueProto = getValueProto(raw, 'oldValue');
116+
let oldReadTime = dateToTimestampProto(_.get(raw.data, 'oldValue.readTime'));
117+
return firestoreInstance.snapshot_(oldValueProto, oldReadTime, 'json') as DeltaDocumentSnapshot;
173118
},
174119
});
175120
return snapshot;

0 commit comments

Comments
 (0)