{"id":13514981,"url":"https://github.com/stack-auth/pgmock","last_synced_at":"2025-05-16T09:03:13.097Z","repository":{"id":231045355,"uuid":"779428254","full_name":"stack-auth/pgmock","owner":"stack-auth","description":"In-memory Postgres for unit/E2E tests","archived":false,"fork":false,"pushed_at":"2024-08-25T19:42:28.000Z","size":41147,"stargazers_count":1171,"open_issues_count":16,"forks_count":23,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-25T05:37:50.724Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://stackframe-projects.github.io/pgmock/","language":"JavaScript","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/stack-auth.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":"2024-03-29T20:20:46.000Z","updated_at":"2025-04-16T14:02:46.000Z","dependencies_parsed_at":"2024-12-13T06:04:38.446Z","dependency_job_id":"c5c5ba68-5daa-4885-8f74-57a196f54775","html_url":"https://github.com/stack-auth/pgmock","commit_stats":null,"previous_names":["stackframe-projects/pgmock","stack-auth/pgmock"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stack-auth%2Fpgmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stack-auth%2Fpgmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stack-auth%2Fpgmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stack-auth%2Fpgmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stack-auth","download_url":"https://codeload.github.com/stack-auth/pgmock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254501555,"owners_count":22081528,"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":[],"created_at":"2024-08-01T05:01:04.861Z","updated_at":"2025-05-16T09:03:08.087Z","avatar_url":"https://github.com/stack-auth.png","language":"JavaScript","readme":"# pgmock\n\u003ch3 align=\"center\"\u003e\n  \u003ca href=\"https://stack-auth.github.io/pgmock\"\u003eDemo\u003c/a\u003e —\n  \u003ca href=\"https://discord.stack-auth.com\"\u003eDiscord\u003c/a\u003e\n\u003c/h3\u003e\n\n`pgmock` is an in-memory PostgreSQL mock server for unit and E2E tests. It requires no external dependencies and runs entirely within WebAssembly on both Node.js and the browser.\n\n## Installation\n\n```bash\nnpm install pgmock\n```\n\nIf you'd like to run `pgmock` in a browser, see the [Browser support](#browser-support) section for detailed instructions.\n\n## Getting started\nYou can run an in-memory server like so:\n\n```typescript\nimport { PostgresMock } from \"pgmock\";\n\nconst mock = await PostgresMock.create();\nconst connectionString = await mock.listen(5432);\n```\n\nRecommended: If you use `node-postgres` (`pg` on npm), `pgmock` provides you with a configuration object that doesn't require you to serve on a port (and also works in the browser):\n\n```typescript\nimport * as pg from \"pg\";\n\nconst mock = await PostgresMock.create();\nconst client = new pg.Client(mock.getNodePostgresConfig());\n\nawait client.connect();\nconsole.log(await client.query('SELECT $1::text as message', ['Hello world!']));\n```\n\nIt is considered good practice to destroy the mock server after you are done with it to free up resources:\n\n```typescript\nmock.destroy();\n```\n\n## Documentation\n\nCheck the [PostgresMock source file](https://github.com/stackframe-projects/pgmock/blob/main/src/postgres-mock.ts) for a list of all available methods and their documentation.\n\n## Browser support\n\n`pgmock` fully supports browser environments. While webapps can't listen to TCP ports, you can still use `PostgresMock.createSocket` and the `node-postgres` configuration. However, if your bundler statically analyzes imports, the default configuration may show a warning because of missing (optional) Node.js modules. Check `examples/web-demo/next.config.mjs` for an example on how to configure Webpack for bundling.\n\nIf you're only looking to run a database in the browser, you might want to consider [pglite](https://github.com/electric-sql/pglite) instead. It is more performant and lightweight, but only has a limited feature set. `pgmock` is designed for feature parity with production PostgreSQL environments, as you would want in a testing environment.\n\n## How does it work?\n\nThere are two approaches to run Postgres in WebAssembly; by [forking it to support WASM natively](https://github.com/electric-sql/postgres-wasm) or by [emulating the Postgres server in an x86 emulator](https://supabase.com/blog/postgres-wasm). The former is more performant and uses considerably less memory, but only supports single-user mode (no connections), and no extensions.\n\nTo prevent discrepancies between testing and production, and because performance is not usually a concern in tests, `pgmock` currently uses the latter approach. In the mid-term future, once native Postgres WASM forks mature, we plan to make both options available, and eventually, switch to native WASM as default. We don't expect there to be many breaking changes besides the APIs inside `PostgresMock.subtle`.\n\n`pgmock` differs from previous Postgres-in-the-browser projects by providing full feature-compatibility entirely inside the JavaScript runtime, without depending on a network proxy for communication. We did this by simulating a network stack in JavaScript that behaves like a real network, that can simulate TCP connections even on platforms that do not allow raw socket access.\n\n## Wanna contribute? \n\nGreat! We have a [Discord server](https://discord.gg/pD4nyYyKrb) where you can talk to us.\n\n## Can this run other Docker images or databases?\n\nIn theory, yes. I just haven't tested them. Ping me on our [Discord server](https://discord.gg/pD4nyYyKrb) if you're interested.\n\n## Acknowledgements\n\n- [v86](https://github.com/copy/v86), the x86 emulator which makes this possible\n- [Supabase \u0026 Snaplet](https://supabase.com/blog/postgres-wasm) for building their own approach of running Postgres inside WebAssembly, which this is based on\n- [Stackframe](https://stackframe.co) for keeping me on a payroll while I was building `pgmock`\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstack-auth%2Fpgmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstack-auth%2Fpgmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstack-auth%2Fpgmock/lists"}