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

gen-sql

v0.0.8

Published

this is a sql generator

Downloads

11

Readme

gen-sql

简介

这是一个使用es6的一些特性实现的超级简单的sql语句生成器。

安装

npm install --save gen-sql

初始化

let DB = require('gen-sql');

let d = new DB();

之后执行对应的语句即可生成对应的sql语句和data数组,对应data数组的位置在sql语句中会用?占位。

增语句

d.add({
  table: 'xxx', // 表名
  data: {
    // 插入的数据,可为数组,如下面的例子
    xxx: 123,
    yyy: 'xxx'
  }
});
d.add({
  table: 'xxx', // 表名
  data: [{
    // 插入的数据,可为数组
    xxx: 123,
    yyy: 'xxx'
  }, {
    xxx: 124,
    yyy: 'yyy'
  }]
});

删语句

d.del({
  table: 'xxx', // 表名
  condition: 'where id = 123' // 条件
});
d.del({
  table: 'xxx', // 表名
  condition: {
    // 条件,值和key之间用=号连接,条件之间使用and连接
    xxx: 'gasgg',
		yyy: 1322456678112
	}
});
d.del({
  table: 'xxx', // 表名
  condition: [
    // 条件,值和key之间用=号连接,条件之间使用and连接,而每个对象之间会用or连接
    {xxx: 'gasgg'},
		{yyy: 1322456678112}
	]
});
d.del({
  table: 'xxx', // 表名
  condition: {
    // 条件可为多值,最终生成in语句
		id: [12, 22, 55, 66]
	}
});
d.del({
  table: 'xxx', // 表名
  condition: {
    // 条件,当key为数字时,只使用值作为条件,条件之间使用and连接
    '0': 'ss < 12',
    '12': 'ss > 1'
  }
});

改语句

d.update({
  table: 'xxx', // 表名
  data: {
    // 要更新的字段
    xxx: 'xxx'
  },
  condition: {}, // 条件,使用方式同删语句,支持字符串和对象
});

查语句

查语句支持的条件较多,参数主要如下:

d.find({
  table: 'xxx', // 表名
  condition: {}, // 条件,使用方式同删语句,支持字符串和对象
  alias: {'xxx': 'a'}, // 表的别名对象,会生成AS语句
  field: {
    // 获取的字段,当不传时返回的是table字段对应表的所有字段
    // 如果传了field,会将field里的map和attr里所出现的字段进行合并
    map: {
      'attr_1': 'attr1' // 需要返回的字段和该字段的别名
    },
    attr: ['attr2', 'attr3'] // 需要返回的字段,不带别名
  },
  // field: 'attr_1 as attr1', // field 字段同样支持传字符串进来
  join: {
    // 需要进行连接的表,可传入数组
    table: 'yyy', // 要连接的表名
    type: 'left', // 连接方式,支持right\inner\full\left四种方式,默认为left
    on: { // 连接条件,值可为字符串,即ON语句,如 'id = name' ,亦可为如下对象
      def: 'id = name', // 会用AND的方式与下面对象连接
      condition: {} // 结构如同上面的condition字段
    }
  },
  group: {
    // 分组查询
    attr: 'attr1', // 分组字段,可传入数组
    having: 'COUNT(*)' // 分组筛选条件
  },
  like: {
    // 搜索查询,可传入数组
    value: 'xxx', // 搜索条件
    type: 'front', // 搜索方式,支持front\back\all,默认为all,即在搜索条件前后追加%
    attr: ['ttt', 'vvv'] // 需要进行搜索的字段
  },
  likeType: 'or', // 当搜索条件为数组时的连接方式,支持and\or,默认为or
  order: {
    // 排序,可传入数组
    attr: 'xxx',
    type: 'asc', // 排序方式,支持desc\asc,默认为asc,即升序排序
  },
  limit: 10, // 分页的单页限制
  offset: 100, // 分页的偏移量
});

另外,find支持union语句,只要直接传入的参数为数组即可:

d.find([{
  table: 'xxx',
  condition: 'id = 123'
}, {
  table: 'yyy',
  condition: 'id < 10'
}])

协议

MIT