@@ -84,73 +84,21 @@ function isDeltaDocumentSnapshot(data: any): data is DeltaDocumentSnapshot {
84
84
return 'exists' in data ;
85
85
} ;
86
86
87
- function getValueProto ( event ) {
87
+ function getValueProto ( event , valueFieldName ) {
88
88
let data = event . data ;
89
- if ( _ . isEmpty ( _ . get ( data , 'value' ) ) ) {
89
+ if ( _ . isEmpty ( _ . get ( data , valueFieldName ) ) ) {
90
90
// Firestore#snapshot_ takes resource string instead of proto for a non-existent snapshot
91
91
return event . resource ;
92
92
}
93
93
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 ) ,
98
98
} ;
99
99
return proto ;
100
100
} ;
101
101
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
-
154
102
/** @internal */
155
103
export function dataConstructor ( raw : Event < any > ) {
156
104
if ( isDeltaDocumentSnapshot ( raw . data ) ) {
@@ -159,17 +107,14 @@ export function dataConstructor(raw: Event<any>) {
159
107
if ( ! firestoreInstance ) {
160
108
firestoreInstance = firebase . firestore ( apps ( ) . admin ) ;
161
109
}
162
- let valueProto = getValueProto ( raw ) ;
110
+ let valueProto = getValueProto ( raw , 'value' ) ;
163
111
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 ;
165
113
Object . defineProperty ( snapshot , 'previous' , {
166
114
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 ;
173
118
} ,
174
119
} ) ;
175
120
return snapshot ;
0 commit comments