node-jdbc-driver
Advanced tools
Comparing version
import IConnectionType from '../IConnectionType'; | ||
import IConnectionConfig from '../IConnectionConfig'; | ||
import { BaseConfig } from '../IConnectionConfig'; | ||
export default class HiveDriver implements IConnectionType { | ||
protected config: IConnectionConfig; | ||
protected config: BaseConfig; | ||
protected driverName: string; | ||
protected driverVersion: string; | ||
driver: string; | ||
constructor(config: IConnectionConfig); | ||
constructor(config: BaseConfig); | ||
get_config: () => { | ||
@@ -10,0 +10,0 @@ url: string; |
@@ -1,2 +0,6 @@ | ||
export default interface IConnectionConfig { | ||
interface OptionalConfig { | ||
minpoolsize?: number; | ||
maxpoolsize?: number; | ||
} | ||
export interface BaseConfig extends OptionalConfig { | ||
host: string; | ||
@@ -8,1 +12,6 @@ port: number; | ||
} | ||
export interface SQLiteConfig extends OptionalConfig { | ||
path: string; | ||
} | ||
type IConnectionConfig<T> = T extends 'S' ? SQLiteConfig : BaseConfig; | ||
export default IConnectionConfig; |
@@ -6,2 +6,3 @@ export default interface IDrivers { | ||
sql: (sql: string) => object; | ||
ddl: (sql: string) => object; | ||
get_version: () => string; | ||
@@ -8,0 +9,0 @@ get_columns: (tableName: string) => object; |
import IConnectionType from "./IConnectionType"; | ||
import IConnectionConfig from "./IConnectionConfig"; | ||
import IDrivers from "./IDrivers"; | ||
import HiveDriver from "./hive/HiveDriver"; | ||
import PostgreSQL from "./postgresql/PostgreSQL"; | ||
import Sqlite from "./sqlite/Sqlite"; | ||
export declare enum ConnectionType { | ||
hive = "H", | ||
postgreSql = "P" | ||
postgreSql = "P", | ||
sqlite = "S" | ||
} | ||
export default class JdbcDriver implements IDrivers { | ||
export default class JdbcDriver<T extends ConnectionType> implements IDrivers { | ||
protected jarPath: string; | ||
protected static connection: any; | ||
protected driverInstance: IConnectionType; | ||
protected type: ConnectionType; | ||
constructor(type: ConnectionType, connectionConfig: IConnectionConfig); | ||
protected type: T; | ||
constructor(type: T, connectionConfig: IConnectionConfig<T>); | ||
protected createDriverInstance(type: T, config: IConnectionConfig<T>): HiveDriver | PostgreSQL | Sqlite; | ||
get_version: () => string; | ||
@@ -23,4 +28,6 @@ get_columns: (tableName: string) => Promise<unknown>; | ||
sql: (sql: string) => Promise<unknown>; | ||
protected close: () => void; | ||
ddl: (sql: string) => Promise<unknown>; | ||
protected close: (connObj: any) => Promise<void>; | ||
protected executeQuery: (sql: any) => Promise<unknown>; | ||
protected executeUpdate: (sql: any) => Promise<unknown>; | ||
protected createStatement: () => Promise<unknown>; | ||
@@ -27,0 +34,0 @@ protected open: () => Promise<unknown>; |
@@ -48,2 +48,3 @@ "use strict"; | ||
var PostgreSQL_1 = __importDefault(require("./postgresql/PostgreSQL")); | ||
var Sqlite_1 = __importDefault(require("./sqlite/Sqlite")); | ||
// Import dependecies | ||
@@ -57,9 +58,9 @@ var jdbc = require('jdbc'); | ||
ConnectionType["hive"] = "H"; | ||
// mysql = 'M', // using mysql driver | ||
ConnectionType["postgreSql"] = "P"; | ||
ConnectionType["sqlite"] = "S"; | ||
})(ConnectionType || (exports.ConnectionType = ConnectionType = {})); | ||
var CType = { | ||
H: HiveDriver_1.default, | ||
// M: '', | ||
P: PostgreSQL_1.default, | ||
S: Sqlite_1.default, | ||
}; | ||
@@ -91,3 +92,3 @@ var JdbcDriver = /** @class */ (function () { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.sql("SELECT COUNT(*) from ".concat(tableName))]; | ||
case 0: return [4 /*yield*/, this.sql("SELECT COUNT(*) as total from ".concat(tableName))]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
@@ -120,35 +121,33 @@ } | ||
}); }; | ||
this.close = function () { | ||
try { | ||
var coon = JdbcDriver.connection.get(_this.type); | ||
if (coon) { | ||
if (coon._reserved && coon._reserved.length) { | ||
coon._reserved[0].conn.close(function (err) { | ||
if (err) | ||
console.log('Reserved Connection closing issues::::'); | ||
else | ||
console.log('Reserved Connection closed'); | ||
}); | ||
} | ||
else { | ||
console.log('Reserved connection not found!'); | ||
} | ||
if (coon._pool && coon._pool.length) { | ||
coon._pool[0].conn.close(function (err) { | ||
if (err) | ||
console.log('Pool Connection closing issues::::'); | ||
else | ||
console.log('Pool Connection closed'); | ||
}); | ||
} | ||
else { | ||
console.log('Pool connection not found!'); | ||
} | ||
JdbcDriver.connection.delete(_this.type); | ||
this.ddl = function (sql) { return __awaiter(_this, void 0, void 0, function () { | ||
var res; | ||
return __generator(this, function (_a) { | ||
try { | ||
res = this.executeUpdate(sql); | ||
return [2 /*return*/, res]; | ||
} | ||
} | ||
catch (err) { | ||
console.log('Connection close error:::::', err); | ||
} | ||
}; | ||
catch (err) { | ||
console.log('Error in ddl:::', err); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
this.close = function (connObj) { return __awaiter(_this, void 0, void 0, function () { | ||
var coon; | ||
return __generator(this, function (_a) { | ||
try { | ||
coon = JdbcDriver.connection.get(this.type); | ||
coon.release(connObj, function (err) { | ||
if (err) | ||
console.log('Connection relase issues::::'); | ||
else | ||
console.log('Connection relase'); | ||
}); | ||
} | ||
catch (err) { | ||
console.log('Connection close error:::::', err); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
this.executeQuery = function (sql) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -158,3 +157,3 @@ var _this = this; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var statement; | ||
var stat; | ||
var _this = this; | ||
@@ -165,4 +164,4 @@ return __generator(this, function (_a) { | ||
case 1: | ||
statement = _a.sent(); | ||
statement.executeQuery(sql, function (err, resultset) { return __awaiter(_this, void 0, void 0, function () { | ||
stat = _a.sent(); | ||
stat[0].executeQuery(sql, function (err, resultset) { return __awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
@@ -180,8 +179,8 @@ return __generator(this, function (_a) { | ||
resolve(rows); | ||
statement.close(function (err) { | ||
stat[0].close(function (err) { | ||
if (err) | ||
console.log('Statement closing issues::::'); | ||
console.log('Statement closing issues...', err); | ||
else { | ||
console.log('Statement closed'); | ||
_this.close(); | ||
console.log('Statement closed.'); | ||
_this.close(stat[1]); | ||
} | ||
@@ -203,2 +202,37 @@ }); | ||
}); }; | ||
this.executeUpdate = function (sql) { return __awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var stat; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.createStatement()]; | ||
case 1: | ||
stat = _a.sent(); | ||
stat[0].executeUpdate(sql, function (err, count) { return __awaiter(_this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (err) | ||
reject(err); | ||
else | ||
resolve(count); | ||
stat[0].close(function (err) { | ||
if (err) | ||
console.log('Statement closing issues::::'); | ||
else { | ||
console.log('Statement closed'); | ||
_this.close(stat[1]); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); })]; | ||
}); | ||
}); }; | ||
this.createStatement = function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -221,3 +255,3 @@ var _this = this; | ||
else | ||
resolve(statement); | ||
resolve([statement, connObj]); | ||
}); | ||
@@ -245,6 +279,10 @@ } | ||
resolve(connection._reserved[0]); | ||
return [3 /*break*/, 3]; | ||
case 1: return [4 /*yield*/, this.init(connection)]; | ||
return [3 /*break*/, 4]; | ||
case 1: | ||
if (!!connection._pool.length) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, this.init(connection)]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: | ||
connection.reserve(function (err, connObj) { | ||
@@ -258,4 +296,4 @@ if (err) { | ||
}); | ||
_a.label = 3; | ||
case 3: return [2 /*return*/]; | ||
_a.label = 4; | ||
case 4: return [2 /*return*/]; | ||
} | ||
@@ -289,3 +327,3 @@ }); | ||
this.type = type; | ||
this.driverInstance = new CType[type](connectionConfig); | ||
this.driverInstance = this.createDriverInstance(type, connectionConfig); | ||
if (!jinst.isJvmCreated()) { | ||
@@ -298,2 +336,9 @@ jinst.addOption('-Xrs'); | ||
} | ||
JdbcDriver.prototype.createDriverInstance = function (type, config) { | ||
var DriverClass = CType[type]; | ||
if (!DriverClass) { | ||
throw new Error("Invalid type ".concat(type)); | ||
} | ||
return new DriverClass(config); | ||
}; | ||
JdbcDriver.connection = new Map(); | ||
@@ -300,0 +345,0 @@ return JdbcDriver; |
import IConnectionType from '../IConnectionType'; | ||
import IConnectionConfig from '../IConnectionConfig'; | ||
import { BaseConfig } from '../IConnectionConfig'; | ||
export default class PostgreSQL implements IConnectionType { | ||
protected config: IConnectionConfig; | ||
protected config: BaseConfig; | ||
protected driverName: string; | ||
protected driverVersion: string; | ||
driver: string; | ||
constructor(config: IConnectionConfig); | ||
constructor(config: BaseConfig); | ||
get_config: () => { | ||
@@ -10,0 +10,0 @@ url: string; |
@@ -63,3 +63,3 @@ "use strict"; | ||
var index_1 = __importStar(require("./index")); | ||
// Set the connection details for the Hive server | ||
// Set the connection details for the Hive and postgreSql | ||
var host = '<host>'; | ||
@@ -70,2 +70,11 @@ var port = 0; // <port> | ||
var password = '<password>'; | ||
// Set the connection details for the sqlite | ||
var path = '<db_path>'; | ||
// Set optional parameters | ||
var minpoolsize = '<min_pool_size>'; | ||
var maxpoolsize = '<max_pool_size>'; | ||
// Available Connection types | ||
index_1.ConnectionType.hive; // for hive connection | ||
index_1.ConnectionType.postgreSql; // for postgreSql connection | ||
index_1.ConnectionType.sqlite; // for sqlite connection | ||
var get_count = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -76,3 +85,3 @@ var jdbc, count; | ||
case 0: | ||
jdbc = new index_1.default(index_1.ConnectionType.postgreSql, { host: host, port: port, database: database, username: username, password: password }); | ||
jdbc = new index_1.default(index_1.ConnectionType.sqlite, { path: '/Users/username/Downloads/demo' }); | ||
return [4 /*yield*/, jdbc.count('test')]; | ||
@@ -79,0 +88,0 @@ case 1: |
{ | ||
"name": "node-jdbc-driver", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "This package is help to use jdbc connection", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
116
README.md
@@ -1,21 +0,39 @@ | ||
# node-jdbc-driver | ||
JDBC API Wrapper for node.js, The API offers methods to open and close database connections, issue query and update statements, and also retrieve information -- such as table and column structure -- in a database-independent way. | ||
# node-jdbc-driver | ||
`node-jdbc-driver` is a JDBC API wrapper for Node.js, providing methods to manage database connections, execute queries and update statements, and retrieve database information such as table and column structures in a database-independent manner. | ||
## Latest Version | ||
- 1.1.2 | ||
 | ||
 | ||
## Installation | ||
- Release: ```npm i --save node-jdbc-driver``` | ||
To install the package, use the following command: | ||
```sh | ||
npm install --save node-jdbc-driver | ||
``` | ||
## Usage | ||
Some mininal examples are given below. | ||
Below are some minimal usage examples demonstrating how to use the `node-jdbc-driver` package. | ||
### Initialize | ||
### Initialization | ||
You can initialize the JDBC driver using either CommonJS or ES6 syntax: | ||
```javascript | ||
// CommonJS | ||
const { default: JdbcDriver, ConnectionType } = require("node-jdbc-driver") | ||
const { default: JdbcDriver, ConnectionType } = require("node-jdbc-driver"); | ||
// ES6 | ||
import JdbcDriver, { ConnectionType } from 'node-jdbc-driver' | ||
import JdbcDriver, { ConnectionType } from 'node-jdbc-driver'; | ||
``` | ||
### Connection Setup | ||
For different types of databases, you need to provide specific connection details. | ||
// Set the connection details for the JDBC connection | ||
#### Available Connection types | ||
```javascript | ||
ConnectionType.hive // for hive connection | ||
ConnectionType.postgreSql // for postgreSql connection | ||
ConnectionType.sqlite // for sqlite connection | ||
``` | ||
#### Hive and PostgreSQL Connection | ||
For Hive and PostgreSQL connections, provide the host, port, database name, username, and password: | ||
```javascript | ||
const host = '<host>'; | ||
@@ -27,33 +45,75 @@ const port = '<port>'; | ||
// Choose connection type | ||
ConnectionType.hive // for hive connection | ||
ConnectionType.postgreSql // for postgreSql connection | ||
// Set optional parameters | ||
const minpoolsize = '<min_pool_size>' | ||
const maxpoolsize = '<max_pool_size>' | ||
// Create jdbc connection | ||
const jdbc = new JdbcDriver(ConnectionType.postgreSql, {host, port, database, username, password}) | ||
// For Hive | ||
const jdbc = new JdbcDriver(ConnectionType.hive, { host, port, database, username, password }); | ||
// For PostgreSql | ||
const jdbc = new JdbcDriver(ConnectionType.postgreSql, { host, port, database, username, password }); | ||
``` | ||
#### SQLite Connection | ||
For SQLite connections, provide the path to the SQLite database file: | ||
```javascript | ||
const path = '<db_path>'; | ||
// check driver version | ||
const version = jdbc.get_version() | ||
// Set optional parameters | ||
const minpoolsize = '<min_pool_size>' | ||
const maxpoolsize = '<max_pool_size>' | ||
// count example | ||
const total = await jdbc.count('<table_name>') | ||
const jdbc = new JdbcDriver(ConnectionType.sqlite, { path }); | ||
``` | ||
// find example | ||
const row = await jdbc.find('<table_name>', '<where_clouse>') | ||
## Basic Operations | ||
Here are some basic operations you can perform using the `node-jdbc-driver`: | ||
// find all example | ||
const rows = await jdbc.findAll('<table_name>') | ||
### Check Driver Version | ||
You can check the driver version using the following method: | ||
```javascript | ||
const version = jdbc.get_version(); | ||
``` | ||
// sql example | ||
const results = await jdbc.sql('<sql_query>') | ||
### Count Example | ||
To count the number of rows in a table: | ||
```javascript | ||
const total = await jdbc.count('<table_name>'); | ||
``` | ||
// list table columns | ||
const columns = await jdbc.get_columns('<table_name>') | ||
### Find Example | ||
To find a single row based on a WHERE clause: | ||
```javascript | ||
const row = await jdbc.find('<table_name>', '<where_clause>'); | ||
``` | ||
// describe table properties | ||
const tblproperties = await jdbc.get_table_properties('<table_name>') | ||
### Find All Example | ||
To retrieve all rows from a table: | ||
```javascript | ||
const rows = await jdbc.findAll('<table_name>'); | ||
``` | ||
### SQL Query Example | ||
To execute a custom SQL query: | ||
```javascript | ||
const results = await jdbc.sql('<sql_query>'); | ||
``` | ||
### DDL Example | ||
To execute Data Definition Language (DDL) queries: | ||
```javascript | ||
const results = await jdbc.ddl('<sql_query>'); | ||
``` | ||
### List Table Columns | ||
To retrieve information about table columns: | ||
```javascript | ||
const columns = await jdbc.get_columns('<table_name>'); | ||
``` | ||
### Describe Table Properties | ||
To get properties of a table: | ||
```javascript | ||
const tblProperties = await jdbc.get_table_properties('<table_name>'); | ||
``` | ||
## License | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
22817564
16.37%26
18.18%689
22.38%119
101.69%1
Infinity%