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 schema.prisma
file as well as a Pulse subscription found in the index.ts
file.
To successfully run the project, you will need the following:
- The connection string of a Pulse-compatible database (if you don't have one yet, you can configure your database following the instructions in our docs or use a Railway template)
- A Pulse API key which you can get by enabling Pulse in a project in your Prisma Data Platform account
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
Now go into the .env
file and update the DATABASE_URL
and PULSE_API_KEY
environment variables:
DATABASE_URL="postgres://postgres:password@host:PORT/database_name"
PULSE_API_KEY="your_secure_pulse_api_key"
DATABASE_URL
: The connection string to your database.PULSE_API_KEY
: Reference the Environment 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.stream();
if (subscription instanceof Error) {
throw subscription;
}
for await (const event of subscription) {
console.log("just received an event:", event);
}
}
The following instructions uses 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. 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", "created": { "id": 1, "email": "test@prisma.io", "name": "test" } }
You can also deploy this project on Railway by following the instructions in our docs.