BanglaCode provides production-grade database connectors for PostgreSQL, MySQL, MongoDB, and Redis with built-in connection pooling, async/await support, and SQL injection protection.
Supported Databases
BanglaCode supports four popular databases with both synchronous and asynchronous APIs:
PostgreSQL - Advanced relational database with ACID compliance
MySQL - Popular relational database for web applications
MongoDB - NoSQL document database for flexible schemas
Redis - In-memory data store for caching and real-time apps
Connection Pooling (50-100x Faster)
Connection pooling is crucial for performance. Creating a new database connection for every query is extremely slow (~10-50ms per connection). With connection pooling, you reuse existing connections, making queries 50-100x faster (~0.1-1ms per query).
Connection Pool Example
database_pool.bangbanglacode
1
// Create connection pool (reuses connections for maximum performance)
2
dhoro pool = db_pool_banao("postgres", {
3
"host": "localhost",
4
"port": 5432,
5
"database": "myapp",
6
"user": "admin",
7
"password": "secret"
8
}, 10); // Max 10 connections
9
10
// Get connection from pool (very fast, ~0.1ms)
11
dhoro conn = db_pool_nao(pool);
12
13
// Execute query
14
dhoro users = db_query(conn, "SELECT * FROM users WHERE age > 25");
15
16
// Iterate results
17
ghuriye (dhoro i =0; i <dorghyo(users["rows"]); i = i +1) {
Transactions ensure that multiple database operations either all succeed or all fail together, maintaining data integrity.
postgres_transaction.bangbanglacode
1
// Begin transaction
2
dhoro tx = db_transaction_shuru_postgres(conn);
3
4
chesta {
5
// Transfer money between accounts (atomic operation)
6
db_exec_postgres(conn, "UPDATE accounts SET balance = balance - 100 WHERE id = 1");
7
db_exec_postgres(conn, "UPDATE accounts SET balance = balance + 100 WHERE id = 2");
8
9
// Commit if all operations succeed
10
db_commit_postgres(tx);
11
dekho("Transaction completed successfully!");
12
13
} dhoro_bhul (error) {
14
// Rollback if any operation fails
15
db_rollback_postgres(tx);
16
dekho("Transaction failed and rolled back:", error);
17
}
PostgreSQL Functions
Function
Parameters
Description
db_jukto_postgres
config
Connect to PostgreSQL database
db_query_postgres
conn, sql
Execute SELECT query
db_exec_postgres
conn, sql
Execute INSERT/UPDATE/DELETE
db_proshno_postgres
conn, sql, params
Prepared statement (SQL injection safe)
db_transaction_shuru_postgres
conn
Begin transaction
db_commit_postgres
tx
Commit transaction
db_rollback_postgres
tx
Rollback transaction
MySQL
MySQL is a popular relational database used by millions of websites and applications.
mysql_example.bangbanglacode
1
// Connect to MySQL
2
dhoro conn = db_jukto("mysql", {
3
"host": "localhost",
4
"port": 3306,
5
"database": "webapp",
6
"user": "root",
7
"password": "password"
8
});
9
10
// Complete CRUD operations
11
// CREATE - Insert new user
12
db_proshno(conn, "INSERT INTO users (name, email) VALUES (?, ?)",
13
["Karim", "karim@example.com"]);
14
15
// READ - Query users
16
dhoro users = db_query(conn, "SELECT * FROM users");
17
ghuriye (dhoro i =0; i <dorghyo(users["rows"]); i = i +1) {
18
dekho("User:", users["rows"][i]["name"]);
19
}
20
21
// UPDATE - Update user email
22
db_proshno(conn, "UPDATE users SET email = ? WHERE name = ?",
23
["new@example.com", "Karim"]);
24
25
// DELETE - Delete user
26
db_proshno(conn, "DELETE FROM users WHERE name = ?", ["Karim"]);
27
28
db_bandho(conn);
MongoDB
MongoDB is a NoSQL document database that stores data in flexible JSON-like documents. Perfect for applications with evolving schemas or hierarchical data.
Find Documents
mongodb_find.bangbanglacode
1
// Connect to MongoDB
2
dhoro conn = db_jukto("mongodb", {
3
"host": "localhost",
4
"port": 27017,
5
"database": "mydb"
6
});
7
8
// Find documents matching filter
9
dhoro users = db_khojo_mongodb(conn, "users", {
10
"age": {"$gt": 25}, // Age greater than 25
11
"city": "Dhaka"// City is Dhaka
12
});
13
14
dekho("Found", dorghyo(users["rows"]), "users");
15
16
// Display results
17
ghuriye (dhoro i =0; i <dorghyo(users["rows"]); i = i +1) {
Redis is an in-memory data store used for caching, session management, real-time analytics, and message queues. Extremely fast (sub-millisecond response times).
Key-Value Operations
redis_kv.bangbanglacode
1
// Connect to Redis
2
dhoro conn = db_jukto("redis", {
3
"host": "localhost",
4
"port": 6379
5
});
6
7
// Set key-value
8
db_set_redis(conn, "user:1", "Rahim Ahmed");
9
10
// Set with TTL (time to live - expires after 1 hour)
These functions work with all supported databases (PostgreSQL, MySQL, MongoDB, Redis). The db_jukto function automatically routes to the correct database driver.
Function
Parameters
Description
db_jukto
type, config
Connect to database (type: "postgres", "mysql", "mongodb", "redis")
db_jukto_async
type, config
Connect to database (async, returns promise)
db_bandho
conn
Close connection
db_bandho_async
conn
Close connection (async)
db_query
conn, sql
Execute SELECT query (SQL databases only)
db_query_async
conn, sql
Execute SELECT query async
db_exec
conn, sql
Execute INSERT/UPDATE/DELETE (SQL databases only)
db_exec_async
conn, sql
Execute INSERT/UPDATE/DELETE async
db_proshno
conn, sql, params
Prepared statement (SQL injection safe)
db_proshno_async
conn, sql, params
Prepared statement async
Connection Pool Functions
Function
Parameters
Description
db_pool_banao
type, config, maxConns
Create connection pool
db_pool_nao
pool
Get connection from pool
db_pool_ferot
pool, conn
Return connection to pool (important!)
db_pool_bondho
pool
Close connection pool
db_pool_tothyo
pool
Get pool statistics
Async Database Queries
Use proyash and opekha for async database operations. This is essential for non-blocking I/O and high-performance applications.
async_database.bangbanglacode
1
// Async database function
2
proyash kaj fetchUsers() {
3
// Connect async
4
dhoro conn = opekha db_jukto_async("postgres", {
5
"host": "localhost",
6
"database": "myapp"
7
});
8
9
// Query async
10
dhoro users = opekha db_query_async(conn, "SELECT * FROM users");