npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@axolo/leosql

v0.2.3

Published

Generate MySQL from qs.parse(Querystring).

Downloads

39

Readme

LeoSQL

HTTPquerystringbody转换为存储引擎(比如MySQL)能够理解和执行的格式, 同时确保存储安全,不被恶意注入攻击,是一项不小的挑战。常规的检索数据方法有:

  • 条件(WHERE
    • 等于(_eq
    • 不等于(_ne
    • 大于(_gt
    • 小于(_lt
    • 大于等于(_gte
    • 小于等于(_lte
    • 含有(_have
    • 开头含有(_start
    • 结尾含有(_end
  • 关系(_logic
    • 且(AND
    • 或(OR
  • 排序(ORDER BY
    • 顺序(_asc
    • 逆序(_desc
  • 分页(LIMIT
    • 页码(_page
    • 单页记录数(_limit
  • 全文(FULLTEXT
    • 含有(_q

| 请求 | 类型 | 值 | | --------- | ------------ | -------------------------------------------------- | | _method | 字符串或数组 | 方法,尽量由程序生成 | | _table | 字符串或数组 | 表,尽量由程序生成 | | _column | 字符串或数组 | 列,尽量由程序生成 | | _value | 字符串或数组 | 值,用于匹配INSERTUPDATE时的_column | | *_eq | 字符串或数组 | 等于,* 表示字段名 | | *_ne | 字符串或数组 | 不等于,* 表示字段名 | | *_gt | 字符串 | 大于,* 表示字段名 | | *_lt | 字符串 | 小于,* 表示字段名 | | *_gte | 字符串 | 大于等于(不小于),* 表示字段名 | | *_lte | 字符串 | 小于等于(不大于),* 表示字段名 | | *_have | 字符串 | 含有,* 表示字段名 | | *_start | 字符串 | 开头含有,* 表示字段名 | | *_end | 字符串 | 结尾含有,* 表示字段名 | | _logic | 字符串或数组 | 条件之间的逻辑关系,默认为AND。 | | _asc | 字符串或数组 | 顺序 | | _desc | 字符串或数组 | 逆序 | | _page | 正整数 | 分页的页码 | | _limit | 正整数 | 分页的单页记录数 | | _q | 字符串 | 全文检索,对表进行全文搜索,建议独立使用(未实现) |

其中_logicstringarray两种类型,类型不同解析方法也不同,具体如下:

  • string

    统一为各个条件匹配指定的关系。比如_logic=or,则所有条件之间的关系均为OR

  • array

    _logic数组顺序逐一为各个条件匹配关系,条件结束匹配完成,数组超出的部分直接丢弃。 数组长度不足时,后续未匹配到的条件关系均默认为AND

安装

npm install @axolo/leosql

方法

参见LeoSQL文档

示例

const qs = require('qs')
const sqlFormatter = require('sql-formatter')
const leosql = require('../src')

const query = `_table=user\
&_column=id&_column=name&_column=mail\
&_value=ID&_value=NAME&_value=MAIL\
&spawned_gte=20190101&spawned_lte=20190105\
&name=guest&name_ne=admin&name_ne=root\
&destroied_eq=true&destroied_eq=false\
&mail_end=%40mail.com\
&_logic=and&_logic=and&_logic=or\
&_desc=spawned&_desc=modified&_asc=mail\
&_limit=20&_page=3`

const leo = leosql(qs.parse(query))

console.log(sqlFormatter.format(leo.select))

相当于生成如下 MySQL 语句

SELECT
  `id`,
  `name`,
  `mail`
FROM
  `user`
WHERE
  (
    `name` IN ('guest')
    OR `name` NOT IN ('admin', 'root')
  )
  AND `spawned` >= '20190101'
  AND `spawned` <= '20190105'
  AND `destroied` IN ('true', 'false')
  AND `mail` like '%@mail.com'
ORDER BY
  `mail` ASC,
  `spawned` DESC,
  `modified` DESC
LIMIT
  40, 20

测试

yarn test

版本

TODO

  • 条件分组,考虑以括号分组条件,更加贴近SQL,避免产生错误。
  • 排序先后顺序,考虑排序的先后顺序。

0.2.3

精简NPM发布内容。

0.2.2

组合处理逻辑关系OR。

0.2.1

调整版本号。

0.2.0

方法改为属性。

0.1.3

接受无需转义的请求。

0.1.2

更新引入方式,符合用户习惯。

0.1.1

更新LIMIT逻辑,避免LIMIT 0, 0

0.1.0

重构,且不兼容0.0.x

0.0.x

历史版本,不再维护。