multi-db-connector
v1.0.20
Published
A robust npm module for managing multiple database connections (Snowflake, Neo4j, PostgreSQL, MySQL) and executing queries in parallel
Maintainers
Readme
Multi-Database Connector (TypeScript)
A robust TypeScript npm module for managing multiple database connections (Snowflake, Neo4j, PostgreSQL, MySQL) and executing queries in parallel.
Features
- ✅ TypeScript Support: Fully typed with comprehensive type definitions
- 🔄 Multiple Database Support: Snowflake, Neo4j, PostgreSQL, MySQL
- ⚡ Parallel Query Execution: Execute queries across multiple databases simultaneously
- 🔌 Connection Pooling: Efficient connection management for each database type
- 🔁 Automatic Retry: Built-in retry mechanism for failed queries
- 🛡️ Type Safety: Full TypeScript support with interfaces and type checking
- 📦 Easy to Use: Simple, intuitive API
Installation
npm install multi-db-connectorTypeScript Development Workflow
For hot-reloading during development, use:
npm run devThis uses
nodemonandts-nodeto watch and reload.tsfiles automatically.To build the project:
npm run buildTo run the compiled server:
npm run start
Troubleshooting
- Unknown file extension ".ts": Make sure you are using
ts-nodefor development, notnodedirectly on.tsfiles. - ReferenceError: Cannot access 'dotenv' before initialization: Ensure all import statements are at the top of your
.tsfiles before any code execution. - TypeError: MultiDatabaseQueryManager is not a constructor: Use the correct import syntax:
import { MultiDatabaseQueryManager } from 'multi-db-connector';
TypeScript Usage
Basic Example
import { MultiDatabaseQueryManager } from 'multi-db-connector';
const manager = new MultiDatabaseQueryManager();
// Add connections
await manager.addConnection('snowflake', 'snowflake', {
account: 'your-account',
username: 'your-username',
password: 'your-password',
warehouse: 'COMPUTE_WH',
database: 'ANALYTICS',
schema: 'PUBLIC'
});
await manager.addConnection('graph', 'neo4j', {
uri: 'neo4j://localhost:7687',
username: 'neo4j',
password: 'password'
});
// Execute queries
const results = await manager.executeQuery('snowflake',
'SELECT * FROM customers LIMIT 10'
);
console.log(results);
// Close connections
await manager.closeAllConnections();Type Definitions
The package includes comprehensive TypeScript type definitions:
import {
MultiDatabaseQueryManager,
DatabaseType,
DatabaseCredentials,
QueryRequest,
QueryResult,
SnowflakeCredentials,
Neo4jCredentials,
PostgreSQLCredentials,
MySQLCredentials
} from 'multi-db-connector';Supported Databases
Snowflake
import { SnowflakeCredentials } from 'multi-db-connector';
const credentials: SnowflakeCredentials = {
account: 'your-account',
username: 'your-username',
password: 'your-password',
warehouse: 'COMPUTE_WH',
database: 'ANALYTICS',
schema: 'PUBLIC',
role: 'ANALYST' // optional
};
await manager.addConnection('snowflake', 'snowflake', credentials);Neo4j
import { Neo4jCredentials } from 'multi-db-connector';
const credentials: Neo4jCredentials = {
uri: 'neo4j://localhost:7687',
username: 'neo4j',
password: 'password',
database: 'neo4j', // optional
maxPoolSize: 50, // optional
connectionTimeout: 30000 // optional
};
await manager.addConnection('graph', 'neo4j', credentials);PostgreSQL
import { PostgreSQLCredentials } from 'multi-db-connector';
const credentials: PostgreSQLCredentials = {
host: 'localhost',
port: 5432, // optional, defaults to 5432
database: 'mydb',
user: 'postgres',
password: 'password',
maxConnections: 20, // optional
idleTimeout: 30000, // optional
connectionTimeout: 10000 // optional
};
await manager.addConnection('postgres', 'postgresql', credentials);MySQL
import { MySQLCredentials } from 'multi-db-connector';
const credentials: MySQLCredentials = {
host: 'localhost',
port: 3306, // optional, defaults to 3306
database: 'mydb',
user: 'root',
password: 'password',
maxConnections: 10 // optional
};
await manager.addConnection('mysql', 'mysql', credentials);API Reference
MultiDatabaseQueryManager
Methods
addConnection(connectionId: string, type: DatabaseType, credentials: DatabaseCredentials): Promise<void>
Add a new database connection.
executeQuery(connectionId: string, query: string, params?: any[]): Promise<any[]>
Execute a single query on a specific connection.
executeQueriesParallel(connectionId: string, queries: (string | QueryObject)[]): Promise<QueryResult[]>
Execute multiple queries in parallel on the same connection.
executeMultiConnectionQueries(queryRequests: QueryRequest[]): Promise<QueryResult[]>
Execute queries across multiple connections in parallel.
executeQueryWithRetry(connectionId: string, query: string, params?: any[], maxRetries?: number): Promise<any[]>
Execute a query with automatic retry on failure.
getConnectionInfo(connectionId: string): ConnectionInfo | null
Get information about a specific connection.
listConnections(): string[]
List all active connection IDs.
closeConnection(connectionId: string): Promise<void>
Close a specific connection.
closeAllConnections(): Promise<void>
Close all connections.
getActiveQueryCount(): number
Get count of active queries.
getSupportedDatabases(): string[]
Get list of supported database types.
Development
Building from Source
# Install dependencies
npm install
# Build TypeScript
npm run build
# Watch mode
npm run build:watch
# Run tests
npm test
# Clean build artifacts
npm run cleanProject Structure
multi-db-connector/
├── adapters/
│ ├── BaseAdapter.ts # Abstract base adapter class
│ ├── SnowflakeAdapter.ts # Snowflake implementation
│ ├── Neo4jAdapter.ts # Neo4j implementation
│ ├── PostgreSQLAdapter.ts # PostgreSQL implementation
│ └── MySQLAdapter.ts # MySQL implementation
├── index.ts # Main entry point
├── examples.ts # Usage examples
├── test.ts # Test suite
├── tsconfig.json # TypeScript configuration
└── package.json # Package configurationExamples
See the examples.ts file for comprehensive usage examples including:
- Basic usage with multiple database types
- Cross-database analytics
- Neo4j graph queries
- Multi-region Snowflake queries
- PostgreSQL + MySQL integration
- Error handling and retry mechanisms
TypeScript Configuration
The project uses strict TypeScript settings:
- Strict null checks
- No implicit any
- Strict function types
- No unused locals/parameters
- Full type inference
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
v1.0.3 (TypeScript Conversion)
- ✅ Converted entire codebase to TypeScript
- ✅ Added comprehensive type definitions
- ✅ Added type-safe interfaces for all database credentials
- ✅ Improved IDE autocomplete and type checking
- ✅ Added source maps for debugging
- ✅ Added declaration files for npm package consumers
Publishing a New Version
To publish a new version of the package to npm:
# Clean previous build artifacts
npm run clean ; npm run build ; npm version patch ;npm publish