Skip to content

Commit

Permalink
update getting started
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed May 3, 2024
1 parent f04ed58 commit 70bda45
Showing 1 changed file with 92 additions and 28 deletions.
120 changes: 92 additions & 28 deletions content/400-pulse/200-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ tocDepth: 4
toc: true
---


## Prerequisites

<Admonition type="info">
Expand All @@ -17,19 +16,39 @@ Prisma Pulse currently supports PostgreSQL. We'd love to hear [which databases](

To get started with Pulse, you will need the following:

- A [Prisma Data Platform workspace](https://console.prisma.io).
- [Prisma Client](/orm/prisma-client) version `4.16.1` or higher and [`@prisma/extension-pulse`](https://www.npmjs.com/package/@prisma/extension-pulse) version `1.0.1` or higher.
- A publicly accessible PostgreSQL (version 12+) database with [logical replication](https://www.postgresql.org/docs/current/logical-replication-quick-setup.html) enabled. View our [setup guide](/pulse/database-setup/general-database-instructions#enable-logical-replication) on configuring logical replication for your database.
- An account in the [Prisma Data Platform](https://console.prisma.io).
- The **connection string** of a Pulse-ready database (if you don't have one yet, you can configure your database the instructions [here](/pulse/database-setup) or [use a Railway template](https://railway.app/template/pulse-pg?referralCode=VQ09uv))

## 1. Enable Pulse in the Platform Console

### 1.1. Select workspace -> project -> environment

Open the [Platform Console](https://console.prisma.io), navigate to your workspace of choice, then select the project and choose the environment in which you want to enable Pulse.

### 1.2. Enable Pulse

When you're there, click the **Enable Pulse** button.

## 1. Enable Pulse
### 1.3. Configure Pulse

Navigate to your Prisma Data Platform project, choose an environment, and enable Pulse. We'll connect to your database and verify connectivity during setup.
The Pulse **Setup** screen requires you to:

> Once enabled, you'll be prompted to generate an API key that you'll use in your extended Prisma Client to authenticate requests. Store this API key in your application's `.env` file:
>
> ```env file=.env showLineNumbers
> PULSE_API_KEY="your_secure_pulse_api_key"
> ```
- provide your **Database connection string**
- select a **Region** where Pulse should be hosted
- decide whether you want to use the **Automatic setup** for **Database replication** (only available on paid plans)

When you're done with that, click the **Enable Pulse** button at the bottom of the screen. This will test the connectivity to your database.


### 1.4. Generate an API key

You can generate an API key by clicking the **Generate API key** button. Store the API key in a secure location or add it to the `.env` file of your project:

```env file=.env
PULSE_API_KEY="your_secure_pulse_api_key"
```

You won't be able to access the same API key again afterwards.

## 2. Add Pulse to your application

Expand All @@ -43,10 +62,10 @@ Pulse requires [Prisma Client](/orm/prisma-client) version `4.16.1` or higher an

</Admonition>

Install the latest version of Prisma Client and the Pulse Client extension
Install the Pulse Client extension:

```bash
npm install @prisma/client@latest @prisma/extension-pulse@latest
```terminal
npm install @prisma/extension-pulse@latest
```

### 2.2. Extend your Prisma Client instance with the Pulse extension
Expand All @@ -62,46 +81,91 @@ const prisma = new PrismaClient().$extends(
)
```

<Admonition type="note">

You stored this API key in your .env file after [enabling Pulse](#1-enable-pulse). If needed, you can navigate to your respective project environment and generate a new API key.

</Admonition>

### 2.3. Create your first Pulse subscription

With the Pulse extension applied, you can use Pulse's `subscribe()` method on any model defined in your Prisma Schema to subscribe to data change events.

In the below example, a subscription is made to a `notification` model that listens for _any_ change event on that table:
In the below example, it is assumed that your Prisma schema has a `User` model. A subscription is made to that `User` model that listens for _any_ change event on that table:

<TabbedContent code>
<TabItem value="Subscription">

```ts
const prisma = new PrismaClient().$extends(withPulse({ apiKey: apiKey }))
import { PrismaClient } from '@prisma/client'
import { withPulse } from '@prisma/extension-pulse'

const prisma = new PrismaClient().$extends(
withPulse({ apiKey: process.env.PULSE_API_KEY })
)

async function main() {
const subscription = await prisma.notification.subscribe()
const subscription = await prisma.user.subscribe()

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

main()
```

All done! You've successfully added Pulse to your application. Explore next steps to learn more.
</TabItem>

<TabItem value="Prisma schema">

```prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
```

</TabItem>

</TabbedContent>

## 3. Test your subscription

After running the code snippet above, you can test the subscription by _creating_, _updating_ or _deleting_ a `User` record in your database.

You can do that using Prisma Studio (by running `npx prisma studio`) or by using any other database client of your choice (like [Postico](https://eggerapps.at/postico2/) or `psql`).

If everything worked, you should see the event being logged to the terminal where you can the code snippet from above.

## Next steps

[Navigate to the API section](/pulse/api-reference) to explore available filtering options for Pulse's `subscribe()` method.
Yopu can try out more filters on your Pulse subscription, for example:

**Subscribe only to `create` events**:

```ts
const subscription = await prisma.user.subscribe({
create: { },
})
```

**Subscribe only to `update` events**:

```ts
const subscription = await prisma.notification.subscribe({
create: {
userId: 123, // subscribe to all notifications created for the user with ID 123
},
const subscription = await prisma.user.subscribe({
update: { },
})
```


**Subscribe only to `delete` events**:

```ts
const subscription = await prisma.user.subscribe({
delete: { },
})
```

Pulse offers even more fine-grained filters than these. You can explore these in the [API reference](/pulse/api-reference).

## Need help?

Reach out to us in the `#help-and-questions` channel on our [Discord](https://pris.ly/discord), or connect with our community to see how others are using Pulse.

0 comments on commit 70bda45

Please sign in to comment.