Skip to content

Commit c49c6a9

Browse files
authored
Merge pull request #161 from gregolsky/v4.0
Subscriptions example
2 parents c5bf625 + a85fa55 commit c49c6a9

File tree

3 files changed

+97
-20
lines changed

3 files changed

+97
-20
lines changed

Diff for: README.md

+59-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1-
# RavenDB client for Node.js
1+
# RavenDB Client for Node.js
22

3-
[![build status](https://travis-ci.org/ravendb/ravendb-nodejs-client.svg?branch=v4.0)](https://travis-ci.org/ravendb/ravendb-nodejs-client) [![Known Vulnerabilities](https://snyk.io/test/github/ravendb/ravendb-nodejs-client/badge.svg)](https://snyk.io/test/github/ravendb/ravendb-nodejs-client)
4-
5-
## Changelog
3+
[![NPM](https://nodei.co/npm/ravendb.png?compact=true)](https://nodei.co/npm/ravendb/)
64

7-
### 4.0.3 - 2018-10-01
8-
Added support for the following features:
9-
- [Streaming](#streaming)
10-
- More like this
11-
- [Suggestions](#suggestions)
12-
- [Revisions](#revisions)
13-
- [Advanced patching](#advanced-patching)
5+
[![build status](https://travis-ci.org/ravendb/ravendb-nodejs-client.svg?branch=v4.0)](https://travis-ci.org/ravendb/ravendb-nodejs-client) [![Known Vulnerabilities](https://snyk.io/test/github/ravendb/ravendb-nodejs-client/badge.svg)](https://snyk.io/test/github/ravendb/ravendb-nodejs-client)
146

15-
### 4.0.2 - 2018-09-14
16-
Added support for the following features:
17-
- [Attachments](#attachments)
18-
- [Bulk Insert](#bulk-insert)
19-
- [Changes API](#changes-api)
207

218
## Installation
229

2310
```bash
2411
npm install --save ravendb
2512
```
2613

14+
## Releases and Changelog - [click here](https://github.com/ravendb/ravendb-nodejs-client/releases)
15+
2716
## Getting started
2817

2918
1. Require `DocumentStore` class from package
@@ -747,14 +736,67 @@ session.advanced.patch("users/1", "underAge", false);
747736

748737
await session.saveChanges();
749738
```
739+
### Subscriptions
740+
```javascript
741+
// create a subscription
742+
const subscriptionName = await store.subscriptions.create({
743+
query: "from users where age >= 30"
744+
});
745+
746+
// get subscription worker for your subscription
747+
const subscription = store.subscriptions.getSubscriptionWorker({ subscriptionName });
748+
749+
subscription.on("error", err => {
750+
// handle errors
751+
});
752+
753+
subscription.on("batch", (batch, callback) => {
754+
try {
755+
// do batch processing on batch.items
756+
// batch.items:
757+
// [ Item {
758+
// changeVector: 'A:2-r6nkF5nZtUKhcPEk6/LL+Q',
759+
// id: 'users/1-A',
760+
// rawResult:
761+
// { name: 'John',
762+
// age: 30,
763+
// registeredAt: '2017-11-11T00:00:00.0000000',
764+
// kids: [Array],
765+
// '@metadata': [Object],
766+
// id: 'users/1-A' },
767+
// rawMetadata:
768+
// { '@collection': 'Users',
769+
// '@nested-object-types': [Object],
770+
// 'Raven-Node-Type': 'User',
771+
// '@change-vector': 'A:2-r6nkF5nZtUKhcPEk6/LL+Q',
772+
// '@id': 'users/1-A',
773+
// '@last-modified': '2018-10-18T11:15:51.4882011Z' },
774+
// exceptionMessage: undefined } ]
775+
// ...
776+
777+
// call the callback, once you're done
778+
callback();
779+
} catch(err) {
780+
// if processing fails for a particular batch
781+
// pass the error to the callback
782+
callback(err);
783+
}
784+
});
785+
```
750786

751787
## Using object literals for entities
752788

753-
In order to comfortably use object literals as entities set function getting collection name based on the content of the object - `store.conventions.findCollectionNameForObjectLiteral()`. This needs to be done *before* an `initialize()` call on `DocumentStore` instance. If you fail to do so, your entites will land up in *@empty* collection having an *UUID* for an ID. E.g.
789+
In order to comfortably use object literals as entities set the function getting collection name based on the content of the object - `store.conventions.findCollectionNameForObjectLiteral()`.
790+
754791
```javascript
792+
const store = new DocumentStore(urls, database);
755793
store.conventions.findCollectionNameForObjectLiteral = entity => entity["collection"];
794+
// ...
795+
store.initialize();
756796
```
757797

798+
This needs to be done *before* an `initialize()` call on `DocumentStore` instance. If you fail to do so, your entites will land up in *@empty* collection having an *UUID* for an ID. E.g.
799+
758800
## Using classes for entities
759801

760802
1. Define your model as class. Attributes should be just public properties:

Diff for: perf/results/bulk-insert.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bulk-insert-2018-16-10-pipeline x10: 21635.223ms
1111
Buffer.concat() usage and redundant buffering logic removal.
1212

1313
```
14-
bulk-insert-2018-16-10-pipeline x10: 2490.231ms
15-
bulk-insert-2018-16-10-pipeline x50: 8280.333ms
16-
bulk-insert-2018-16-10-pipeline x100: 15802.916ms
14+
bulk-insert-2018-10-18-pipeline x10: 2490.231ms
15+
bulk-insert-2018-10-18-pipeline x50: 8280.333ms
16+
bulk-insert-2018-10-18-pipeline x100: 15802.916ms
1717
```

Diff for: test/Documents/ReadmeSamples.ts

+35
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,41 @@ describe("Readme query samples", function () {
428428
}
429429
});
430430

431+
it("can subscribe", async () => {
432+
433+
// create a subscription
434+
const subscriptionName = await store.subscriptions.create({
435+
query: "from users where age >= 30"
436+
});
437+
438+
// get subscription worker for your subscription
439+
const subscription = store.subscriptions.getSubscriptionWorker({ subscriptionName });
440+
441+
subscription.on("error", err => {
442+
// handle errors
443+
});
444+
445+
let done;
446+
const testDone = new Promise(resolve => done = resolve);
447+
subscription.on("batch", (batch, callback) => {
448+
try {
449+
// do batch processing
450+
print(batch.items);
451+
452+
// call the callback, once you're done
453+
callback();
454+
} catch (err) {
455+
// if processing fails for a particular batch
456+
// pass the error to the callback
457+
callback(err);
458+
}
459+
460+
done();
461+
});
462+
463+
await testDone;
464+
});
465+
431466
});
432467

433468
it("can use advanced.patch", async () => {

0 commit comments

Comments
 (0)