Skip to content

Commit 85e133c

Browse files
committed
Add script to add delta handler
This script at least ensures it looks the same in various services. A more streamlined solution may be preferred in the future.
1 parent fb64ed1 commit 85e133c

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,18 @@ docker-compose up -d your-microservice-name
148148
You can install additional dependencies by including a `package.json` file next to your `app.js`. It works as you would expect: just define the packages in the `dependencies` section of the `package.json`. They will be installed automatically at build time and in development mode. There is no need to restart the container.
149149

150150
### Handle delta's from the delta-service
151-
If you are building a reactive service that should execute certain logic based on changes in the database, you want to hook it up to the [delta-notifier](https://github.com/mu-semtech/delta-notifier/). Some extra steps need to be taken to properly handle delta's, specifically the route handling delta's will need to use a specific bodyParser.
151+
If you are building a reactive service that should execute certain logic based on changes in the database, you want to hook it up to the [delta-notifier](https://github.com/mu-semtech/delta-notifier/). Some extra steps need to be taken to properly handle delta's, specifically the route handling delta's will need to use a specific bodyParser.
152152

153153
The default bodyParser provided by the template will only accept `application/vnd.api+json` and the delta-notifier is sending `application/json` content. Aside from that the body of a delta message may be very large, often several megabytes. By specifying the bodyParser on the route accepting delta messages you can easily modify it when required.
154154

155-
An example
155+
The mu script `add-delta-handler` can be ran to update your app.js file:
156+
157+
```bash
158+
mu script add-delta-handler
159+
```
160+
161+
The resulting changes will look similar to:
162+
156163
```javascript
157164
// app.js
158165
import bodyParser from 'body-parser';

scripts/add-delta-handler/run.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
if [ -f /app/app.js ]
3+
then
4+
APP_COPY=$(mktemp)
5+
cp /app/app.js "$APP_COPY"
6+
awk '!/^import/ && !x {print "import bodyParser from \"body-parser\";"; x=1} 1' "$APP_COPY" >/app/app.js
7+
rm "$APP_COPY"
8+
9+
cat <<EOF>> /app/app.js
10+
11+
app.post('/delta', bodyParser.json({ limit: '50mb' }), (req, _res) => {
12+
for( const {inserts, deletes: _deletes} of req.body ) {
13+
for( const triple of inserts ) {
14+
console.log(triple.subject.value, triple.predicate.value, triple.object.value);
15+
}
16+
}
17+
});
18+
EOF
19+
else
20+
echo "Can only insert into app.js"
21+
fi

scripts/config.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828
"mounts": {
2929
"service": "/app"
3030
}
31+
},
32+
{
33+
"documentation": {
34+
"command": "add-delta-handler",
35+
"description": "Sets up the delta handler for the scripts",
36+
"arguments": []
37+
},
38+
"environment": {
39+
"image": "ubuntu:25.10",
40+
"interactive": false,
41+
"script": "add-delta-handler/run.sh"
42+
},
43+
"mounts": {
44+
"service": "/app"
45+
}
3146
}
3247
]
3348
}

0 commit comments

Comments
 (0)