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 🙏

© 2026 – Pkg Stats / Ryan Hefner

linq2sql

v1.0.2

Published

Linq语法基础库,用于支持数据库操作。

Readme

项目介绍

Linq语法基础库,用于支持数据库操作。

快速开始

安装

npm install --save linq2sql acorn

使用

var expression=require("linq2sql"),acorn = require("acorn");
function getEntry(expressionTree) {
    var ast = acorn.parse(expressionTree.toString());
    var statement = ast.body[0];
    if (statement.type != "ExpressionStatement") {
        throw new Error("not support ExpressionStatement");
    }
    statement = statement.expression;
    if (statement.type != "ArrowFunctionExpression") {
        throw new Error("not support ArrowFunctionExpression");
    }
    var params = statement.params;
    var body = statement.body;
    return {
        body: body,
        params: params
    }
}
var lambda=p=>p.x=1;
var data=getEntry(lambda);
var sqlTree = expression(data.body, data.params, {}, [{ value: 'table', parent: { value: 'db' } }]);

由此,我们可以得到一个SQL表达式对象;根据SQL表达式对象,我们可以解析为不同的数据库支持语法。

接口介绍

expression

表达式解析入口,仅接收ArrowFunctionExpression类型的表达式,参数入下:

  • body ArrowFunctionExpression类型的表达式。
  • params Array,表达式入参列表,与数据库表一一对应,该值来源于lambda表达式的函数入参列表。
  • consts Object,表达式中所使用的外部变量。
  • dbinfo Array,数据库信息,与参数params的顺序一一对应。

参数详细说明

body

用户传入的原始表达式一般为p=>p.x==1,解析后的原始表达式不能直接做为参数传入,需再次分析后,取ArrowFunctionExpression类型的表达式,可参考开始章节示例。

支持的表达式类型请参考测试文件

params

表达式的入参列表,参考开始章节示例。

consts

表达式中所使用的外部变量列表,如表达式p=>p.x+y==1,其中的y需要通过consts传入。 参数示例:

{
    y:2
}

dbinfo

数据库信息。表达式中的入参需要解析为数据库字段信息,此参数可传入数据库的完整信息,多个参数表示多个数据库支持。也可支持数据库别名。

通过级联的方式支持表与库的信息。示例:

{ value: 'table', parent: { value: 'db' } }    

开始层级为数据库表一级,通过parent属性指定上一级的信息。

expression.formater

数据库转义函数。默认为mysql支持的格式:

expression.formater=function(field){
    return "`" + field + "`";  
}

expression.entity

把一个Object对象换为SQL表达式对象示,一般用于insert或update语句,特殊处理后也可用于where语句。

参数列表:

  • obj Object需要转换的对象。
  • dbinfo Object数据库对象,参考上一章节中数据库对象的描述。

obj中的key会和数据库的信息进行整合,组合成一个完成的字段信息。

示例:

expression.entity({a:1},{ value: 'table', parent: { value: 'db' } }  )

返回值示例:

[{
    left:{type:"field",value:"`db`.`table`.`a`"},
    right:{type:"const",value:1},
    operator:"set"
}]

expression.spliter

数据库分隔符,默认为.

返回值描述

返回一个数组或者一个Object对象,数组中的单个元素含义与单个Object相同。

Object对象共分两总:

  1. 包含属性left,right,operator
  2. 包含属性type,value

情况一

一般是一个完整的条件,如a=1,其中left=a,operator='=',right=1。left和right的具体内容可根据情况二进行解析。

支持的操作符列表: +,-,*,/,%,like,in,set,>,<,>=,<=,=,or,and,not

set操作符表示赋值语句,一般用于update的语句中。

当为not操作符时,left值为null

情况二

根据type确定value内容的含义。type值如下:

|type值 |含义 | |----|----| |field|数据库字段| |const|常量值,一般转义后写入数据库,防止SQL注入|

参考资料

辅助工具