npxbase v1.0.0
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:
npm install npxbase
or
yarn add npxbase
๐ ๏ธ Usage
Example: Connecting to MySQL
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
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
{
host: "localhost",
user: "root",
password: "password",
database: "test_db",
}
PostgreSQL Configuration
{
host: "localhost",
user: "postgres",
password: "password",
database: "test_db",
}
๐งช Testing
To run tests, install development dependencies and run the test script:
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.
6 months ago