Skip to content

Commit 04039a2

Browse files
authored
Version v1.4.0 Beta 2 (#100)
* fix using with yarn - `.npmignore` file removed so that `.yarn.lock` file will not be included in npm release (only files listed in package file `files` property) * `pushWithMeta`, `setWithMeta`, and `updateWithMeta` methods added - write to firebase with createdAt/updatedAt and createdBy/updatedBy
1 parent d65aa2f commit 04039a2

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

.npmignore

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-redux-firebase",
3-
"version": "1.4.0-beta",
3+
"version": "1.4.0-beta.2",
44
"description": "Redux integration for Firebase. Comes with a Higher Order Component for use with React.",
55
"browser": "dist/react-redux-firebase.js",
66
"main": "lib/index.js",

src/compose.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
67
import { defaultConfig } from './constants'
78
import { validateConfig } from './utils'
89
import { 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,

tests/unit/compose.spec.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const reducer = sinon.spy()
77
const generateCreateStore = (params) =>
88
compose(composeFunc(
99
params ? omit(fbConfig, params) : fbConfig,
10-
{ userProfile: 'users', enableLogging: false, enableRedirectHandling: false }
10+
{
11+
userProfile: 'users',
12+
enableLogging: false,
13+
enableRedirectHandling: false
14+
}
1115
))(createStore)
1216
const helpers = generateCreateStore()(reducer).firebase.helpers
1317

@@ -37,14 +41,36 @@ describe('Compose', () => {
3741
helpers.set('test', {some: 'asdf'})
3842
)
3943

44+
describe('setWithMeta', () => {
45+
it('accepts object', () =>
46+
helpers.setWithMeta('test', {some: 'asdf'})
47+
)
48+
it('does not attach meta to string', () =>
49+
// TODO: confirm that data set actually does not include meta
50+
helpers.setWithMeta('test', 'asdd')
51+
)
52+
})
53+
4054
describe('push', () =>
4155
helpers.push('test', {some: 'asdf'})
4256
)
4357

58+
describe('pushWithMeta', () => {
59+
it('accepts object', () =>
60+
helpers.pushWithMeta('test', {some: 'asdf'})
61+
)
62+
})
63+
4464
describe('update', () =>
4565
helpers.update('test', {some: 'asdf'})
4666
)
4767

68+
describe('updateWithMeta', () => {
69+
it('accepts object', () =>
70+
helpers.updateWithMeta('test', {some: 'asdf'})
71+
)
72+
})
73+
4874
describe('uniqueSet', () =>{
4975
// remove test root after test are complete
5076
after(() =>

0 commit comments

Comments
 (0)