Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Created July 22, 2016 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/6d7b830756a513e73a03e56d00226e12 to your computer and use it in GitHub Desktop.
Save adrianmcli/6d7b830756a513e73a03e56d00226e12 to your computer and use it in GitHub Desktop.
test rethinkdb query
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