2 ms·
What did you use to roll your own? I've only done typegraphql-typeorm stuff before, trying now with prisma-nexus
by agustif 6y ago
What did you use to roll your own? I've only done typegraphql-typeorm stuff before, trying now with prisma-nexus
- beaconstudios 6y agoI use bookshelf js for reading and knex for writing. The bookshelf models are a hangover from my previous REST API; I'd just use knex for reading now as a nicer syntax sugar over SQL. I also have a couple of small helper functions for generating data loaders; I'll edit this comment and post them in a min when I'm back on my laptop.
- beaconstudios 6y agoi'm back too late to edit; I'll just double-reply. These two functions take a foreign key and a function to retrieve the data you're after and return you a dataloader with the correct indexing applied. Quite nifty, and you'd need the equivalent to both of these eventually. const oneToOne = (foreign, fn) => new Dataloader(async (keys) => { const results = await fn(keys); const index = {}; for(const result of results) { index[result.get(foreign)] = result; } return keys.map(key => key in index ? index[key] : null); }); const oneToMany = (foreign, fn) => new Dataloader(async (keys) => { const results = await fn(keys); const index = {}; for(const result of results) { const foreignKey = result.get(foreign); if(!(foreignKey in index)) { index[foreignKey] = []; } index[foreignKey].push(result); } return keys.map(key => key in index ? index[key] : []); });
- agustif 6y agoThanks for such thorough answer! Will keep in mind when looking into dataloader, looks nice.