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

@pakwoon/mysql-utils

v1.1.4

Published

nodejs 连接 mysql 辅助方法

Downloads

31

Readme

@pakwoon/mysql-utils

nodejs 连接 mysql 辅助方法

便捷操作

genOperation

生成 mysql 便捷执行器

入参

  • config (Config) - mysql 连接信息
    • host (string) - 服务地址/IP
    • port (string) - 端口
    • user (string) - 用户名
    • password (string) - 密码
    • database (string) - 数据库名称
    • multipleStatements (boolean) - 一次执行多条 sql

返回

  • object (Object) - 操作器对象

    • executor ((sql: Sql) => Promise) - 执行器
    • insert ((options: InsertOptions) => Promise) - 插入执行器
    • del ((options: DeleteOptions) => Promise) - 删除执行器
    • update ((options: UpdateOptions) => Promise) - 更新执行器
    • detail ((options: SelectOptions) => Promise) - 详情执行器
    • list ((options: SelectOptions) => Promise) - 列表执行器
    • pageList ((options: SelectOptions) => Promise) - 分页列表执行器

    InsertOptions、DeleteOptions、UpdateOptions、SelectOptions 详细信息请查看下文的拼接字符串部分

连接 mysql

genExecutor

生成 mysql 执行器

入参

  • config (Config) - mysql 连接信息
    • host (string) - 服务地址/IP
    • port (string) - 端口
    • user (string) - 用户名
    • password (string) - 密码
    • database (string) - 数据库名称
    • multipleStatements (boolean) - 一次执行多条 sql

返回

((sql: Sql) => Promise) - 执行器

executor

调用 genExecutor 生产的执行器方法 executor

入参

  • sql (Sql)
    • sqlStr (string) - sql 字符串
    • params ((string | number | boolean)[]) - mysql2 包中 connection.query 方法接收的第二个参数

返回

connection.query 查询结果

sql 字符串便捷拼接

getInsertSqlStr

拼接 insert sql 字符串

入参 - InsertOptions

  • table (string) - 表格名称
  • key (string) - 插入行对应的 key
  • values (string) - 插入行对应的 values

返回

(string) - 拼接后的 insert sql 字符串

getDeleteSqlStr

拼接 delete sql 字符串

入参 - DeleteOptions

  • table (string) - 表格名称
  • where (string) - 删除条件

返回

(string) - 拼接后的 delete sql 字符串

getUpdateSqlStr

拼接 update sql 字符串

入参 - UpdateOptions

  • table (string) - 表格名称
  • set (string) - 更新字段字符串
  • where (string) - 更新条件

返回

(string) - 拼接后的 update sql 字符串

getSelectSqlStr

拼接 select sql 字符串

入参 - SelectOptions

  • select (string) - 查询字段
  • table (string) - 表格名称
  • where ? (string) - 过滤条件
  • pageInfo ? (PageInfo) - 分页对象
    • pageIndex (number) - 查询页码(起始:1)
    • pageSize (number) - 单页条数

返回

(string) - 拼接后的 select sql 字符串

获取标准返回对象

ApiRes

标准返回对象

属性

  • success (boolean) - 返回成功/失败
  • message (string) - 返回信息
  • errorCode (string) - 错误码
  • data ? (T=any) - 返回数据对象

genGetApiResult

生成获取返回对象方法,用于添加扩展错误信息

入参

  • errorMessage ? (Record<string, string>) - 附加自定义错误信息

返回

(<T = any>(options?: Partial<ApiRes>) => ApiRes) - 获取返回对象方法

getApiResult

调用 genGetApiResult 方法,返回的 getApiResult

入参

  • options (Partial) - ApiRes 对象可选化

返回

(ApiRes) - 标准返回对象

内部逻辑

默认返沪成功的标准返回对象
const res = {
  success: true,
  message: _errorMessage[ERROR_CODE.SUCCESS], // “成功”
  errorCode: ERROR_CODE.SUCCESS, // "SUCCESS"
};

options.success === false 并无传入 options.errorCode

const res = {
  success: false,
  message: _errorMessage[ERROR_CODE.FAIL], // “失败”
  errorCode: ERROR_CODE.FAIL, // "FAIL"
};

options.errorCode 传入并不等于 “SUCCESS“

const res = {
  success: false,
  message: _errorMessage[errorCode], // 自动匹配对应错误信息
  errorCode: errorCode, // 传入的 errorCode
};

错误码与错误信息

/** 错误码 */
export enum ERROR_CODE {
  SUCCESS = 'SUCCESS',
  FAIL = 'FAIL',
  SYSTEM_ERROR = 'SYSTEM_ERROR',
  NOT_FOUND = 'NOT_FOUND',
  MISSING_PARAMS = 'MISSING_PARAMS',
}

/** 错误信息 */
export const ErrorMessage: Record<ERROR_CODE, string> = {
  [ERROR_CODE.SUCCESS]: '成功',
  [ERROR_CODE.FAIL]: '失败',
  [ERROR_CODE.SYSTEM_ERROR]: '系统错误',
  [ERROR_CODE.NOT_FOUND]: '找不到对应信息',
  [ERROR_CODE.MISSING_PARAMS]: '缺少必传参数',
};

辅助方法

missingParams

检查 params 对象中是否存在值为 undefined 的属性

入参

  • params (Record<string | number | symbol, any>) - 检查对象
  • keys ? ((number | string | symbol)[]) - 检查属性名称集合

返回

(boolean) - 是否存在