@devbro/sql-generator
Version:
generic sql generator
108 lines • 3.98 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var PostgresqlConnection_exports = {};
__export(PostgresqlConnection_exports, {
PostgresqlConnection: () => PostgresqlConnection
});
module.exports = __toCommonJS(PostgresqlConnection_exports);
var import_Connection = require("../../Connection");
var import_pg2 = require("pg");
var import_Query = require("../../Query");
var import_PostgresqlQueryGrammar = require("./PostgresqlQueryGrammar");
var import_Schema = require("../../Schema");
var import_PostgresqlSchemaGrammar = require("./PostgresqlSchemaGrammar");
var import_pg_cursor = __toESM(require("pg-cursor"));
class PostgresqlConnection extends import_Connection.Connection {
connection;
static pool;
static defaults = {
port: 5432,
ssl: false,
max: 20,
idleTimeoutMillis: 1,
// wait X milli seconds before closing an idle/released connection
connectionTimeoutMillis: 3e4,
// wait up to 30 seconds to obtain a new connection
maxUses: 7500
};
constructor(params) {
super();
if (!PostgresqlConnection.pool) {
PostgresqlConnection.pool = new import_pg2.Pool({ ...PostgresqlConnection.defaults, ...params });
}
}
async connect() {
this.connection = await PostgresqlConnection.pool.connect();
return true;
}
async runQuery(sql) {
const result = await this.connection?.query(sql.sql, sql.bindings);
return result?.rows;
}
async runCursor(sql) {
return this.connection?.query(new import_pg_cursor.default(sql.sql, sql.bindings));
}
async disconnect() {
await this.connection?.release();
return true;
}
getQuery() {
return new import_Query.Query(this, new import_PostgresqlQueryGrammar.PostgresqlQueryGrammar());
}
getSchema() {
return new import_Schema.Schema(this, new import_PostgresqlSchemaGrammar.PostgresqlSchemaGrammar());
}
async beginTransaction() {
if (!this.connection) {
throw new Error("No active connection to begin a transaction.");
}
await this.connection.query("BEGIN");
}
async commit() {
if (!this.connection) {
throw new Error("No active connection to commit a transaction.");
}
await this.connection.query("COMMIT");
}
async rollback() {
if (!this.connection) {
throw new Error("No active connection to rollback a transaction.");
}
await this.connection.query("ROLLBACK");
}
static async destroy() {
PostgresqlConnection.pool.end();
return;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PostgresqlConnection
});
//# sourceMappingURL=PostgresqlConnection.js.map