aios
Version:
Socket client for Aios server
328 lines (326 loc) • 8.89 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const _ = require("lodash");
const dataSourceTypes = {};
function DataSource(server, options) {
options = options || {};
this.options = options;
this.dsn = options.name;
this.type = options.type;
this.server = server;
}
exports.DataSource = DataSource;
// @ts-ignore
DataSource.get = function (type) {
return dataSourceTypes[type];
};
DataSource.prototype.getId = function () {
return this.options.id;
};
DataSource.prototype.getName = function () {
return this.dsn;
};
DataSource.prototype.getType = function () {
return this.type;
};
DataSource.prototype.register = function (fn) {
const self = this;
if (fn && _.isFunction(fn)) {
this.server.command('ds', { method: 'register', spec: self.options }, function (err, response) {
self.options = _.merge(self.options, response.spec);
fn(err, self);
});
}
else {
const response = this.server.command('ds', { method: 'register', spec: self.options }, fn);
this.options = _.merge(this.options, response.spec);
return this;
}
};
DataSource.prototype.unregister = function () {
};
DataSource.prototype.query = function (query) {
};
DataSource.prototype.listTables = function () {
};
DataSource.prototype.listCatalogs = function () {
};
DataSource.prototype.listDatabases = function () {
};
const JdbcDataSource = function (server, options) {
options = options || {};
DataSource.call(this, server, options);
};
dataSourceTypes['jdbc'] = JdbcDataSource;
util_1.inherits(JdbcDataSource, DataSource);
JdbcDataSource.prototype.execute = function (sql, fn) {
const data = {
dsn: this.getName(),
query: {
'@t': 'jdbc.q.exec',
sql: sql
}
};
if (fn && _.isFunction(fn)) {
this.server.command('ds.query', data, function (err, res) {
if (res && res.result && res.result['@t']) {
delete res.result['@t'];
}
if (err) {
fn(err);
}
else {
fn(null, res.result);
}
});
}
else {
const response = this.server.command('ds.query', data, fn);
delete response.result['@t'];
return response.result;
}
};
JdbcDataSource.prototype.select = function (sql, fn) {
return this.query(sql, fn);
};
JdbcDataSource.prototype.query = function (sql, fn) {
const data = {
dsn: this.getName(),
query: {
'@t': 'jdbc.q.select',
sql: sql
}
};
if (fn && _.isFunction(fn)) {
this.server.command('ds.query', data, function (err, res) {
if (err) {
fn(err);
}
else {
fn(null, res.result ? res.result.data : []);
}
});
}
else {
const response = this.server.command('ds.query', data, fn);
return response.result.data;
}
};
JdbcDataSource.prototype.update = function (sql, fn) {
const data = {
dsn: this.getName(),
query: {
'@t': 'jdbc.q.update',
sql: sql
}
};
if (fn && _.isFunction(fn)) {
this.server.command('ds.query', data, function (err, res) {
if (res && res.result && res.result['@t']) {
delete res.result['@t'];
}
if (err) {
fn(err);
}
else {
fn(null, res.result);
}
});
}
else {
const response = this.server.command('ds.query', data, fn);
delete response.result['@t'];
return response.result;
}
};
JdbcDataSource.prototype.executeBatch = function (sqls, fn) {
const data = {
dsn: this.getName(),
query: {
'@t': 'jdbc.q.batch',
sqls: sqls
}
};
if (fn && _.isFunction(fn)) {
this.server.command('ds.query', data, function (err, res) {
if (res && res.result && res.result['@t']) {
delete res.result['@t'];
}
if (err) {
fn(err);
}
else {
fn(null, res.result);
}
});
}
else {
const response = this.server.command('ds.query', data, fn);
delete response.result['@t'];
return response.result;
}
};
const _processCatalogResults = function (res) {
const dbs = [];
if (res && res.result) {
if (res.result.catalogs) {
res.result.catalogs.forEach(function (x) {
const db = { name: x, schemas: [] };
dbs.push(db);
if (res.result.schemas) {
res.result.schemas.forEach(function (s) {
if (s.catalog === x && s.schema) {
db.schemas.push(s.schema);
}
});
}
});
}
}
return dbs;
};
JdbcDataSource.prototype.listCatalogs = function (fn) {
const data = {
dsn: this.getName(),
query: {
'@t': 'jdbc.q.schema'
}
};
if (fn && _.isFunction(fn)) {
this.server.command('ds.query', data, function (err, res) {
const dbs = _processCatalogResults(res);
fn(err, dbs);
});
}
else {
const response = this.server.command('ds.query', data, fn);
return _processCatalogResults(response);
}
};
JdbcDataSource.prototype.listDatabases = function (fn) {
return this.listCatalogs(fn);
};
JdbcDataSource.prototype.listTables = function (selection, fn) {
const data = {
dsn: this.getName(),
query: {
'@t': 'jdbc.q.table',
}
};
if (_.isString(selection)) {
const split = selection.split('.');
if (split.length === 2) {
data.query.catalog = split[1];
data.query.schema = split[0];
}
else {
data.query.catalog = selection;
}
// if dot split
}
else if (_.isObject(selection)) {
data.query = _.merge(data.query, selection);
}
// TODO Catalog must be present
if (fn && _.isFunction(fn)) {
this.server.command('ds.query', data, function (err, res) {
// TODO capture errors
if (err) {
fn(err);
}
else {
fn(null, res.result.tables);
}
});
}
else {
const response = this.server.command('ds.query', data, fn);
return response.result.tables;
}
};
DataSource.prototype.registerAsync = function () {
return new Promise((resolve, reject) => {
this.register((err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
JdbcDataSource.prototype.executeAsync = function (sql) {
return new Promise((resolve, reject) => {
this.execute(sql, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
JdbcDataSource.prototype.updateAsync = function (sql) {
return new Promise((resolve, reject) => {
this.update(sql, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
JdbcDataSource.prototype.queryAsync = function (sql) {
return new Promise((resolve, reject) => {
this.query(sql, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
JdbcDataSource.prototype.selectAsync = function (sql) {
return new Promise((resolve, reject) => {
this.select(sql, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
JdbcDataSource.prototype.listCatalogsAsync = function () {
return new Promise((resolve, reject) => {
this.listCatalogs((err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
JdbcDataSource.prototype.listTablesAsync = function (selection) {
return new Promise((resolve, reject) => {
this.listTables(selection, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
};
//# sourceMappingURL=datasource.js.map
;