0.0.2 • Published 5 years ago

ali-rds-async v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

ali-rds-async

申明

SDK仅仅是将ali-rdsSDKgenerator书写方法使用co 模块改造成现在市场上流行的Promise的方式,参考文档直接看ali-rds 官网就可以

使用示例

  • 1、安装模块

    npm install ali-rds-async
  • 2、导包及连接数据库

    import AsyncAliRds from 'ali-rds-async';
    // or
    const { AsyncAliRds } = require('ali-rds-async');
    
    const config = {
      host: 'your-rds-address.mysql.rds.aliyuncs.com',
      port: 3306,
      user: 'your-username',
      password: 'your-password',
      database: 'your-database-name',
    
      // optional params
      // The charset for the connection.
      // This is called "collation" in the SQL-level of MySQL (like utf8_general_ci).
      // If a SQL-level charset is specified (like utf8mb4)
      // then the default collation for that charset is used. (Default: 'UTF8_GENERAL_CI')
      // charset: 'utf8_general_ci',
      //
      // The maximum number of connections to create at once. (Default: 10)
      // connectionLimit: 10,
      //
      // The maximum number of connection requests the pool will queue
      // before returning an error from getConnection.
      // If set to 0, there is no limit to the number of queued connection requests. (Default: 0)
      // queueLimit: 0,
    };
    // 创建连接
    let mysql = new AsyncAliRds(config);
  • 3、使用案例

    (async () => {
      const r = await mysql.query('select * from dog');
      console.log(r);
    })();
    
    (async () => {
      const r = await mysql.select('dog');
      console.log('结果2', r);
    })();
    
    (async () => {
      const r = await mysql.insert('dog', { name: '小狗', color: '黄色' });
      console.log('结果', r);
    })();

主要实现的方法

  • query使用原生sql语句
  • insert插入语句
  • update更新单条语句
  • updateRows更新多条语句
  • get获取一条数据
  • select查询数据
  • delete删除数据
  • count计数
  • beginTransaction事务