Instantly share code, notes, and snippets.
Created
June 23, 2018 05:47
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save technoplato/be56fe6426cef9e1d8abcdb0850393a2 to your computer and use it in GitHub Desktop.
Social Following / Follower / Feed / Posts structure on Firebase in Node
This file contains hidden or 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
const admin = require('firebase-admin'); | |
const serviceAccount = require('/Users/lustig/Development/Javascript/FirebasePractice/service-account.json'); | |
const axios = require('axios'); | |
const myUid = '-LFcicCx0VxhBraCZbfP'; | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://react-native-firebase-18440.firebaseio.com/' | |
}); | |
const db = admin.database(); | |
const root = db.ref(); | |
const followers = root.child('followers'); | |
const users = root.child('users'); | |
const feeds = root.child('feeds'); | |
const myPosts = root.child('posts').child(myUid); | |
const myFollowers = followers.child(myUid); | |
const myPost = { | |
time: new Date().toLocaleTimeString(), | |
text: 'Hello, what is good with thee?' | |
}; | |
function createNewPost() { | |
myPosts.push(myPost); | |
myFollowers.on('child_added', function(data) { | |
const theirFeed = root.child('feed').child(data.key); | |
theirFeed.push(myPost); | |
}); | |
} | |
function populateFollowers() { | |
const myFollowers = followers.child(myUid); | |
setInterval(function() { | |
myFollowers.push({ | |
username: 'user' + (new Date().getTime() % 10) | |
}); | |
}, 3000); | |
} | |
function getAllChildrenPromise(listRef, debug = false) { | |
const keysLink = listRef.toString() + '.json?shallow=true'; | |
if (debug) { | |
console.log('Keys Link:', keysLink); | |
} | |
return axios.get(keysLink).then(res => { | |
if (res.data) { | |
const keys = Object.keys(res.data); | |
const promises = []; | |
keys.forEach(key => { | |
promises.push(listRef.child(key).once('value')); | |
}); | |
return Promise.all(promises); | |
} else { | |
return Promise.resolve([]); | |
} | |
}); | |
} | |
function populateAllChildrenPromise(ref, id, values) { | |
const promises = []; | |
values.forEach(value => { | |
promises.push( | |
ref | |
.child(id) | |
.child(value.key) | |
.set(value.val()) | |
); | |
}); | |
return Promise.all(promises); | |
} | |
module.exports.addFollower = function() { | |
const name = process.argv[1] || 'Jimmy Johns'; | |
const age = process.argv[2] || 17; | |
// extract key for creating feed | |
const newFollowerRef = myFollowers.push(); | |
const newFollowerKey = newFollowerRef.key; | |
newFollowerRef | |
.set({ | |
name: name, | |
age: age | |
}) | |
// After we add the follower, add the followed's posts to follower's feed | |
.then(followerAdded => { | |
return getAllChildrenPromise(myPosts) | |
.then(values => { | |
return populateAllChildrenPromise(feeds, newFollowerKey, values); | |
}) | |
.then(complete => { | |
console.log('We should be done...'); | |
process.exit(0); | |
}); | |
}); | |
}; | |
module.exports.addTestMyPost = function() { | |
const newMyPostref = myPosts.push(); | |
newMyPostref | |
.set(myPost) | |
.then(complete => { | |
console.log('new post key', newMyPostref.key); | |
return getAllChildrenPromise(myFollowers); | |
}) | |
.then(followers => { | |
const postFeed = []; | |
followers.forEach(follower => { | |
console.log('follower key', follower.key); | |
postFeed.push( | |
feeds | |
.child(follower.key) | |
.child(newMyPostref.key) | |
.set(myPost) | |
); | |
}); | |
if (postFeed) { | |
return Promise.all(postFeed); | |
} else { | |
return Promise.resolve([]); | |
} | |
}) | |
.then(completo => { | |
console.log('pray'); | |
process.exit(0); | |
}); | |
}; | |
module.exports.deleteData = function() { | |
Promise.all([ | |
root.child('feeds').remove(), | |
root.child('followers').remove() | |
]).then(complete => { | |
process.exit(0); | |
}); | |
}; | |
function print() { | |
console.log('hi'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment