|
| 1 | +# Prisma Generator Postgres Realtime |
| 2 | + |
| 3 | +A prisma generator that turns your Postgres Database into a realtime Database and make it easy to subscribe to changes from Prisma Client type-safe Api |
| 4 | + |
| 5 | +## How it works? |
| 6 | + |
| 7 | +1. On `prisma generate` it will generate the `prismaExtension.ts` and `prismaRealtimeStatus.json` (to store what migrations have been generated and avoid regenerating them) file and the needed `migrations` to make your database send realtime events |
| 8 | +2. Run `prisma migrate dev` to apply all generated migrations |
| 9 | +3. Use the generated extension in prisma client to enable a new client/models method called `$subscribe` and start watching to realtime events sent |
| 10 | + |
| 11 | +OBS: |
| 12 | +- (Optional) Use the `generatorConfigPath` generator option to customize some options for your project (like excluding models, ...) |
| 13 | + |
| 14 | +### Set up |
| 15 | + |
| 16 | +#### Install generator |
| 17 | +`npm install --save-dev prisma-generator-postgres-realtime` (or yarn | pnpm version) |
| 18 | + |
| 19 | +#### Install node-postgres peer dependancy |
| 20 | +`npm install pg` |
| 21 | + |
| 22 | +#### Add the generator to your `schema.prisma` |
| 23 | + |
| 24 | +```prisma |
| 25 | +// schema.prisma |
| 26 | +generator client { |
| 27 | + provider = "prisma-client-js" |
| 28 | +} |
| 29 | +
|
| 30 | +// new generator here ⬇️ |
| 31 | +generator realtime { |
| 32 | + provider = "prisma-generator-postgres-realtime" |
| 33 | + generatorConfigPath = "../src/realtime/configs.js" // (optional) |
| 34 | +} |
| 35 | +
|
| 36 | +/// This is an user |
| 37 | +model User { |
| 38 | + id String @id |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### Usage |
| 43 | + |
| 44 | +1. Run `prisma generate` to generate the `prismaExtension.ts` file and migrations |
| 45 | +2. Run `prisma migrate dev` to apply all generated migrations |
| 46 | +3. Import the extension to your prisma client |
| 47 | +```ts |
| 48 | +/* src/db.ts */ |
| 49 | +import { PrismaClient } from "@prisma/client"; |
| 50 | +// Import auto generated extension |
| 51 | +import { prismaRealtimeExtension } from './realtime/prismaExtension'; |
| 52 | + |
| 53 | +const prisma = new PrismaClient().$extends(PrismaExtension); |
| 54 | + |
| 55 | +// global subscription |
| 56 | +prisma.$subscribe(({ dbUser, model, newRow, oldRow, operation, tableName, tableSchema, timestamp }) => { |
| 57 | + console.log(`${operation} in ${model} at ${timestamp}`) |
| 58 | +}) |
| 59 | + |
| 60 | +// typesafe model subscription |
| 61 | +prisma.user.$subscribe(({ dbUser, model, newRow, oldRow, operation, tableName, tableSchema, timestamp }) => { |
| 62 | + console.log(`${operation} in ${model} at ${timestamp}`) |
| 63 | +}, { |
| 64 | + // (optional) Enable logs for connection, by defaults only shows errors |
| 65 | + logLevel: "all", |
| 66 | + // (optional) Custom postgres connection string, by default it uses the one from `process.env.DATABASE_URL` |
| 67 | + connectionString: "postgres://postgres:postgres@localhost:5432/postgres" |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +### Configuration file (optional) |
| 72 | +Create a configuration file (optional) and reference it in your `schema.prisma` generator config called `generatorConfigPath` |
| 73 | + |
| 74 | +This configuration file enables some options like customize generated code, file paths, Prisma importer for some projects like Monorepos and disabling realtime for some specific models |
| 75 | + |
| 76 | +```ts |
| 77 | +// ./src/realtime/configs.js |
| 78 | + |
| 79 | +// /** @type {import('prisma-generator-postgres-realtime').Config} */ |
| 80 | + |
| 81 | +/** @type {import('../../../../src').Config} */ |
| 82 | +module.exports = { |
| 83 | + migrations: { |
| 84 | + // Option to disable the generation of migrations |
| 85 | + disabled: false, |
| 86 | + // Directory to generate the custom migrations from project root |
| 87 | + outputDirPath: "./prisma/migrations", |
| 88 | + // Path to generate the status file to from project root |
| 89 | + outputStatusFilePath: "./src/realtime/prismaRealtimeStatus.json", |
| 90 | + // A function to replace generated source |
| 91 | + replacer: (str) => str |
| 92 | + // Included models in migrations |
| 93 | + excludeModels: [], |
| 94 | + // Excluded models in migrations (Ignored if includeModels is set) |
| 95 | + includeModels: [], |
| 96 | + }, |
| 97 | + extension: { |
| 98 | + // Option to disable the generation of crud |
| 99 | + disabled: false, |
| 100 | + // Path to generate the inputs file to from project root |
| 101 | + outputFilePath: "./src/realtime/prismaExtension.ts", |
| 102 | + // The code to import the prisma client (Util for some projects like Monorepos) |
| 103 | + prismaClientImporter: `import { Prisma } from "@prisma/client";`, |
| 104 | + // A function to replace generated source |
| 105 | + replacer: (str) => str, |
| 106 | + }, |
| 107 | + global: { |
| 108 | + // Function to run before generate |
| 109 | + beforeGenerate: (dmmf) => {}, |
| 110 | + // Function to run after generate |
| 111 | + afterGenerate: (dmmf) => {}, |
| 112 | + // A function to replace generated source |
| 113 | + replacer: (str) => str, |
| 114 | + // The name of the postgres trigger |
| 115 | + triggerName: "prisma_postgres_realtime_trigger", |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +### Examples |
| 122 | + |
| 123 | +Check for the [example](/examples/simple) for a running sample |
| 124 | + |
| 125 | +### Disclaimer |
| 126 | + |
| 127 | +#### Prisma Views |
| 128 | + |
| 129 | +Currently, prisma does not enable us distinguish between models and views [(issue)](https://github.com/prisma/prisma/issues/17754). So if you are working with views you can disable generation of views from "options.migrations.excludeModels" or by simply deleting this part of code from the migration |
0 commit comments