Git Product home page Git Product logo

h-mysql's Introduction

h-mysql

  • 网站访问项目github地址
  • 介绍:一直以来很喜欢Thinkphp的数据操作风格,所以在nodejs上也封装了一个MYSQL数据库的常用操作,支持链式调用,实现语义化的数据库操作。

安装及引用

// npm安装
npm i h-mysql --save

// yarn安装
yarn add h-mysql

const hMysql = require('h-mysql');

初始化配置

const hMysql = new mysql({
  host: '127.0.0.1',
  user: 'root',
  password: '',
  database: 'test-db',
  port: 3306,

  acquireTimeout: 1000,
  waitForConnections: true,     //是否等待链接  
  connectionLimit: 10,          // 连接池数
  queueLimit: 0,                // 排队限制 

  defaultSqlPre: '',
  isPool: true,
  isDebug: true
});

const {error, result} = await hMysql.table('user')
  .where({ id: 100 })
  .select()
  .execSql();

console.log(result);

调用方式

规则:调用方法跟顺序无关

let hMysql = new hMysql(config)

hMysql.table()
      .where()
      .limit()
      .select()
      .execSql()
  • 添加:[insert] [table] [data]
  • 查询:[select] [field] from [table] [alias] [where] [join]
  • 更新:[update] [table] set [data] [where]
  • 删除:[delete] from [table] [where]

迭代计划

  • 完善API文档、架构文档
  • 完善案例
  • 批量插入、批量更新、批量删除
  • 支持更复杂的链式操作
  • 添加测试用例
  • 重写异常处理方式
  • 添加性能测试、性能优化并添加每条语句执行的时间
  • 添加安全测试,防注入
  • 增加对数据库的操作(现在的功能都是针对表)
  • 考虑是不是要支持ORM

版本更新

  • v1.0.0
    • 实现常用方法的链式调用,未经过测试,慎用到生产环境,待更新到完善

项目骨干

贡献

欢迎大家到 Issues 交流或通过提交PR的形式对本项目进行贡献

License

MIT

h-mysql's People

Contributors

haizhilin2013 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

h-mysql's Issues

使用 node demo.js 无法运行事例

1)按例子写,会报错如下:
const {error, result}= await hMysql.table('mytable').where().select().execSql();
^^^^^
SyntaxError: await is only valid in async function
2)去掉 await,返回值是一个 promise;不报错,但没有响应结果
2020-08-03 13:50:00 | SELECT * FROM mytable
undefined
3)如果只是第一句const result= hMysql.table('mytable');console.log(result);
不报错,能返回一个大对象,如下

hMysql {
connection: Pool {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
config: PoolConfig {
acquireTimeout: 1000,
connectionConfig: [ConnectionConfig],
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
},
_acquiringConnections: [],
_allConnections: [],
_freeConnections: [],
_connectionQueue: [],
_closed: false,
[Symbol(kCapture)]: false
},
sqlObj: { table: 'mytable' },
tempObj: {},
config: {
host: 'localhost',
port: 3306,
user: 'root',
password: '‘,
database: 'exercise',
acquireTimeout: 1000,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
isPool: true,
defaultSqlPre: '',
isDebug: true
},
mixed: {
table: [Function: bound table],
alias: [Function: bound alias],
field: [Function: bound field],
where: [Function: bound where],
data: [Function: bound data],
order: [Function: bound order],
limit: [Function: bound limit],
page: [Function: bound page],
join: [Function: bound join],
union: [Function: bound union],
distinct: [Function: bound distinct],
lock: [Function: bound lock],
comment: [Function: bound comment],
group: [Function: bound group],
having: [Function: bound having],
count: [Function: bound count],
max: [Function: bound max],
min: [Function: bound min],
avg: [Function: bound avg],
sum: [Function: bound sum],
select: [Function: bound select],
find: [Function: bound find],
update: [Function: bound update],
insert: [Function: bound insert],
delete: [Function: bound delete],
query: [Function: bound query],
connect: [Function: bound connect],
poolEvent: [Function: bound poolEvent],
execSql: [Function: bound execSql] AsyncFunction,
transaction: [Function: bound transaction] AsyncFunction
},
isMixedWith: [Function (anonymous)],
pageNum: 1,
pageSize: 50
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.