1.0.2 • Published 2 years ago

postgres-node-container v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Postgres node container

Setup a postgres container from NodeJS. Ideal for testing or a throwaway database.

Installation

npm install --save postgres-node-container

Usage

Example usage of setting up a postgres container from NodeJS

import {PostgresNodeContainerService} from "postgres-node-container";

// setup a postgres container with random port and default credentials
const postgresContainerService = new PostgresNodeContainerService();
const postgresContainer = await postgresContainerService.setupPostgresContainer();

// pass options to create a postgres client. Or pass the connection options to your ORM this is just a example usage
const client = new Client({
    user: postgresContainer.postgresUsername,
    host: postgresContainer.postgresHost,
    database: postgresContainer.postgresDatabase,
    password: postgresContainer.postgresPassword,
    port: postgresContainer.postgresPort,
});


// Will stop the container and delete the database content
await postgresContainer.stop();

Or set custom database with custom settings and credentials

const username = 'johnDoe';
const password = 'superSecret';
const database = 'superSecret';
const databaseVersion = '11-alpine';

const postgresContainerService = new PostgresNodeContainerService();
const postgresContainer = await postgresContainerService.setupPostgresContainer(username, password, database, databaseVersion);
const client = new Client({
    user: postgresContainer.postgresUsername,
    host: postgresContainer.postgresHost,
    database: postgresContainer.postgresDatabase,
    password: postgresContainer.postgresPassword,
    port: postgresContainer.postgresPort,
});

await postgresContainer.stop();

You can also get generate connection string from the container

const postgresContainerService = new PostgresNodeContainerService();
const postgresContainer = await postgresContainerService.setupPostgresContainer();
const connectionString = postgresContainer.getPostgresConnectionString();

// connection string for example postgres://postgres:postgres@localhost:32768/postgres

Development

Install dependencies

npm install

Run tests

npm run test

Project roadmap

  • Start postgres container
  • Stop container
  • Get connection details
  • Select postgres version
  • Add tests
  • Add examples
  • Add documentation
  • Fix package installation and usage with examples