Last active
December 1, 2016 15:29
-
-
Save you-fail-me/e90fce8f98a59781f79a4d1ece04ee4b to your computer and use it in GitHub Desktop.
A draft of recent txs denormalisation
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
/* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May be ok, but could cause more code bloat
They are also using
publishComposite
See
v2/src/server/publications/transactions.js