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

none-sql

v2.0.1

Published

Connect to mysql as promise.

Downloads

21

Readme

none-sql

MySQL连接中间件

基于npm开源库mysql开发而来,增加了Promise风格的方法,增加了常用的数据库操作的链式调用编程方法。

附带koa2使用方法

  • 安装

    npm install --save none-sql
  • 引用

    import { DB, Pool } from 'none-sql';
  • 配置

      interface IConfig {
        mapUnderscoreToCamelCase: boolean;  // 返回数据字段自动下划线转驼峰,查询数据字段自动驼峰转下划线
      }
    
      const configDefault: IConfig = {
        mapUnderscoreToCamelCase: false,
      };
  • 创建数据库对象

    const db = new DB({ database: string, user: string, password: string, host = 'localhost', port = 3306, config: IConfig = configDefault });
  • 创建连接池

    const pool = new Pool({ database: string, user: string, password: string, host = 'localhost', port = 3306, connectionLimit = 10, config: IConfig = configDefault });
    • 获取数据库连接
      const result = pool.getConnection();
      result.then((connection) => {
        connection.connect('tableName').get().then((data) => {
          console.log(data);
          connection.release();  // 使用完后将该数据库连接还给连接池
        }).catch((err) => {
          console.error(err);
          connection.release();  // 使用完后将该数据库连接还给连接池
        });
      });
  • 关闭数据库连接
    请在最后的数据库操作返回的promise对象的then/catch方法中调用此方法

    db.close();
  • 基本操作

    • 查——获取users表里所有记录
      db.connect('tableName').get();
      return Promise<Result>
    • 条件查——获取users表里city属性为shanghai的记录
      .where()参数为条件对象如{ username: 'james', age: 20 }
      该对象内的条件为且的关系,条件为或的关系请用.where().orWhere()
      db.connect('tableName').where({city: 'shanghai'}).get();
      return Promise<Result>
    • 增——向users表里添加若干条记录
      db.connect('tableName').add(any[]);
      return Promise<Result>
    • 删——删除users表里的若干条记录
      db.connect('tableName').where().delete();
      return Promise<Result>
    • 改——更改users表里的若干条记录
      db.connect('tableName').where().update({city: 'shanghai'});
      return Promise<Result>
    • 排序——
      db.connect('tableName').orderBy({password: true, username: false}).get();
      return Promise<Result>

    上式含义为按password将结果升序排序,按username将结果降序排序,这里password的优先级高于username

  • 事务操作

    db.transaction(async () => {
        //对数据库的操作
        await db.connect('tableName').add(any[]);
        await db.connect('tableName').where().delete();
    })
  • 嵌套查询

    db.connect('tableName1').where({
        userId: db.connect('tableName2').where({name: 'james'}).get()[0].id
    })
  • 执行自定义sql语句

    let sql = 'SELECT * FROM ?? WHERE ??=?';
    db.query(sql,['users','name','james']);

    相当于执行了

    SELECT * FROM `users` WHERE `name`="james"

    ?? 会自动在参数两侧添加反引号``
    ? 会自动在参数两侧添加双引号""

  • 在koa2中使用
    入口文件
    index.ts

    import { Pool } from 'none-sql';
    
    const pool = new Pool(database: string, user: string, password: string, host = 'localhost', connectionLimit = 10);
    
    app.use(
      async (ctx: any, next: any) => {
        ctx.request.db = await pool.getConnection();
        await next();
        ctx.request.db.connection.release();
      }
    );

    请求处理文件
    contorller.ts

    login = async (ctx: any) => {
      let result = (await ctx.request.db.connect('tableName').get())
      result.then((data) => {
        console.log(data);
      }).catch((err) => {
        console.error(err);
      });
    }