Skip to content

Commit 873ad2b

Browse files
committed
add subscriptions example
1 parent b2e7fd4 commit 873ad2b

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
# RavenDB client for Node.js
22

3+
[![NPM](https://nodei.co/npm/ravendb.png?compact=true)](https://nodei.co/npm/ravendb/)
4+
35
[![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)
46

57
## Changelog
68

9+
### 4.0.4 - 2018-10-18
10+
- added support for [Subscriptions](#subscriptions)
11+
- added support for storing timezone info and dates in UTC
12+
- enhanced load and bulk insert performance
13+
- other bug fixes
14+
715
### 4.0.3 - 2018-10-01
816
Added support for the following features:
917
- [Streaming](#streaming)
@@ -747,14 +755,67 @@ session.advanced.patch("users/1", "underAge", false);
747755

748756
await session.saveChanges();
749757
```
758+
### Subscriptions
759+
```javascript
760+
// create a subscription
761+
const subscriptionName = await store.subscriptions.create({
762+
query: "from users where age >= 30"
763+
});
764+
765+
// get subscription worker for your subscription
766+
const subscription = store.subscriptions.getSubscriptionWorker({ subscriptionName });
767+
768+
subscription.on("error", err => {
769+
// handle errors
770+
});
771+
772+
subscription.on("batch", (batch, callback) => {
773+
try {
774+
// do batch processing on batch.items
775+
// batch.items:
776+
// [ Item {
777+
// changeVector: 'A:2-r6nkF5nZtUKhcPEk6/LL+Q',
778+
// id: 'users/1-A',
779+
// rawResult:
780+
// { name: 'John',
781+
// age: 30,
782+
// registeredAt: '2017-11-11T00:00:00.0000000',
783+
// kids: [Array],
784+
// '@metadata': [Object],
785+
// id: 'users/1-A' },
786+
// rawMetadata:
787+
// { '@collection': 'Users',
788+
// '@nested-object-types': [Object],
789+
// 'Raven-Node-Type': 'User',
790+
// '@change-vector': 'A:2-r6nkF5nZtUKhcPEk6/LL+Q',
791+
// '@id': 'users/1-A',
792+
// '@last-modified': '2018-10-18T11:15:51.4882011Z' },
793+
// exceptionMessage: undefined } ]
794+
// ...
795+
796+
// call the callback, once you're done
797+
callback();
798+
} catch(err) {
799+
// if processing fails for a particular batch
800+
// pass the error to the callback
801+
callback(err);
802+
}
803+
});
804+
```
750805

751806
## Using object literals for entities
752807

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.
808+
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()`.
809+
754810
```javascript
811+
const store = new DocumentStore(urls, database);
755812
store.conventions.findCollectionNameForObjectLiteral = entity => entity["collection"];
813+
// ...
814+
store.initialize();
756815
```
757816

817+
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.
818+
758819
## Using classes for entities
759820

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

perf/results/bulk-insert.md

Lines changed: 3 additions & 3 deletions
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
```

test/Documents/ReadmeSamples.ts

Lines changed: 35 additions & 0 deletions
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)