# mzbl_node_utils

> public node utils

Latest version **0.0.8** (published 2019-11-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install mzbl_node_utils
pnpm add mzbl_node_utils
yarn add mzbl_node_utils
bun add mzbl_node_utils
```

## 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.0.8 |
| Published | 2019-11-01 |
| First published | 2019-10-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 194.7 KB |
| Known vulnerabilities | 0 (+8 in 5 direct dependencies) |
| Install scripts | no |
| Author | JERRMY.Y |
| Maintainers | jerrmy |
| Keywords | utils |

## Links

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

## Dependencies (8)

- [mocha](https://npm.io/package/mocha.md) ^6.2.1
- [log4js](https://npm.io/package/log4js.md) ^5.3.0
- [marked](https://npm.io/package/marked.md) ^0.7.0
- [moment](https://npm.io/package/moment.md) ^2.24.0
- [request](https://npm.io/package/request.md) ^2.88.0
- [markdown](https://npm.io/package/markdown.md) ^0.5.0
- [alipay-sdk](https://npm.io/package/alipay-sdk.md) ^3.0.8
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1

## Recent versions

- 0.0.8 (latest) — 2019-11-01
- 0.0.7 — 2019-10-31
- 0.0.6 — 2019-10-31
- 0.0.5 — 2019-10-30
- 0.0.4 — 2019-10-25
- 0.0.3 — 2019-10-25
- 0.0.2 — 2019-10-25
- 0.0.1 — 2019-10-25

## README

### node后端通用工具类

---

### 目录

* [如何使用](#如何使用)

* [1.公共类](#公共类)

* [2.数据库查询](#数据库查询)

* [3.集成的modules列表](#集成的modules列表)

---

### 如何使用

> npm install mzbl_node_utils

* 

``` javascript
    var utils = require('mzbl_node_utils');
```

---

### 公共类

1. 如何使用
``` javascript
    var publicMethod = utils.public;
```
2. streamToBuffer(stream) 将stream转为Buffer

3. publicRequest(data) 公共请求方法 返回Promise

    | 参数名 | 类型 | 备注 |
    | ----- | --- | ---- |
    | data.url | string | 请求地址 default '' |
    | data.method | string | 请求方法 |
    | data.header | object | 请求头 default {} |
    | data.body | object | 请求数据 default {} |

---

### 数据库查询

1. 实例化

``` javascript
    var dbMethod = new utils.sql('localhost:9800');
```

2. sql查询

* 方法名 query(data)
* 查询参数

    | 参数名 | 类型 | 备注 |
    | ----- | --- | ---- |
    | data.name | string | 需要查询的数据库名 |
    | data.sql | string | sql语句 |
    | data.params | object | 查询参数 |
    | data.isInsertRecord | boolean | 执行成功是否插入查询记录 |
    | data.programName | string | 执行sql的程序名称(非必填) |

* e.g

``` javascript
    var dbMethod = new utils.sql('localhost:9800');
    var sqlData = {
        name: 'testDb',
        sql: 'select 1=1 as test;',
        params: {},
        isInsertRecord: false,
        programName: 'test',
    }
    dbMethod.query(sqlData)
    .then(msg => { console.log(msg) });
```

---

3. sql配置查询

* 方法名: queryBySqlConfig(data)
* 查询参数

    | 参数名 | 类型 | 备注 |
    | ----- | --- | ---- |
    | data.name | string | sql配置名 |
    | data.program | string | sql程序名 |
    | data.params | boolean | 查询参数 |

* e.g

``` javascript
    var dbMethod = new utils.sql('localhost:9800');
    var sqlData = {
        name: 'testConfig',
        program: 'test',
        params: {},
    }
    dbMethod.queryBySqlConfig(sqlData)
    .then(msg => { console.log(msg) });
```

---

4. sequelize查询

* 方法名: querying(data)
* 查询参数

    | 参数名 | 类型 | 备注 |
    | ----- | --- | ---- |
    | data.dbName | string | 数据库名 |
    | data.tableName | string | 数据表名 |
    | data.method | string | sequelize查询方法 |
    | data.condition | array object | 查询条件 |
    | data.condition.attributes | object | 查询字段 |
    | condition.attributes.fn | string | 函数名 |
    | condition.attributes.col | array | 表字段 |
    | condition.attributes.name | string | 自定义字段名 |
    | condition.attributes.fnArr | array | 函数参数 |
    | condition.attributes.paramPosition | 'front' \| 'last' | 参数位置 |
    | data.condition.where | object | 查询where条件 |
    | data.condition.order | array array | 查询排序 |

* e.g

``` javascript
    var dbMethod = new utils.sql('localhost:9800');
    var sqlData = {
        dbName: 'data_system',
        tableName: 'tbsqlconfig',
        method: 'findAll',
        condition: {
            attributes: [
                'id',
                {
                    fn: 'date_format',
                    col: [`createDate`],
                    name: 'createDate',
                    fnArr: ['%Y-%m-%d %H:%i:%s'],
                    paramPosition: 'last',
                }
            ],
            where: {
                id: 1,
            },
            order: [
                ['id','DESC'],
            ]
        },
    }
    dbMethod.querying(sqlData)
    .then(msg => { console.log(msg) });
```

---

5. redis查询

* 方法名: redis(data)
* 查询参数

    | 参数名 | 类型 | 备注 |
    | ----- | --- | ---- |
    | data.dbName | string | sql配置名 |
    | data.methodName | string | sql程序名 |
    | data.params | array | 查询参数 |

* e.g

``` javascript
    var dbMethod = new utils.sql('localhost:9800');
    var sqlData = {
        dbName: 'defaultDb',
        methodName: 'lrangeAsync',
        params: ['xcfunds_s19e01<|>awardPool<|>sub', 0, -1],
    }
    dbMethod.redis(sqlData);
    .then(msg => { console.log(msg) });
```

---

### 集成的modules列表

1. logger
2. moment
3. jsonwebtoken
4. alipaySdk

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