Skip to content

Commit

Permalink
Add bindForClient utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmusso committed Apr 6, 2016
1 parent c4ce5c3 commit 4583662
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -249,6 +249,28 @@ client.stream('g.v(vid)', { vid: 1 }, { args: { language: 'nashorn' }})
.pipe(/* ... */);
```

### Gremlin.bindForClient()

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.

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.

```javascript
import { bindForClient, createClient } from 'gremlin';

// A function returning a Gremlin query object { gremlin, bindings }
const getByName = (name) => ({
gremlin: 'g.V().has("name", name)',
bindings: { name }
});

const client = createClient();
const queries = bindForClient(client, { getByName });

// Then, within an async function:
const users = await queries.getByName('Alice');
```

### Using Gremlin-JavaScript syntax with Nashorn

Please see [/docs/UsingNashorn.md](Using Nashorn).
Expand Down
35 changes: 31 additions & 4 deletions src/index.js
@@ -1,6 +1,8 @@
import GremlinClient from './GremlinClient';
import _ from 'lodash';
import template from 'gremlin-template-string';

import GremlinClient from './GremlinClient';


export function createClient(port, host, options) {
if (typeof port === 'object') {
Expand All @@ -16,8 +18,15 @@ export function createClient(port, host, options) {
return new GremlinClient(port, host, options);
};

export const makeTemplateTag = (client) => (...gremlinChunks) => {
const query = template(...gremlinChunks);

/**
* Given a query object, returns a Promise of executing that query with a
* given client.
* @param {GremlinClient} client Gremlin client to execute queries with
* @param {Object} query A query Object { gremlin: String, bindings: Object }
* @return {Promise} Promise of execution of the query
*/
const makePromise = (client, query) => {
const promise = new Promise((resolve, reject) =>
client.execute(query, (err, results) =>
err ? reject(err) : resolve(results)
Expand All @@ -29,7 +38,25 @@ export const makeTemplateTag = (client) => (...gremlinChunks) => {
return promise;
}

export const makeTemplateTag = (client) =>
(...gremlinChunks) => makePromise(client, template(...gremlinChunks));

/**
* Given a map of functions returning query objects, returns a map
* of function promising execution of these queries with the given Gremlin
* client.
*
* @param {GremlinClient} client Gremlin client to execute queries with
* @param {Object<String, Function<Object>>} functions
* @return {Object<String, Function<Promise<Results>>>}
*/
export const bindForClient = (client, functions) => _(functions)
.mapValues((fn) => (...args) => makePromise(client, fn(...args)))
.value();


export default {
createClient,
makeTemplateTag
makeTemplateTag,
bindForClient
}
25 changes: 25 additions & 0 deletions test/bindForClient.js
@@ -0,0 +1,25 @@
import { createClient, bindForClient } from '../';
import { assert } from 'chai';


const getByName = (name) => ({
gremlin: 'g.V().has("name", name)',
bindings: {
name
}
});

describe('.bindForClient()', () => {
it('should return a map of bound functions', async (done) => {
const client = createClient();
const queries = bindForClient(client, { getByName });
assert.isFunction(queries.getByName);

const promise = queries.getByName('marko');
assert.property(promise, 'query');

const result = await promise;
result.length.should.equal(1)
done();
});
});

0 comments on commit 4583662

Please sign in to comment.