mysqlデータベースを迅速に操作するNodejs環境

githubアドレスhttps://github.com/dmhsq/dmhsq-mysql-db
は、Tencent CloudSCFおよびクラウド開発環境で使用できます。
エラー処理は
完了していません。エラー参照mysqlエラー。依存関係パッケージnpminstalldmhsq-mysqlを導入してください。-db。mysqlの使用

簡素化するため効果は次のとおりです
ここに画像の説明を挿入します

未经本人允许,禁止转载

インストール

通常版 npm install dmhsq-mysql-db

データ接続プールのバージョン npm install dmhsq-mysql-pool

使用例

mysqlエラー処理の迅速な操作が完了していません。一部のエラーはmysqlエラーを参照しています

返されるのはすべてPromiseオブジェクトです

すべての操作は、最後にget()を実行する必要があります

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

get()を除くすべての操作は最後に呼び出す必要があり、特定の順序で呼び出すことはできません。

たとえば、collection.sort(())。where()。get()はcollection.where()。sort(())。get()と書くことができます。

リソースを持ち込む

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

データベースに接続します

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

参照表

let collection = db.table("user")

条件一致

collection.where(params)

paramsオブジェクトタイプの形式は{username: "zc"、old:18}です。ここで、username、oldはクエリするフィールド値です。

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

あいまい一致

collection.like(array)

array配列タイプの形式は[["データベースキー名1"、 "値"、like]、["データベースキー名2"、 "値"、like]]です。

同様の値は「top」を取ることができます:フィールドで始まる「end」:フィールドで終わる「in」:フィールドやその他の不正な値を含めると、次のように扱われます

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

クエリデータの戻り形式

クエリがこの形式である限り、ファジークエリ条件クエリ

dataはクエリされたデータは配列型です

{
    
    
  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
    }
  ]
}

すべてのクエリ/データの取得

collection.get()

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

カウント

collection.count()。get()

次の場合に使用できます(条件付きクエリ)like(ファジークエリ)

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

戻り形式

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

条件付きクエリ

条件マッチング+データの取得

collection.where(params).get()

params:オブジェクトタイプの形式は{データベースキー1: "値"、データベースキー2: "値"}です。

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

ファジークエリ

あいまい一致+データの取得

collection.like(array).get()

array配列タイプの形式は[["データベースキー名1"、 "値"、like]、["データベースキー名2"、 "値"、like]]です。

同様の値は「top」を取ることができます:フィールドで始まる「end」:フィールドで終わる「in」:フィールドやその他の不正な値を含めると、次のように扱われます

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

戻り形式

{
    
    
  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
    }
  ]
}

データを挿入

collection.add(params、isIgnore)

params:オブジェクトタイプの形式は{データベースキー1: "値"、データベースキー2: "値"}です。

isIgnoreオプション、デフォルトはfalse、trueの場合はINSERT IGNOREINTOを使用

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

戻り形式

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

データの更新

collection.updata(params)

params:オブジェクトタイプの形式は{データベースキー1: "値"、データベースキー2: "値"}です。

好きな場所で使用できます


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

データを削除する

collection.del()削除操作

好きな場所で使用できます

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

戻り形式

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

指定されたフィールドを返します(追加しないとすべてが返されます)

collection.field(array)

配列:配列タイプの形式は["データベースキー名1"、 "データベースキー名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)
})

戻り形式

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

ソート

collection.sort(params)

params:オブジェクトタイプの形式は{データベースキー名1: "DESC"、データベースキー名2: "ASC"}です。

DESCは降順ですASC降順

単一フィールド
collection.sort({_ id: "DESC"})

複数のフィールド
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)
})

並べ替え後に返されるデータ形式は、クエリデータの結果形式と同じです。

カスタムクエリステートメント

上記の方法でニーズを満たせない場合は、クエリステートメントをカスタマイズできます

collection.sqlQuery(sql、type)

sqlはカスタムクエリステートメントです

タイプは未記入または未記入のままにすることができ、操作のタイプを自動的に認識します

タイプはクエリに影響せず、返されるデータ形式の形式のみに影響します

タイプのオプションの値は、updata(更新)del(削除)count(カウント)add(挿入)です。

get()サフィックスを追加する必要はありません


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

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

おすすめ

転載: blog.csdn.net/qq_42027681/article/details/115038010