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

pg-container

v1.0.0

Published

Postgres containers for Node

Downloads

7

Readme

pg-container

Utilities for manipulating Postgres containers from Node.

Motivation

Most Node applications persist their state against a database, and it is often a Postgres backend. When performing integration or end-to-end testing, you need an actual postgres instance running alongside your tests. You typically need to perform tests concurrently, and to avoid mutation conflicts, you need separate databases.

A common approach is to have a docker-compose.yml file for tests, and to run docker-compose in a separate shell prior to running the tests, and setup scripts to create multiple databases for your tests.

This works fine, but what if we could instead control this setup programmtically, right from your tests code, such a Jest tests?

Example

const parentConfig = {
  user: "...",
  password: "...",
};
const pgContainer = new PgContainer(parentConfig);
const pgChildrenPool = new PgChildrenPool({ parentConfig });

beforeAll(async () => {
  await pgContainer.create();
});

afterAll(async () => {
  await pgContainer.remove();
});

test("...", async () => {
  await pgChildrenPool.useChild((childConfig) => {
    const client = new pg.Client(childConfig);
    // use client for tests
    await client.end();
  });
});

The library ensures a single postgres container is running, but each child from the pool is a unique, separate database, where you can mutate the schema and the data without impacting other test cases.

Installation

npm i --save-dev pg-container

or

yarn add --dev pg-container

You also need core peer dependencies:

npm i --save-dev dockerode pg

Optionally you may want to use pg-native, pg-promise or any pg-related lib.

API

PgContainer

An instance of PgContainer represents a single Postgres container. It is typically a disposable container that will be recreated/removed each time you run tests.

const parentConfig = {
  host: "localhost",
  port: 5432,
  user: "user",
  password: "password",
  database: "default database",
  connectionTimeousMillis, // optional
};
const pgContainer = new PgContainer(parentConfig);

await pgContainer.find(); // null if container doesn't exist

await pgContainer.remove(); // remove existing container

await pgContainer.create(); // without options, a container with an anonymous data volume with the default "postgres:latest" image will be created
await pgContainer.create({
  image: "postgres:12",
  pgData: join(process.cwd(), "pg-test-data"), // optional local data volume binding
});

PgChildrenPool

An instance of PgChildrenPool represents a pool of "children" databases from a postgres container. Each child created in the pool will have its own, separated database. It is best used in conjunction with PgContainer, although it can also be used in standalone.

const pgChildrenPool = new PgChildrenPool({
  parentConfig,
  childDatabasePrefix: "child", // optional
});

const child = await pgChildrenPool.create(); // anonymous child with a default database name
const child = await pgChildrenPool.create("custom_database_name");
const childClient = new pg.Pool(child); // or e.g. pg-promise
await childClient.query("...");
await childClient.end();
await pgChildrenPool.remove(child); // drop underlying database

// convenient helper that takes care of create/remove within a try/finally block
await pgChildrenPool.useChild((child) => {
  // ...
});

Misc utilities

// Wait until a postgres client successfully connects or a timeout has expired
await waitForInteractive(
  {
    host,
    port,
    user,
    password,
    database,
    connectionTimeoutMillis, // optional
  },
  {
    timeout: 10000, // optional
    retryInterval: 333, // optional
  }, // optional
);

Typescript

This library is written in Typescript and ships it own type definitions.