Skip to content

Commit 5e81cdc

Browse files
committed
Break out examples to a separate directory.
Mentioned in #52
1 parent a8af693 commit 5e81cdc

File tree

4 files changed

+123
-115
lines changed

4 files changed

+123
-115
lines changed

README.md

Lines changed: 5 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ could result in cached data incorrectly appearing in each request. Typically,
159159
DataLoader instances are created when a Request begins, and are not used once the
160160
Request ends.
161161

162-
For example, when using with `express`:
162+
For example, when using with [express][]:
163163

164164
```js
165165
function createLoaders(authToken) {
@@ -417,6 +417,7 @@ let usernameLoader = new DataLoader(names => genUsernames(names).then(users => {
417417
}));
418418
```
419419

420+
420421
## Custom Caches
421422

422423
DataLoader 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+

examples/CouchDB.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Using DataLoader with CouchDB
2+
3+
CouchDB is a "NoSQL" document database which supports batch loading via the
4+
[HTTP Bulk Document API](http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API),
5+
making it well suited for use with DataLoader.
6+
7+
This example uses the [nano][] CouchDB client which offers a `fetch` method
8+
supporting the bulk document API.
9+
10+
```js
11+
var DataLoader = require('dataloader');
12+
var nano = require('nano');
13+
14+
var couch = nano('http://localhost:5984');
15+
16+
var userDB = couch.use('users');
17+
var userLoader = new DataLoader(keys => new Promise((resolve, reject) => {
18+
userDB.fetch({ keys: keys }, (error, docs) => {
19+
if (error) {
20+
return reject(error);
21+
}
22+
resolve(docs.rows.map(row => row.error ? new Error(row.error) : row.doc));
23+
});
24+
}));
25+
26+
// Usage
27+
28+
var promise1 = userLoader.load('8fce1902834ac6458e9886fa7f89c0ef');
29+
var promise2 = userLoader.load('00a271787f89c0ef2e10e88a0c00048b');
30+
31+
Promise.all([ promise1, promise2 ]).then(([ user1, user2]) => {
32+
console.log(user1, user2);
33+
});
34+
```
35+
36+
[nano]: https://github.com/dscape/nano

examples/Redis.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Using DataLoader with Redis
2+
3+
Redis is a very simple key-value store which provides the batch load method
4+
[MGET](http://redis.io/commands/mget) which makes it very well suited for use
5+
with DataLoader.
6+
7+
Here we build an example Redis DataLoader using [node_redis][].
8+
9+
```js
10+
var DataLoader = require('dataloader');
11+
var redis = require('redis');
12+
13+
var client = redis.createClient();
14+
15+
var redisLoader = new DataLoader(keys => new Promise((resolve, reject) => {
16+
client.mget(keys, (error, results) => {
17+
if (error) {
18+
return reject(error);
19+
}
20+
resolve(results.map((result, index) =>
21+
result !== null ? result : new Error(`No key: ${keys[index]}`)
22+
));
23+
});
24+
}));
25+
```
26+
27+
[node_redis]: https://github.com/NodeRedis/node_redis

examples/SQL.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Using DataLoader with SQLite
2+
3+
While not a key-value store, SQL offers a natural batch mechanism with
4+
`SELECT * WHERE IN` statements. While `DataLoader` is best suited for key-value
5+
stores, it is still suited for SQL when queries remain simple. This example
6+
requests the entire row at a given `id`, however your usage may differ.
7+
8+
This example uses the [sqlite3][] client which offers a `parallelize` method to
9+
further batch queries together. Another non-caching `DataLoader` utilizes this
10+
method to provide a similar API. `DataLoaders` can access other `DataLoaders`.
11+
12+
```js
13+
var DataLoader = require('dataloader');
14+
var sqlite3 = require('sqlite3');
15+
16+
var db = new sqlite3.Database('./to/your/db.sql');
17+
18+
// Dispatch a WHERE-IN query, ensuring response has rows in correct order.
19+
var userLoader = new DataLoader(ids => {
20+
var params = ids.map(id => '?' ).join();
21+
var query = `SELECT * FROM users WHERE id IN (${params})`;
22+
return queryLoader.load([query, ids]).then(
23+
rows => ids.map(
24+
id => rows.find(row => row.id === id) || new Error(`Row not found: ${id}`)
25+
)
26+
);
27+
});
28+
29+
// Parallelize all queries, but do not cache.
30+
var queryLoader = new DataLoader(queries => new Promise(resolve => {
31+
var waitingOn = queries.length;
32+
var results = [];
33+
db.parallelize(() => {
34+
queries.forEach((query, index) => {
35+
db.all.apply(db, query.concat((error, result) => {
36+
results[index] = error || result;
37+
if (--waitingOn === 0) {
38+
resolve(results);
39+
}
40+
}));
41+
});
42+
});
43+
}), { cache: false });
44+
45+
// Usage
46+
47+
var promise1 = userLoader.load('1234');
48+
var promise2 = userLoader.load('5678');
49+
50+
Promise.all([ promise1, promise2 ]).then(([ user1, user2]) => {
51+
console.log(user1, user2);
52+
});
53+
```
54+
55+
[sqlite3]: https://github.com/mapbox/node-sqlite3

0 commit comments

Comments
 (0)