@@ -3,6 +3,7 @@ import * as firebase from 'firebase'
33// import 'firebase/auth'
44// import 'firebase/database'
55// import 'firebase/storage'
6+ import { isObject } from 'lodash'
67import { defaultConfig } from './constants'
78import { validateConfig } from './utils'
89import { authActions , queryActions , storageActions } from './actions'
@@ -108,6 +109,30 @@ export default (fbConfig, otherConfig) => next =>
108109 configurable : true
109110 } )
110111
112+ /**
113+ * @private
114+ * @description Calls a method and attaches meta to value object
115+ * @param {String } method - Method to run with meta attached
116+ * @param {String } path - Path to location on Firebase which to set
117+ * @param {Object|String|Boolean|Number } value - Value to write to Firebase
118+ * @param {Function } onComplete - Function to run on complete
119+ * @return {Promise } Containing reference snapshot
120+ */
121+ const withMeta = ( method , path , value , onComplete ) => {
122+ if ( isObject ( value ) ) {
123+ const prefix = method === 'update' ? 'updated' : 'created'
124+ const dataWithMeta = {
125+ ...value ,
126+ [ `${ prefix } At` ] : firebase . database . ServerValue . TIMESTAMP
127+ }
128+ if ( instance . auth ( ) . currentUser ) {
129+ dataWithMeta [ `${ prefix } By` ] = instance . auth ( ) . currentUser . uid
130+ }
131+ return rootRef . child ( path ) [ method ] ( dataWithMeta , onComplete )
132+ }
133+ return rootRef . child ( path ) [ method ] ( value , onComplete )
134+ }
135+
111136 /**
112137 * @description Sets data to Firebase.
113138 * @param {String } path - Path to location on Firebase which to set
@@ -127,6 +152,19 @@ export default (fbConfig, otherConfig) => next =>
127152 const set = ( path , value , onComplete ) =>
128153 rootRef . child ( path ) . set ( value , onComplete )
129154
155+ /**
156+ * @description Sets data to Firebase along with meta data. Currently,
157+ * this includes createdAt and createdBy. *Warning* using this function
158+ * may have unintented consequences (setting createdAt even if data already
159+ * exists)
160+ * @param {String } path - Path to location on Firebase which to set
161+ * @param {Object|String|Boolean|Number } value - Value to write to Firebase
162+ * @param {Function } onComplete - Function to run on complete (`not required`)
163+ * @return {Promise } Containing reference snapshot
164+ */
165+ const setWithMeta = ( path , value , onComplete ) =>
166+ withMeta ( 'set' , path , value , onComplete )
167+
130168 /**
131169 * @description Pushes data to Firebase.
132170 * @param {String } path - Path to location on Firebase which to push
@@ -146,6 +184,17 @@ export default (fbConfig, otherConfig) => next =>
146184 const push = ( path , value , onComplete ) =>
147185 rootRef . child ( path ) . push ( value , onComplete )
148186
187+ /**
188+ * @description Pushes data to Firebase along with meta data. Currently,
189+ * this includes createdAt and createdBy.
190+ * @param {String } path - Path to location on Firebase which to set
191+ * @param {Object|String|Boolean|Number } value - Value to write to Firebase
192+ * @param {Function } onComplete - Function to run on complete (`not required`)
193+ * @return {Promise } Containing reference snapshot
194+ */
195+ const pushWithMeta = ( path , value , onComplete ) =>
196+ withMeta ( 'push' , path , value , onComplete )
197+
149198 /**
150199 * @description Updates data on Firebase and sends new data.
151200 * @param {String } path - Path to location on Firebase which to update
@@ -165,6 +214,18 @@ export default (fbConfig, otherConfig) => next =>
165214 const update = ( path , value , onComplete ) =>
166215 rootRef . child ( path ) . update ( value , onComplete )
167216
217+ /**
218+ * @description Updates data on Firebase along with meta. *Warning*
219+ * using this function may have unintented consequences (setting
220+ * createdAt even if data already exists)
221+ * @param {String } path - Path to location on Firebase which to update
222+ * @param {Object|String|Boolean|Number } value - Value to update to Firebase
223+ * @param {Function } onComplete - Function to run on complete (`not required`)
224+ * @return {Promise } Containing reference snapshot
225+ */
226+ const updateWithMeta = ( path , value , onComplete ) =>
227+ withMeta ( 'update' , path , value , onComplete )
228+
168229 /**
169230 * @description Removes data from Firebase at a given path.
170231 * @param {String } path - Path to location on Firebase which to remove
@@ -352,10 +413,13 @@ export default (fbConfig, otherConfig) => next =>
352413 firebase . helpers = {
353414 ref : path => firebase . database ( ) . ref ( path ) ,
354415 set,
416+ setWithMeta,
355417 uniqueSet,
356418 push,
419+ pushWithMeta,
357420 remove,
358421 update,
422+ updateWithMeta,
359423 login,
360424 logout,
361425 uploadFile,
0 commit comments