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

m-query-builder

v2.0.0

Published

A SQL query builder.

Downloads

6

Readme

query-builder

A SQL query builder.

use

import Builder from 'm-query-builder'

const build = new Builder('t_users', 'u')
  .select(['id', 'name', 'sex'])
  .where({
    id: 1001
  })

console.log(buid.toSql()) // select `id`,`name`,`sex` from `t_users` as `u` where `id` = 1001

API

const build = new Builder(tableName [, aliasName]) // 创建一个sql构建;注:toSql后,对象不能重复使用
  • Builder.prototype.select([attrs]) 指定需要查询字段,默认 ['*']
build.select() // `*`
build.select(['u.userId', 'id', 'name']) // `u`.`userId`,`id`,`name`
  • Builder.prototype.insert(data: Array<{ [key: string]: any }>) 插入多条数据
build.insert([
  { name: 'zhangsan', age: 24 },
  { name: 'lisi', age: 23 }
]) // insert into table(`name`, `age`) values('zhangsan', 24),('lisi', 23)
  • Builder.prototype.create(data: { [key: string]: any }) 插入一条数据
build.create({ name: 'zhangsan', age: 24 }) // insert into table(`name`, `age`) values('zhangsan', 24)
  • Builder.prototype.update(data: { [key: string]: any }) 更新数据,条件需要连缀 where 方法增加条件

  • Builder.prototype.delete() 删除数据,条件需要连缀 where 方法

  • Builder.prototype.where(conditions: conditionType) 条件

  • Builder.prototype.having(conditions: conditionType) having 条件

conditionType 定义如下

type conditionType = {
  [key: string]: any,
  $and?: { [key: string]: any }[] | { [key: string]: any },
  $or?: { [key: string]: any }[] | { [key: string]: any },
  $raw?: string
}
build.where({ status: 1, age: { $in: [18, 20, 25] } }) // where `status` = 1 and age in (18,20,25)

build.where({
  $and: [
    { age: { $gte: 18 } },
    { age: { $lte: 30 } }
  ]
}) // where `age` >= 18 and `age` <= 30
  • Builder.prototype.order(fields: { [key: string]: orderType | Array<[string, orderType]) 排序
build.order({ createdAt: 'desc', score: 'asc' }) // order by `createdAt` desc, `asc` asc
build.order([ ['createdAt', 'desc'], [ 'score', 'asc' ] ]) // order by `createdAt` desc, `asc` asc
  • Builder.prototype.join(table: string | Builder, opt?: joinOptionType) 连表查询,支持多次调用
  • Builder.prototype.joins(arr: joinsOptionType[]) 连表查询,参数为数组
interface joinsOptionType {
  table: string | Builder,
  as?: string,
  direction?: 'left' | 'right' | 'inner',
  on?: conditionType
}

interface joinOptionType {
  as?: string,
  direction?: 'left' | 'right' | 'inner',
  on?: conditionType
}
build.join('t_orders', {
  as: 'o',
  direction: 'left',
  on: { 'o.userId': { $id: 'u.id' } }
}) // left join `t_orders` as o on `o`.`userId` = `u`.`id`
  • Builder.prototype.limit(offset: number = 1, limit: number) 取部分
  • Builder.prototype.toSql(alias?: string) 生成sql

操作符

$gt: 10 // > 10
$gte: 10 // >= 10
$lt: 10 // < 10
$lte: 10 // <= 10
$eq: 10 // = 10
$neq: 10 // != 10
$between: [18, 30] // between 18 and 30 
$notBetween: [18, 30] // not between 18 and 30 
$in: [1, 3, 5, 7] // in(1,3,5,7)
$notIn: [1, 3, 5, 7] // not in(1,3,5,7)
$like: 'ab%c' // like 'ab%c'
$notLike: 'ab%c' // not like 'ab%c'
$raw: 'is not null' // is not null
$id: 'u.userId' // = `u`.`userId`