Skip to content

Commit 4583662

Browse files
committed
Add bindForClient utility function
1 parent c4ce5c3 commit 4583662

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,28 @@ client.stream('g.v(vid)', { vid: 1 }, { args: { language: 'nashorn' }})
249249
.pipe(/* ... */);
250250
```
251251

252+
### Gremlin.bindForClient()
253+
254+
Given a map of functions returning query `Object`s (`{ gremlin, bindings }`), returns a map of function promising execution of these queries with the given Gremlin client.
255+
256+
This function is especially useful when used with [gremlin-loader](https://github.com/jbmusso/gremlin-loader), a Webpack loader which imports functions from `.groovy` files as `Object<String, Functions>` where each functions returns query `Object`s that need to be executed with a client.
257+
258+
```javascript
259+
import { bindForClient, createClient } from 'gremlin';
260+
261+
// A function returning a Gremlin query object { gremlin, bindings }
262+
const getByName = (name) => ({
263+
gremlin: 'g.V().has("name", name)',
264+
bindings: { name }
265+
});
266+
267+
const client = createClient();
268+
const queries = bindForClient(client, { getByName });
269+
270+
// Then, within an async function:
271+
const users = await queries.getByName('Alice');
272+
```
273+
252274
### Using Gremlin-JavaScript syntax with Nashorn
253275

254276
Please see [/docs/UsingNashorn.md](Using Nashorn).

src/index.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import GremlinClient from './GremlinClient';
1+
import _ from 'lodash';
22
import template from 'gremlin-template-string';
33

4+
import GremlinClient from './GremlinClient';
5+
46

57
export function createClient(port, host, options) {
68
if (typeof port === 'object') {
@@ -16,8 +18,15 @@ export function createClient(port, host, options) {
1618
return new GremlinClient(port, host, options);
1719
};
1820

19-
export const makeTemplateTag = (client) => (...gremlinChunks) => {
20-
const query = template(...gremlinChunks);
21+
22+
/**
23+
* Given a query object, returns a Promise of executing that query with a
24+
* given client.
25+
* @param {GremlinClient} client Gremlin client to execute queries with
26+
* @param {Object} query A query Object { gremlin: String, bindings: Object }
27+
* @return {Promise} Promise of execution of the query
28+
*/
29+
const makePromise = (client, query) => {
2130
const promise = new Promise((resolve, reject) =>
2231
client.execute(query, (err, results) =>
2332
err ? reject(err) : resolve(results)
@@ -29,7 +38,25 @@ export const makeTemplateTag = (client) => (...gremlinChunks) => {
2938
return promise;
3039
}
3140

41+
export const makeTemplateTag = (client) =>
42+
(...gremlinChunks) => makePromise(client, template(...gremlinChunks));
43+
44+
/**
45+
* Given a map of functions returning query objects, returns a map
46+
* of function promising execution of these queries with the given Gremlin
47+
* client.
48+
*
49+
* @param {GremlinClient} client Gremlin client to execute queries with
50+
* @param {Object<String, Function<Object>>} functions
51+
* @return {Object<String, Function<Promise<Results>>>}
52+
*/
53+
export const bindForClient = (client, functions) => _(functions)
54+
.mapValues((fn) => (...args) => makePromise(client, fn(...args)))
55+
.value();
56+
57+
3258
export default {
3359
createClient,
34-
makeTemplateTag
60+
makeTemplateTag,
61+
bindForClient
3562
}

test/bindForClient.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createClient, bindForClient } from '../';
2+
import { assert } from 'chai';
3+
4+
5+
const getByName = (name) => ({
6+
gremlin: 'g.V().has("name", name)',
7+
bindings: {
8+
name
9+
}
10+
});
11+
12+
describe('.bindForClient()', () => {
13+
it('should return a map of bound functions', async (done) => {
14+
const client = createClient();
15+
const queries = bindForClient(client, { getByName });
16+
assert.isFunction(queries.getByName);
17+
18+
const promise = queries.getByName('marko');
19+
assert.property(promise, 'query');
20+
21+
const result = await promise;
22+
result.length.should.equal(1)
23+
done();
24+
});
25+
});

0 commit comments

Comments
 (0)