Fix give-brownie-points/about API DB queries

This should speed up these api calls significantly
This commit is contained in:
Berkeley Martinez
2016-04-08 22:10:48 -07:00
parent 5fdc961587
commit 7f24986c58
2 changed files with 137 additions and 78 deletions

View File

@@ -1,3 +1,4 @@
import { Observable } from 'rx';
import passport from 'passport';
import { PassportConfigurator } from 'loopback-component-passport';
import passportProviders from './passport-providers';
@@ -70,22 +71,21 @@ PassportConfigurator.prototype.init = function passportInit(noSession) {
});
passport.deserializeUser((id, done) => {
this.userModel.findById(id, { fields }, (err, user) => {
if (err || !user) {
return done(err, user);
Observable.combineLatest(
this.userModel.findById$(id, { fields }),
this.userModel.getPointsById$(id),
(user, points) => {
if (user) { user.points = points; }
return user;
}
return this.app.dataSources.db.connector
.collection('user')
.aggregate([
{ $match: { _id: user.id } },
{ $project: { points: { $size: '$progressTimestamps' } } }
], function(err, [{ points = 1 } = {}]) {
if (err) { return done(err); }
user.points = points;
return done(null, user);
});
});
)
.doOnNext(user => {
if (!user) { throw new Error('deserialize found no user'); }
})
.subscribe(
user => done(null, user),
done
);
});
};