# @mac-xiang/mysql-convert

> 由特定格式下的json转为mysql语句

Latest version **1.0.1-11** (published 2021-03-09) · AGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @mac-xiang/mysql-convert
pnpm add @mac-xiang/mysql-convert
yarn add @mac-xiang/mysql-convert
bun add @mac-xiang/mysql-convert
```

## 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.1-11 |
| Published | 2021-03-09 |
| First published | 2021-02-11 |
| Weekly downloads | 0 |
| License | AGPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 80.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tmkmk |
| Maintainers | tmkmk |
| Keywords | convert, typescript, mysql |

## Links

- npm: https://www.npmjs.com/package/@mac-xiang/mysql-convert
- Repository: https://gitee.com/tmkmk/mysql-convert
- npm.io page: https://npm.io/package/@mac-xiang/mysql-convert

## Dependencies (1)

- [@mac-xiang/method](https://npm.io/package/@mac-xiang/method.md) ^1.0.2-3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1-11 (latest) — 2021-03-09
- 1.0.1-10 — 2021-03-04
- 1.0.1-9 — 2021-03-04
- 1.0.1-8 — 2021-03-01
- 1.0.1-7 — 2021-02-20
- 1.0.1-6 — 2021-02-20
- 1.0.1-5 — 2021-02-20
- 1.0.1-4 — 2021-02-19
- 1.0.1-3 — 2021-02-19
- 1.0.1-2 — 2021-02-19
- 1.0.1-1 — 2021-02-18
- 1.0.1-0 — 2021-02-18
- 1.0.0 — 2021-02-11

## README

# mysql-convert

#### 介绍

由特定格式下的 json 转为 mysql 语句

#### 软件架构

```
├─dist                ts编译后存放路径,其结构基本与src一致,没啥好看的
├─src                 源码目录
|─test                测试开发目录
|  |─dev.ts           测试用代码很少一看就懂
│  └─其他文件          没啥说的.偶尔测试会用
```

#### 安装教程

npm i tmk-pay

#### 使用说明

1.  npm i @mac-xiang/mysql-convert
2.  import {Literal, TmkMysql} from("tmk-mysql-convert");
3.  const tmkMysqlConvert = new TmkMysql(param);

param 参数介绍

      备注:凡是没有默认值的,都为必填项

| 属性名    | 数据类型  | 简介                                                                       |
| --------- | --------- | -------------------------------------------------------------------------- |
| field     | typeField | 数据表结构,详情参考下方 typeField 结构说明                                 |
| table     | string    | 数据库表名                                                                 |
| limit     | int       | 在查找数据时限制所得数据的数目条数,避免数据过多卡机.默认:100               |
| offset    | int       | 查找数据时,起始偏移数.默认:0                                               |
| auto      | string[]  | 数据库中自动赋值字段(不允许插入值中含有此字段).默认:["id"]                 |
| read      | string[]  | 数据库中的只读字段.默认:与 auto 字段相等                                   |
| showField | string[]  | 在查询数据库时,如果未指定所需展示的字段名时,所包含的字段名.默认:field 全部 |

```
  [propName: string]: [number, any?, any?];
```

typeField 数据类型:

    例:{
      字段名1:[数据最小值,数据最大值,是否整数], // 如果成员为3,则代表此字段为数值型,如果第三参数为非 0/空,则表示整数,否则为float
      字段名2:[数据长度,超出长度后截取方式] // 如果成员为2,则表示此字段为文本型.数据长度则为此数据最大长度,超出长度后根据 超出长度截取方式进行截断数据. if(超出长度后截取方式) 最终值为取前 数据长度 数据.else 取数据尾部 长度为数据长度的值.
    }

步骤 3 中所得 tmkMysqlConvert;
const sql=tmkMysqlConvert.sql(object); // sql 就是最终 sql 语句;
object 成员取值如下:

#### method:方法,取值范围如下; 默认:0

| 值                      | 作用      |
| ----------------------- | --------- |
| 0 \| get \| r \| read   | 查找      |
| 1 \| up \| u \| update  | 修改      |
| 2 \| add \| c \| create | 新增/插入 |
| 3 \| del \| d \| delete | 删除      |

#### table:如果使用 new Literal("string") 则值完全等于 string.否则取之前新建对象传入的数据表名.

#### where: 条件语句,为数组,其成员数据如下:

    注:凡是没有默认值则为必填项.否则此条数据作废.

| 成员名 | 数据类型                                                                  | 默认值        | 说明                                                                                                                                                                                                                                                                                                                                                                  |
| ------ | ------------------------------------------------------------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| field  | string                                                                    | 无            | 数据库字段名                                                                                                                                                                                                                                                                                                                                                          |
| logic  | "and" \| "or"                                                             | 非"or"为"and" | 与前一个条件的逻辑关系,首条件强制为 "and"                                                                                                                                                                                                                                                                                                                             |
| method | ">" \| "<" \| ">=" \| "<=" \| "!=" \| "in" \| "nin" \| "regexp" \| "like" | "="           | mysql 的逻辑运算符,如果 值为 in/nin 则判断 value 是否为数组,如果不为数组,则放弃此条件,如果为数组,则根据字段值,进行每个元素的取值,对字段为数值类型非常严格,如果成员不能转化为数值类型,则放弃此值.文本型则进行 JSON 编码.并保证 mysql 识别为字符串编码.如果为 regexp,则 value 必须是 string,并且将其复制为正则表达式.如果为 like.则将 value 转为字符串,并在其前后增加 % |
| value  | string \| number \| (string \| number)[] \| Literal                       | 无            | 如果数据类型为 Literal 则 method 失效.并不做后面判断. 如果字段为数值类型,则判断此值是否为所设字段区间内(大于等于最小值,小于等于最大值),并且根据是否取整进行取整操作,符合条件则取值,否则作废. 如果字段为非数值类型,则将其 JSON 格式化为字符串,并转译其内容,保证内容在 mysql 语句中为字符串.                                                                            |

#### value: 在改/增时有效.

    修改/新增 对应数据.属性名就是字段名,值为当前所设值.优先判定属性名是否在新建实例时所设field内,不在其内则放弃.对于非数值类型 则先 JSON 数据,再进行 mysql 特殊符号转码.如果是文本型数据,则判断是否超出字段允许长度.如果超出,则根据规则进行截取字符串的 前/后 限定长度数据. 如果方法为新增数据则:当做完判定后,在根据最终得到的数据进行字段判定.如果缺少某一字段.则根据字段类型进行填充,数值类型填充为0,文本型填充为空文本"".

#### limit:int 默认:创建对象时设定的值 数据长度限制.查找/修改 数据时有效

#### offset:int 默认:创建对象时设定的值 数据起始偏移.查找/修改 数据时有效

#### other: Literal, 默认:空. 处于安全考虑 此值必须是 Literal 类型.

    此值只能由后端手动添加. 这里的值将附加在 mysql语句中 limit前面.用于自定义操作.例如添加排序语句等.

#### 参与贡献

就我一个,但使用了 n 多包.

#### 特技

由前端进行 mysql 语句规则创建,发送 json 到后端.然后后端解析 json 为 object 后,将值传入 sql 方法 得到返回值就是 sql 语句了.安全防注入.并且规则几乎能满足大多数需求了.甚至可以达到多表联查的功能,当然,非标准方法得用到 Literal,而 Literal 只能由 new Literal("xxx")进行创建.

npm 上传包命令
npm publish --registry http://registry.npmjs.org
npm config set registry https://registry.npmjs.org
npm publish --access public --registry https://registry.npmjs.org
npm config set scope mac-xiang --global

npm version prerelease

npm version prepatch
别笑,记不住

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