This repo contains a starter Node.js server application which has HTTP endpoints to authorize a PowerSync enabled application to sync data between a SQLite client and a Postgres database. This repo is intended for use as a starter template on Railway.
For an overview of PowerSync, see here.
Once you have deployed this template on Railway, you'll need to:
- Implement endpoints to accept client writes
- Implement an endpoint that generates a JWT for clients to use
- Implement a JWKS endpoint, used by PowerSync to authenticate JWTs
The relevant endpoints are:
-
PUT
/api/data
- PowerSync uses this endpoint to sync upsert events that occurred on the client application.
-
PATCH
/api/data
- PowerSync uses this endpoint to sync update events that occurred on the client application.
-
DELETE
/api/data
- PowerSync uses this endpoint to sync delete events that occurred on the client application.
See our docs for additional details.
- GET
/api/auth/token
- PowerSync uses this endpoint to retrieve an JWT token that is generated by the server and is used to authenticate requests.
- GET
api/auth/keys
- PowerSync uses this endpoint to retrieve the JWKS to validate the token provided in the response above.
- node-postgres is used to interact with the Postgres database when a PowerSync enabled client performs requests to the
/api/data
endpoint. - jose is used to sign the JWT which PowerSync uses for authorization.
-
Clone the repository and
nvm use && yarn install
-
Generate a new public/private key pair by running the following command:
yarn keys
This will output two new keys in the terminal window. Set these as environment variables on the Railway app:
POWERSYNC_JWT_PUBLICKEY
POWERSYNC_JWT_PRIVATEKEY
- Create a new
.env
file in the root project directory and add the variables as defined in the.env
file:
cp .env.template .env
- Run the following to start the application
yarn start
This will start the app on http://127.0.0.1:PORT
, where PORT is what you specify in your .env
file.
-
Test if the app is working by opening
http://127.0.0.1:PORT/api/auth/keys/
in the browser -
You should get a JSON object as the response to that request