Created
July 22, 2016 11:04
-
-
Save adrianmcli/6d7b830756a513e73a03e56d00226e12 to your computer and use it in GitHub Desktop.
test rethinkdb query
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
const query = () => r.db('dataDb') | |
.table('trackedUsers') | |
.filter({ appId }) | |
.concatMap(user => r.db('dataDb').table('alive') | |
.getAll(user('userId'), { index: 'userId' }) | |
.do( | |
result => r.branch( | |
result.count().eq(0), | |
[{ left: user }], | |
result.map(alive => ({ left: user, right: alive })) | |
) | |
) | |
) | |
.group(userAlive => ({ | |
appId: userAlive('left')('appId'), | |
userId: userAlive('left')('userId'), | |
})) | |
.ungroup() | |
.map(doc => ({ | |
appId: doc('group')('appId'), | |
userId: doc('group')('userId'), | |
numEvents: doc('reduction').count(), | |
totalTime: doc('reduction')('right')('aliveFor').sum(), | |
lastSeen: doc('reduction')('right')('receivedAt').max().default(null), | |
createdAt: doc('reduction')('left')('createdAt').max().default(null), | |
emails: doc('reduction')('left')('email')(0), | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment