liteq
Version:
QueryBuilder for JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle and more. Works in NodeJS.
72 lines (68 loc) • 2 kB
JavaScript
/**
* @Author: richen
* @Date: 2018-02-09 15:46:34
* @Copyright (c) - <richenlin(at)gmail.com>
* @Last Modified by: richen
* @Last Modified time: 2018-07-24 17:22:51
*/
const knex = require('knex');
const adapter = require('../adapter.js');
const helper = require('../helper.js');
const parser = require('../parsers/postgresql.js');
const defaultConfig = {
db_type: 'postgresql',
db_host: '127.0.0.1',
db_port: 5432,
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
}
};
module.exports = class extends adapter {
/**
*
*
* @param {*} config
*/
init(config) {
this.config = helper.extend(defaultConfig, config, true);
this.knexClient = knex({
client: this.config.db_type || 'postgresql',
connection: {
host: this.config.db_host || '127.0.0.1',
port: this.config.db_port || 5432,
user: this.config.db_user || '',
password: this.config.db_pwd || '',
database: this.config.db_name || '',
client_encoding: this.config.db_charset || 'utf8',
connectionTimeoutMillis: this.config.db_timeout * 1000 || 10000, //try connection timeout
},
pool: { min: 1, max: this.config.db_ext_config.db_pool_size || 10 }
});
this.parser = parser;
}
/**
* 增加数据
*
* @param {any} data
* @param {any} [options={}]
* @returns
*/
add(data, options = {}) {
options.method = 'ADD';
options.alias = undefined;
return this.execute(data, options).then(res => {
if (helper.isArray(res)) {
return res.length === 1 ? res[0] : res;
} else {
return [];
}
});
}
};