Skip to content

Instantly share code, notes, and snippets.

@you-fail-me
Last active December 1, 2016 15:29
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 you-fail-me/e90fce8f98a59781f79a4d1ece04ee4b to your computer and use it in GitHub Desktop.
Save you-fail-me/e90fce8f98a59781f79a4d1ece04ee4b to your computer and use it in GitHub Desktop.
A draft of recent txs denormalisation
/* DB DOC */
{
_id: 'z12xc124v24bnm', // corresponds to owner's userId
transactions: [
{
collection: 'Transactions',
doc: 'identifier',
createdAt: 'xxxxxx'
}, {
collection: 'WavecrestTransactions',
doc: 'identifier',
createdAt: 'yyyyyy'
}
]
}
/* UPDATING RECENT TRANSACTIONS WHEN A NEW TX APPEARS */
/* SHOULD BE A FUNCTION, CALLED WITHIN HOOKS OF ALL TRANSACTION COLLECTIONS */
Collections.RecentTransactions.update({
owher: this.userId
}, {
$push: {
transactions: {
$each: [{
collection: 'WavecrestTransactions',
doc: 'identifier',
createdAt: 'zzzzz'
}],
$sort: {
createdAt: -1
},
$slice: 10
}
}
});
// that $each, $sort and $slice thing is keeping the array length constant, so it always has X (here 10) most recent txs
// In fact, we can make things simpler by storing full copies of recent transactions
// and keeping that there're only e.g. 10 per user, so the transaction widget grabs docs directly and has no need
// to look up at other collections. The question is - is it acceptable in terms of storage and increasing the amount of data to send
// (will need to send 10 more docs to every user)?
// map ids to collections to make less queries to mongo
function sortRecentTransactions(transactions) {
return transactions.reduce((map, rt, i) => {
if (Array.isArray(map[rt.collection])) {
map[rt.collection].push(rt.doc);
} else {
map[rt.collection] = [rt.doc];
}
return map;
}, {});
}
Meteor.publish('transactions.recent', function () {
const recents = RecentTransactions.find({
owner: this.userId
}).fetch();
const sortedIds = sortRecentTransactions(recents);
const cursors = [];
Object.keys(sortedIds).forEach(coll => {
cursors.push(...Collections[coll].find({
_id: {
$in: sortedIds[coll]
}
}));
});
return [ ...cursors, ...RecentTransactions.find({owner: this.userId}) ];
});
/* CLIENT */
if (Meteor.subscribe('transactions.recent').ready()) {
const recents = RecentTransactions.find().fetch();
const sortedIds = sortRecentTransactions(recents);
const transactions = [];
Object.keys(sortedIds).forEach(coll => {
transactions.push(...Collections[coll].find({
_id: {
$in: sortedIds[coll]
}
}).fetch());
});
transactions.sort((a, b) => b.createdAt - a.createdAt);
}
@yogiben
Copy link

yogiben commented Dec 1, 2016

// In fact, we can make things simpler by storing full copies of recent transactions
Don't we then have to update the cached docs?

May be ok, but could cause more code bloat

Meteor.publish('transactions.recent', function () {
Currently transactions pubs have quite a lot of biz logic. This would need to be reused.

They are also using publishComposite

See v2/src/server/publications/transactions.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment