github地址https://github.com/dmhsq/dmhsq-mysql-db 引入依赖包 npm install dmhsq-mysql-db 可用于腾讯云SCF 云函数 云开发环境 效果如下 简化了mysql的使用
未经本人允许,禁止转载
安装
npm install dmhsq-mysql-db
使用示例
快速操作mysql 错误处理尚未完善 部分错误参考mysql错误
返回的均为Promise对象
所有操作结束末尾必须携带get()
比如 collection.sort({}).get() collection.del({}).get() collection.add({}).get()
所有操作除了get()必须末尾调用 都可以不分先后调用
比如 collection.sort({}).where().get()可以写出 collection.where().sort({}).get()
引入资源
1const database = require("dmhsq-mysql-db")
连接数据库
1let db = new database({ 2 host: 'xxx', 3 port: 'xxx', 4 user: 'xxxx', 5 password: 'xxxx', 6 database: "xxxx" 7})
引用表
1let collection = db.table("user")
条件匹配
collection.where(params)
params 对象类型 格式为{username:"zc",old:18} 其中username,old是你要查询的字段值
1//如果需要获取数据 就要调用collection.where({username:"zcc"}).get() 2collection.where({username:"zcc"})
模糊匹配
collection.like(array)
array 数组类型 格式为[["数据库键名1","值",like],["数据库键名2","值",like]]
like 值可取 "top":以字段开头的 "end":是以字段结尾的 "in":包含字段 输入其他非法值均以in处理
1//如果需要获取数据 就要调用 2//collection.like([ 3// ["username", "z", "top"], 4// ["old", "8", "end"], 5// ["address", "村", "in"] 6//]).get() 7collection.like([ 8 ["username", "z", "top"], 9 ["old", "8", "end"], 10 ["address", "村", "in"] 11])
查询数据返回格式
模糊查询 条件查询 只要是查询 都是这个格式
data为查询到的数据 为数组类型
1{ 2 code: 0, 3 msg: 'SUCCESS', 4 data: [ //这里的返回数据 是模拟数据 5 { 6 username: 'dmhsq1', 7 password: '123zc', 8 phone: 11, 9 email: null, 10 _id: 2, 11 token: null, 12 token_expired: null, 13 last_login_time: null 14 }, 15 { 16 username: 'zcc1', 17 password: '123', 18 phone: 3, 19 email: null, 20 _id: 1, 21 token: null, 22 token_expired: null, 23 last_login_time: null 24 } 25 ] 26}
查询全部/获取数据
collection.get()
1collection.get().then(res => { 2 console.log(res) 3})
统计个数
collection.count().get()
可搭配下面的where(条件查询) like(模糊查询)
1collection.count().get().then(res => { 2 console.log(res) 3})
返回格式
1{ 2 code: 0, 3 msg: 'SUCCESS', 4 data: { 5 count: 2 6 }, 7 count: '查询到2个数据' 8}
条件查询
条件匹配+获取数据
collection.where(params).get()
params:对象类型 格式为 {数据库键1:"值",数据库键2:"值"}
1collection.where({ 2 username: "dmhsq" 3}).get().then(res => { 4 console.log(res) 5}) 6
模糊查询
模糊匹配+获取数据
collection.like(array).get()
array 数组类型 格式为[["数据库键名1","值",like],["数据库键名2","值",like]]
like 值可取 "top":以字段开头的 "end":是以字段结尾的 "in":包含字段 输入其他非法值均以in处理
1collection.like([ 2 ["username", "z", "top"], 3 ["old", "8", "end"], 4 ["address", "村", "in"] 5]).get()
返回格式
1{ 2 code: 0, 3 msg: 'SUCCESS', 4 data: [ //这里的返回数据 是模拟数据 5 { 6 username: 'dmhsq1', 7 password: '123zc', 8 phone: 11, 9 email: null, 10 _id: 2, 11 token: null, 12 token_expired: null, 13 last_login_time: null 14 }, 15 { 16 username: 'zcc1', 17 password: '123', 18 phone: 3, 19 email: null, 20 _id: 1, 21 token: null, 22 token_expired: null, 23 last_login_time: null 24 } 25 ] 26}
插入数据
collection.add(params)
params:对象类型 格式为 {数据库键1:"值",数据库键2:"值"}
1collection.add({ 2 username: "dmhsq", 3 password: "dmhsq", 4 _id: 123176312 5}).get().then(res => { 6 console.log(res) 7}) 8
返回格式
1{ 2 code: 0, 3 msg: 'SUCCESS', 4 data: { 5 add: 1 //数据增加个数 6 }, 7 add: '增加1个数据' 8}
更新数据
collection.updata(params)
params:对象类型 格式为 {数据库键1:"值",数据库键2:"值"}
可使用 where like
<br />1 2collection.updata({ 3 password: "zccc", 4 old:18 5}).where({ 6 username: "dmhsq" 7}).get().then(res=>{ 8 console.log(res) 9})
删除数据
collection.del() 删除操作
可使用 where like
1collection.del().where({ 2 username: "dmhsq" 3}).get().then(res => { 4 console.log(res) 5}) 6
返回格式
<br />1{ 2 code: 0, 3 msg: 'SUCCESS', 4 data: { 5 del: 1 6 }, 7 del: '删除1个数据' 8}
返回指定字段(不添加会返回全部)
collection.field(array)
array:数组类型 格式为["数据库键名1","数据库键名2"]
1 2//获取全部 3collection.field(["username"]).get() 4 5//模糊匹配 6collection.field(["username"]).like({username:"z"},"top").get().then(res=>{ 7 console.log(res) 8}) 9 10//条件匹配 11collection.field(["username"]).where({username:"zcc"}).get().then(res=>{ 12 console.log(res) 13}) 14
返回格式
<br />1{ 2 code: 0, 3 msg: 'SUCCESS', 4 data: [{ 5 username: 'zcc1', 6 _id: 1 7 }, { 8 username: 'dmhsq1', 9 _id: 2 10 }] 11}
排序
collection.sort(params)
params:对象类型 格式为 {数据库键名1:"DESC",数据库键名2:"ASC"}
DESC为降序 ASC降序
单个字段 collection.sort({_id:"DESC"})
多个字段 collection.sort({_id:"DESC",phone:"DESC"})
1 2collection.sort({_id:"DESC",phone:"DESC"}).like({username:"1"},"in").get().then(res=>{ 3 console.log(res) 4}) 5//或者 6collection.like({username:"1"},"in").sort({_id:"DESC",phone:"DESC"}).get().then(res=>{ 7 console.log(res) 8}) 9
排序后返回的数据格式和查询数据结果格式一样 <br />
自定义查询语句
如果以上方法无法满足您的需求 您可以自定义查询语句
collection.sqlQuery(sql,type)
sql为自定义查询语句
type可不填 不填自动识别操作类型
type不影响查询 只是格式化返回的数据格式
type可选值为 updata(更新) del(删除) count(计数) add(插入)
无需后缀添加get()
1 2//如果是查询数据库数据 3collection.sqlQuery("SELECT * FROM user").then(res=>{ 4 console.log(res) 5}) 6 7//如果是删除数据 8collection.sqlQuery("DELETE FROM user WHERE username = 'zcc2'").then(res=>{ 9 console.log(res) 10})

