This repository has been created to help you get started with Pulse. You will be able to use this project with any Pulse-ready PostgreSQL database. This project comes with a basic prisma.schema
file as well as a Pulse subscription found in the index.ts
file.
Pulse is currently in Early Access. Please sign up here and join our community on Slack or Discord to share your feedback.
Table of contents:
- Local development
- Create a Pulse Starter Project on railway.app
- More example Pulse projects
- More information about Pulse
Clone the repository, navigate into it and install dependencies:
git clone https://github.com/prisma/pulse-starter.git
cd pulse-starter
npm install
Rename the existing .env.example
to .env
:
mv .env.example .env
The .env
file contains the following environment variables:
DATABASE_URL=""
PULSE_API_KEY=""
DATABASE_URL
: The connection string to your database.PULSE_API_KEY
: Reference the Project API Keys section in our documentation to learn how get an API key for your Pulse project.
The prisma/schema.prisma
contains three models based on our hello-prisma example project:
npx prisma migrate dev --name init
Run the script that contains the code to subscribe to database events:
npx ts-node index.ts
This will run a basic subscription on the User
table. The code can be found in the index.ts
file. To learn more about the Pulse API and how to use it, check out our documentation.
Pulse user table subscription
async function main() {
const subscription = await prisma.user.subscribe();
if (subscription instanceof Error) {
throw subscription;
}
for await (const event of subscription) {
console.log("just received an event:", event);
}
}
The following instructions use Prisma Studio to create a new record in the User
table. However, you can use any other method to write to the User
table (e.g. using a SQL client like psql
or TablePlus) in order to trigger a database change event in Pulse.
- Start Prisma Studio in a new terminal:
npx prisma studio
- Add a new record to the
User
table from Prisma Studio. - Return to your terminal where you ran the
npx ts-node index.ts
command. - If everything is set up properly you will see an output that is similar to the following.
{
"action": "create",
"after": {
"id": 1,
"email": "[email protected]",
"name": "test"
}
}
This project is used together with the prisma/pulse-railway-pg-config repo to build a template on railway.app.
You will see three things in your railway project:
- A PostgreSQL database: The database that will be used with Pulse. It will have tables that map to the models in the
prisma.schema
file. - A service called
restart-db-then-delete-me
: Used to configure your database to work with Pulse. - A service called
pulse-starter
: The service that will run your Pulse project. It also has aprisma.schema
file with models that map to tables in your database.
When you deploy this template, you will have two repositories that match the names of the services created on your GitHub account. These repositories are connected to the services in your railway.app project. When you push to these repositories, railway.app will automatically deploy the changes to your project.
1. Get your database connection string and delete the restart-db-then-delete-me
service.
- Click on the service called
restart-db-then-delete-me
. - You will see a list of deployments under the Deployments tab.
- Click the most recent build's View Logs button.
- Click on the Deploy Logs tab. If the service ran correctly, you should see a message in the logs that says
All done please restart the database
along with the value of yourDATABASE_URL
env var. - Copy the
DATABASE_URL
connection string and save it for later. - Close the logs view with the X in the top right corner of the opened drawer.
- Navigate to the Settings tab of the
restart-db-then-delete-me
service. - Scroll down to the bottom and click the red Delete Service from All Environments button.
2. Restart your database
- Go into your project on the railway dashboard.
- Click on the Postgres database.
- Navigate to the Settings tab.
- Click the button that says Restart Database.
- Your database is getting restarted.
3. Connect Pulse to your database.
- Go to your Prisma Data Platform dashboard.
- Click on the project you want to add Pulse to (or create a new one).
- Click on Configure Pulse.
- Paste in the connection string from the railway dashboard. The connection string can be found by clicking on the Postgres database and navigating to the Connect tab, then clicking the copy-icon next to
DATABASE_URL
.
Once you have done that, you will need to wait for Pulse to establish the connection. This can take a few minutes.
4. Set the PULSE_API_KEY
environment variable
- Once you have connected your database to your Pulse project, you will be able to create an API Key.
- With your
API_KEY
, you can return to your railway.app project. - Click on the service called
pulse-railway-starter
.Note: You'll likely find that the build failed. This is because the database was not ready when the Pulse connection was made. Do not worry, this is to be expected and not a problem for the next steps.
- Click on the Variables tab.
- You will see a variable called
PULSE_API_KEY
. If you do not have that variable, create it. - Click the three vertical dots on the
PULSE_API_KEY
row and select Edit. - Paste in the
API_KEY
and click the check mark.
5. Rebuild the service
- Click on the Deployments tab.
- Click on the three verticle dots on the deployment that failed. Then click Redeploy.
- When the deployment starts, click the View Logs button.
- Then click on the Deploy Logs tab.
- If everything is set up properly, you should see a message that looks like the following.
Hello from 12fcb1f8adc06640f7d89483bb4ce89d7b3cf7444df7b34ea5b706ed8919a6e6
This means that your Pulse project is running and listening for events from your database.
6. See user table create event in action
- Click on your Postgres database in your railway.app project.
- It should open on the Data tab.
- Click the
User
table and click Add Row. - Fill out the
email
andname
fields, then click Insert. - Return to the logs of your
pulse-railway-starter
service. - You should be able to see an output from Pulse for the user being created. Something similar to the following:
just received an event: { action: 'create', after: { id: 1, email: 'test', name: '[email protected]' } }
Congrats! You now have a Pulse project up and running on railway.app