npxbase
Version:
A professional and modular database management library for Node.js with support for MySQL, PostgreSQL, and MongoDB.
150 lines (101 loc) โข 3.33 kB
Markdown
# NPXBase
**NPXBase** is a professional, flexible, and modular database management library for Node.js. It provides a unified interface for interacting with multiple database types, such as MySQL, PostgreSQL, and MongoDB. The library is designed to make database operations easier, more efficient, and secure.
## ๐ Features
- **Unified Interface**: Seamlessly interact with various databases through a common API.
- **Adapters for Multiple Databases**: Supports MySQL, PostgreSQL, and MongoDB out of the box.
- **Connection Management**: Provides efficient connection pooling and management.
- **Query Execution**: Simplified methods for executing database queries.
- **Error Handling**: Comprehensive error messages and logging.
- **Modular Design**: Easily extendable to support additional databases.
## ๐ฆ Installation
To install NPXBase, use npm or yarn:
```bash
npm install npxbase
```
or
```bash
yarn add npxbase
```
## ๐ ๏ธ Usage
### Example: Connecting to MySQL
```javascript
const { DBManager, MySQLAdapter } = require("npxbase");
(async () => {
const mysqlAdapter = new MySQLAdapter();
const db = new DBManager(mysqlAdapter);
await db.connect({
host: "localhost",
user: "root",
password: "password",
database: "test_db",
});
const results = await db.query("SELECT * FROM users");
console.log("Results:", results);
await db.disconnect();
})();
```
### Example: Connecting to PostgreSQL
```javascript
const { DBManager, PostgresAdapter } = require("npxbase");
(async () => {
const postgresAdapter = new PostgresAdapter();
const db = new DBManager(postgresAdapter);
await db.connect({
host: "localhost",
user: "postgres",
password: "password",
database: "test_db",
});
const results = await db.query("SELECT * FROM users");
console.log("Results:", results);
await db.disconnect();
})();
```
## ๐ API Reference
### DBManager
The `DBManager` class is the core of NPXBase. It manages connections and executes queries.
#### Methods:
- **`connect(config)`**: Establishes a connection to the database.
- **`disconnect()`**: Closes the connection to the database.
- **`query(sql, params)`**: Executes a SQL query with optional parameters.
### Adapters
- **`MySQLAdapter`**: Adapter for MySQL.
- **`PostgresAdapter`**: Adapter for PostgreSQL.
- **`MongoDBAdapter`** *(planned)*: Adapter for MongoDB (coming soon).
## โ๏ธ Configuration
Each adapter requires a configuration object to establish a connection. Examples:
### MySQL Configuration
```javascript
{
host: "localhost",
user: "root",
password: "password",
database: "test_db",
}
```
### PostgreSQL Configuration
```javascript
{
host: "localhost",
user: "postgres",
password: "password",
database: "test_db",
}
```
## ๐งช Testing
To run tests, install development dependencies and run the test script:
```bash
npm test
```
## ๐ Contributions
Contributions are welcome! If you want to add new features, fix bugs, or improve the documentation, feel free to submit a pull request.
## ๐ License
NPXBase is licensed under the [MIT License](LICENSE).