Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ yarn-error.log*

# wrangler project
.dev.vars
.wrangler/
.wrangler/
21 changes: 21 additions & 0 deletions docs/400-pulse/100-what-is-pulse.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: 'What is Pulse'
metaTitle: 'What is Pulse'
metaDescription: 'Learn about Pulse, a managed Change Data Capture service that lets you build real-time applications with ease.'
tocDepth: 3
toc: true
---

<TopBlock>

[Pulse](https://www.prisma.io/data-platform/pulse) is a managed [change data capture (CDC)](https://en.wikipedia.org/wiki/Change_data_capture) service that captures change events from your database and delivers them instantly to your applications. With Pulse, you can quickly build real-time applications in a type-safe manner using [Prisma Client](/orm/prisma-client).

<Admonition type="warning">

Pulse is currently in [Early Access](/platform/maturity-levels#early-access). Although we already have high confidence in it, the nature of an Early Access product is that significant iterations might happen at any time. Therefore, we advise against using it in a system that requires stability.<br /><br />

We strongly recommend evaluating Pulse with a dedicated database instance that is exclusively used for Pulse and where downtime and data loss would be acceptable. Prisma assumes no responsibility for downtime or data loss.

</Admonition>

</TopBlock>
209 changes: 209 additions & 0 deletions docs/400-pulse/200-getting-started.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
---
title: 'Getting started'
metaTitle: 'Getting started with Pulse'
metaDescription: 'Learn how to get up and running with Pulse.'
tocDepth: 3
toc: true
---

<TopBlock></TopBlock>

## Prerequisites

To participate in Pulse's Early Access program, you need to meet the following prerequisites:

- A GitHub account.
- Pulse requires [Prisma Client](/orm/prisma-client) version `4.16.1` or higher and [`@prisma/extension-pulse`](https://www.npmjs.com/package/@prisma/extension-pulse) version `v0.2.1` or higher.
- A publicly accessible PostgreSQL database.
- Ability to use the superuser account of the database instance. In the future, we will support the ability to connect to your database from Pulse with a limited access, non-superuser account.

You will also need a database with the following configurations:

- PostgreSQL version 12+.
- Ensure your database is publicly accessible.
- [Set the `wal_level` setting in PostgreSQL to `logical`](/pulse/getting-started#wal_level).
- A database superuser that can be used for connections inside Pulse.
- Connect to the database using `sslmode=disable` if the database provider uses self-signed certificates.

## 1. Database setup

### General database configuration

#### Required settings

##### [<inlinecode id="wal_level">wal_level</inlinecode>](https://www.postgresql.org/docs/current/runtime-config-wal.html)

Some providers may not allow direct access to this setting. If you are unable to change this setting, please refer to the provider-specific guides for further assistance.

```sql
ALTER SYSTEM SET wal_level = logical;
```

You will need to restart the database after changing this setting.

#### Optional settings

The following increases the memory usage of the [write-ahead log](https://www.postgresql.org/docs/current/wal-intro.html) on your PostgreSQL database. We suggest setting these values initially and adjusting them if necessary.

##### <inlinecode>[max_replication_slots](https://www.postgresql.org/docs/current/runtime-config-replication.html)</inlinecode>

```sql
ALTER SYSTEM SET max_replication_slots = 20;
```

##### <inlinecode>[wal_keep_size](https://www.postgresql.org/docs/current/runtime-config-replication.html)</inlinecode>

```sql
ALTER SYSTEM SET wal_keep_size = 2048;
```

### Provider specific configuration

To learn about the database providers that Pulse supports, visit [here](/pulse/faq#what-database-providers-are-supported-with-pulse).

#### Railway

[Railway.app](https://railway.app) offers an excellent [templates feature](https://railway.app/templates). If you wish to quickly start with Pulse, you can use either of two templates:

- [Prisma Pulse DB Only](https://railway.app/template/pulse-pg): Provides a fresh, pre-configured PostgreSQL database which you can use with Pulse.
- [Prisma Pulse DB & App](https://railway.app/template/pulse-starter): Provides a pre-configured PostgreSQL database and a [Pulse starter app](https://github.com/prisma/pulse-starter).

##### Setup without using a template

<details>
<summary>1. Change the PostgreSQL database settings</summary>

You can run these queries in the Railway Database **Query** tab, using the [railway cli](https://docs.railway.app/databases/postgresql), or any other way you might run queries on your database.

1. Drop the Timescale extension:

```sql
DROP EXTENSION timescaledb;
```

2. Set the <inlinecode>[wal_level](https://www.postgresql.org/docs/current/runtime-config-wal.html)</inlinecode> to `logical`:

```sql
ALTER SYSTEM SET wal_level = logical;
```

3. Set the <inlinecode>[max_replication_slots](https://www.postgresql.org/docs/current/runtime-config-replication.html)</inlinecode> to `20`:

```sql
ALTER SYSTEM SET max_replication_slots = 20;
```

4. Set the <inlinecode>[wal_keep_size](https://www.postgresql.org/docs/current/runtime-config-replication.html)</inlinecode> to `2048`:

```sql
ALTER SYSTEM SET wal_keep_size = 2048;
```

5. Reload the PostgreSQL configuration:

```sql
SELECT pg_reload_conf();
```

</details>
<details>
<summary>2. Restart your database</summary>

1. Click on your database.

2. Navigate to the Deployments tab.

3. Go into the three-dots menu on the latest deployment and click the `Restart` option.

</details>

##### SSL mode

As Railway uses a self-signed certificate, you have to use [`sslmode=disable`](/orm/overview/databases/postgresql#configuring-an-ssl-connection) with Pulse.

## 2. Enable Pulse in a project

Log into the [Prisma Data Platform](https://console.prisma.io/login), create a new project and enable Pulse for that new project.

> An API key will be created after you enable and setup Pulse in your [project](/platform/concepts/projects).

## 3. Use Pulse in your application

<Admonition type="info">

We have created an [example repository](https://github.com/prisma/pulse-starter) on GitHub to help you get started using Pulse. If you would like to start there, you can do so.

</Admonition>

The following will show how you can utilize Pulse in an existing application. We will be adding Pulse to the [hello-prisma](/getting-started/setup-prisma/start-from-scratch/relational-databases-typescript-postgresql) example from our documentation.

### 3.1. Install the Pulse Prisma Client extension

In a project using [Prisma Client](/orm/prisma-client), run the following command to install the Pulse extension:

```terminal
npm install @prisma/extension-pulse
```

### Store your Pulse API key in your .env file

The Pulse extension requires you to use an API key.

<Admonition type="info">

You should have received an API key when you added Prisma Pulse to your project in the Platform Console.

</Admonition>

In `.env`, add a variable named `PULSE_API_KEY`:

```env file=.env
PULSE_API_KEY="YOUR-API-KEY"

# Example:
# PULSE_API_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiNGMxNzM0MGItMmFhYy00MGMxLWE1ZDctNzYyNmRjNjg3NjM4IiwidGVuYW50X2lkIjoiY2VhZjE0NThkZGUyYzJmNTU0ZmNkNTI2MmFmOWY1ODljMWJiZmRhNDU0N2UxMjM1ODk3MGQ2MGI1ZjRlNTU0OCIsImludGVybmFsX3NlY3JldCI6ImM1ZTcxYjJhLTE0NzdawdwDliZS1hM2IzLTczODFkNDM5ZmEwZSJ9.wCUlghC_suFBr2vnk0q_5I8iRNRDyEQo0W9rnhf6mCw"
```

### 3.2. Create a Pulse-enabled Prisma Client

To use Pulse, you must extend Prisma Client with the Pulse extension.
Add the following to extend your existing Prisma Client instance with the Pulse extension:

```ts
import { PrismaClient } from '@prisma/client'
import { withPulse } from '@prisma/extension-pulse'

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

### 3.3. Create your first Pulse subscription

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

In the example below, a subscription is made on a `user` table that listens for _any_ change event on that table:

```ts
const prisma = new PrismaClient().$extends(withPulse({ apiKey: apiKey }))

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)
}
}

main()
```

<Admonition type="info">

Refer to the [API Reference](/pulse/api-reference) section for more detail on the filtering options available to the `subscribe()` method.

</Admonition>
23 changes: 23 additions & 0 deletions docs/400-pulse/300-concepts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: 'Concepts'
metaTitle: 'Pulse: Concepts'
metaDescription: 'Learn about the concepts that are important to understand when using Pulse.'
tocDepth: 3
toc: true
---

<TopBlock></TopBlock>

## Change data capture

[Change data capture (CDC)](https://en.wikipedia.org/wiki/Change_data_capture) is a technique used to track and capture changes in a database enabling real-time updates. It allows applications to be informed about the modifications in the database, ensuring data consistency between multiple applications.

## Logical replication

Logical replication is a method of replicating data objects and their changes based on their replication identity (usually a primary key). You can read more about logical replication and how it pertains to your database in Postgres' documentation [here](https://www.postgresql.org/docs/current/logical-replication.html).

## Write-ahead log

A [write-ahead log (WAL)](https://www.postgresql.org/docs/current/wal-intro.html) is a standard way of ensuring data integrity by only allowing updates to the data in a database _after_ a log has been written to permanent storage describing the change to take place.

This enhances data integrity because all changes to a database are recorded in these log files. In the event of a database crash, the database is recoverable using those logs and can even be recovered to a specific point in time.
Loading