mariadb¶
This page explains how to use ts-sql-query
with the mariadb driver. It covers two approaches: using a connection pool or using a single connection directly.
Do not share connections between requests
A ts-sql-query
connection object — along with the query runner instances passed to its constructor — represents a dedicated connection to the database.
Therefore, you must not share the same connection object between concurrent HTTP requests. Instead, create a new connection object for each request, along with its own query runners.
Even if the query runner internally uses a connection pool, the ts-sql-query
connection still represents a single active connection, acquired from the pool. It must be treated as such and never reused across requests.
Using a connection pool¶
Enables executing queries through a mariadb connection obtained from a pool.
import { createPool } from "mariadb";
import { MariaDBPoolQueryRunner } from "ts-sql-query/queryRunners/MariaDBPoolQueryRunner";
const pool = createPool({
host: 'mydb.com',
user: 'myUser',
password: 'myPwd',
database: 'myDB',
connectionLimit: 5
});
async function main() {
const connection = new DBConnection(new MariaDBPoolQueryRunner(pool));
// Do your queries here
}
Using a single connection¶
Enables executing queries through a dedicated mariadb connection.
import { createPool } from "mariadb";
import { MariaDBQueryRunner } from "ts-sql-query/queryRunners/MariaDBQueryRunner";
const pool = createPool({
host: 'mydb.com',
user: 'myUser',
password: 'myPwd',
database: 'myDB',
connectionLimit: 5
});
async function main() {
const mariaDBConnection = await pool.getConnection();
try {
const connection = new DBConnection(new MariaDBQueryRunner(mariaDBConnection));
// Do your queries here
} finally {
mariaDBConnection.release();
}
}