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

@hyoga/mysql

v1.1.10

Published

一个MYSQL数据库的常用操作封装,操作方式类似THINKPHP的数据库操作

Downloads

30

Readme

hyoga-mysql

NPM version npm download

一个 MYSQL 数据库的常用操作封装,使用连续点操作,实现语义化的数据库操作。

安装

npm i @hyoga/mysql

使用

基础操作

const mysql = require('@hyoga/mysql').default;
// import mysql from '@hyoga/mysql';
const inst = new mysql({
  host: '127.0.0.1',
  user: 'root',
  password: '',
  database: 'test-db',
  port: 3306,
});
const users = await inst.table('user').where({ status: 1 }).select();
console.log(users);

更多操作方式

见下方API

API

@hyoga/mysql

Mysql 数据库实例,封装了常用操作方式

Author: [email protected]

@hyoga/mysql~Mysql

Kind: inner class of @hyoga/mysql

new Mysql(config)

创建 Mysql 实例

| Param | Type | Description | | ------ | ------------------- | -------------- | | config | object | 数据库连接配置 |

mysql.query(sql) ⇒ Promise.<any>

直接执行 sql 语句

Kind: instance method of Mysql
Returns: Promise.<any> - sql 执行结果

| Param | Type | Description | | ----- | ------------------- | ----------- | | sql | string | sql 语句 |

mysql.table(tableName) ⇒ Mysql

设置表名

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | --------- | ------------------- | ----------- | | tableName | string | 表名 |

mysql.alias(tableAlias) ⇒ Mysql

设置表的别名

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ---------- | ------------------- | ----------- | | tableAlias | string | 主表别名 |

mysql.field(fields) ⇒ Mysql

设置需要选取的字段,字符串或数组格式

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ------ | ----------------------------------------- | -------------- | | fields | string | Array | 需要选取的字段 |

Example

// SELECT `admins`.`id`, `admins`.`name` FROM `admins` limit 1
mysql.table('admins').field('id, name').find();
// SELECT `admins`.`id`, `admins`.`name` as a, `admins`.`status` as b FROM `admins` limit 1
mysql
  .table('admins')
  .field(['id', 'name as a', { status: 'b' }])
  .find();

mysql.group(columns) ⇒ Mysql

group by 操作

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ------- | ----------------------------------------- | -------------------------------------------- | | columns | Array | string | 分组列名,可为数组或字符串,字符串以逗号分隔 |

mysql.where(where) ⇒ Mysql

where 条件设置,接受字符串或者对象形式,可以多次调用,每次调用都作为一个整体,多次调用使用 AND 连接

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ----- | ------------------------------------------ | ----------- | | where | object | string | where 条件 |

Example

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`status` = 'on') limit 1
mysql.table('admins').where({ status: 'on' }).find();

// SELECT `admins`.`*` FROM `admins` WHERE (id = 10 OR id < 2) limit 1
mysql.table('admins').where('id = 10 OR id < 2').find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` != 1) limit 1
mysql.table('admins').where({id: ['!=', 1]}).find();

// NULL操作

SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` IS NULL) limit 1
mysql.table('admins').where({id: null}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` IS NOT NULL) limit 1
mysql.table('admins').where({id: [ '!=', null ]}).find();

// LIKE 操作

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`name` LIKE '%admin%') limit 1
mysql.table('admins').where({name: [ 'like', '%admin%' ]}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`name` NOT LIKE '%admin%') limit 1
mysql.table('admins').where({name: [ 'notlike', '%admin%' ]}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`name` LIKE '%admin%' OR `admins`.`email` LIKE '%admin%') limit 1
mysql.table('admins').where({'name|email': [ 'like', '%admin%' ]}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`name` LIKE '%admin%' AND `admins`.`email` LIKE '%admin%') limit 1
mysql.table('admins').where({'name&email': [ 'like', '%admin%' ]}).find();

// 一对多操作
// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`name` = 'admin' OR `admins`.`name` = 'editor') limit 1
mysql.table('admins').where({name: [ '=', [ 'admin', 'editor' ] ]}).find();

// IN 操作
// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` IN (5,10)) limit 1
mysql.table('admins').where({'id': [ 'in', [5, 10] ]}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` IN (5, 10)) limit 1
mysql.table('admins').where({'id': [ 'in', '5, 10' ]}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` NOT IN (5,10)) limit 1
mysql.table('admins').where({'id': [ 'notin', [5, 10] ]}).find();

// BETWEEN 操作
// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` BETWEEN 5 AND 10) limit 1
mysql.table('admins').where({'id': [ 'between', [5, 10] ]}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` BETWEEN 5 AND 10 AND `admins`.`name` = 'admin') limit 1
mysql.table('admins').where({'id': [ 'between', [5, 10] ], 'name': 'admin'}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`id` BETWEEN 5 AND 10 OR `admins`.`name` = 'admin') limit 1
mysql.table('admins').where({'id': [ 'between', [5, 10] ], 'name': 'admin', '_logic': 'OR'}).find();

// 多字段操作
// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`status` = 'on') AND (`admins`.`id` >= 1 AND `admins`.`id` <= 10) limit 1
mysql.table('admins').where({'status': 'on'}).where({'id': {'>=': 1, '<=': 10}}).find();

// SELECT `admins`.`*` FROM `admins` WHERE (`admins`.`status` = 'on') AND (`admins`.`id` >= 1 OR `admins`.`id` <= 10) limit 1
mysql.table('admins').where({'status': 'on'}).where({'id': {'>=': 1, '<=': 10, '_logic': 'OR'}}).find();

mysql.limit(limit) ⇒ Mysql

设置结果的条数限制

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ----- | ------------------- | -------------- | | limit | number | 结果的条数限制 |

mysql.page(page, pageSize) ⇒ Mysql

分页操作

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Default | Description | | -------- | ------------------- | -------------- | ----------- | | page | number | 1 | 当前页数 | | pageSize | number | 1 | 每页大小 |

mysql.data(data) ⇒ Mysql

设置数据

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ----- | ------------------- | ----------- | | data | object | 数据 |

mysql.order(order) ⇒ Mysql

排序

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ----- | ----------------------------------------- | ----------- | | order | array | string | 排序 |

Example

// SELECT `article_categorys`.`*` FROM `article_categorys` ORDER BY id desc
mysql.table('article_categorys').order('id desc').select();

//SELECT `article_categorys`.`*` FROM `article_categorys` ORDER BY id desc, name asc
mysql.table('article_categorys').order(['id desc', 'name asc']).select();

mysql.join(join) ⇒ Mysql

设置 join 条件,可以多次 join

Kind: instance method of Mysql
Returns: Mysql - 实例

| Param | Type | Description | | ----- | ------------------- | ----------- | | join | object | join 条件 |

Example

// SELECT `a`.`*`, `b`.`*` FROM `article_posts` as a LEFT JOIN `article_categorys` AS b ON (a.`category_id`=b.`id`) limit 1
mysql
  .table('article_posts')
  .alias('a')
  .field(['a.*', 'b.*'])
  .join({
    article_categorys: {
      as: 'b',
      on: { category_id: 'id' },
    },
  })
  .find();

// SELECT `a`.`*`, `article_categorys`.`*` FROM `article_posts` as a LEFT JOIN `article_categorys` ON (a.`category_id`=article_categorys.`id`) limit 1
mysql
  .table('article_posts')
  .alias('a')
  .field(['a.*', 'article_categorys.*'])
  .join({
    article_categorys: {
      // as: 'b',
      on: { category_id: 'id' },
    },
  })
  .find();

mysql.find(where) ⇒ Promise.<any>

查找一条数据

Kind: instance method of Mysql
Returns: Promise.<any> - 查询结果

| Param | Type | Default | Description | | ----- | ------------------------------------------ | ------------- | ----------- | | where | object | string | | where 条件 |

mysql.select(where) ⇒ Promise.<any>

查找数据

Kind: instance method of Mysql
Returns: Promise.<any> - 查询结果

| Param | Type | Default | Description | | ----- | ------------------------------------------ | ------------- | ----------- | | where | object | string | | where 条件 |

mysql.update(column, where) ⇒ Promise.<any>

更新操作

Kind: instance method of Mysql
Returns: Promise.<any> - 更新结果

| Param | Type | Default | Description | | ------ | ------------------------------------------ | ------------- | ---------------------------- | | column | object | | {name: value} 更新的字段与值 | | where | object | string | | where 条件,参见[where]方法 |

mysql.updateMany(columnList, where) ⇒ Promise.<any>

一次性更新多条数据

Kind: instance method of Mysql
Returns: Promise.<any> - 更新结果

| Param | Type | Description | | ---------- | ------------------------------------------ | --------------------------------------------------- | | columnList | Array.<object> | [{id: 1, name: value}] 更新的字段与值,必须包含主键 | | where | object | string | where 条件,参见[where]方法 |

mysql.increase(field, step) ⇒ Promise.<any>

自增操作

Kind: instance method of Mysql
Returns: Promise.<any> - 更新结果

| Param | Type | Default | Description | | ----- | ------------------- | -------------- | -------------- | | field | string | | 字段名 | | step | number | 1 | 自增数,默认 1 |

mysql.decrement(field, step) ⇒ Promise.<any>

自减操作

Kind: instance method of Mysql
Returns: Promise.<any> - 更新结果

| Param | Type | Default | Description | | ----- | ------------------- | -------------- | -------------- | | field | string | | 字段名 | | step | number | 1 | 自减数,默认 1 |

mysql.add(column, duplicate) ⇒ Promise.<any>

新增数据

Kind: instance method of Mysql
Returns: Promise.<any> - 操作结果

| Param | Type | Default | Description | | --------- | ------------------- | ------------------ | ------------------------------------------------- | | column | object | | 字段键值对 | | duplicate | object | false | 出现重复则更新,{id : 100, name : VALUES('test')} |

mysql.addMany(columnList, duplicate) ⇒ Promise.<any>

批量新增数据

Kind: instance method of Mysql
Returns: Promise.<any> - 操作结果

| Param | Type | Default | Description | | ---------- | ------------------- | ------------------ | ------------------------------------------------- | | columnList | object | | 字段键值对数组 | | duplicate | object | false | 出现重复则更新,{id : 100, name : VALUES('test')} |

mysql.delete(where) ⇒ Promise.<any>

删除操作,彻底删除一条数据,一般不建议删除数据,可以通过字段开关控制

Kind: instance method of Mysql
Returns: Promise.<any> - 操作结果

| Param | Type | Description | | ----- | ------------------------------------------ | --------------------------- | | where | object | string | where 条件,参见[where]方法 |

mysql._sql() ⇒ string

打印生成的 sql 语句,用于调试

Kind: instance method of Mysql
Returns: string - 生成的 sql 语句