A robust npm module for managing multiple database connections (Snowflake, Neo4j, PostgreSQL, MySQL) and executing queries in parallel
npm install multi-db-connectorA robust TypeScript npm module for managing multiple database connections (Snowflake, Neo4j, PostgreSQL, MySQL) and executing queries in parallel.
- ā
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
``bash`
npm install multi-db-connector
- For hot-reloading during development, use:
`bash`
npm run dev
nodemon
This uses and ts-node to watch and reload .ts files automatically.
- To build the project:
`bash`
npm run build
- To run the compiled server:
`bash`
npm run start
- Unknown file extension ".ts": Make sure you are using ts-node for development, not node directly on .ts files..ts
- ReferenceError: Cannot access 'dotenv' before initialization: Ensure all import statements are at the top of your files before any code execution.`
- TypeError: MultiDatabaseQueryManager is not a constructor: Use the correct import syntax:
typescript`
import { MultiDatabaseQueryManager } from 'multi-db-connector';
`typescript
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();
`
The package includes comprehensive TypeScript type definitions:
`typescript`
import {
MultiDatabaseQueryManager,
DatabaseType,
DatabaseCredentials,
QueryRequest,
QueryResult,
SnowflakeCredentials,
Neo4jCredentials,
PostgreSQLCredentials,
MySQLCredentials
} from 'multi-db-connector';
`typescript
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);
`
`typescript
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);
`
`typescript
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);
`
`typescript
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);
`
#### Methods
##### addConnection(connectionId: string, type: DatabaseType, credentials: DatabaseCredentials): Promise
Add a new database connection.
##### executeQuery(connectionId: string, query: string, params?: any[]): Promise
Execute a single query on a specific connection.
##### executeQueriesParallel(connectionId: string, queries: (string | QueryObject)[]): Promise
Execute multiple queries in parallel on the same connection.
##### executeMultiConnectionQueries(queryRequests: QueryRequest[]): Promise
Execute queries across multiple connections in parallel.
##### executeQueryWithRetry(connectionId: string, query: string, params?: any[], maxRetries?: number): Promise
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
Close a specific connection.
##### closeAllConnections(): Promise
Close all connections.
##### getActiveQueryCount(): number
Get count of active queries.
##### getSupportedDatabases(): string[]
Get list of supported database types.
`bashInstall dependencies
npm install
$3
`
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 configuration
`Examples
See the examples.ts file for comprehensive usage examples including:
1. Basic usage with multiple database types
2. Cross-database analytics
3. Neo4j graph queries
4. Multi-region Snowflake queries
5. PostgreSQL + MySQL integration
6. 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
$3
- ā
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:
`bash
Clean previous build artifacts
npm run clean ; npm run build ; npm version patch ;npm publish
``