UNPKG

liteq

Version:

QueryBuilder for JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle and more. Works in NodeJS.

85 lines (80 loc) 2.57 kB
/** * @Author: richen * @Date: 2018-02-09 15:46:34 * @Copyright (c) - <richenlin(at)gmail.com> * @Last Modified by: richen * @Last Modified time: 2019-02-27 22:34:02 */ const knex = require('knex'); const adapter = require('../adapter.js'); const helper = require('../helper.js'); const parser = require('../parsers/base.js'); const defaultConfig = { db_type: 'mysql', db_host: '127.0.0.1', db_port: 3306, db_name: 'test', db_user: '', db_pwd: '', db_prefix: 'think_', db_charset: 'utf8', db_timeout: 30, db_ext_config: { db_pool_size: 10, //连接池大小 db_log_sql: true, //打印sql db_dateStrings: false, } }; module.exports = class extends adapter { /** * * * @param {*} config */ init(config) { this.config = helper.extend(defaultConfig, config, true); //node-mysql2 not support utf8 or utf-8 let charset = (this.config.encoding || '').toLowerCase(); if (charset === 'utf8' || charset === 'utf-8') { this.config.charset = 'utf8mb4'; } const poolSize = this.config.db_ext_config.db_pool_size || 10; this.knexClient = knex({ client: 'mysql2', connection: { host: this.config.db_host || '127.0.0.1', port: this.config.db_port || 3306, user: this.config.db_user || '', password: this.config.db_pwd || '', database: this.config.db_name || '', charset: this.config.charset || 'utf8mb4', dateStrings: this.config.db_ext_config.db_dateStrings || false, connectTimeout: this.config.db_timeout * 1000 || 10000, //try connection timeout }, pool: { min: 1, max: poolSize } }); this.parser = parser; } /** * 执行修改 * * @param {any} data * @param {any} options * @param {number} [times=0] * @returns */ execute(data, options, times = 0) { return this.parser.querySql(this.config, data, options, this.knexClient).then(res => { if (res && res.length === 1) { return res[0]; } return res || []; }).catch(err => { // if server close connection, then retry it if (times < 3 && err.message && err.message.indexOf('PROTOCOL') > -1) { return this.query(options, times + 1); } return Promise.reject(err); }); } };