@@ -159,7 +159,7 @@ could result in cached data incorrectly appearing in each request. Typically,
159159DataLoader instances are created when a Request begins, and are not used once the
160160Request ends.
161161
162- For example, when using with ` express ` :
162+ For example, when using with [ express] [ ] :
163163
164164``` js
165165function createLoaders (authToken ) {
@@ -417,6 +417,7 @@ let usernameLoader = new DataLoader(names => genUsernames(names).then(users => {
417417}));
418418```
419419
420+
420421## Custom Caches
421422
422423DataLoader can optionaly be provided a custom Map instance to use as its
@@ -430,118 +431,8 @@ short-lived.
430431
431432## Common Back-ends
432433
433- Looking to get started with a specific back-end? Try these example loaders:
434-
435-
436- #### Redis
437-
438- Redis is a very simple key-value store which provides the batch load method
439- [ MGET] ( http://redis.io/commands/mget ) . Here we build a Redis DataLoader
440- using [ node_redis] [ ] .
441-
442- ``` js
443- var DataLoader = require (' dataloader' );
444- var redis = require (' redis' );
445-
446- var client = redis .createClient ();
447-
448- var redisLoader = new DataLoader (keys => new Promise ((resolve , reject ) => {
449- client .mget (keys, (error , results ) => {
450- if (error) {
451- return reject (error);
452- }
453- resolve (results .map ((result , index ) =>
454- result !== null ? result : new Error (` No key: ${ keys[index]} ` )
455- ));
456- });
457- }));
458- ```
459-
460-
461- #### CouchDB
462-
463- This example uses the [ nano] [ ] CouchDB client which offers a ` fetch ` method
464- implementing the [ HTTP Bulk Document API] ( http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API ) .
465-
466- ``` js
467- var DataLoader = require (' dataloader' );
468- var nano = require (' nano' );
469-
470- var couch = nano (' http://localhost:5984' );
471-
472- var userDB = couch .use (' users' );
473- var userLoader = new DataLoader (keys => new Promise ((resolve , reject ) => {
474- userDB .fetch ({ keys: keys }, (error , docs ) => {
475- if (error) {
476- return reject (error);
477- }
478- resolve (docs .rows .map (row => row .error ? new Error (row .error ) : row .doc ));
479- });
480- }));
481-
482- // Usage
483-
484- var promise1 = userLoader .load (' 8fce1902834ac6458e9886fa7f89c0ef' );
485- var promise2 = userLoader .load (' 00a271787f89c0ef2e10e88a0c00048b' );
486-
487- Promise .all ([ promise1, promise2 ]).then (([ user1 , user2 ]) => {
488- console .log (user1, user2);
489- });
490- ```
491-
492-
493- #### SQLite
434+ Looking to get started with a specific back-end? Try the [ loaders in the examples directory] ( /examples ) .
494435
495- SQL offers a natural batch mechanism with ` SELECT * WHERE IN ` . ` DataLoader `
496- is designed to operate over key-value stores, so in this example just requests
497- the entire row at a given ` id ` .
498-
499- This example uses the [ sqlite3] [ ] client which offers a ` parallelize ` method to
500- further batch queries together. Another non-caching ` DataLoader ` utilizes this
501- method to provide a similar API. ` DataLoaders ` can access other ` DataLoaders ` .
502-
503- ``` js
504- var DataLoader = require (' dataloader' );
505- var sqlite3 = require (' sqlite3' );
506-
507- var db = new sqlite3.Database (' ./to/your/db.sql' );
508-
509- // Dispatch a WHERE-IN query, ensuring response has rows in correct order.
510- var userLoader = new DataLoader (ids => {
511- var params = ids .map (id => ' ?' ).join ();
512- var query = ` SELECT * FROM users WHERE id IN (${ params} )` ;
513- return queryLoader .load ([query, ids]).then (
514- rows => ids .map (
515- id => rows .find (row => row .id === id) || new Error (` Row not found: ${ id} ` )
516- )
517- );
518- });
519-
520- // Parallelize all queries, but do not cache.
521- var queryLoader = new DataLoader (queries => new Promise (resolve => {
522- var waitingOn = queries .length ;
523- var results = [];
524- db .parallelize (() => {
525- queries .forEach ((query , index ) => {
526- db .all .apply (db, query .concat ((error , result ) => {
527- results[index] = error || result;
528- if (-- waitingOn === 0 ) {
529- resolve (results);
530- }
531- }));
532- });
533- });
534- }), { cache: false });
535-
536- // Usage
537-
538- var promise1 = userLoader .load (' 1234' );
539- var promise2 = userLoader .load (' 5678' );
540-
541- Promise .all ([ promise1, promise2 ]).then (([ user1 , user2 ]) => {
542- console .log (user1, user2);
543- });
544- ```
545436
546437## Video Source Code Walkthrough
547438
@@ -556,6 +447,5 @@ Promise.all([ promise1, promise2 ]).then(([ user1, user2]) => {
556447[ cache algorithms ] : https://en.wikipedia.org/wiki/Cache_algorithms
557448[ express ] : http://expressjs.com/
558449[ babel/polyfill ] : https://babeljs.io/docs/usage/polyfill/
559- [ node_redis ] : https://github.com/NodeRedis/node_redis
560- [ nano ] : https://github.com/dscape/nano
561- [ sqlite3 ] : https://github.com/mapbox/node-sqlite3
450+
451+
0 commit comments