YDbClient
is a robust and intuitive database client for managing and executing SQL queries efficiently. It provides streamlined methods to perform operations while maintaining flexibility and error handling capabilities.
- Execute complex SQL queries with ease.
- Built-in error handling for database operations.
- Supports query parameterization to prevent SQL injection.
- Dedicated methods for executing single-row and limited-result queries.
- Optimized for both CommonJS (CJS) and ECMAScript Modules (ESM).
To install the package, use the following commands:
# Using npm
npm install ydb-client
# Using yarn
yarn add ydb-client
const { connect } = require("ydb-client");
import { connect } from "ydb-client";
const accessToken = "your-access-token";
const db = connect(accessToken);
(async () => {
try {
const result = await db.execute("SELECT * FROM users");
console.log(result.rows);
} catch (error) {
console.error(error.message);
}
})();
(async () => {
try {
const result = await db.execute("SELECT * FROM users WHERE id = ?", [1]);
console.log(result.rows);
} catch (error) {
console.error(error.message);
}
})();
(async () => {
try {
const result = await db.getSingle("SELECT * FROM users WHERE id = ?", [1]);
console.log(result.row);
} catch (error) {
console.error(error.message);
}
})();
(async () => {
try {
const result = await db.getWithLimit("SELECT * FROM users", 10);
console.log(result.rows);
} catch (error) {
console.error(error.message);
}
})();
Establishes a connection to the database client.
-
accessToken
(string): Your authentication token for accessing the database.
-
connection
: An instance of the database client.
-
execute
: Executes a SQL query. -
getSingle
: Executes a SQL query and returns a single row. -
getWithLimit
: Executes a SQL query with a specified row limit.
Executes a SQL query with optional parameterized values.
-
sql
(string): The SQL query string. -
values
(array, optional): An array of values to parameterize the query.
-
Promise<queryResponse<T>>
: The result of the query execution.
Executes a SELECT query and ensures the result contains a single row.
-
sql
(string): The SQL query string. OnlySELECT
statements are allowed. -
values
(array, optional): An array of values to parameterize the query.
-
YdbError
if the query is not aSELECT
statement.
-
Promise<dataResponse<T>>
: The result containing a single row.
Executes a SELECT query with a limit on the number of rows returned.
-
sql
(string): The SQL query string. OnlySELECT
statements are allowed. -
values
(array, optional): An array of values to parameterize the query. -
limit
(number, optional): The maximum number of rows to retrieve (default is 1000).
-
YdbError
if the query is not aSELECT
statement. -
YdbError
if the limit exceeds 100,000 rows.
-
Promise<queryResponse<T>>
: The result containing limited rows.
An alias for the YDbClient
instance.
Represents the response of a successful database query.
-
rows
(array): The result rows. -
fields
(array): Metadata about the result fields. -
rowCount
(number): The total number of rows returned. -
affectedRows
(number): The number of rows affected by the query. -
insertId
(number): The ID of the last inserted row (for INSERT queries).
Represents the structure of an error response from the database.
-
success
(boolean): Indicates if the operation was successful. -
message
(string): A descriptive error message. -
code
(string): The error code. -
errno
(string): The error number. -
sql
(string): The SQL query that caused the error. -
sqlMessage
(string): The database's error message.
The YdbError
class extends the built-in Error
class and provides additional information about database-related errors.
YDbClient
is licensed under the MIT License. See the LICENSE file for details.