Skip to content

Commit f99fea9

Browse files
Mysql Demo (#37)
1 parent 8ef02a0 commit f99fea9

File tree

9 files changed

+184
-0
lines changed

9 files changed

+184
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ This repository contains basic demonstrations in the `demos` folder.
1818

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

21+
- [Node.js (MySQL)](./demos/nodejs-mysql/README.md)
22+
23+
- This can be started from the repo root with `docker compose -f demos/nodejs-mysql/docker-compose.yaml up`
24+
2125
- [Django](./demos/django/README.md)
2226

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

demos/nodejs-mysql/.env

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ==================== MySQL credentials ================================
2+
PS_DATA_SOURCE_URI=mysql://repl_user:good_password@mysql:3306/powersync
3+
4+
# ==================== Demo config =========================================
5+
DEMO_BACKEND_PORT=6060
6+
DEMO_BACKEND_DATABASE_TYPE=mysql
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=3035
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-mysql/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# JavaScript PowerSync + MySQL Self Hosted Demo
2+
3+
This demo contains a NodeJS + MySQL 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-mysql/docker-compose.yaml up
21+
```
22+
23+
The frontend can be accessed at `http://localhost:3035` in a browser.
24+
25+
## Configuration
26+
27+
See [MySQL Configuration](../../services/mysql/init-scripts/my.cnf) for MySQL server configuration.
28+
The MySQL server is initialized with the [init](../../services/mysql/init-scripts/mysql.sql) script.
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: mysql
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+
# yaml-language-server: $schema=https://unpkg.com/@powersync/service-sync-rules@0.0.0-dev-20241018075839/schema/sync_rules.json
6+
bucket_definitions:
7+
global:
8+
data:
9+
- select * from lists
10+
- select * from todos
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
# MySQL Data source configuration
11+
- path: ../../services/mysql/mysql.yaml
12+
13+
services:
14+
# Extend PowerSync with Mongo and Postgres healthchecks
15+
powersync:
16+
extends:
17+
file: ../../services/powersync.yaml
18+
service: powersync
19+
depends_on:
20+
mysql:
21+
condition: service_healthy
22+
mongo-rs-init:
23+
condition: service_completed_successfully
24+
# MySQL support is available via this image version
25+
image: journeyapps/powersync-service:0.0.0-dev-20241024055608
26+
volumes:
27+
- ./config:/config

services/mysql/init-scripts/my.cnf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[mysqld]
2+
gtid_mode = ON
3+
enforce-gtid-consistency = ON
4+
# Row format required for ZongJi
5+
binlog_format = row
6+
log_bin=mysql-bin
7+
server-id=1
8+
binlog-do-db=powersync
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Create a user with necessary privileges
2+
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'good_password';
3+
4+
-- Grant replication client privilege
5+
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl_user'@'%';
6+
7+
-- Grant access to the specific database
8+
GRANT ALL PRIVILEGES ON powersync.* TO 'repl_user'@'%';
9+
10+
-- Apply changes
11+
FLUSH PRIVILEGES;
12+
13+
CREATE TABLE lists (
14+
id CHAR(36) NOT NULL DEFAULT (UUID()), -- String UUID (36 characters)
15+
created_at VARCHAR(50) NULL,
16+
name TEXT NOT NULL,
17+
owner_id CHAR(36) NOT NULL,
18+
PRIMARY KEY (id)
19+
);
20+
21+
CREATE TABLE todos (
22+
id CHAR(36) NOT NULL DEFAULT (UUID()), -- String UUID (36 characters)
23+
created_at VARCHAR(50) NULL,
24+
completed_at VARCHAR(50) NULL,
25+
description TEXT NOT NULL,
26+
completed BOOLEAN NOT NULL DEFAULT FALSE,
27+
created_by CHAR(36) NULL,
28+
completed_by CHAR(36) NULL,
29+
list_id CHAR(36) NOT NULL,
30+
PRIMARY KEY (id),
31+
FOREIGN KEY (list_id) REFERENCES lists (id) ON DELETE CASCADE
32+
);
33+
34+
INSERT INTO lists (id, name, owner_id)
35+
VALUES
36+
(UUID(), 'Do a demo', UUID());

services/mysql/mysql.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
services:
2+
mysql:
3+
image: mysql:8.0
4+
environment:
5+
MYSQL_ROOT_PASSWORD: root_password
6+
MYSQL_DATABASE: powersync
7+
MYSQL_USER: myuser
8+
MYSQL_PASSWORD: mypassword
9+
ports:
10+
- "3306:3306"
11+
volumes:
12+
- ./init-scripts/my.cnf:/etc/mysql/my.cnf
13+
- ./init-scripts/mysql.sql:/docker-entrypoint-initdb.d/init_user.sql
14+
- mysql_data:/var/lib/mysql
15+
healthcheck:
16+
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
17+
interval: 30s
18+
timeout: 10s
19+
retries: 5
20+
start_period: 30s
21+
22+
volumes:
23+
mysql_data:

0 commit comments

Comments
 (0)