Skip to content

Commit 3387382

Browse files
committed
Create messageObservable command
1 parent ae9d5a8 commit 3387382

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed

README.md

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ $ `rejectUnauthorized`: when using ssl, whether to reject self-signed certificat
9595
### Executing Gremlin queries
9696

9797
The client currently supports three modes:
98-
* callback mode (with internal buffer)
99-
* promise mode
100-
* streaming moderesults
101-
* streaming protocol messages (low level API, for advanced usages)
98+
* callback
99+
* promise
100+
* Observable (RxJS)
102101

103102
#### Callback mode: client.execute(script, bindings, message, callback)
104103

@@ -157,58 +156,34 @@ const fetchByName = async (name) => {
157156
fetchByName('Alice');
158157
```
159158

160-
#### Stream mode
159+
#### Observable mode
161160

162-
##### client.stream(script, bindings, message)
161+
##### client.observable(script, bindings, message)
163162

164-
Return a Node.js ReadableStream set in Object mode. The stream emits a distinct `data` event per query result returned by Gremlin Server.
163+
Return an RxJS `Observable` of results.
165164

166-
Internally, a 1-level flatten is performed on all raw protocol messages returned. If you do not wish this behavior and prefer handling raw protocol messages with batched results, prefer using `client.messageStream()`.
167-
168-
The order in which results are returned is guaranteed, allowing you to effectively use `order` steps and the like in your Gremlin traversal.
169-
170-
The stream emits an `end` event when the client receives the last `statusCode: 299` message returned by Gremlin Server.
165+
Internally, a 1-level flatten is performed on all protocol messages returned.
171166

172167
```javascript
173-
const query = client.stream('g.V()');
174-
175-
// If playing with classic TinkerPop graph, will emit 6 data events
176-
query.on('data', (result) => {
177-
// Handle first vertex
178-
console.log(result);
179-
});
168+
const query$ = client.observable('g.V()');
180169

181-
query.on('end', () => {
182-
console.log('All results fetched');
183-
});
170+
query$.subscribe(
171+
(vertex) => console.log(vertex), // will log 6 times,
172+
(err) => console.error(err),
173+
() => console.log('Done!')
174+
);
184175
```
185176

186-
This allows you to effectively `.pipe()` the stream to any other Node.js WritableStream/TransformStream.
187-
188-
##### client.messageStream(script, bindings, message)
189-
190-
A lower level method that returns a `ReadableStream` which emits the raw protocol messages returned by Gremlin Server as distinct `data` events.
191-
192-
If you wish a higher-level stream of `results` rather than protocol messages, please use `client.stream()`.
177+
##### client.messageObservable(script, bindings, message)
193178

194-
Although a public method, this is recommended for advanced usages only.
195-
196-
```javascript
197-
const client = Gremlin.createClient();
198-
199-
const stream = client.messageStream('g.V()');
200-
201-
// Will emit 3 events with a resultIterationBatchSize set to 2 and classic graph defined in gremlin-server.yaml
202-
stream.on('data', (message) => {
203-
console.log(message.result); // Array of 2 vertices
204-
});
205-
```
179+
A lower level method that returns an `Observable` of raw protocol messages return by Gremlin Server.
180+
Recommended for advanced usages/troubleshooting.
206181

207182
### Adding bound parameters to your scripts
208183

209184
For better performance and security concerns (script injection), you must send bound parameters (`bindings`) with your scripts.
210185

211-
`client.execute()`, `client.stream()` and `client.messageStream()` share the same function signature: `(script, bindings, querySettings)`.
186+
`client.execute()`, `client.observable()` and `client.messageObservable()` share the same function signature: `(script, bindings, querySettings)`.
212187

213188
Notes/Gotchas:
214189
- Any bindings set to `undefined` will be automatically escaped with `null` values (first-level only) in order to generate a valid JSON string sent to Gremlin Server.
@@ -251,7 +226,7 @@ client.execute('g.v(1)', null, { args: { language: 'nashorn' }}, (err, results)
251226
// Handle result
252227
});
253228
```
254-
Basically, all you have to do is provide an Object as third parameter to any `client.stream()`, `client.execute()` or `client.streamMessage()` methods.
229+
Basically, all you have to do is provide an Object as third parameter to any `client.observable()`, `client.execute()` or `client.messageObservable()` methods.
255230

256231
Because we're not sending any bound parameters (`bindings`) in this example, notice how the second argument **must** be set to `null` so the low level message object is not mistaken with bound arguments.
257232

@@ -263,9 +238,9 @@ client.execute('g.v(vid)', { vid: 1 }, { args: { language: 'nashorn' }}, (err, r
263238
});
264239
```
265240

266-
Or in stream mode:
241+
Or in Observable mode:
267242
```javascript
268-
client.stream('g.v(vid)', { vid: 1 }, { args: { language: 'nashorn' }})
243+
client.observable('g.v(vid)', { vid: 1 }, { args: { language: 'nashorn' }})
269244
.pipe(/* ... */);
270245
```
271246

src/GremlinClient.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class GremlinClient extends EventEmitter {
247247
)
248248
}
249249

250-
observable(script, bindings, rawMessage) {
250+
messageObservable(script, bindings, rawMessage) {
251251
const command = {
252252
message: this.buildMessage(script, bindings, rawMessage),
253253
}
@@ -294,12 +294,15 @@ class GremlinClient extends EventEmitter {
294294
noContentMessage$,
295295
errorMessages$
296296
)
297-
.flatMap(({ result: { data }}) => data)
298-
299297
.takeUntil(terminationMessages$);
300298

301299
return results$;
302300
}
301+
302+
observable(script, bindings, rawMessage) {
303+
return this.messageObservable(script, bindings, rawMessage)
304+
.flatMap(({ result: { data }}) => data)
305+
}
303306
}
304307

305308
export default GremlinClient;

0 commit comments

Comments
 (0)