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

linq2sqlite3

v1.0.0

Published

在Nodejs中,使用Linq语法查询Sqlite3数据库。

Readme

介绍

在Nodejs中,使用Linq语法查询Sqlite3数据库。

此项目是对linq2mysql的移植。

入门

安装

npm install --save linq2sqlite3

使用

var linq = require('linq2sqlite3');
var db = await linq.Open("db.db")

linq.Open方法,一共有三个参数:

  • file - 要打开的数据库文件地址,若为空则打开内存数据库(:memory:)
  • mode - 打开数据库的选项,可选值为:linq.Mode.OPEN_READWRITE/linq.Mode.OPEN_READONLY/linq.Mode.OPEN_CREATE,默认值为:linq.Mode.OPEN_READWRITE|linq.Mode.OPEN_READONLY|linq.Mode.OPEN_CREATE
  • logger - 在执行SQL语句前,调用此回调进行日志记录
  • busyTimeout - 繁忙处理的超时时间,默认值:3000

在通常情况下,我们新打开一个数据库时,需要在初始化表结构,可以在Open成功后,执行DDL用于创建表结构。

查询

where

查询条件使用where方法添加,可以级联多个where条件,多个where方法调用生成的SQL语句用AND进行连接。

where参数可以是lambda表达式,也可以是一个对象。对象各字段条件用AND语句连接,单个key转换为=

关于where的更多内容请参考linq2sql

基础查询

await db.table("users").where(p=>p.age==0).toArray()

带变量的查询

await db.table("users").where(p=>p.age==age,{age:0}).toArray()

只返回第一个数据

await db.table("users").where(p=>p.age==0).first()

统计

await db.table("users").where(p=>p.age==0).count()

分页

await db.table("users").where(p=>p.age==0).skip(1).take(1).toArray()

返回指定字段

await db.table("users").where(p=>p.age==0).select(p=>{p.age,p.id}).toArray()

排序

排序共有四个方法:

  • orderBy - 升序
  • thenBy - 升序
  • orderByDescending - 降序
  • thenByDescending - 降序

连接

支持左连接、右连接、内连接

  • leftJoin
  • rightJoin
  • innerJoin

连接方法返回的对象支持on方法,用于添加连接条件;on方法返回的对象不再具有on方法。

await db.table('users').leftJoin('scores').on((p,q)=>p.id==q.userid).where(p=>p.age>=0).select((p,q)=>{
        p.id,
        p.username,
        p.password,
        p.age,
        q.score
    }).toArray();

在连接查询中,有些时候只需要返回一个表的所有字段,可以使用*来指定,多个表的字段输出,使用,分隔。

await db.table('users').leftJoin('scores').on((p,q)=>p.id==q.userid).where(p=>p["*"]
    }).toArray();

Insert

await db.table('users').insert({
        username:'admin',
        password:'admin888',
        age:39
    })
await db.table('users').insert({
        username:'admin',
        password:'admin888',
        age:39
    },{
        username:'admin',
        password:'chagepwd',
        age:40
    })    
await db.table('users').insert([{
        username:'admin',
        password:'admin888',
        age:39
    },{
        username:'admin',
        password:'admin888',
        age:39
    }])

insert 方法参数可以是一个对象或数组。

当包含第二个参数时,第一个参数只能是一个对象,此时,只能插入一个对象。当第一个参入写入到数据库中发生主键冲突时,使用第二个参数更新冲突的数据库行。 当第二个参数是一个表达式时,第三个参数为表达式的常量。

Update

await db.table("users").where(p=>p.age==0).update({age:10});
await db.table("scores").where(p=>p.userid==1).update(p=>{
        p.score=p.score+1
    });

Delete

await db.table("users").where(p=>p.age==0).delete()

Count

var count=await db.table("scores").where(p=>p.userid==1).count();

Exists

判断指定的查询条件是否在数据库中有数据。

var exists=await db.table("scores").where(p=>p.userid==1).exists();

SqlTable

在查询的时候,可以使用SQL语句做为一个虚拟表。

var items=await db.table(new linq.SqlTable('select * from scores where score>10')).where(p=>p.userid==1).toArray();

更新或插入对象

已废弃,请直接使用insertreplacer参数。

在某些时候,我们需要判断指定查询条件的在数据库中是否有值,在有的时候调用更新语句,没有的时候调用写入语句。

await db.table("scores").where({ id: 1 }).insertOrUpdate({ userid: 1, score: 50 });
await db.table("scores").where({ id: 1 }).insertOrUpdate({ userid: 1, score: 50 }, function (e, m) {
        return {
            entity: { userid: 1, score: 99 },
            mode: "INSERT"
        }
    })

insertOrUpdate方法有两入参数,insertOrUpdate(entity,handler)

  • entity 要插入或更新的对象
  • handler 在更新或插入对象前,对对象数据进行处理,handler有两个参数handler(entity,mode),entityinsertOrUpdate方法传入的数据对象,mode是将进行的操作UPDATEINSERT。返回值是一个对象,有两个属性:entity是要插入或更新的对象,mode是将要进行的操作,可选值同上。

db.table

该方法返回一个Linq实例,只有在调用count,insert,delete,update,first,toArray,exists,insertOrUpdate,才会返回数据,其它方法均返回对象本身。

参数

  • table - 可以是表名称,SqlTable对象和db.table实例。
  • [database] - 指定库名称,默认值:main。

db.execute

执行sql语句,并返回结果。

await db.execute(sql,[values]);

其它

在最终执行的时update,insert,delete语句时,返回一个对象,其中包含lastIDchanges属性。

{ lastID: 11, changes: 1 }