@fraym/streams 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +136 -0
  2. package/package.json +42 -0
package/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # streams-go
2
+
3
+ Client implementation in javascript for the event streaming service [streams](https://github.com/fraym-work/streams).
4
+
5
+ ## Installation
6
+
7
+ ```shell
8
+ npm i @fraym/streams
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Create the client
14
+
15
+ ```typescript
16
+ const client = await newClient({
17
+ serverAddress: "127.0.0.1:9000",
18
+ groupId: "your-services-group-identifier",
19
+ });
20
+ ```
21
+
22
+ ### Publish events
23
+
24
+ Events are published in a transactional manner:
25
+ * you can publish a set of events in a transaction
26
+ * if one event in your set cannot be published no event will be published
27
+ * as soon as all events are published they appear in their streams
28
+
29
+ You can only publish events of one topic per transaction.
30
+ Most of the fields of an event are optional. You do not have to use or specify them, but you can use them if you want to.
31
+
32
+ ```typescript
33
+ await client.publish("topic", [
34
+ {
35
+ id: uuid(),
36
+ tenantId: "tenantId",
37
+ payload: {
38
+ key: "value",
39
+ },
40
+ // set `broadcast` to true if you want all subscribers of a group to process the event.
41
+ // set to false (or remove) if you want this event to be handled only once by a group of subscribers.
42
+ broadcast: false,
43
+ type: "event-type",
44
+ stream: "stream-name",
45
+ correlationId: uuid(),
46
+ causationId: uuid(),
47
+ reason: "the reason why this event was triggered",
48
+ },
49
+ ]);
50
+ ```
51
+
52
+ ### Get events of a stream
53
+
54
+ If you want to get all events that belong to a given stream you can do this by calling `getStream``:
55
+
56
+ ```typescript
57
+ const streamEvents = await client.getStream("tenantId", "stream");
58
+ ```
59
+
60
+ ### Register event handlers for event subscriptions
61
+
62
+ ```typescript
63
+ // this handler is called for events of all types
64
+ client.useEventHandlerForAllEventTypes(async (event: SubscriptionEvent) => {
65
+ // @todo: handle the event in this callback
66
+ });
67
+
68
+ // this handler is only called for events of the given type "event-type"
69
+ client.useEventHandler("event-type", async (event: SubscriptionEvent) => {
70
+ // @todo: handle the event in this callback
71
+ });
72
+ ```
73
+
74
+ ### Subscribe to events
75
+
76
+ You can subscribe to events of all topics by not providing any parameters to the subscribe function:
77
+
78
+ ```typescript
79
+ await client.subscribe();
80
+ ```
81
+
82
+ You can subscribe to events of a given set of topics by specifying the `includedTopics` list:
83
+
84
+ ```typescript
85
+ await client.subscribe(["topic-1", "topic-2"]);
86
+ ```
87
+
88
+ You can subscribe to all events except of a given set of topics by specifying the `excludedTopics` list:
89
+
90
+ ```typescript
91
+ await client.subscribe([], ["topic-1", "topic-2"]);
92
+ ```
93
+
94
+ One client instance is only able to subscribe once. If you try to subscribe twice you will get an error.
95
+
96
+ ### Get all events for a given topic filter
97
+
98
+ The `getAllEvents` function uses the same filter logic as described for the [subscribe](#subscribe-to-events) function
99
+
100
+ ```typescript
101
+ await client.getAllEvents(async (event: SubscriptionEvent) => {
102
+ // @todo: handle the event in this callback
103
+ });
104
+ ```
105
+
106
+ ### Invalidate gdpr data
107
+
108
+ You will not need to use this if you use our GDPR service.
109
+
110
+ ```typescript
111
+ await client.invalidateGdprData("tenantId", "topic-1", "grprId")
112
+ ```
113
+
114
+ ### Create a snapshot of a topic
115
+
116
+ A snapshot moves events from the "fast access" database to a "long time storage".
117
+ The long time storage will have slower access time.
118
+ Use this if you have topics that have a lot of old events which are not queried regularly.
119
+
120
+ Creating a snapshot will only affect performance. You will not l lose any of your events.
121
+
122
+ Snapshots can be used to clean up your "fast access" event database and therefore increase performance on new events.
123
+
124
+ ```typescript
125
+ await client.createSnapshot("topic-1", new Date());
126
+ ```
127
+
128
+ The second parameter is a date. All events that are older than that date will be moved to the snapshot.
129
+
130
+ ### Gracefully close the client
131
+
132
+ You won't lose any data if you don't. Use it for your peace of mind.
133
+
134
+ ```typescript
135
+ client.close();
136
+ ```
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@fraym/streams",
3
+ "version": "0.1.1",
4
+ "license": "UNLICENSED",
5
+ "homepage": "https://github.com/fraym-work/streams-nodejs",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/fraym-work/streams-nodejs.git"
9
+ },
10
+ "description": "nodejs client implementation for our event streaming service",
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 0",
13
+ "format": "prettier --write \"**/*.{ts,tsx,json}\"",
14
+ "lint": "prettier --check \"**/*.{ts,tsx,json}\"",
15
+ "build": "npm run clean && tsc",
16
+ "clean": "rm -rf dist",
17
+ "prepublishOnly": "npm test && npm run lint && npm run build",
18
+ "preversion": "npm run lint",
19
+ "protobuf": "./protobuf.sh"
20
+ },
21
+ "files": [
22
+ "dist/**/*"
23
+ ],
24
+ "main": "dist/index.js",
25
+ "types": "dist/index.d.ts",
26
+ "devDependencies": {
27
+ "@becklyn/prettier": "^1.0.2",
28
+ "@types/google-protobuf": "^3.15.6",
29
+ "@types/uuid": "^8.3.4",
30
+ "grpc_tools_node_protoc_ts": "^5.3.2",
31
+ "grpc-tools": "^1.11.2",
32
+ "prettier": "^2.7.1",
33
+ "typescript": "^4.8.4"
34
+ },
35
+ "prettier": "@becklyn/prettier",
36
+ "dependencies": {
37
+ "@grpc/grpc-js": "^1.7.1",
38
+ "grpc": "^1.24.11",
39
+ "np": "^7.6.2",
40
+ "uuid": "^9.0.0"
41
+ }
42
+ }