Code Monkey home page Code Monkey logo

pgmock's Introduction

pgmock

Demo โ€” Discord

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.

Installation

npm install pgmock

If you'd like to run pgmock in a browser, see the Browser support section for detailed instructions.

Getting started

You can run an in-memory server like so:

import { PostgresMock } from "pgmock";

const mock = await PostgresMock.create();
const connectionString = await mock.listen(5432);

Recommended: 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):

import * as pg from "pg";

const mock = await PostgresMock.create();
const client = new pg.Client(mock.getNodePostgresConfig());

await client.connect();
console.log(await client.query('SELECT $1::text as message', ['Hello world!']));

It is considered good practice to destroy the mock server after you are done with it to free up resources:

mock.destroy();

Documentation

Check the PostgresMock source file for a list of all available methods and their documentation.

Browser support

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.

If you're only looking to run a database in the browser, you might want to consider 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.

How does it work?

There are two approaches to run Postgres in WebAssembly; by forking it to support WASM natively or by emulating the Postgres server in an x86 emulator. The former is more performant and uses considerably less memory, but only supports single-user mode (no connections), and no extensions.

To 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.

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.

Wanna contribute?

Great! We have a Discord server where you can talk to us.

Can this run other Docker images or databases?

In theory, yes. I just haven't tested them. Ping me on our Discord server if you're interested.

Acknowledgements

  • v86, the x86 emulator which makes this possible
  • Supabase & Snaplet for building their own approach of running Postgres inside WebAssembly, which this is based on
  • Stackframe for keeping me on a payroll while I was building pgmock

pgmock's People

Contributors

n2d4 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.