Skip to content

Commit fc03488

Browse files
authored
cleaning up some formatting and adding clarifications.
1 parent d19468f commit fc03488

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

examples/RethinkDB.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,44 @@
22

33
RethinkDb offers a batching method called `getAll` but there are a few caveats :
44
* Order of results is not guaranteed ([rethinkdb/rethinkdb#5187](https://github.com/rethinkdb/rethinkdb/issues/5187))
5-
* Non-existent keys will not return and empty record
6-
Assuming a table `example_table` with those records :
5+
* Non-existent keys will not return an empty record
6+
7+
For example, against a table `example_table` with these records:
78
```js
89
[
910
{"id": 1, "name": "Document 1"},
1011
{"id": 2, "name": "Document 2"}
1112
]
1213
```
13-
A query `r.getAll(1, 2, 3)` could return such an array :
14+
A query `r.getAll(1, 2, 3)` could return:
1415
```js
1516
[
1617
{"id": 2, "name": "Document 2"},
1718
{"id": 1, "name": "Document 1"}
1819
]
1920
```
2021

21-
In essence, this naive implementation won't work :
22+
Because query keys and values are associated by position in the dataloader cache, this naive implementation won't work (with the same table as above):
2223
```js
2324
var r = require('rethinkdb');
2425
var db = await r.connect();
2526

2627
var batchLoadFn = keys => db.table('example_table').getAll(...keys).then(res => res.toArray());
2728
var exampleLoader = new DataLoader(batchLoadFn);
2829

29-
await exampleLoader.loadMany([1, 2, 3]); // Throws, values length !== keys length
30+
await exampleLoader.loadMany([1, 2, 3]); // Throws values length !== keys length
3031

3132
await exampleLoader.loadMany([1, 2]);
3233
await exampleLoader.load(1); // {"id": 2, "name": "Document 2"}
3334
```
3435

3536
A solution is to normalize results returned by `getAll` to match the structure of supplied `keys`.
3637

37-
To achieve this efficiently, we first write an indexing function. This function will return a Map indexing results.
38-
Parameters :
39-
* `results` : Array of RethinkDb results
40-
* `indexField` : String indicating which field was used as index for this batch query
41-
* `cacheKeyFn` : Optional function used to serialize non-scalar index field values
38+
To achieve this efficiently, we first write an indexing function. This function will return a `Map` indexing results.
39+
Parameters:
40+
* `results`: Array of RethinkDb results
41+
* `indexField`: String indicating which field was used as index for this batch query
42+
* `cacheKeyFn`: Optional function used to serialize non-scalar index field values
4243
```js
4344
function indexResults(results, indexField, cacheKeyFn = key => key) {
4445
var indexedResults = new Map();
@@ -58,7 +59,7 @@ function normalizeRethinkDbResults(keys, indexField, cacheKeyFn = key => key) {
5859
}
5960
```
6061

61-
Full dataloader implementation :
62+
Full dataloader implementation:
6263
```js
6364
var r = require('rethinkdb');
6465
var db = await r.connect();

0 commit comments

Comments
 (0)