# pg-cluster

> Simple module for building cluster with postgresql databases for node v >=7 with 1 dependencies(pg). Support replication mode, maintenance mode.

Latest version **1.0.0** (published 2017-05-15) · 0 weekly downloads

## Install

```sh
npm install pg-cluster
pnpm add pg-cluster
yarn add pg-cluster
bun add pg-cluster
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-05-15 |
| First published | 2017-02-20 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.0.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | a.gorelov |
| Maintainers | ekonomizer |
| Keywords | postgresql, npm, pg, cluster, simple, async, await, node, replication, maintenance |

## Links

- npm: https://www.npmjs.com/package/pg-cluster
- npm.io page: https://npm.io/package/pg-cluster

## Dependencies (1)

- [pg](https://npm.io/package/pg.md) 6.1.2

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2017-05-15
- 1.0.7 — 2017-05-05
- 1.0.6 — 2017-02-21
- 1.0.5 — 2017-02-21
- 1.0.4 — 2017-02-21
- 1.0.3 — 2017-02-20
- 1.0.2 — 2017-02-20

## README

# pg-cluster.
## Description.
Simple module for building cluster with postgresql databases for node v >=7 with 1 dependencies(pg). Support replication mode, maintenance mode.

###Install module.
```
npm install pg-cluster
```

## Usage.

#### Cluster is class, and you can extend him by you custom cluster.
#### Define sample config.
```
Define sample config:
2 shards dev0, dev1(in maintenance) - in master mode
2 shards dev2, dev3 - in slave mode

const config = {
    dev0: {
        adapter: "postgresql"
        port: 5432,
        host: "127.0.0.1",
        database: "dev0",
        shard_id: "00",
        username: "postgres",
        password: "pass"
    },
    dev1: {
        adapter: "postgresql"
        port: 5432,
        host: "127.0.0.1",
        database: "dev1",
        shard_id: "01",
        username: "postgres",
        password: "pass",
        maintenance: true
    },

    dev_slave0: {
        adapter: "postgresql"
        port: 5432,
        host: "127.0.0.1",
        database: "dev2",
        shard_id: "00",
        username: "postgres",
        password: "pass",
        slave: true
    },
    dev_slave1: {
        adapter: "postgresql"
        port: 5432,
        host: "127.0.0.1",
        database: "dev3",
        shard_id: "01",
        username: "postgres",
        password: "pass",
        slave: true
    }
};
```

#### Init cluster.
```
const Cluster = require('pg-cluster');
const customLog = console.log;
try {
    const db = new Cluster(config, customLog);
    await db.masterMode(false); // want switch to replications cluster;
    await db.init();
} catch (e) {
    console.log("Some Error", err)
}
```

#### Create model.
```
const onSuccessGetByUserId = function(res, params, cb) {
    cb(res)
}

const onErrorGetByUserId = function(err, params, cb) {
    cb(err)
}

const getByUserId = function() {
    const firstSlaveShard = db.getById("00");
    const userId = "userId";
    return new Promise((cb ,rej)=> {
        firstSlaveShard.sqlQuery(`SELECT * FROM users WHERE user_id=$1;`,[userId])
           .success(onSuccessGetByUserId, cb)
           .error(onErrorGetByUserId, rej)
    })
}
```

#### Call sql request.
```
Call model method:
try {
    const res = await getByUserId()
} catch (e) {
    console.log("Some Error wit db request", err)
}
```

#### Close cluster when shoutdown your server, or reinit cluster.
```
db.close();
```

#### Errors: postgresql can return serious error with codes("08P01", "EPIPE"), if this happened, cluster set shard status on "error", and this shard is locked. You need try reconnect you broken shards.
```
if (db.hasBrokenShards)
    db.reconnectBrokenShards();
```

#### Main methods
```
db.init();
db.close();
db.masterMode();
db.hasBrokenShards();
db.reconnectBrokenShards();
db.randomShard();
db.getById();
db.afterInit();
```

## License MIT

---
_Source: https://npm.io/package/pg-cluster · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
