{"id":28519581,"url":"https://github.com/mgcrea/prisma-queue","last_synced_at":"2026-01-12T15:51:19.395Z","repository":{"id":66212579,"uuid":"460620098","full_name":"mgcrea/prisma-queue","owner":"mgcrea","description":"Minimalist postgresql job queue for Prisma","archived":false,"fork":false,"pushed_at":"2024-10-29T13:49:05.000Z","size":447,"stargazers_count":55,"open_issues_count":6,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-09T06:08:01.340Z","etag":null,"topics":["job","postgresql","prisma","queue","worker"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mgcrea.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2022-02-17T21:51:44.000Z","updated_at":"2025-04-15T05:39:05.000Z","dependencies_parsed_at":"2023-03-13T20:30:20.882Z","dependency_job_id":"de6cc5c1-2a53-48f3-b1e6-f906a16bca8e","html_url":"https://github.com/mgcrea/prisma-queue","commit_stats":{"total_commits":43,"total_committers":1,"mean_commits":43.0,"dds":0.0,"last_synced_commit":"8722b7ec6559511b36d73cc0b40083e7cb152857"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"purl":"pkg:github/mgcrea/prisma-queue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgcrea%2Fprisma-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgcrea%2Fprisma-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgcrea%2Fprisma-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgcrea%2Fprisma-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgcrea","download_url":"https://codeload.github.com/mgcrea/prisma-queue/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgcrea%2Fprisma-queue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263727012,"owners_count":23502247,"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":["job","postgresql","prisma","queue","worker"],"created_at":"2025-06-09T06:07:29.003Z","updated_at":"2026-01-12T15:51:19.384Z","avatar_url":"https://github.com/mgcrea.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prisma Queue\n\n\u003c!-- markdownlint-disable MD033 --\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@mgcrea/prisma-queue\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/@mgcrea/prisma-queue.svg?style=for-the-badge\" alt=\"npm version\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@mgcrea/prisma-queue\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/dt/@mgcrea/prisma-queue.svg?style=for-the-badge\" alt=\"npm total downloads\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@mgcrea/prisma-queue\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/dm/@mgcrea/prisma-queue.svg?style=for-the-badge\" alt=\"npm monthly downloads\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@mgcrea/prisma-queue\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/l/@mgcrea/prisma-queue.svg?style=for-the-badge\" alt=\"npm license\" /\u003e\n  \u003c/a\u003e\n  \u003cbr /\u003e\n  \u003ca href=\"https://github.com/mgcrea/prisma-queue/actions/workflows/main.yml\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/actions/workflow/status/mgcrea/prisma-queue/main.yml?style=for-the-badge\u0026branch=master\" alt=\"build status\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://depfu.com/github/mgcrea/prisma-queue\"\u003e\n    \u003cimg src=\"https://img.shields.io/depfu/dependencies/github/mgcrea/prisma-queue?style=for-the-badge\" alt=\"dependencies status\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003c!-- markdownlint-enable MD037 --\u003e\n\n## Features\n\nSimple, reliable and efficient concurrent work queue for [Prisma](https://prisma.io) + [PostgreSQL](https://www.postgresql.org/)\n\n- Leverages PostgreSQL [SKIP LOCKED](https://www.2ndquadrant.com/en/blog/what-is-select-skip-locked-for-in-postgresql-9-5/) feature to reliably dequeue jobs\n- Supports [crontab](https://crontab.guru) syntax for complex scheduled jobs\n- Written in [TypeScript](https://www.typescriptlang.org/) for static type checking with exported types along the library.\n- Built by [tsup](https://tsup.egoist.dev) to provide both CommonJS and ESM packages.\n\n## Install\n\n```bash\nnpm install @mgcrea/prisma-queue --save\n# or\npnpm add @mgcrea/prisma-queue\n```\n\n## Quickstart\n\n1. If you use an old version of Prisma ranging from 2.29.0 to 4.6.1 (included), you must first add `\"interactiveTransactions\"` to your `schema.prisma` client configuration:\n\n```prisma\ngenerator client {\n  provider        = \"prisma-client-js\"\n  previewFeatures = [\"interactiveTransactions\"]\n}\n```\n\n1. Append the [`QueueJob` model](./prisma/schema.prisma) to your `schema.prisma` file\n\n1. Create your queue\n\n```ts\ntype JobPayload = { email: string };\ntype JobResult = { status: number };\n\nexport const emailQueue = createQueue\u003cJobPayload, JobResult\u003e({ name: \"email\" }, async (job, client) =\u003e {\n  const { id, payload } = job;\n  console.log(`Processing job#${id} with payload=${JSON.stringify(payload)})`);\n  // await someAsyncMethod();\n  await job.progress(50);\n  const status = 200;\n  if (Math.random() \u003e 0.5) {\n    throw new Error(`Failed for some unknown reason`);\n  }\n  console.log(`Finished job#${id} with status=${status}`);\n  return { status };\n});\n```\n\n- Queue a job\n\n```ts\nimport { emailQueue } from \"./emailQueue\";\n\nconst main = async () =\u003e {\n  const job = await emailQueue.enqueue({ email: \"foo@bar.com\" });\n};\n\nmain();\n```\n\n- Schedule a recurring job\n\n```ts\nimport { emailQueue } from \"./emailQueue\";\n\nconst main = async () =\u003e {\n  const nextJob = await queue.schedule(\n    { key: \"email-schedule\", cron: \"5 5 * * *\" },\n    { email: \"foo@bar.com\" },\n  );\n};\n\nmain();\n```\n\n- Start queue processing (usually in another process)\n\n```ts\nimport { emailQueue } from \"./emailQueue\";\n\nconst main = async () =\u003e {\n  await queue.start();\n};\n\nmain();\n```\n\n## Advanced usage\n\n### Edge Environments\n\nWhen using this library in edge environments (Cloudflare Workers, Vercel Edge Functions, etc.) where Prisma's DMMF (Datamodel Meta Format) may not be available, you should explicitly provide the `tableName` option:\n\n```ts\nexport const emailQueue = createQueue\u003cJobPayload, JobResult\u003e(\n  {\n    name: \"email\",\n    tableName: \"queue_job\", // Explicit table name for edge environments\n  },\n  async (job, client) =\u003e {\n    // ...\n  },\n);\n```\n\nThe library will automatically fall back to a snake_case conversion of the model name (e.g., `QueueJob` → `queue_job`) if DMMF is unavailable, but providing `tableName` explicitly is recommended for edge deployments.\n\n### Threading\n\nYou can easily spin of your workers in separate threads using [worker_threads](https://nodejs.org/api/worker_threads.html#worker-threads) (Node.js \u003e= 12.17.0).\n\nIt enables you to fully leverage your CPU cores and isolate your main application queue from potential memory leaks or crashes.\n\n```ts\nimport { JobPayload, JobResult, PrismaJob } from \"@mgcrea/prisma-queue\";\nimport { Worker } from \"node:worker_threads\";\nimport { ROOT_DIR } from \"src/config/env\";\nimport { log } from \"src/config/log\";\n\nconst WORKER_SCRIPT = `${ROOT_DIR}/dist/worker.js`;\n\nexport const processInWorker = async \u003cP extends JobPayload, R extends JobResult\u003e(\n  job: PrismaJob\u003cP, R\u003e,\n): Promise\u003cR\u003e =\u003e\n  new Promise((resolve, reject) =\u003e {\n    const workerData = getJobWorkerData(job);\n\n    log.debug(`Starting worker thread for job id=${job.id} in queue=${job.record.queue}`);\n    try {\n      const worker = new Worker(WORKER_SCRIPT, {\n        workerData,\n      });\n\n      worker.on(\"message\", resolve);\n      worker.on(\"error\", reject);\n      worker.on(\"exit\", (code) =\u003e {\n        if (code !== 0) {\n          reject(\n            new Error(\n              `Worker for job id=${job.id} in queue=${job.record.queue} stopped with exit code ${code}`,\n            ),\n          );\n        }\n      });\n    } catch (error) {\n      reject(error as Error);\n    }\n  });\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type JobWorkerData\u003cP extends JobPayload = any\u003e = {\n  id: bigint;\n  payload: P;\n  queue: string;\n};\n\nconst getJobWorkerData = \u003cP extends JobPayload, R extends JobResult\u003e(job: PrismaJob\u003cP, R\u003e): JobWorkerData =\u003e {\n  // Prepare the job data for structured cloning in worker thread\n  return {\n    id: job.id,\n    payload: job.payload,\n    queue: job.record.queue,\n  };\n};\n```\n\n- `worker.ts`\n\n```ts\nimport { parentPort, workerData } from \"node:worker_threads\";\nimport { log } from \"src/config/log\";\nimport { workers } from \"src/queue\";\nimport { type JobWorkerData } from \"src/utils/queue\";\nimport { logMemoryUsage } from \"./utils/system\";\n\nlog.info(`Worker thread started with data=${JSON.stringify(workerData)}`);\n\nconst typedWorkerData = workerData as JobWorkerData;\nconst { queue } = typedWorkerData;\nconst workerName = queue.replace(/Queue$/, \"Worker\") as keyof typeof workers;\n\nlog.debug(`Importing worker ${workerName} for queue=${queue}`);\nconst jobWorker = workers[workerName];\n// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\nif (!jobWorker) {\n  log.error(`No worker found for queue=${queue}`);\n  process.exit(1);\n}\n\nlog.info(`Running worker for queue=${queue}`);\nconst result = await jobWorker(typedWorkerData);\nlog.info(`Worker for queue=${queue} completed with result=${JSON.stringify(result)}`);\nparentPort?.postMessage(result);\nprocess.exit(0);\n```\n\n## Authors\n\n- [Olivier Louvignes](https://github.com/mgcrea) \u003c\u003colivier@mgcrea.io\u003e\u003e\n\n## Credits\n\nInspired by\n\n- [pg-queue](https://github.com/OrKoN/pg-queue) by\n\n## License\n\n```txt\nThe MIT License\n\nCopyright (c) 2022 Olivier Louvignes \u003colivier@mgcrea.io\u003e\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated\ndocumentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the\nrights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit\npersons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the\nSoftware.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\nWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\nOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgcrea%2Fprisma-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgcrea%2Fprisma-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgcrea%2Fprisma-queue/lists"}