# chestnut-utils

> util for chestnut app

Latest version **0.2.17** (published 2024-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install chestnut-utils
pnpm add chestnut-utils
yarn add chestnut-utils
bun add chestnut-utils
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

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

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.17 |
| Published | 2024-12-17 |
| First published | 2017-08-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 68.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nandy007 |
| Maintainers | nandy007 |
| Keywords | chestnut-app, util |

## Links

- npm: https://www.npmjs.com/package/chestnut-utils
- Repository: https://github.com/nandy007/chestnut-utils
- Homepage: https://github.com/nandy007/chestnut-utils#readme
- Issues: https://github.com/nandy007/chestnut-utils/issues
- npm.io page: https://npm.io/package/chestnut-utils

## Dependencies (7)

- [pg](https://npm.io/package/pg.md) ^7.0.1
- [mssql](https://npm.io/package/mssql.md) ^4.1.0
- [mysql](https://npm.io/package/mysql.md) ^2.13.0
- [amqplib](https://npm.io/package/amqplib.md) ^0.5.5
- [cheerio](https://npm.io/package/cheerio.md) 1.0.0-rc.2
- [request](https://npm.io/package/request.md) ^2.81.0
- [javascript-obfuscator](https://npm.io/package/javascript-obfuscator.md) ^0.27.3

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 0.2.17 (latest) — 2024-12-17
- 0.2.16 — 2020-06-08
- 0.2.15 — 2020-01-07
- 0.2.14 — 2019-06-17
- 0.2.13 — 2019-06-17
- 0.2.12 — 2018-12-28
- 0.2.11 — 2018-12-21
- 0.2.10 — 2018-12-20
- 0.2.9 — 2018-12-17
- 0.2.8 — 2018-12-14
- 0.2.7 — 2018-12-02
- 0.2.6 — 2018-07-26
- 0.2.5 — 2018-07-20
- 0.2.4 — 2018-07-19
- 0.2.3 — 2018-01-22
- … 11 more at https://npm.io/package/chestnut-utils/versions

## README

# chestnut-utils KOA2工具类


配合[chestnut-app](https://github.com/nandy007/chestnut-app)使用，也可单独在Koa2中使用

特别注意：

由于oracle使用环境复杂，依赖包未添加到工程中，如需使用oracle数据库请单独安装依赖包

具体请参考[https://oracle.github.io/node-oracledb/INSTALL.html#quickstart](https://oracle.github.io/node-oracledb/INSTALL.html#quickstart)

配置后请执行

```bash
npm install oracledb
```

## 提供的方法

### 数据库操作类

require('chestnut-utils').db

```javascript

// 首次调用初始化，初始化mysql
const dbmysql = require('chestnut-utils').db({
	id: 'main',
    type: 'mysql',
    database: 'test',
    user: 'root',
    password: 'root',
    port: '3306',
    host: 'localhost'
});

// 初始化pgsql
const dbpg = require('chestnut-utils').db({
    id: 'pg',
    type: "pgsql",
    host: "localhost",
    port: "5432",
    database: "test",
    user: "postgres",
    password: "postgres"
  });


// 后续再次调用可只传id
const db = require('chestnut-utils').db('main');


```
db类提供query、find、insert、update、delete函数进行操作，这几个函数均返回promise对象。



**query(sql, values)**

其中：

sql为可带?的sql语句，比如:select * from table where username=? and age=?

values为sql语句中?对应的数组参数，比如:['nandy007', '18']

此方法返回一个promise对象

示例：

```javascript

const db = require('chestnut-utils').db('main');
const rs = await db.query('select activity_id, activity_name from tbl_activity_main where activity_id = ? AND activity_name = ? ', ['4','444']

```


**find(table, keys, clause, values)**

其中：

table为查找的表名

keys为要查找的字段

clause为条件语句，可包含?

values为条件语句中?对应的参数

```javascript

const db = require('chestnut-utils').db('main');

// 序列化为 select id,title from article_info where id=1 and title='2'
let rs = await db.find('article_info',['id','title'],{id:1, title:'2'});

// 序列化为 select activity_id,activity_name from tbl_activity_main where activity_id='4' and activity_name='444'
rs = await db.find('tbl_activity_main', ['activity_id', 'activity_name'], '  where ? ', [{activity_id:'4',activity_name:'444'}]);


```

**insert(table, model)**

其中：

table为插入的表名

model为插入的数据对象

```javascript

const db = require('chestnut-utils').db('main');

// 序列化为 insert into tbl_activity_main (activity_id, activity_name) values ('4', '444')
let rs = await db.insert('tbl_activity_main', {activity_id:'4', activity_name:'444'});


```

**update(table, clause, values)**

其中：

table为更新的表名

clause为更新的条件，可带?

values为条件需要的对应参数


```javascript

const db = require('chestnut-utils').db('main');

// 序列化为 update tbl_activity_main set activity_name='333' where activity_id='3'
let rs = await db.update('tbl_activity_main',' ? where ? ', [{activity_name:'333'}, {activity_id:'3'}]);


```


**delete(table, clause, values)**

其中：

table为删除的表名

clause为删除的条件，可带?

values为条件需要的参数

```javascript

const db = require('chestnut-utils').db('main');

// 序列化为 delete from tbl_activity_main where activity_id=2 OR activity_id=4
let rs = await db.delete('tbl_activity_main', ' where ? activity_id=? OR activity_id=?', [null,2,4]);


```

### 页面抓取类

require('chestnut-utils').fetch

此类基于[https://github.com/request/request](https://github.com/request/request)

**fetch (url, opts)**

其中：

url为要请求的url

opts为请求参数，可参考https://github.com/request/request中的options，同时添加了两个属性：

*ctx* 为koa的ctx，用户标识当前请求的会话，相同的会话发起的fetch请求的cookie是维持的

*requestId* 默认为请求的url的host部分，可以自己设置（某些url使用会话共享，不同host的会话一致，可以给它们设置相同的requestId解决）

*cookies* 为请求设置自定义cookie，形式可以是数组和对象。
    数组形如：cookies: [cookie1, cookie2]  // cookie值为字符串形如cookieName=cookieValue
    对象形如：cookies: {cookieName1: cookieValue1, cookieName2: cookieValue2}

```javascript
exports.index = async (ctx) => {
	const fetch = require('chestnut-utils').fetch;
	let rs = await fetch('https://auth.exmobi.cn/login?output=json', {
      	ctx: ctx,
      	requestId: 'exmobi',
      	method: 'post',
      	form: {username:'huangnan', password:'111111'},
      	headers: {
        	'Content-Type': 'application/x-www-form-urlencoded'
      	}
    });
	console.log(rs.body);
}

```


**fetch.clear(sids)**

用于根据会话id删除缓存,sids为一个数组，其元素为会话id


### html页面拣选类

require('chestnut-utils').jqlite

此类基于cheerio实现，语法类似jquery

用法：

```javascript

const html = '<div><ul id="ul1"><li>1</li><li>2</li><li>3</li></ul><ul id="ul2"><li>4</li><li>5</li><li>6</li></ul></div>';

const jqlite = require('chestnut-utils').jqlite;
const $ = jqlite(html);

const lis = $('#ul2 li');

```


### 编解码类

require('chestnut-utils').codec

此类封装了基于crypto模块的一些编解码功能

**aesCipher(str, key)**

aes编码。

```javascript

const codec = require('chestnut-utils').codec;

const ret = codec.aesCipher('这是明文内容', 'a password');// 将明文信息进行aes编码

```

**aesDecipher(str, key)**

aes解码。

```javascript

const codec = require('chestnut-utils').codec;

const ret = codec.aesDecipher('这是aes加密内容', 'a password');// 将加密信息解码，得到明文

```

**md5(str)**

md5编码。

```javascript

const codec = require('chestnut-utils').codec;

const ret = codec.md5('这是明文内容');// 将明文信息进行md5编码

```

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