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

@andyrmitchell/pgmock

v1.0.3

Published

In-memory Postgres for unit/E2E tests

Downloads

2

Readme

pgmock

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