Skip to content

ngtranthanhtoan/birdwatch-pulse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prisma Pulse Example: Starter

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 stream found in the index.ts file.

Prerequisites

To successfully run the project, you will need the following:

  • The connection string of a Pulse-ready 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 (learn more in the docs)

Getting started

1. Clone the respository

Clone the repository, navigate into it and install dependencies:

git clone [email protected]:prisma/prisma-examples.git --depth=1
cd prisma-examples/pulse/starter
npm install

2. Configure environment variables

Create a .env in the root of the project directory:

touch .env

Now, open the .env file and update the DATABASE_URL and PULSE_API_KEY environment variables with the values of your connection string and your Pulse API key:

# .env
DATABASE_URL="__YOUR_DATABASE_CONNECTION_STRING__"
PULSE_API_KEY="__YOUR_PULSE_API_KEY__"

Note that __YOUR_DATABASE_CONNECTION_STRING__ and __YOUR_PULSE_API_KEY__ are placeholder values that you need to replace with the values of your connection string and your Pulse API key.

3. Run a database migration to create the User table

The Prisma schema file contains a single User model. You can map this model to the database and create the corresponding User table using the following command:

npx prisma migrate dev --name init

You now have an empty User table in your database.

4. Start the Pulse stream

Run the script that contains the code to stream database events:

npx ts-node index.ts

This will create a basic stream on the User table. Whenever a record is created, updated or deleted in that table, an event will fire and the script will execute a console.log statement with details of the event it received.

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 stream on the `User` table
async function main() {
  const stream = await prisma.user.stream()

  for await (const event of stream) {
    console.log('just received an event:', event)
  }
}

5. Test the stream

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. a SQL client like psql or TablePlus) in order to trigger a database change event in Pulse.

  1. Start Prisma Studio in a new terminal: npx prisma studio

  2. Add a new record to the User table via Prisma Studio UI.

  3. Return to your terminal where you ran the npx ts-node index.ts command.

  4. If everything is set up properly you will see an output that is similar to the following.

    just received an event: {
      "action": "create",
      "created": {
        "id": 1,
        "email": "[email protected]",
        "name": "test"
      }
    }
    

Deployment

You can also deploy this project on Railway by following the instructions in our docs.

Deploy on Railway

Resources

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published