Giter Club home page Giter Club logo

embedded-postgresql's Introduction

(WIP) embedded-postgresql

A npm package that helps installing and managing an embedded PostgreSQL.

Features

  • ๐Ÿ“ฆ Install and uninstall;
  • ๐Ÿ”„ Initialize, start, stop and get status;
  • โœ๏ธ Update postgresql.conf;
  • ๐Ÿ“˜ Documented;
  • ๐Ÿงช Tested;
  • ๐Ÿ–ฅ๏ธ Supports Linux, Windows and MacOS (thanks to embedded-postgres-binaries);

Install

npm install --save embedded-postgresql

Quick start

Installing an embedded PostgreSQL:

import { checkInstallation, install, uninstall } from 'embedded-postgresql';

async function main() {
    if (!await checkInstallation())
        await install('13.2.0');
}
main();

Creating an instance with user postgres, update its configuration, start and stop:

import { EmbeddedPostgreSQL } from 'embedded-postgresql';

async function main() {
    const embeddedPostgreSQL = new EmbeddedPostgreSQL(testDataPath);

    if (!await embeddedPostgreSQL.isInitialized())
        await embeddedPostgreSQL.initialize();

    await embeddedPostgreSQL.updateConfig({
        wal_level: 'minimal',
        max_worker_processes: 1,
        max_parallel_workers: 1
    });

    await embeddedPostgreSQL.start();

    await embeddedPostgreSQL.stop();
}
main();

Creating an instance with user postgres, password from a file, and connect to it using node-postgres:

import { EmbeddedPostgreSQL } from 'embedded-postgresql';
import pg from 'pg'; // install it with npm install pg

async function main() {
    const password = 'secretpassword';

    await fsExtra.writeFile('./password.txt', password);
    
    if (!await embeddedPostgreSQL.isInitialized())
        await embeddedPostgreSQL.initialize(['-U', 'postgres', '-A', 'md5', '--pwfile', './password.txt']);

    await fsExtra.remove('./password.txt');

    await embeddedPostgreSQL.start();

    const client = new pg.Client({
        host: 'localhost',
        port: 5432,
        user: 'postgres',
        password: password,
        database: 'postgres'
    });

    await client.connect();
    const res = await client.query('SELECT $1::text as message', ['Hello world!']);
    
    await client.end();
}
main();

Enabling log:

import { EmbeddedPostgreSQL, logger } from 'embedded-postgresql';

if (process.env.NODE_ENV == 'development') {
    logger.setSettings({
        minLevel: 'debug',
        suppressStdOutput: false
    });
}

async function main() {
    const embeddedPostgreSQL = new EmbeddedPostgreSQL(testDataPath);

    await embeddedPostgreSQL.start();

    await embeddedPostgreSQL.stop();
}
main();

For more examples check the tests.

Contributing

Make sure that you:

  1. Created tests for your changes;
  2. Run npm run contribution:check and all tasks passed;

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.