{"id":21151290,"url":"https://github.com/cauen/prisma-generator-postgres-realtime","last_synced_at":"2025-04-13T11:45:36.416Z","repository":{"id":263161749,"uuid":"888628403","full_name":"Cauen/prisma-generator-postgres-realtime","owner":"Cauen","description":"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","archived":false,"fork":false,"pushed_at":"2024-11-16T18:28:15.000Z","size":139,"stargazers_count":11,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T16:16:54.268Z","etag":null,"topics":["codegen","database","db","extension","generator","postgres","prisma","realtime","subscription","typescript"],"latest_commit_sha":null,"homepage":"https://npmjs.com/prisma-generator-postgres-realtime","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cauen.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-14T18:15:44.000Z","updated_at":"2025-02-25T08:51:19.000Z","dependencies_parsed_at":"2024-11-16T21:09:28.949Z","dependency_job_id":null,"html_url":"https://github.com/Cauen/prisma-generator-postgres-realtime","commit_stats":null,"previous_names":["cauen/prisma-generator-postgres-realtime"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cauen%2Fprisma-generator-postgres-realtime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cauen%2Fprisma-generator-postgres-realtime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cauen%2Fprisma-generator-postgres-realtime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cauen%2Fprisma-generator-postgres-realtime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cauen","download_url":"https://codeload.github.com/Cauen/prisma-generator-postgres-realtime/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710404,"owners_count":21149185,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegen","database","db","extension","generator","postgres","prisma","realtime","subscription","typescript"],"created_at":"2024-11-20T10:15:46.298Z","updated_at":"2025-04-13T11:45:36.410Z","avatar_url":"https://github.com/Cauen.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prisma Generator Postgres Realtime\n\nA 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\n\n## How it works?\n\n1. 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\n2. Run `prisma migrate dev` to apply all generated migrations\n3. Use the generated extension in prisma client to enable a new client/models method called `$subscribe` and start watching to realtime events sent\n\nOBS:\n- (Optional) Use the `generatorConfigPath` generator option to customize some options for your project (like excluding models, ...)\n\n### Set up\n\n#### Install generator \n`npm install --save-dev prisma-generator-postgres-realtime` (or yarn | pnpm version)\n\n#### Install node-postgres peer dependancy\n`npm install pg`\n\n#### Add the generator to your `schema.prisma`\n\n```prisma\n// schema.prisma\ngenerator client {\n  provider = \"prisma-client-js\"\n}\n\n// new generator here ⬇️\ngenerator realtime {\n  provider            = \"prisma-generator-postgres-realtime\"\n  generatorConfigPath = \"../src/realtime/configs.js\" // (optional)\n}\n\n/// This is an user\nmodel User {\n  id  String  @id\n}\n```\n\n### Usage\n\n1. Run `prisma generate` to generate the `prismaExtension.ts` file and migrations\n2. Run `prisma migrate dev` to apply all generated migrations\n3. Import the extension to your prisma client\n```ts\n/* src/db.ts */\nimport { PrismaClient } from \"@prisma/client\";\n// Import auto generated extension\nimport { prismaRealtimeExtension } from './realtime/prismaExtension';\n\nconst prisma = new PrismaClient().$extends(PrismaExtension);\n\n// global subscription\nprisma.$subscribe(({ dbUser, model, newRow, oldRow, operation, tableName, tableSchema, timestamp }) =\u003e {\n  console.log(`${operation} in ${model} at ${timestamp}`)\n})\n\n// typesafe model subscription\nprisma.user.$subscribe(({ dbUser, model, newRow, oldRow, operation, tableName, tableSchema, timestamp }) =\u003e {\n  console.log(`${operation} in ${model} at ${timestamp}`)\n}, {\n  // (optional) Enable logs for connection, by defaults only shows errors\n  logLevel: \"all\",\n  // (optional) Custom postgres connection string, by default it uses the one from `process.env.DATABASE_URL`\n  connectionString: \"postgres://postgres:postgres@localhost:5432/postgres\"\n})\n```\n\n### Configuration file (optional)\nCreate a configuration file (optional) and reference it in your `schema.prisma` generator config called `generatorConfigPath`\n\nThis 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\n\n```ts\n// ./src/realtime/configs.js\n\n// /** @type {import('prisma-generator-postgres-realtime').Config} */\n\n/** @type {import('../../../../src').Config} */\nmodule.exports = {\n  migrations: {\n    // Option to disable the generation of migrations\n    disabled: false,\n    // Directory to generate the custom migrations from project root\n    outputDirPath: \"./prisma/migrations\",\n    // Path to generate the status file to from project root\n    outputStatusFilePath: \"./src/realtime/prismaRealtimeStatus.json\",\n    // A function to replace generated source\n    replacer: (str) =\u003e str\n    // Included models in migrations\n    excludeModels: [],\n    // Excluded models in migrations (Ignored if includeModels is set)\n    includeModels: [],\n  },\n  extension: {\n    // Option to disable the generation of crud\n    disabled: false,\n    // Path to generate the inputs file to from project root\n    outputFilePath: \"./src/realtime/prismaExtension.ts\",\n    // The code to import the prisma client (Util for some projects like Monorepos)\n    prismaClientImporter: `import { Prisma } from \"@prisma/client\";`,\n    // A function to replace generated source\n    replacer: (str) =\u003e str,\n  },\n  global: {\n    // Function to run before generate\n    beforeGenerate: (dmmf) =\u003e {},\n    // Function to run after generate\n    afterGenerate: (dmmf) =\u003e {},\n    // A function to replace generated source\n    replacer: (str) =\u003e str,\n    // The name of the postgres trigger\n    triggerName: \"prisma_postgres_realtime_trigger\",\n  },\n};\n\n```\n\n### Examples\n\nCheck for the [example](/examples/simple) for a running sample\n\n### Disclaimer\n\n#### Prisma Views\n\nCurrently, 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcauen%2Fprisma-generator-postgres-realtime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcauen%2Fprisma-generator-postgres-realtime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcauen%2Fprisma-generator-postgres-realtime/lists"}