Skip to content

Commit 8986ce6

Browse files
authored
Merge pull request #29 from powersync-ja/mongo-demo
[Feature] MongoDB Demo
2 parents 7bbb564 + 3d80ceb commit 8986ce6

File tree

17 files changed

+127
-54
lines changed

17 files changed

+127
-54
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# PowerSync Self Hosted Example
22

3+
## v0.5.0
4+
5+
- Added alpha demo for MongoDB replication
6+
37
## v0.4.1
48

59
- Cleaned `powersync.yaml` config file placeholder values

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ Learn more about self-hosting PowerSync [here](https://docs.powersync.com/self-h
1010

1111
This repository contains basic demonstrations in the `demos` folder.
1212

13-
- [NodeJS](./demos/nodejs/README.md)
13+
- [NodeJS (Postgres)](./demos/nodejs/README.md)
1414

1515
- This can be started from the repo root with `docker compose -f demos/nodejs/docker-compose.yaml up`
1616

17+
- [NodeJS (MongoDB)](./demos/nodejs-mongodb/README.md)
18+
19+
- This can be started from the repo root with `docker compose -f demos/nodejs-mongodb/docker-compose.yaml up`
20+
1721
- [Django](./demos/django/README.md)
1822

1923
- This can be started from the repo root with `docker compose -f demos/django/docker-compose.yaml up`

config/powersync.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ replication:
3838

3939
# The connection URI or individual parameters can be specified.
4040
# Individual params take precedence over URI params
41-
uri: !env PS_PG_URI
41+
uri: !env PS_DATA_SOURCE_URI
4242

4343
# Or use individual params
4444

demos/django/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PG_DATABASE_NAME=django_demo
33
PG_DATABASE_PORT=5432
44
PG_DATABASE_USER=postgres
55
PG_DATABASE_PASSWORD=mypassword
6-
6+
PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
77
# ==================== Demo config =========================================
88
DEMO_BACKEND_PORT=6061
99
# The front-end demo application is accessible at this port on the host machine

demos/nodejs-mongodb/.env

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ==================== MongoDB credentials ================================
2+
PS_DATA_SOURCE_URI=mongodb://mongo:27017/powersync_demo_backend
3+
4+
# ==================== Demo config =========================================
5+
DEMO_BACKEND_PORT=6060
6+
DEMO_BACKEND_DATABASE_TYPE=mongodb
7+
DEMO_BACKEND_DATABASE_URI=${PS_DATA_SOURCE_URI}
8+
# The front-end demo application is accessible at this port on the host machine
9+
DEMO_CLIENT_PORT=3033
10+
PS_JWKS_URL=http://demo-backend:${DEMO_BACKEND_PORT}/api/auth/keys
11+
12+
# These can be generated by following the instructions in the `key-generator` folder
13+
# A temporary key will be used if these are not specified
14+
DEMO_JWKS_PUBLIC_KEY=
15+
DEMO_JWKS_PRIVATE_KEY=
16+
17+
# ==================== PowerSync variables ====================
18+
# The PowerSync API is accessible via this port
19+
PS_PORT=8080

demos/nodejs-mongodb/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# JavaScript PowerSync + MongoDB Self Hosted Demo
2+
3+
This demo contains a NodeJS + MongoDB backend and React frontend which are linked to a self hosted PowerSync instance.
4+
5+
Backend code can be found [here](https://github.com/powersync-ja/powersync-nodejs-backend-todolist-demo)
6+
7+
## Running
8+
9+
The `.env` file contains default configuration for the services. Reference this to connect to any services locally.
10+
11+
This demo can be started by running the following in this demo directory
12+
13+
```bash
14+
docker compose up
15+
```
16+
17+
or in the root directory run
18+
19+
```bash
20+
docker compose -f demos/nodejs-mongodb/docker-compose.yaml up
21+
```
22+
23+
The frontend can be accessed at `http://localhost:3033` in a browser.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# yaml-language-server: $schema=../schema/schema.json
2+
telemetry:
3+
# Opt out of reporting anonymized usage metrics to PowerSync telemetry service
4+
disable_telemetry_sharing: false
5+
6+
# Settings for source database replication
7+
replication:
8+
connections:
9+
- type: mongodb
10+
uri: !env PS_DATA_SOURCE_URI
11+
12+
# Connection settings for sync bucket storage
13+
storage:
14+
type: mongodb
15+
uri: !env PS_MONGO_URI
16+
17+
# The port which the PowerSync API server will listen on
18+
port: !env PS_PORT
19+
20+
# Specify sync rules
21+
sync_rules:
22+
path: sync_rules.yaml
23+
24+
# Client (application end user) authentication settings
25+
client_auth:
26+
# JWKS URIs can be specified here
27+
jwks_uri: !env PS_JWKS_URL
28+
29+
audience: ["powersync-dev", "powersync"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# See Documentation for more information:
2+
# https://docs.powersync.com/usage/sync-rules
3+
# Note that changes to this file are not watched.
4+
# The service needs to be restarted for changes to take effect.
5+
bucket_definitions:
6+
global:
7+
data:
8+
# mongodb uses _id as the primary key. * excludes _ fields.
9+
- select _id as id, * from lists
10+
- select _id as id, * from todos
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Include syntax requires Docker compose > 2.20.3
2+
# https://docs.docker.com/compose/release-notes/#2203
3+
include:
4+
# Creates a MongoDB replica set. This is used for internal and data storage
5+
- path: ../../services/mongo.yaml
6+
7+
# Demo NodeJS backend server and front-end web client
8+
- path: ../nodejs/ps-nodejs-demo.yaml
9+
10+
services:
11+
# Extend PowerSync with Mongo and Postgres healthchecks
12+
powersync:
13+
extends:
14+
file: ../../services/powersync.yaml
15+
service: powersync
16+
depends_on:
17+
mongo-rs-init:
18+
condition: service_completed_successfully
19+
# MongoDB support is available via this image version
20+
image: journeyapps/powersync-service:0.0.0-dev-20240919151547
21+
volumes:
22+
- ./config:/config

demos/nodejs/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ PG_DATABASE_NAME=postgres
33
PG_DATABASE_PORT=5432
44
PG_DATABASE_USER=postgres
55
PG_DATABASE_PASSWORD=mypassword
6+
PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
67

78
# ==================== Demo config =========================================
89
DEMO_BACKEND_PORT=6060
10+
DEMO_BACKEND_DATABASE_TYPE=postgres
11+
DEMO_BACKEND_DATABASE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
912
# The front-end demo application is accessible at this port on the host machine
1013
DEMO_CLIENT_PORT=3030
1114
PS_JWKS_URL=http://demo-backend:${DEMO_BACKEND_PORT}/api/auth/keys

0 commit comments

Comments
 (0)