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

func-stateless-mysql-sdk-nodejs

v1.0.3

Published

UOS Func Stateless CRUD MySQL Nodejs SDK

Downloads

65

Readme

UOS Func Stateless CRUD MySQL Nodejs SDK

用法

1.安装 UOS Func Stateless CRUD MySQL Nodejs SDK

npm install func-stateless-mysql-sdk-nodejs

2.引用本 SDK 并连接 MySQL Client

context.database = require('func-stateless-mysql-sdk-nodejs').database;
// If you do not specify a databaseId, the sdk will automatically search for and connect to an available database.
// 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
// Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
// 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
// Please note: if there are multiple available mysql databases, you must use the databaseId to specify the desired database you wish to connect to.
// 注意:如果有多个可用的 mysql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId。
try {
  const databaseId = '';
  const databasePassword = '';
  const database = await context.database(databaseId, databasePassword);
  const client = await database.connection();
} catch (error) {
  console.error(error);
}

3.使用 MySQL Client 操作数据库

例:

try {
  const databaseName = 'test';
  await connection.query(`CREATE DATABASE IF NOT EXISTS \`${databaseName}\``);
  console.log(`Database ${databaseName} ensured.`);

  await connection.query(`USE \`${databaseName}\``);
  console.log(`Using database ${databaseName}`);

  const createTableQuery = `
         CREATE TABLE IF NOT EXISTS users (
           id INT AUTO_INCREMENT PRIMARY KEY,
           name VARCHAR(255) NOT NULL,
           email VARCHAR(255) NOT NULL UNIQUE
         )
       `;
  await connection.query(createTableQuery);
  console.log('table created successfully');

  const insertQuery = `INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')`;
  await connection.query(insertQuery);
  console.log('rows inserted successfully');

  const [rows] = await connection.query('SELECT * FROM users');
  console.log('selected rows:', rows);

  const deleteTableQuery = `
         DROP TABLE IF EXISTS users
       `;
  await connection.query(deleteTableQuery);
  console.log('table deleted successfully');
} catch (error) {
  console.error('error while setting up the database:', error);
} finally {
  // Please note: mysql connection should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
  // 注意:mysql 客户端需要被手动释放,无论查询过程是否发生错误
  await connection.release();

  //test end pool
  await database.endConnection();
  console.log('closed the connection pool');
}

4.完整操作示例

/*
环境变量(自动生成):
process.env['UOS_APP_ID'] = 8972b2ba-ffb4-4211-bbef-1274083a2c09
process.env['UOS_APP_SECRET'] = 9e131ad6-e73e-4e47-bc9d-936df2b1e70a
process.env['UOS_APP_SERVICE_SECRET'] = f4f57f44-273c-4005-ab70-d469d7b45053
*/

exports.main = async (event, context) => {
  context.database = require('func-stateless-mysql-sdk-nodejs').database;
  // If you do not specify a databaseId, the system will automatically search for and connect to an available database.
  // 如果没有指定 databaseId 的话,sdk 会自动获取可用的数据库进行连接。
  // Please note: if uos is authorized to store your database password in an encrypted form, this SDK will automatically fill in the password, if not authorized, you will need to specify the database password in your code.
  // 注意:如果uos被授权以加密形式存储您的数据库密码,本sdk将自动填入密码,如果未被授权,您需要在代码中指定数据库密码。
  // Please note: if there are multiple available mysql databases, you must use the databaseId to specify the desired database you wish to connect to.
  // 注意:如果有多个可用的 mysql 数据库的话,请在代码中指定要用于连接的数据库的 databaseId
  const databaseId = '';
  const databasePassword = '';
  try {
    const database = await context.database(databaseId, databasePassword);
    const connection = await database.connection();
    try {
      // 如果在创建数据库实例时已经指定DATABASE则无需使用CREATE DATABASE命令,并且此时不推荐使用USE命令切换SCHEMA
      const databaseName = 'test';
      await connection.query(
        `CREATE DATABASE IF NOT EXISTS \`${databaseName}\``
      );
      console.log(`Database ${databaseName} ensured.`);

      await connection.query(`USE \`${databaseName}\``);
      console.log(`Using database ${databaseName}`);

      const createTableQuery = `
         CREATE TABLE IF NOT EXISTS users (
           id INT AUTO_INCREMENT PRIMARY KEY,
           name VARCHAR(255) NOT NULL,
           email VARCHAR(255) NOT NULL UNIQUE
         )
       `;
      await connection.query(createTableQuery);
      console.log('table created successfully');

      const insertQuery = `INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')`;
      await connection.query(insertQuery);
      console.log('rows inserted successfully');

      const [rows] = await connection.query('SELECT * FROM users');
      console.log('selected rows:', rows);

      const deleteTableQuery = `
         DROP TABLE IF EXISTS users
       `;
      await connection.query(deleteTableQuery);
      console.log('table deleted successfully');
    } catch (error) {
      console.error('error while setting up the database:', error);
    } finally {
      // Please note: mysql connection should be released manually if you successfully check it out, regardless of whether there was an error with the queries you ran on the client.
      // 注意:mysql 客户端需要被手动释放,无论查询过程是否发生错误
      await connection.release();
    }

    //test end pool
    await database.endConnection();
    console.log('closed the connection pool');
  } catch (error) {
    console.error(error);
  }
};