Skip to content

Commit d7e918c

Browse files
sebelgaleebyron
authored andcommitted
Add example for Google Datastore (#131)
* Add example for Google Datastore * Simplify I think the example code you supplied was more complicated than necessary and possibly also incorrect, though I'm not as familiar with GC datastore. I've made some edits for you to consider. Please take a look at this change and let me know if it looks correct
1 parent 49e2659 commit d7e918c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

examples/GoogleDatastore.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Using DataLoader with Google Datastore
2+
3+
Google Datastore is a "NoSQL" document database which supports [batch operations](https://cloud.google.com/datastore/docs/concepts/entities#batch_operations),
4+
making it well suited for use with DataLoader.
5+
6+
Here we build an example Google Datastore DataLoader using [@google-cloud/datastore](https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore).
7+
8+
```js
9+
const Datastore = require('@google-cloud/datastore');
10+
11+
const datastore = new Datastore();
12+
13+
const datastoreLoader = new DataLoader(keys =>
14+
datastore.get(keys).then(results => {
15+
// Sort resulting entities by the keys they were requested with.
16+
const entities = results[0];
17+
const entitiesByKey = {};
18+
entities.forEach(entity => {
19+
entitiesByKey[JSON.stringify(entity[datastore.KEY])] = entity;
20+
});
21+
return keys.map(key => entitiesByKey[JSON.stringify(key)] || null);
22+
}),
23+
{
24+
// Datastore complex keys need to be converted to a string for use as cache keys
25+
cacheKeyFn: key => JSON.stringify(key),
26+
}
27+
);
28+
```

0 commit comments

Comments
 (0)