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

sojs-mysql

v0.1.6

Published

A simple mysql database utility base on [sojs](https://github.com/zhangziqiu/sojs)

Downloads

1

Readme

sojs-mysql

A simple mysql database utility base on sojs

基于sojs的mysql工具库, 更方便可读地生成SQL语句, 减少手动拼接SQL的成本

Install 安装

npm install sojs-mysql

Document 文档

DB.table(tableName) 返回的是一个Query实例, Query实例的大部分方法(不包括execute)都返回自身实例(this)

execute方法返回一个Promise(sojs.promise)

具体看以下例子

Usage 基本使用

require('sojs');

var options = {
    connection: {
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: '123456',
        database: 'test'
    }
};
var DB = sojs.create('sojs.mysql.db', options);

// SELECT例子
// select('string')用于更方便地使用sum(),count()等函数
// 或者 select(['字段1', '字段2']), 默认为*

DB.table('user')
// .select(['name', 'id'])
// .select('count(name)')
.select(['name', 'count(*)'])
.where('name__like', '%rjy%')
.where('age__gt', 10)
.where('school', 'xd')
.orWhere('sex', 'man')
.orderBy('age', 'desc')
.limit(5)
.get().execute()
.then(function (res) {
    console.log(res);
})
.catch(function (err) {
    console.log(err);
});

// WHERE, 借鉴Django ORM的语法, 列名 + __like, __lte, __lt, __gt, __gte, __range, __in, __eq, __neq, __isnull 作为第一个参数, 第二个参数为值

queryInstance.where('column__like', 'abc') // column like 'abc'
queryInstance.where('column0', 1).orWhere('column1', 2) // column = 1 or column1 = 2
queryInstance.notWhere('column', 'xyz') // column != 'xyz'

// where组使用回调函数来嵌套条件,一个回调函数相当于给这些条件加上 (  )

console.log(queryInstance
.table('user')
.select(['name'])
.where('a', 1)
.where(function (q) {
    q.where('b', 2);
    q.orWhere('c', 3);
    q.where(function (q1) {
        q1.where('e', 5);
        q1.where('f', 6);
    })
})
.where('d', 4)
.statement);

// 生成语句:SELECT `name` FROM user  WHERE `a` = 1 AND (`b` = 2 OR `c` = 3 AND (`e` = 5 AND `f` = 6)) AND `d` = 4

// ORDER BY

queryInstance.orderBy('column', 'asc');
queryInstance.orderBy(['column1', 'asc'], ['column2', 'desc']);

// LIMIT, 注意limit要在order by之后写

queryInstance.limit(offset, num);
queryInstance.limit(num);

// JOIN

queryInstance.join('table1', 'table0.id', 'table1.t_id', 'INNER') // inner join on table0.id = table1.t_id

// GROUP BY && HAVING

queryInstance.groupBy('column').having('column', '>', 5) // group by column having column > 5

// INSERT

DB.table('user').insert({name: 'ranjiayu2'}).execute()
.then(function (res) {
    console.log(res);
})
.catch(function (err) {
    console.log(err);
});

// UPDATE

DB.table('user').where('name', 'ranjiayu2').update({name: 'ranjiayu3'}).execute()
.then(function (res) {
    console.log(res);
})
.catch(function (err) {
    console.log(err);
});

// DELETE
DB.table('user').where('name__in', ['t5', 't6', 't7']).delete().execute()
.then(function (res) {
    console.log(res);
})
.catch(function (err) {
    console.log(err);
})

// TRANSACTION 事务

// 先实例多个Query实例
var batchInsert = [];
batchInsert.push(DB.table('user').insert({name: 't5'}));
batchInsert.push(DB.table('user').where('name', 't5').delete());
batchInsert.push(DB.table('user').insert({name: 't6'}));

// 执行, 第二个参数false表示不是顺序执行, true表示顺序执行
DB.transactions(batchInsert, false)
.then(function (res) {
    console.log(res);
}).catch(function (err) {
    console.log(err);
});

// EXECUTE 方法, 之前的.get(), .insert() 等方法, 都是构造出一个SQL, 返回的是this(query实例自身)
// 而 execute() 方法是真正地执行该语句, 并返回Promise对象(sojs.promise)

// 推荐使用 Async Await 语法

async function getResult () {
    let result = await DB.table('user')
    .select(['age', 'count(*)'])
    .groupBy('age')
    .get().execute();
    console.log(result);
}