knex入门
const configs = require('../../config');
module.exports = require('knex')({
client: 'mysql',
connection: {
host: configs.mysql.host,
port: configs.mysql.port,
user: configs.mysql.user,
password: configs.mysql.pass,
database: configs.mysql.db,
}
})
基础查询
knex.column('title', 'author', 'year').select().from('books')
// select 'title', 'author', 'year' from 'books'
修改
knex('books')
.where('published_date', '<', 2000)
.update({
status: 'archived',
thisKeyIsSkipped: undefined
})
// update 'books' set 'status' = 'archived' where 'published_date' < 2000
删除
knex('accounts')
.where('activated', false)
.del()
// delete from 'accounts' where 'activated' = false
knex('accounts').truncate(); // 清空表