# @authkeys/mysql-connman

> Opinionated mysql2 connection manager

Latest version **0.8.2** (published 2021-09-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @authkeys/mysql-connman
pnpm add @authkeys/mysql-connman
yarn add @authkeys/mysql-connman
bun add @authkeys/mysql-connman
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.2 |
| Published | 2021-09-01 |
| First published | 2019-09-28 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 43.7 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Michele Azzolari |
| Maintainers | macno, akteam |
| Keywords | node, mysql, mariadb |

## Links

- npm: https://www.npmjs.com/package/@authkeys/mysql-connman
- Repository: https://github.com/authkeys/node-mysql-connman
- Homepage: https://github.com/authkeys/node-mysql-connman#readme
- Issues: https://github.com/authkeys/node-mysql-connman/issues
- npm.io page: https://npm.io/package/@authkeys/mysql-connman

## Dependencies (12)

- [eslint](https://npm.io/package/eslint.md) ^6.0.1
- [mysql2](https://npm.io/package/mysql2.md) ^2.1.0
- [prettier](https://npm.io/package/prettier.md) ^1.5.3
- [babel-eslint](https://npm.io/package/babel-eslint.md) ^10.0.2
- [eslint-plugin-node](https://npm.io/package/eslint-plugin-node.md) ^9.1.0
- [eslint-plugin-import](https://npm.io/package/eslint-plugin-import.md) ^2.18.1
- [eslint-plugin-promise](https://npm.io/package/eslint-plugin-promise.md) ^4.2.1
- [eslint-config-prettier](https://npm.io/package/eslint-config-prettier.md) ^6.0.0
- [eslint-config-standard](https://npm.io/package/eslint-config-standard.md) ^13.0.1
- [eslint-plugin-prettier](https://npm.io/package/eslint-plugin-prettier.md) ^3.1.0
- [eslint-plugin-standard](https://npm.io/package/eslint-plugin-standard.md) ^4.0.0
- [@fluidware-it/opentracing-injector](https://npm.io/package/@fluidware-it/opentracing-injector.md) ^0.2.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 0.8.2 (latest) — 2021-09-01
- 0.8.1 — 2021-09-01
- 0.8.0 — 2021-06-13
- 0.7.0 — 2021-02-21
- 0.6.2 — 2020-09-11
- 0.6.1 — 2020-09-10
- 0.6.0 — 2020-07-06
- 0.5.0 — 2019-11-07
- 0.5.0-rc.0 — 2019-10-18
- 0.4.0 — 2019-09-29
- 0.3.3 — 2019-09-28
- 0.3.2 — 2019-09-28
- 0.3.1 — 2019-09-28
- 0.3.0 — 2019-09-28
- 0.2.0 — 2019-09-28
- … 1 more at https://npm.io/package/@authkeys/mysql-connman/versions

## README

# mysql-connman

## Opinionated mysql2 connection manager

This package helps you connecting to mysql/mariadb. It uses [mysql2](https://www.npmjs.com/package/mysql2) driver.

It supports pooled connection to single node database and to master/slaves clusters.

### ConnectionManager

### Pooled connection to single node

Connection will be released to pool 

```
import ConnectionManager from '@authkeys/mysql-connman';

const settings = {
  config: {
    host: 'dbhost',
    port: 3306,
    user: 'dbuser',
    password: 'dbpwd',
    database: 'dbname'
  }
};

const connectionManager = new ConnectionManager(settings);

const client = connectionManager.getClient();
[...]
client.close();

```

### NON Pooled connection to single node

Connection will be closed

```
import ConnectionManager from '@authkeys/mysql-connman';

const settings = {
    config: {
        host: 'dbhost',
        port: 3306,
        user: 'dbuser',
        password: 'dbpwd',
        database: 'dbname'
    },
    noDbPool: true
};

const connectionManager = new ConnectionManager(settings);
const client = connectionManager.getClient();
[...]
client.close();

```

### Pooled connection to master/slave cluster

Connection will be released to pool 

```
import ConnectionManager from '@authkeys/mysql-connman';

const settings = {
    config: {
        user: 'dbuser',
        password: 'dbpwd',
        database: 'dbname'
    },
    cluster: {
        rw: [{
            host: 'masternode',
            port: 3306
        }],
        ro: [{
             host: 'slavenode1',
             port: 3306
         }, {
            host: 'slavenode2',
            port: 3306
        }]
    }
};

const connectionManager = new ConnectionManager(settings);

const clientReadWrite = connectionManager.getClient('rw');
const clientReadOnly = connectionManager.getClient('ro');
[...]
clientReadWrite.close();
clientReadOnly.close();
```

### NOT Pooled connection to master/slave cluster

Connection will be closed

```
import ConnectionManager from '@authkeys/mysql-connman';

const settings = {
    config: {
        user: 'dbuser',
        password: 'dbpwd',
        database: 'dbname'
    },
    cluster: {
        rw: [{
            host: 'masternode',
            port: 3306
        }],
        ro: [{
             host: 'slavenode1',
             port: 3306
         }, {
            host: 'slavenode2',
            port: 3306
        }]
    },
    noDbPool: true
};

const connectionManager = new ConnectionManager(settings);

const clientReadWrite = connectionManager.getClient('rw');
const clientReadOnly = connectionManager.getClient('ro');
[...]
clientReadWrite.close();
clientReadOnly.close();
```

### Client 

#### run

`run(sql, params)`
   
Execute a generic sql, return `{ results, fields }` 


```

const runSql = async (sql, params) => {
    const client = connectionManager.getClient();
    await client.open();
    const {results, fields} = await client.run(sql, parms) ;
    client.close();
});

```

#### insert

`insert(sql, params)`  

Insert one row, return insertId

```

const insertRow = async val => {
    const client = connectionManager.getClient();
    await client.open();
    const insertId = await client.insert('insert into tab1 (col1) values (?) ', [val]) ;
    client.close();
});

```

#### update

`update(sql, params)`  

Update row(s), return affectedRows

```

const updateRow = async (id, val) => {
    const client = connectionManager.getClient();
    await client.open();
    const affectedRows = await client.update('update tab1 set col1 = ? where id = ? ', [val, id]) ;
    client.close();
});

```

#### delete

`delete(sql, params)`  

Delete row(s), return affectedRows

```

const deleteRow = async id => {
    const client = connectionManager.getClient();
    await client.open();
    const affectedRows = await client.delete('delete from tab1 where id = ? ', [id]) ;
    client.close();
});

```

#### fetch 1 row

`get(sql, params)`   

Fetch one row

```

const selectRow = async id => {
    const client = connectionManager.getClient();
    await client.open();
    const row = await client.get('select id, col1 from tab1 where id = ? ', [id]) ;
    if(row) {
        const { id, col1 } = row;
        console.log('id: %s, col1: %s', id, col1);
    }
    client.close();
});

```


#### fetch row(s)

`all(sql, params)`   

Fetch row(s)

```

const selectRows = async minId => {
    const client = connectionManager.getClient();
    await client.open();
    const rows = await client.all('select id, col1 from tab1 where id >= ? ', [id]) ;
    rows.forEach(row => {
        const { id, col1 } = row;
        console.log('id: %s, col1: %s', id, col1);
    });
    client.close();
});

```

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