Skip to content

Commit 2c1396d

Browse files
Custom checkpoints (#36)
1 parent f99fea9 commit 2c1396d

File tree

20 files changed

+1919
-1653
lines changed

20 files changed

+1919
-1653
lines changed

README.md

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

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

17+
- [Node.js (Postgres + Custom Write Checkpoints)](./demos/nodejs-custom-checkpoints/README.md)
18+
19+
- This can be started from the repo root with `docker compose -f demos/nodejs-custom-checkpoints/docker-compose.yaml up`
20+
1721
- [Node.js (MongoDB)](./demos/nodejs-mongodb/README.md)
1822

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

config/powersync.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ client_auth:
9898

9999
# JWKS audience
100100
audience: ["powersync-dev", "powersync"]
101+
102+
api:
103+
tokens:
104+
# These tokens are used for local admin API route authentication
105+
- use_a_better_token_in_production

config/sync_rules.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
# yaml-language-server: $schema=https://unpkg.com/@powersync/service-sync-rules@0.0.0-dev-20241022111143/schema/sync_rules.json
2+
#
13
# See Documentation for more information:
24
# https://docs.powersync.com/usage/sync-rules
5+
#
36
# Note that changes to this file are not watched.
47
# The service needs to be restarted for changes to take effect.
8+
59
bucket_definitions:
610
global:
711
data:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# ==================== Postgres credentials ================================
2+
PG_DATABASE_NAME=postgres
3+
PG_DATABASE_PORT=5432
4+
PG_DATABASE_USER=postgres
5+
PG_DATABASE_PASSWORD=mypassword
6+
PS_DATA_SOURCE_URI=postgres://${PG_DATABASE_USER}:${PG_DATABASE_PASSWORD}@pg-db:${PG_DATABASE_PORT}/${PG_DATABASE_NAME}
7+
8+
# ==================== Demo config =========================================
9+
DEMO_BACKEND_PORT=6060
10+
DEMO_BACKEND_DATABASE_TYPE=postgres
11+
DEMO_BACKEND_DATABASE_URI=${PS_DATA_SOURCE_URI}
12+
# The front-end demo application is accessible at this port on the host machine
13+
DEMO_CLIENT_PORT=3034
14+
PS_JWKS_URL=http://demo-backend:${DEMO_BACKEND_PORT}/api/auth/keys
15+
16+
# These can be generated by following the instructions in the `key-generator` folder
17+
# A temporary key will be used if these are not specified
18+
DEMO_JWKS_PUBLIC_KEY=
19+
DEMO_JWKS_PRIVATE_KEY=
20+
21+
# ==================== PowerSync variables ====================
22+
# The PowerSync API is accessible via this port
23+
PS_PORT=8080
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# JavaScript PowerSync + Custom Write Checkpoints
2+
3+
This is a demo for using custom Write Checkpoints with PowerSync. Custom Write Checkpoints eliminate flicker when uploads do not directly reflect in the backend source database. This feature is only available for customers on [Team and Enterprise](https://www.powersync.com/pricing) plans.
4+
5+
Docs are available [here](https://docs.powersync.com/usage/use-case-examples/custom-write-checkpoints).
6+
7+
## Running
8+
9+
The `.env` file contains default configuration for the services. Reference this to connect to any services locally.
10+
11+
Ensure you have authenticated with our Docker Image repository. Please reach out to support for an access token.
12+
13+
```bash
14+
docker login container-registry@journeyapps.com -u user
15+
```
16+
17+
This demo can be started by running the following in this demo directory
18+
19+
```bash
20+
docker compose up
21+
```
22+
23+
or in the root directory run
24+
25+
```bash
26+
docker compose -f demos/nodejs-custom-checkpoints/docker-compose.yaml up
27+
```
28+
29+
The frontend can be accessed at `http://localhost:3034` in a browser.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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: postgresql
10+
# The PowerSync server container can access the Postgres DB via the DB's service name.
11+
# In this case the hostname is pg-db
12+
13+
# The connection URI or individual parameters can be specified.
14+
# Individual params take precedence over URI params
15+
uri: !env PS_DATA_SOURCE_URI
16+
sslmode: disable # 'verify-full' (default) or 'verify-ca' or 'disable'
17+
18+
# Connection settings for sync bucket storage
19+
storage:
20+
type: mongodb
21+
uri: !env PS_MONGO_URI
22+
23+
# The port which the PowerSync API server will listen on
24+
port: !env PS_PORT
25+
26+
# Specify sync rules
27+
sync_rules:
28+
path: sync_rules.yaml
29+
30+
# Client (application end user) authentication settings
31+
client_auth:
32+
# JWKS URIs can be specified here
33+
jwks_uri: !env PS_JWKS_URL
34+
35+
audience: ["powersync-dev", "powersync"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# yaml-language-server: $schema=https://unpkg.com/@powersync/service-sync-rules@0.0.0-dev-20241022111143/schema/sync_rules.json
2+
#
3+
# See Documentation for more information:
4+
# https://docs.powersync.com/usage/sync-rules
5+
#
6+
# Note that changes to this file are not watched.
7+
# The service needs to be restarted for changes to take effect.
8+
9+
event_definitions:
10+
# Note this event is only supported for customers on [Team and Enterprise](https://www.powersync.com/pricing) plans.
11+
write_checkpoints:
12+
payloads:
13+
# This defines where the replicated custom Write Checkpoints should be extracted from
14+
- SELECT user_id, checkpoint, client_id FROM checkpoints
15+
16+
bucket_definitions:
17+
global:
18+
data:
19+
- select * from lists
20+
- select * from todos
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
services:
8+
# Extend PowerSync with Mongo and Postgres healthchecks
9+
powersync:
10+
extends:
11+
file: ../../services/powersync.yaml
12+
service: powersync
13+
depends_on:
14+
mongo-rs-init:
15+
condition: service_completed_successfully
16+
# Custom Write Checkpoints are only available for customers on [Team and Enterprise](https://www.powersync.com/pricing) plans.
17+
image: container-registry.journeyapps.com/powersync-service:next
18+
volumes:
19+
- ./config:/config
20+
21+
pg-db:
22+
extends:
23+
file: ../../services/postgres.yaml
24+
service: pg-db
25+
volumes:
26+
- ./init-scripts:/docker-entrypoint-initdb.d
27+
28+
demo-client:
29+
extends:
30+
file: ../nodejs/ps-nodejs-demo.yaml
31+
service: demo-client
32+
build:
33+
args:
34+
VITE_CHECKPOINT_MODE: custom
35+
36+
demo-backend:
37+
extends:
38+
file: ../nodejs/ps-nodejs-demo.yaml
39+
service: demo-backend
40+
41+
volumes:
42+
# Postgres data
43+
pg_data:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- TODO change this if changing the DB connection name
2+
\connect postgres;
3+
4+
-- Create tables
5+
create table public.lists (
6+
id uuid not null default gen_random_uuid (),
7+
created_at timestamp with time zone not null default now(),
8+
name text not null,
9+
owner_id uuid not null,
10+
constraint lists_pkey primary key (id)
11+
);
12+
13+
create table public.todos (
14+
id uuid not null default gen_random_uuid (),
15+
created_at timestamp with time zone not null default now(),
16+
completed_at timestamp with time zone null,
17+
description text not null,
18+
completed boolean not null default false,
19+
created_by uuid null,
20+
completed_by uuid null,
21+
list_id uuid not null,
22+
photo_id uuid null,
23+
constraint todos_pkey primary key (id)
24+
);
25+
26+
CREATE TABLE checkpoints (
27+
user_id VARCHAR(255),
28+
client_id VARCHAR(255),
29+
checkpoint INTEGER,
30+
PRIMARY KEY (user_id, client_id)
31+
);
32+
33+
-- Creates some initial data to be synced
34+
INSERT INTO lists (id, name, owner_id) VALUES ('75f89104-d95a-4f16-8309-5363f1bb377a', 'Getting Started', gen_random_uuid() );
35+
INSERT INTO todos(description, list_id, completed) VALUES ('Run services locally', '75f89104-d95a-4f16-8309-5363f1bb377a', true);
36+
INSERT INTO todos (description, list_id, completed) VALUES ('Create a todo here. Query the todos table via a Postgres connection. Your todo should be synced', '75f89104-d95a-4f16-8309-5363f1bb377a', false);
37+
38+
-- Create publication for PowerSync
39+
-- Note that the `checkpoints` table is required in the publication
40+
create publication powersync for table lists, todos, checkpoints;

demos/nodejs-mongodb/config/powersync.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# yaml-language-server: $schema=../schema/schema.json
1+
# yaml-language-server: $schema=../../../schema/schema.json
22
telemetry:
33
# Opt out of reporting anonymized usage metrics to PowerSync telemetry service
44
disable_telemetry_sharing: false
@@ -27,3 +27,8 @@ client_auth:
2727
jwks_uri: !env PS_JWKS_URL
2828

2929
audience: ["powersync-dev", "powersync"]
30+
31+
api:
32+
tokens:
33+
# These tokens are used for local admin API route authentication
34+
- use_a_better_token_in_production

0 commit comments

Comments
 (0)