pooling-mysql-mongo
v1.0.1
Published
`pooling` is a Node.js package for efficient connection pooling for MySQL and MongoDB databases. It offers customizable options for connection limits and pooling sizes, improving performance and scalability for database interactions.
Downloads
4
Readme
Pooling
pooling
is a Node.js package for efficient connection pooling for MySQL and MongoDB databases. It offers customizable options for connection limits and pooling sizes, improving performance and scalability for database interactions.
Installation
To install the package, use npm:
npm install pooling-mysql-mongo
Usage
MySQL Pooling
1. Import the MySQLPool class:
import MySQLPool from 'pooling/dist/mysql-pool';
2. Create an instance of MySQLPool with your configuration:
const mysqlConfig = {
host: 'localhost',
user: 'root',
password: 'password',
database: 'test',
connectionLimit: 15
};
const mysqlPool = new MySQLPool(mysqlConfig);
3. Run queries using the pool:
async function testMySQL() {
try {
const result = await mysqlPool.query('SELECT 1 + 1 AS solution');
console.log('MySQL Result:', result);
} catch (error) {
console.error('Error:', error);
} finally {
await mysqlPool.close();
}
}
testMySQL();
MongoDB Pooling
1. Import the MongoPool class:
import MongoPool from 'pooling/dist/mongo-pool';
Create an instance of MongoPool with your configuration:
typescript
Copy code
const mongoConfig = {
host: 'localhost',
port: 27017,
dbName: 'test',
username: 'yourUsername', // Optional
password: 'yourPassword', // Optional
maxPoolSize: 20 // Optional
};
const mongoPool = new MongoPool(mongoConfig);
2. Interact with the MongoDB database:
async function testMongo() {
try {
const db = await mongoPool.getDb();
const result = await db.collection('test').findOne({});
console.log('MongoDB Result:', result);
} catch (error) {
console.error('Error:', error);
} finally {
await mongoPool.close();
}
}
testMongo();
API
MySQLPool
* Constructor: new MySQLPool(config: MySQLConfig)
* Methods:
async getConnection(): Gets a connection from the pool.
async query(sql: string, values?: any): Executes a query.
async close(): Closes the pool.
MongoPool
* Constructor: new MongoPool(config: MongoConfig)
* Methods:
async connect(): Connects to the database if not already connected.
async getDb(): Returns the database instance.
async close(): Closes the connection.
Contributing
If you would like to contribute to this project, please fork the repository and submit a pull request with your changes. Contributions are welcome!
Contact
For any questions or issues, please open an issue on the GitHub repository or contact the maintainer at [email protected]