Skip to content

Commit

Permalink
Merge pull request #29 from powersync-ja/mongo-demo
Browse files Browse the repository at this point in the history
[Feature] MongoDB Demo
  • Loading branch information
kobiebotha authored Sep 20, 2024
2 parents 7bbb564 + 3d80ceb commit 8986ce6
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# PowerSync Self Hosted Example

## v0.5.0

- Added alpha demo for MongoDB replication

## v0.4.1

- Cleaned `powersync.yaml` config file placeholder values
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ Learn more about self-hosting PowerSync [here](https://docs.powersync.com/self-h

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

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

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

- [NodeJS (MongoDB)](./demos/nodejs-mongodb/README.md)

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

- [Django](./demos/django/README.md)

- This can be started from the repo root with `docker compose -f demos/django/docker-compose.yaml up`
Expand Down
2 changes: 1 addition & 1 deletion config/powersync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ replication:

# The connection URI or individual parameters can be specified.
# Individual params take precedence over URI params
uri: !env PS_PG_URI
uri: !env PS_DATA_SOURCE_URI

# Or use individual params

Expand Down
2 changes: 1 addition & 1 deletion demos/django/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PG_DATABASE_NAME=django_demo
PG_DATABASE_PORT=5432
PG_DATABASE_USER=postgres
PG_DATABASE_PASSWORD=mypassword

PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
# ==================== Demo config =========================================
DEMO_BACKEND_PORT=6061
# The front-end demo application is accessible at this port on the host machine
Expand Down
19 changes: 19 additions & 0 deletions demos/nodejs-mongodb/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ==================== MongoDB credentials ================================
PS_DATA_SOURCE_URI=mongodb://mongo:27017/powersync_demo_backend

# ==================== Demo config =========================================
DEMO_BACKEND_PORT=6060
DEMO_BACKEND_DATABASE_TYPE=mongodb
DEMO_BACKEND_DATABASE_URI=${PS_DATA_SOURCE_URI}
# The front-end demo application is accessible at this port on the host machine
DEMO_CLIENT_PORT=3033
PS_JWKS_URL=http://demo-backend:${DEMO_BACKEND_PORT}/api/auth/keys

# These can be generated by following the instructions in the `key-generator` folder
# A temporary key will be used if these are not specified
DEMO_JWKS_PUBLIC_KEY=
DEMO_JWKS_PRIVATE_KEY=

# ==================== PowerSync variables ====================
# The PowerSync API is accessible via this port
PS_PORT=8080
23 changes: 23 additions & 0 deletions demos/nodejs-mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# JavaScript PowerSync + MongoDB Self Hosted Demo

This demo contains a NodeJS + MongoDB backend and React frontend which are linked to a self hosted PowerSync instance.

Backend code can be found [here](https://github.com/powersync-ja/powersync-nodejs-backend-todolist-demo)

## Running

The `.env` file contains default configuration for the services. Reference this to connect to any services locally.

This demo can be started by running the following in this demo directory

```bash
docker compose up
```

or in the root directory run

```bash
docker compose -f demos/nodejs-mongodb/docker-compose.yaml up
```

The frontend can be accessed at `http://localhost:3033` in a browser.
29 changes: 29 additions & 0 deletions demos/nodejs-mongodb/config/powersync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# yaml-language-server: $schema=../schema/schema.json
telemetry:
# Opt out of reporting anonymized usage metrics to PowerSync telemetry service
disable_telemetry_sharing: false

# Settings for source database replication
replication:
connections:
- type: mongodb
uri: !env PS_DATA_SOURCE_URI

# Connection settings for sync bucket storage
storage:
type: mongodb
uri: !env PS_MONGO_URI

# The port which the PowerSync API server will listen on
port: !env PS_PORT

# Specify sync rules
sync_rules:
path: sync_rules.yaml

# Client (application end user) authentication settings
client_auth:
# JWKS URIs can be specified here
jwks_uri: !env PS_JWKS_URL

audience: ["powersync-dev", "powersync"]
10 changes: 10 additions & 0 deletions demos/nodejs-mongodb/config/sync_rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# See Documentation for more information:
# https://docs.powersync.com/usage/sync-rules
# Note that changes to this file are not watched.
# The service needs to be restarted for changes to take effect.
bucket_definitions:
global:
data:
# mongodb uses _id as the primary key. * excludes _ fields.
- select _id as id, * from lists
- select _id as id, * from todos
22 changes: 22 additions & 0 deletions demos/nodejs-mongodb/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Include syntax requires Docker compose > 2.20.3
# https://docs.docker.com/compose/release-notes/#2203
include:
# Creates a MongoDB replica set. This is used for internal and data storage
- path: ../../services/mongo.yaml

# Demo NodeJS backend server and front-end web client
- path: ../nodejs/ps-nodejs-demo.yaml

services:
# Extend PowerSync with Mongo and Postgres healthchecks
powersync:
extends:
file: ../../services/powersync.yaml
service: powersync
depends_on:
mongo-rs-init:
condition: service_completed_successfully
# MongoDB support is available via this image version
image: journeyapps/powersync-service:0.0.0-dev-20240919151547
volumes:
- ./config:/config
3 changes: 3 additions & 0 deletions demos/nodejs/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ PG_DATABASE_NAME=postgres
PG_DATABASE_PORT=5432
PG_DATABASE_USER=postgres
PG_DATABASE_PASSWORD=mypassword
PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}

# ==================== Demo config =========================================
DEMO_BACKEND_PORT=6060
DEMO_BACKEND_DATABASE_TYPE=postgres
DEMO_BACKEND_DATABASE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
# The front-end demo application is accessible at this port on the host machine
DEMO_CLIENT_PORT=3030
PS_JWKS_URL=http://demo-backend:${DEMO_BACKEND_PORT}/api/auth/keys
Expand Down
4 changes: 2 additions & 2 deletions demos/nodejs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# JavaScript Self Hosted Demo
# JavaScript PowerSync + Postgres Self Hosted Demo

This demo contains a NodeJS backend and React frontend which are linked to a self hosted PowerSync instance.
This demo contains a NodeJS + Postgres backend and React frontend which are linked to a self hosted PowerSync instance.

Backend code can be found [here](https://github.com/powersync-ja/powersync-nodejs-backend-todolist-demo)

Expand Down
31 changes: 0 additions & 31 deletions demos/nodejs/demo-backend/Dockerfile

This file was deleted.

5 changes: 0 additions & 5 deletions demos/nodejs/demo-backend/README.md

This file was deleted.

15 changes: 4 additions & 11 deletions demos/nodejs/ps-nodejs-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@ services:
# A backend which provides basic authentication and CRUD access to the Postgress DB from the client
demo-backend:
build:
context: ./demo-backend
dockerfile: Dockerfile
# TODO update branch after merging
context: https://github.com/powersync-ja/powersync-nodejs-backend-todolist-demo.git#configurable-mongo-postgres
environment:
# From the Postgress service name in linked Docker Compose file
DATABASE_HOST: pg-db

# Shared environment variables for Postgres connection
DATABASE_PORT: ${PG_DATABASE_PORT}
DATABASE_NAME: ${PG_DATABASE_NAME}
DATABASE_USER: ${PG_DATABASE_USER}
DATABASE_PASSWORD: ${PG_DATABASE_PASSWORD}

DATABASE_TYPE: ${DEMO_BACKEND_DATABASE_TYPE}
DATABASE_URI: ${DEMO_BACKEND_DATABASE_URI}
# From the PowerSync service name
# This is just used to populate the JWT audience
POWERSYNC_URL: powersync-dev
Expand Down
2 changes: 2 additions & 0 deletions demos/supabase/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ PG_DATABASE_NAME=postgres
PG_DATABASE_PORT=5433 #This port has been configured in supabase/config.toml
PG_DATABASE_USER=postgres
PG_DATABASE_PASSWORD=postgres
PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@${PG_DATABASE_HOSTNAME}:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}


# ==================== PowerSync variables ====================
# The PowerSync API is accessible via this port
Expand Down
2 changes: 1 addition & 1 deletion demos/supabase/config/powersync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ telemetry:
replication:
connections:
- type: postgresql
uri: !env PS_PG_URI
uri: !env PS_DATA_SOURCE_URI

# SSL settings
sslmode: disable # 'verify-full' (default) or 'verify-ca' or 'disable'
Expand Down
2 changes: 1 addition & 1 deletion services/powersync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ services:

# The following will be inserted into the powersync.yaml file via templating
# Templates are used for filesystem and base64 configuration files
PS_PG_URI: postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
PS_DATA_SOURCE_URI: ${PS_DATA_SOURCE_URI}
PS_MONGO_URI: mongodb://mongo:27017/powersync_demo

# Note that powersync.yaml->client_auth->allow_local_jwks must be true for local URLs to work
Expand Down

0 comments on commit 8986ce6

Please sign in to comment.