mysqli/docs/3.x.md

653 lines
10 KiB
Markdown
Raw Normal View History

2021-08-23 19:25:20 +08:00
## Document
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> One or more configurations can be passed in for instantiation. When there is only one database, the default is the `master library`; when there is more than one database service, the first one is automatically used as the `master library`, and the other `slave libraries`.
2018-05-29 19:02:33 +08:00
```javascript
let Mysqli = require('mysqli')
2021-08-23 19:25:20 +08:00
// one config
2018-05-29 19:02:33 +08:00
let conn = new Mysqli({
2021-08-23 19:25:20 +08:00
host: '', // IP/domain
post: 3306, //port, default 3306
user: '', // username
passwd: '', // password
charset: '', // CHARSET of database, default to utf8 【optional】
db: 'test' // the default database name 【optional】
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// two or more configs
2018-05-29 19:02:33 +08:00
let conn = new Mysqli([
{
2021-08-23 19:25:20 +08:00
host: 'host1', //
...
},
{
host: 'host2', //
2018-05-29 19:02:33 +08:00
...
},
...
])
2021-08-23 19:25:20 +08:00
let db = conn.emit() // use master library
// let db = conn.emit(false, 'test')
// let db = conn.emit(true, 'test')
db.table('test').getAll().then(list => {
console.log(list)
})
2018-05-29 19:02:33 +08:00
```
2021-08-23 19:25:20 +08:00
## Static Function
### escape(val)
> Various types of values of sql can be safely escaped
## Instance APIs
2018-05-29 19:02:33 +08:00
### emit([slave], [db])
2021-08-23 19:25:20 +08:00
* slave `<Boolean>` [optional] use the slave libraries, default to false
* db `<String>` [optional] declare the database's name, default to this config passed.
> This method will return a DB instance.
2018-05-29 19:02:33 +08:00
```javascript
let db = conn.emit(false, 'test')
2021-06-21 20:02:08 +08:00
2021-08-23 19:25:20 +08:00
db.debug = true // debugger mode. supported in ^3.1.0
2021-06-21 20:02:08 +08:00
2018-05-29 19:02:33 +08:00
db
.tableList()
.then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
})
.catch(err => {
2021-08-23 19:25:20 +08:00
console.log(err)
2018-05-29 19:02:33 +08:00
})
```
2021-08-23 19:25:20 +08:00
## DB APIs
> All method will return a `Promise` if not declare.
2018-05-29 19:02:33 +08:00
### query(sql)
2019-09-19 15:19:40 +08:00
* sql `<String>` sql 语句, [必传]
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> Execute the sql if you need.
2018-05-29 19:02:33 +08:00
```javascript
db.query('select * from `student` limit 10').then(result => {
2021-08-23 19:25:20 +08:00
console.log(result)
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// if not declare a database before. you can code like that.
2018-05-29 19:02:33 +08:00
db.query('select * from `test`.`student` limit 10').then(result => {
2021-08-23 19:25:20 +08:00
console.log(result)
2018-05-29 19:02:33 +08:00
})
```
### drop(db)
2021-08-23 19:25:20 +08:00
* db `<String>` [optional] the database's name what you want to delete. It will delete the current connected database if not given.
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> delete the current connected database or what you want.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
db.drop() // delete self connected
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// delete foo
2018-05-29 19:02:33 +08:00
db.drop('foo')
```
### dbList()
2021-08-23 19:25:20 +08:00
> return all the databases, base current account.
2018-05-29 19:02:33 +08:00
```javascript
db.dbList().then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
})
```
### tableList()
2021-08-23 19:25:20 +08:00
> return all the tables of current connect database.
2018-05-29 19:02:33 +08:00
```javascript
db.tableList().then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
})
```
### dbCreate(name, options)
2021-08-23 19:25:20 +08:00
* name `<String>` [required], database's name
* options `<Object>`, [optional], optional setting
- charset `<String>` default to utf8mb4
2018-05-29 19:02:33 +08:00
2018-05-29 19:08:38 +08:00
2021-08-23 19:25:20 +08:00
> create a database, return `true` if success.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
db.dbCreate('foo', { charset: 'utf8' }) // default to utf8mb4
2018-05-29 19:02:33 +08:00
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### tableCreate(name, columns ,options)
2021-08-23 19:25:20 +08:00
* name `<String>` [required], table name
* columns `<Array>`, [required], table columns
- name `<String>` field name, Case sensitive. recommend `lowercase` and `_`
- type `<String>` field type, Not case sensitive
- primary `<Boolean>` is primary key or not.(Only one)
- inc `<Boolean>` is auto_increment or not.(must be primary key and int)
- notnull `<Boolean>` if value can be null
- index `<Boolean>` if create index
- unique `<Boolean>` if create unique index
- default `<Any>` the default value
- update `<Boolean>` auto update(only `datetime` | `timestamp`)
* options `<Object>`, [optional], other configs
- charset `<String>` default to utf8mb4
- engine `<String>` default to InnoDB
2018-05-29 19:02:33 +08:00
2018-05-29 19:08:38 +08:00
2021-08-23 19:25:20 +08:00
> create a table, return `true` if success.
2018-05-29 19:02:33 +08:00
```javascript
db.tableCreate('student',
[
{
name: 'id',
type: 'int(5)',
primary: true,
inc: true
},
{
name: 'name',
type: 'varchar(64)',
index: true
},
{
name: 'age',
type: 'int(3)'
},
{
name: 'sex',
type: 'tinyint(1)',
index: true
},
...
2019-09-19 15:19:40 +08:00
])
2018-05-29 19:02:33 +08:00
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### table(name)
2021-08-23 19:25:20 +08:00
* name `<String>` [required], table name
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> select a table. return a Table instance.
2018-05-29 19:02:33 +08:00
```javascript
let table = db.table('student') //
```
2018-05-29 19:08:38 +08:00
2021-08-23 19:25:20 +08:00
## TABLE APIs
2018-05-29 19:02:33 +08:00
### leftJoin(tables)
2021-08-23 19:25:20 +08:00
* tables `<Array>` left join one or more tables.
- table `<String>` table name
- on `<String>` condition
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
```javascript
db.table('student')
.leftJoin([
{
table: 'classroom',
on: 'student.classroom = classroom.id'
},
...
])
```
2018-05-29 19:08:38 +08:00
### rightJoin(tables)
2021-08-23 19:25:20 +08:00
> see leftJoin()
2018-05-29 19:02:33 +08:00
2018-05-29 19:08:38 +08:00
### join(tables)
2021-08-23 19:25:20 +08:00
> see leftJoin()
2018-05-29 19:02:33 +08:00
2018-05-29 19:08:38 +08:00
### filter(options)
2019-09-19 15:19:40 +08:00
* options `<Object>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> Some keywords in `options`.
> * `$and`
> * `$or`
> * `$like` fuzzy query
> * `$sql` Use the result of a certain sql statement as the query condition
> * `$in`
> * `$between`
> * `$lt`
> * `$lte`
> * `$gt`
> * `$gte`
> * `$eq` equal
> * `$ne` not equal
2018-05-29 19:02:33 +08:00
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
2021-08-23 19:25:20 +08:00
.filter({ id: 1234 }) // a simple search. find the item which it's id is 1234
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// find the items which it's name starts with Lee
.filter({ name: { $like: 'Lee%' } })
// you can use sql to search items if needs
2018-05-29 19:02:33 +08:00
.filter({ name: { $sql: 'IS NULL' } })
2018-07-17 11:35:42 +08:00
.filter({ score: { $sql: 'score + 1' } })
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// find the items which it's id is 11,13,29
.filter({ id: { $in: [11, 13, 29] } })
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// find all items which it's age is between 15 and 17
.filter({ age: { $between: [15, 17] } })
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// find all items which it's age is smaller than 16
.filter({ age: { $lt: 16 } })
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// find all items which it's age is smaller than 16 and elder than 13
.filter({ age: { $lt: 16, $gt: 13 } })
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// ***** multi conditions *********
// find all which it's name starts with Lee, and it's age is elder than 15, and it'sex is 1
.filter({ name: { $like: 'Lee%' }, age: { $gt: 15 }, sex: 1 })
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// find all which it's name starts with Lee, or it's age is elder than 15
2018-05-29 19:02:33 +08:00
.filter({
2021-08-23 19:25:20 +08:00
$or: [{ name: { $like: 'Lee%' } }, { age: { $gt: 15 } }]
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// find all which it's name starts with Lee, or it's age is elder than 15 but it'sex is 1
2018-05-29 19:02:33 +08:00
.filter({
2021-08-23 19:25:20 +08:00
$or: [
{ name: { $like: 'Lee%' } }, // condition 1
{ age: { $gt: 15 }, sex: 1 } // condition 2
]
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// find all which it's name starts with Lee or it's age is elder than 15, but it'sex must be 1
2018-05-29 19:02:33 +08:00
.filter({
$and: [
{
$or: [{ name: { $like: '李%' } }, { age: { $gt: 15 } }]
},
{ sex: 1 }
]
})
```
2018-05-29 19:08:38 +08:00
### sort(keys)
2019-09-19 15:19:40 +08:00
* keys `<Object>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> sort the result by fields.
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
2021-08-23 19:25:20 +08:00
.sort({ age: -1 }) // -1: Reverse sequence, 1: Positive sequence
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// sort the age(Reverse sequence), then sort the id(Positive sequence)
2018-05-29 19:02:33 +08:00
.sort({ age: -1, id: 1 })
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### skip(num)
2019-09-19 15:19:40 +08:00
* num `<Number>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> skip the nums from match list.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
db.table('student').skip(10) // return this 10th from match list
2018-05-29 19:02:33 +08:00
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### limit(num)
2019-09-19 15:19:40 +08:00
* num `<Number>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> limit the nums of the result.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
db.table('student').limit(10) // only 10 items will be return
2018-05-29 19:02:33 +08:00
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### slice(start, end)
2019-09-19 15:19:40 +08:00
* start `<Number>`
* end `<Number>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> truncate the result. a friendly method for `skip` and `limit`
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
2021-08-23 19:25:20 +08:00
.slice(11, 20)
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
// equal to
2018-05-29 19:02:33 +08:00
.skip(11)
.limit(10)
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### withFields(fields)
2021-08-23 19:25:20 +08:00
* fields `<Array>` [optional], default to all fields;
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> declare the fields what you need.
2018-05-29 19:02:33 +08:00
```javascript
db.table('student').withFields(['id', 'name', 'sex']) // 只取 学号,姓名,性别3个字段返回
2021-06-21 20:02:08 +08:00
2021-08-23 19:25:20 +08:00
// equal to
db.table('student').withFields('id', 'name', 'sex') // supported in ^3.1.0
2018-05-29 19:02:33 +08:00
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### getAll([ids])
2021-08-23 19:25:20 +08:00
* ids `<Array>` [optional], return all items which you search.
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
.withFields(['id', 'name', 'sex'])
.getAll()
.then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// find the students which it's id is 11,13,28
2018-05-29 19:02:33 +08:00
db
.table('student')
.withFields(['id', 'name', 'sex'])
.getAll([11, 13, 28])
.then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// equal to
2018-05-29 19:02:33 +08:00
db
.table('student')
.withFields(['id', 'name', 'sex'])
.filter({ id: { $in: [11, 13, 28] } })
.getAll()
.then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### get(id)
2021-08-23 19:25:20 +08:00
* id `<Any>` [optional], the ID you want to search。
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> return only one item.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
// find the student which it's id is 11
2018-05-29 19:02:33 +08:00
db
.table('student')
.withFields(['id', 'name', 'sex'])
.getAll(11)
.then(doc => {
2021-08-23 19:25:20 +08:00
console.log(doc)
2018-05-29 19:02:33 +08:00
})
2021-08-23 19:25:20 +08:00
// equal to
2018-05-29 19:02:33 +08:00
db
.table('student')
.withFields(['id', 'name', 'sex'])
.filter({ id: 11 })
.get()
.then(doc => {
2021-08-23 19:25:20 +08:00
console.log(doc)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### count()
2021-08-23 19:25:20 +08:00
> count the num of result.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
// get the num which it's name starts with Lee
2018-05-29 19:02:33 +08:00
db
.table('student')
2021-08-23 19:25:20 +08:00
.filter({name: {$like: 'Lee%'}})
2018-05-29 19:02:33 +08:00
.count()
.then(total => {
2021-08-23 19:25:20 +08:00
console.log(total)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### insert(doc)
2021-08-23 19:25:20 +08:00
* doc `<Object>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> insert data.
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
.insert({
2021-08-23 19:25:20 +08:00
name: 'Tom',
2018-05-29 19:02:33 +08:00
age: 18,
sex: 1,
...
})
2021-08-23 19:25:20 +08:00
.then(res => {
console.log(res)
// {
// fieldCount: 0,
// affectedRows: 1,
// insertId: 4,
// serverStatus: 2,
// warningCount: 0,
// message: '',
// protocol41: true,
// changedRows: 0
// }
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### update(doc)
2021-08-23 19:25:20 +08:00
* doc `<Object>`
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> update the data.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
//
2018-05-29 19:02:33 +08:00
db
.table('student')
2021-08-23 19:25:20 +08:00
.filter({name: 'Tom'})
2018-05-29 19:02:33 +08:00
.update({
age: 17
})
2021-08-23 19:25:20 +08:00
.then(res => {
console.log(res)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
### remove()
2021-08-23 19:25:20 +08:00
> delete items.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
2018-05-29 19:02:33 +08:00
db
.table('student')
.filter({name: {$sql: 'IS NULL'}})
.remove()
2021-08-23 19:25:20 +08:00
.then(res => {
console.log(res)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
### drop()
2021-08-23 19:25:20 +08:00
> delete current table. it is `danger action`.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
//
2018-05-29 19:02:33 +08:00
db
.table('student')
.drop()
.then(result => {
2021-08-23 19:25:20 +08:00
console.log(result)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
### renameTo(name)
2021-08-23 19:25:20 +08:00
> rename current table.
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
.renameTo('student_bac')
.then(result => {
2021-08-23 19:25:20 +08:00
console.log(result)
2018-05-29 19:02:33 +08:00
})
```
### indexList()
2021-08-23 19:25:20 +08:00
> return the index list.
2018-05-29 19:02:33 +08:00
```javascript
db
.table('student')
.indexList()
.then(list => {
2021-08-23 19:25:20 +08:00
console.log(list)
2018-05-29 19:02:33 +08:00
// {
// name,
// column,
// unique,
// cardinality,
// collation,
// }
})
```
2018-05-29 19:08:38 +08:00
2018-05-29 19:02:33 +08:00
### indexDrop(name)
2021-08-23 19:25:20 +08:00
* name `<String>` index name
2018-05-29 19:02:33 +08:00
2021-08-23 19:25:20 +08:00
> delete an index
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
//
2018-05-29 19:02:33 +08:00
db
.table('student')
.indexDrop('name_idx')
.then(result => {
2021-08-23 19:25:20 +08:00
console.log(result)
2018-05-29 19:02:33 +08:00
})
```
2018-05-29 19:08:38 +08:00
2021-08-23 19:25:20 +08:00
### indexCreate(name, options)
2019-09-19 15:19:40 +08:00
* name `<String>` 索引名
* options `<Object>` 索引的配置
- field `<String>` 该索引绑定的字段
- unique `<Bollean>` 是否是唯一索引
2018-05-29 19:02:33 +08:00
2018-05-29 19:08:38 +08:00
2021-08-23 19:25:20 +08:00
> create an index for a field.
2018-05-29 19:02:33 +08:00
```javascript
2021-08-23 19:25:20 +08:00
//
2018-05-29 19:02:33 +08:00
db
.table('student')
.indexCreate('name_idx', {field: 'name'})
.then(result => {
2021-08-23 19:25:20 +08:00
console.log(result)
2018-05-29 19:02:33 +08:00
})
```