Nodejs environment to quickly operate mysql database

The github address https://github.com/dmhsq/dmhsq-mysql-db
can be used in Tencent Cloud SCF and cloud development environment.
Error handling has not been perfected. Error reference mysql error.
Introduce dependency package npm install dmhsq-mysql-db. The
effect is as follows to
simplify mysql use
Insert picture description here

未经本人允许,禁止转载

installation

Normal version npm install dmhsq-mysql-db

Data connection pool version npm install dmhsq-mysql-pool

Usage example

Quickly operate mysql error handling has not been perfected, some errors refer to mysql error

All returned are Promise objects

All operations must carry get() at the end

比如 collection.sort({}).get() collection.del({}).get() collection.add({}).get()

All operations except get() must be called at the end and can be called in no particular order

For example, collection.sort(()).where().get() can write collection.where().sort(()).get()

Bring in resources

const database = require("dmhsq-mysql-db")

Connect to the database

let db = new database({
    
    
	host: 'xxx',
	port: 'xxx',
	user: 'xxxx',
	password: 'xxxx',
	database: "xxxx"
})

Reference table

let collection = db.table("user")

Condition match

collection.where(params)

The params object type format is {username:"zc",old:18} where username,old are the field values ​​you want to query

//如果需要获取数据 就要调用collection.where({username:"zcc"}).get()
collection.where({
    
    username:"zcc"})

Fuzzy matching

collection.like(array)

array The format of the array type is [["database key name 1","value",like],["database key name 2","value",like]]

The like value can take "top": "end" starting with the field: "in" ending with the field: including fields and other illegal values ​​are treated as in

//如果需要获取数据 就要调用
//collection.like([
//	["username", "z", "top"],
//	["old", "8", "end"],
//	["address", "村", "in"]
//]).get()
collection.like([
	["username", "z", "top"],
	["old", "8", "end"],
	["address", "村", "in"]
])

Query data return format

Fuzzy query conditions query as long as the query is in this format

data is the queried data is an array type

{
    
    
  code: 0,
  msg: 'SUCCESS',
  data: [ //这里的返回数据 是模拟数据
    {
    
    
      username: 'dmhsq1',
      password: '123zc',
      phone: 11,
      email: null,
      _id: 2,
      token: null,
      token_expired: null,
      last_login_time: null
    },
    {
    
    
      username: 'zcc1',
      password: '123',
      phone: 3,
      email: null,
      _id: 1,
      token: null,
      token_expired: null,
      last_login_time: null
    }
  ]
}

Query all/get data

collection.get()

collection.get().then(res => {
    
    
	console.log(res)
})

Count

collection.count().get()

Can be used with the following where (conditional query) like (fuzzy query)

collection.count().get().then(res => {
    
    
	console.log(res)
})

Return format

{
    
    
	code: 0,
	msg: 'SUCCESS',
	data: {
    
    
		count: 2
	},
	count: '查询到2个数据'
}

Conditional query

Condition matching + get data

collection.where(params).get()

params: The object type format is {database key 1: "value", database key 2: "value"}

collection.where({
    
    
	username: "dmhsq"
}).get().then(res => {
    
    
	console.log(res)
})

Fuzzy query

Fuzzy match + get data

collection.like(array).get()

array The format of the array type is [["database key name 1","value",like],["database key name 2","value",like]]

The like value can take "top": "end" starting with the field: "in" ending with the field: including fields and other illegal values ​​are treated as in

collection.like([
	["username", "z", "top"],
	["old", "8", "end"],
	["address", "村", "in"]
]).get()

Return format

{
    
    
  code: 0,
  msg: 'SUCCESS',
  data: [ //这里的返回数据 是模拟数据
    {
    
    
      username: 'dmhsq1',
      password: '123zc',
      phone: 11,
      email: null,
      _id: 2,
      token: null,
      token_expired: null,
      last_login_time: null
    },
    {
    
    
      username: 'zcc1',
      password: '123',
      phone: 3,
      email: null,
      _id: 1,
      token: null,
      token_expired: null,
      last_login_time: null
    }
  ]
}

Insert data

collection.add(params,isIgnore)

params: The object type format is {database key 1: "value", database key 2: "value"}

isIgnore optional, default false, use INSERT IGNORE INTO when true

collection.add({
    
    
	username: "dmhsq",
	password: "dmhsq",
	_id: 123176312
}).get().then(res => {
    
    
	console.log(res)
})

Return format

{
    
    
	code: 0,
	msg: 'SUCCESS',
	data: {
    
    
		add: 1 //数据增加个数
	},
	add: '增加1个数据'
}

update data

collection.updata(params)

params: The object type format is {database key 1: "value", database key 2: "value"}

Can use where like


collection.updata({
    
    
	password: "zccc",
	old:18
}).where({
    
    
	username: "dmhsq"
}).get().then(res=>{
    
    
	console.log(res)
})

delete data

collection.del() delete operation

Can use where like

collection.del().where({
    
    
	username: "dmhsq"
}).get().then(res => {
    
    
	console.log(res)
})

Return format

{
    
    
	code: 0,
	msg: 'SUCCESS',
	data: {
    
    
		del: 1
	},
	del: '删除1个数据'
}

Return the specified field (do not add will return all)

collection.field(array)

array: The format of the array type is ["database key name 1", "database key name 2"]


//获取全部
collection.field(["username"]).get()

//模糊匹配
collection.field(["username"]).like({
    
    username:"z"},"top").get().then(res=>{
    
    
	console.log(res)
})

//条件匹配
collection.field(["username"]).where({
    
    username:"zcc"}).get().then(res=>{
    
    
	console.log(res)
})

Return format

{
    
    
	code: 0,
	msg: 'SUCCESS',
	data: [{
    
    
		username: 'zcc1',
		_id: 1
	}, {
    
    
		username: 'dmhsq1',
		_id: 2
	}]
}

Sort

collection.sort(params)

params: The object type format is {database key name 1: "DESC", database key name 2: "ASC"}

DESC is descending order ASC descending order

Single field
collection.sort({_id:"DESC"})

Multiple fields
collection.sort({_id:"DESC",phone:"DESC"})


collection.sort({
    
    _id:"DESC",phone:"DESC"}).like({
    
    username:"1"},"in").get().then(res=>{
    
    
	console.log(res)
})
//或者
collection.like({
    
    username:"1"},"in").sort({
    
    _id:"DESC",phone:"DESC"}).get().then(res=>{
    
    
	console.log(res)
})

The data format returned after sorting is the same as the query data result format

Custom query statement

If the above methods cannot meet your needs, you can customize the query statement

collection.sqlQuery(sql,type)

sql is a custom query statement

Type can be left unfilled or not filled in, automatically recognize the type of operation

The type does not affect the query, only the format of the returned data format

The optional value of type is updata (update) del (delete) count (count) add (insert)

No need to add get() suffix


//如果是查询数据库数据
collection.sqlQuery("SELECT * FROM user").then(res=>{
    
    
	console.log(res)
})

//如果是删除数据
collection.sqlQuery("DELETE FROM user WHERE username = 'zcc2'").then(res=>{
    
    
	console.log(res)
})

Guess you like

Origin blog.csdn.net/qq_42027681/article/details/115038010