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

async-mysql-wrapper

v1.0.0

Published

An async mysql wrapper that provide basic ORM.

Downloads

2

Readme

xpmysql

这是一个适用于NodeJs的MySQL操作类库

特点

支持两种格式的SQL语句占位符

两种占位符 : 和 ?, 其中?方式是mysql库自带的。

? 案例
    sql: `select * from ?? where id=?`
    values: ['user', 1]

: 案例
    sql: `select * from ::table where id=:id` 
    values: {table:'user', id: 1}}

支持字段映射


// 由于mysql数据表的字段名可能是带下划线的风格,例如:user_id
// 但实际情况可能是需要驼峰格式的,例如: userId
// 本库在编写Repository实现类的时候,可以在构造函数中指定字段映射关系

class UserRepository extends Repository {
    
    constructor() {
        // 第1个参数是:表名
        // 第2个参数是:主键名
        // 第3个参数是:字段映射
        super("user", "id", {"id": "_id", "name": "_name"});
    }
}

使用案例

根据主键ID, 查询一条记录

async function selectOneById() {
    let res = await repository.selectOneById(1);
    assert.ok(res != undefined, "获取一条记录失败");
}

查询一条记录

async function selectOne() {
    let res = await repository.selectOne("select * from user where id=:id", {id: 1});
    assert.ok(res != undefined, "获取一条记录失败");
}

查询列表

async function selectList() {
    let res = await repository.selectList("select * from user where name=:name", {name: "jiangyy"});
    console.log(res);
    assert.ok(res.length !== 0, "获取列表失败");

    let res2 = await repository.selectList("select * from user where name=:name limit :start,:limit", {
        name: "jiangyy",
        start: 0,
        limit: 1
    });
    console.log(res2)
    assert.ok(res2.length !== 0, "获取分页列表失败");
}

插入

async function insert() {
    let id = await repository.insert("insert into user(name, age) values(:name,:age)", {
        name: "张三",
        age: 30
    });
    console.log("Got insert id: " + id);
    assert.ok(id > 0, "插入测试失败");
}

async function insert() {
    let id = await repository.insertItem({name: "张三", age: 30});
    console.log("God insert id: " + id);
}

修改

async function update() {
    let affectedRows = await repository.update("update user set age=:age where name=:name", {
        name: "张三",
        age: 40
    });
    console.log("Got update affected rows: " + affectedRows);
    assert.ok(affectedRows > 0, "更新测试失败");
}

async function update() {
    const affectedRows = await repository.updateItem({name: "张三", age: 40});
    console.log("Got update affected rows: " + affectedRows);
}

删除

async function remove() {
    let affectedRows = await repository.delete("delete from user where name=:name", {
        name: "张三"
    });
    console.log("Got delete affected rows: " + affectedRows);
    assert.ok(affectedRows > 0, "删除测试失败");
}

事务处理

async function transaction() {
    let conn = await DB.connection();
    await conn.beginTransaction();
    try {
        await conn.insert("insert into user(name,age) values(:name,:age)", {name: '张三', age: 10});
        // 下面这条语句会发生异常,事务会回滚
        await conn.insert("insert into user(name2,age) values(:name,:age)", {name: '李四', age: 10});
        await conn.commit();
    } catch (e) {
        await conn.rollback();
        throw e;
    } finally {
        await conn.release();
    }
}