npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-generator-postgres-realtime

v1.0.0

Published

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

Downloads

285

Readme

Prisma Generator Postgres Realtime

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

How it works?

  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
  2. Run prisma migrate dev to apply all generated migrations
  3. Use the generated extension in prisma client to enable a new client/models method called $subscribe and start watching to realtime events sent

OBS:

  • (Optional) Use the generatorConfigPath generator option to customize some options for your project (like excluding models, ...)

Set up

Install generator

npm install --save-dev prisma-generator-postgres-realtime (or yarn | pnpm version)

Install node-postgres peer dependancy

npm install pg

Add the generator to your schema.prisma

// schema.prisma
generator client {
  provider = "prisma-client-js"
}

// new generator here ⬇️
generator realtime {
  provider            = "prisma-generator-postgres-realtime"
  generatorConfigPath = "../src/realtime/configs.js" // (optional)
}

/// This is an user
model User {
  id  String  @id
}

Usage

  1. Run prisma generate to generate the prismaExtension.ts file and migrations
  2. Run prisma migrate dev to apply all generated migrations
  3. Import the extension to your prisma client
/* src/db.ts */
import { PrismaClient } from "@prisma/client";
// Import auto generated extension
import { prismaRealtimeExtension } from './realtime/prismaExtension';

const prisma = new PrismaClient().$extends(PrismaExtension);

// global subscription
prisma.$subscribe(({ dbUser, model, newRow, oldRow, operation, tableName, tableSchema, timestamp }) => {
  console.log(`${operation} in ${model} at ${timestamp}`)
})

// typesafe model subscription
prisma.user.$subscribe(({ dbUser, model, newRow, oldRow, operation, tableName, tableSchema, timestamp }) => {
  console.log(`${operation} in ${model} at ${timestamp}`)
}, {
  // (optional) Enable logs for connection, by defaults only shows errors
  logLevel: "all",
  // (optional) Custom postgres connection string, by default it uses the one from `process.env.DATABASE_URL`
  connectionString: "postgres://postgres:postgres@localhost:5432/postgres"
})

Configuration file (optional)

Create a configuration file (optional) and reference it in your schema.prisma generator config called generatorConfigPath

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

// ./src/realtime/configs.js

// /** @type {import('prisma-generator-postgres-realtime').Config} */

/** @type {import('../../../../src').Config} */
module.exports = {
  migrations: {
    // Option to disable the generation of migrations
    disabled: false,
    // Directory to generate the custom migrations from project root
    outputDirPath: "./prisma/migrations",
    // Path to generate the status file to from project root
    outputStatusFilePath: "./src/realtime/prismaRealtimeStatus.json",
    // A function to replace generated source
    replacer: (str) => str
    // Included models in migrations
    excludeModels: [],
    // Excluded models in migrations (Ignored if includeModels is set)
    includeModels: [],
  },
  extension: {
    // Option to disable the generation of crud
    disabled: false,
    // Path to generate the inputs file to from project root
    outputFilePath: "./src/realtime/prismaExtension.ts",
    // The code to import the prisma client (Util for some projects like Monorepos)
    prismaClientImporter: `import { Prisma } from "@prisma/client";`,
    // A function to replace generated source
    replacer: (str) => str,
  },
  global: {
    // Function to run before generate
    beforeGenerate: (dmmf) => {},
    // Function to run after generate
    afterGenerate: (dmmf) => {},
    // A function to replace generated source
    replacer: (str) => str,
    // The name of the postgres trigger
    triggerName: "prisma_postgres_realtime_trigger",
  },
};

Examples

Check for the example for a running sample

Disclaimer

Prisma Views

Currently, prisma does not enable us distinguish between models and views (issue). 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