-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Firebase connector * Added empty line * Updated library to 1.1.0 version
- Loading branch information
1 parent
42cd970
commit 630ab03
Showing
4 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const Connector = require('./Connector'); | ||
|
||
class FirebaseConnector extends Connector { | ||
constructor(firebaseClient, rootRef) { | ||
super(); | ||
this.firebaseClient = firebaseClient; | ||
this.rootRef = rootRef; | ||
} | ||
|
||
getReference(key, field) { | ||
const path = field ? `${key}/${field}` : `${key}`; | ||
return this.firebaseClient.ref(this.rootRef).child(path); | ||
} | ||
|
||
async get(type, key, field) { | ||
const experimentsStatsRef = this.getReference(key, field); | ||
return experimentsStatsRef.once('value') | ||
.then(dataSnapshot => { | ||
return dataSnapshot.val(); | ||
}); | ||
} | ||
|
||
async set(type, key, field, value) { | ||
return value | ||
? this.getReference(key).update({ [field]: value }) | ||
: this.getReference(key, field).remove(); | ||
} | ||
|
||
async increment(type, key, field, amount = 1) { | ||
const experimentsStatsRef = this.getReference(key); | ||
const childRef = experimentsStatsRef.child(field); | ||
const child = await childRef.once('value'); | ||
if (child.val) { | ||
const currentVal = child.val(); | ||
return experimentsStatsRef.update({ [field]: currentVal + amount }); | ||
} else { | ||
return this.save(type, key, { [field]: amount }); | ||
} | ||
|
||
} | ||
|
||
async save(type, key, saveData) { | ||
return this.getReference(key).set(saveData); | ||
} | ||
|
||
async load(type, key) { | ||
const experimentsStatsRef = this.getReference(key); | ||
return experimentsStatsRef.once('value') | ||
.then(dataSnapshot => { | ||
return dataSnapshot.val(); | ||
}); | ||
} | ||
|
||
async reset(type, key, ...fields) { | ||
const saveData = fields.reduce((res, el) => res[field] = 0, {}); | ||
return this.getReference(key).set(saveData); | ||
} | ||
|
||
async delete(type, key) { | ||
return this.getReference(key).remove(); | ||
} | ||
} | ||
|
||
module.exports = FirebaseConnector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
const Multivariate = require('./Multivariate'); | ||
const Connector = require('./connector/Connector'); | ||
const RedisConnector = require('./connector/RedisConnector'); | ||
const FirebaseConnector = require('./connector/FirebaseConnector'); | ||
|
||
module.exports = { | ||
Multivariate: Multivariate, | ||
Connector: Connector, | ||
RedisConnector: RedisConnector | ||
RedisConnector: RedisConnector, | ||
FirebaseConnector: FirebaseConnector | ||
} |