slim-node-mysql
Version:
MySQL database class to abstract pooling and prepared statements
118 lines • 5.04 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlimNodeMySQL = void 0;
const PoolAlreadyExistsError_1 = require("./errors/PoolAlreadyExistsError");
const SlimNodeMySQLPool_1 = require("./SlimNodeMySQLPool");
/**
* The main class for SlimNodeMySQL to create a new connection to the database and perform queries.
*/
class SlimNodeMySQL {
constructor(config, otherPoolOptions) {
this.config = config;
this.otherPoolOptions = otherPoolOptions;
this.pool = new SlimNodeMySQLPool_1.SlimNodeMySQLPool(this.config, this.otherPoolOptions);
}
/**
* Check if there is an open pool.
* @returns true if the pool is open.
*/
hasOpenPool() {
return this.pool != null;
}
/**
* Re-connect to the database if the close method has been called.
* The constructor auto-connects to the database so this method is only needed if the close method has been called.
*/
connect() {
if (this.pool) {
throw new PoolAlreadyExistsError_1.PoolAlreadyExistsError('The pool has already been initialized. Please close the current pool first or create another instance of SlimNodeMySQL.');
}
this.pool = new SlimNodeMySQLPool_1.SlimNodeMySQLPool(this.config, this.otherPoolOptions);
}
/**
* Executes a create, update, or delete SQL query and returns the result.
* @param sql SQL to run
* @param parameters Prepared statement parameters to to replace in the SQL (keys should match the "@" in the SQL)
* @returns An ExecuteResult object.
*/
execute(sql, parameters) {
return this.pool.execute(sql, parameters);
}
/**
* Query the database for a list of records.
* @param sql The SQL to run to get records.
* @param parameters The prepared statement parameters to replace in the SQL (keys should match the "@" in the SQL)
* @returns A list of records.
*/
query(sql, parameters) {
if (parameters) {
return this.pool.query(sql, parameters);
}
return this.pool.query(sql);
}
/**
* Return a single record from the database or null if no record is found. For large queries, use a LIMIT in the SQL for better performance.
* @param sql The SQL to run to get a single record.
* @param parameters The prepared statement parameters to replace in the SQL (keys should match the "@" in the SQL)
* @returns the record or null.
*/
getOne(sql, parameters) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.pool.query(sql, parameters);
if (data.length === 0) {
return null;
}
return Object.assign({}, data[0]);
});
}
/**
* Get a single value from a record. For large queries, use a LIMIT in the SQL for better performance.
* @param column the column of the record to return.
* @param sql the SQL to run to get the record.
* @param parameters the prepared statement parameters to replace in the SQL (keys should match the "@" in the SQL)
* @returns the value of the column or null if no record is found.
*/
getValue(column, sql, parameters) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.pool.query(sql, parameters);
if (!data || data.length === 0) {
return null;
}
return data[0][column];
});
}
/**
* Check if a given record exists.
* @param sql SQL to run to check if a record exists.
* @param parameters Prepared statement parameters to to replace in the SQL (keys should match the "@" in the SQL)
* @returns boolean value if the record exists.
*/
exists(sql, parameters) {
return __awaiter(this, void 0, void 0, function* () {
const items = yield this.pool.query(sql, parameters);
return items.length > 0;
});
}
/**
* Close the database pool.
*/
close() {
return __awaiter(this, void 0, void 0, function* () {
if (this.pool) {
yield this.pool.close();
}
this.pool = null;
});
}
}
exports.SlimNodeMySQL = SlimNodeMySQL;
//# sourceMappingURL=SlimNodeMySQL.js.map