Skip to content

Commit

Permalink
Mysql Demo (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney authored Oct 24, 2024
1 parent 8ef02a0 commit f99fea9
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ This repository contains basic demonstrations in the `demos` folder.

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

- [Node.js (MySQL)](./demos/nodejs-mysql/README.md)

- This can be started from the repo root with `docker compose -f demos/nodejs-mysql/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
19 changes: 19 additions & 0 deletions demos/nodejs-mysql/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ==================== MySQL credentials ================================
PS_DATA_SOURCE_URI=mysql://repl_user:good_password@mysql:3306/powersync

# ==================== Demo config =========================================
DEMO_BACKEND_PORT=6060
DEMO_BACKEND_DATABASE_TYPE=mysql
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=3035
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
28 changes: 28 additions & 0 deletions demos/nodejs-mysql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# JavaScript PowerSync + MySQL Self Hosted Demo

This demo contains a NodeJS + MySQL 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-mysql/docker-compose.yaml up
```

The frontend can be accessed at `http://localhost:3035` in a browser.

## Configuration

See [MySQL Configuration](../../services/mysql/init-scripts/my.cnf) for MySQL server configuration.
The MySQL server is initialized with the [init](../../services/mysql/init-scripts/mysql.sql) script.
29 changes: 29 additions & 0 deletions demos/nodejs-mysql/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: mysql
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-mysql/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.
# yaml-language-server: $schema=https://unpkg.com/@powersync/service-sync-rules@0.0.0-dev-20241018075839/schema/sync_rules.json
bucket_definitions:
global:
data:
- select * from lists
- select * from todos
27 changes: 27 additions & 0 deletions demos/nodejs-mysql/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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

# MySQL Data source configuration
- path: ../../services/mysql/mysql.yaml

services:
# Extend PowerSync with Mongo and Postgres healthchecks
powersync:
extends:
file: ../../services/powersync.yaml
service: powersync
depends_on:
mysql:
condition: service_healthy
mongo-rs-init:
condition: service_completed_successfully
# MySQL support is available via this image version
image: journeyapps/powersync-service:0.0.0-dev-20241024055608
volumes:
- ./config:/config
8 changes: 8 additions & 0 deletions services/mysql/init-scripts/my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[mysqld]
gtid_mode = ON
enforce-gtid-consistency = ON
# Row format required for ZongJi
binlog_format = row
log_bin=mysql-bin
server-id=1
binlog-do-db=powersync
36 changes: 36 additions & 0 deletions services/mysql/init-scripts/mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Create a user with necessary privileges
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'good_password';

-- Grant replication client privilege
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl_user'@'%';

-- Grant access to the specific database
GRANT ALL PRIVILEGES ON powersync.* TO 'repl_user'@'%';

-- Apply changes
FLUSH PRIVILEGES;

CREATE TABLE lists (
id CHAR(36) NOT NULL DEFAULT (UUID()), -- String UUID (36 characters)
created_at VARCHAR(50) NULL,
name TEXT NOT NULL,
owner_id CHAR(36) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE todos (
id CHAR(36) NOT NULL DEFAULT (UUID()), -- String UUID (36 characters)
created_at VARCHAR(50) NULL,
completed_at VARCHAR(50) NULL,
description TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE,
created_by CHAR(36) NULL,
completed_by CHAR(36) NULL,
list_id CHAR(36) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (list_id) REFERENCES lists (id) ON DELETE CASCADE
);

INSERT INTO lists (id, name, owner_id)
VALUES
(UUID(), 'Do a demo', UUID());
23 changes: 23 additions & 0 deletions services/mysql/mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: powersync
MYSQL_USER: myuser
MYSQL_PASSWORD: mypassword
ports:
- "3306:3306"
volumes:
- ./init-scripts/my.cnf:/etc/mysql/my.cnf
- ./init-scripts/mysql.sql:/docker-entrypoint-initdb.d/init_user.sql
- mysql_data:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 30s
timeout: 10s
retries: 5
start_period: 30s

volumes:
mysql_data:

0 comments on commit f99fea9

Please sign in to comment.