@js-ak/db-manager
Version:
180 lines (179 loc) • 6.54 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSequence = void 0;
const connection = __importStar(require("../connection.js"));
const index_js_1 = require("../query-builder/index.js");
const index_js_2 = require("../helpers/index.js");
/**
* @experimental
*/
class BaseSequence {
#isLoggerEnabled;
#logger;
#executeSql;
#initialArgs;
/**
* The PostgreSQL executor.
* - pg.Pool
* - pg.PoolClient
* - pg.Client
*/
#executor;
name;
constructor(data, dbCreds, options) {
const { client } = options || {};
if (client) {
this.#executor = client;
}
else if (dbCreds) {
this.#executor = connection.getStandardPool(dbCreds);
}
else {
throw new Error("No client or dbCreds provided");
}
this.name = data.name;
this.#initialArgs = { data, dbCreds, options };
const { isLoggerEnabled, logger } = options || {};
const preparedOptions = (0, index_js_2.setLoggerAndExecutor)(this.#executor, { isLoggerEnabled, logger });
this.#executeSql = preparedOptions.executeSql;
this.#isLoggerEnabled = preparedOptions.isLoggerEnabled;
this.#logger = preparedOptions.logger;
}
/**
* Gets the database client for the sequence.
*
* @returns The database client for the sequence.
*/
get pool() {
return this.#executor;
}
/**
* Gets the PostgreSQL executor for the sequence.
*
* @returns The PostgreSQL executor for the sequence.
*/
get executor() {
return this.#executor;
}
/**
* Sets the logger for the sequence.
*
* @param logger - The logger to use for the sequence.
*/
setLogger(logger) {
const preparedOptions = (0, index_js_2.setLoggerAndExecutor)(this.#executor, { isLoggerEnabled: true, logger });
this.#executeSql = preparedOptions.executeSql;
this.#isLoggerEnabled = preparedOptions.isLoggerEnabled;
this.#logger = preparedOptions.logger;
}
/**
* Sets the executor for the sequence.
*
* @param executor - The executor to use for the sequence.
*/
setExecutor(executor) {
const preparedOptions = (0, index_js_2.setLoggerAndExecutor)(executor, { isLoggerEnabled: this.#isLoggerEnabled, logger: this.#logger });
this.#executeSql = preparedOptions.executeSql;
this.#isLoggerEnabled = preparedOptions.isLoggerEnabled;
this.#logger = preparedOptions.logger;
this.#executor = executor;
}
get isLoggerEnabled() {
return this.#isLoggerEnabled;
}
get executeSql() {
return this.#executeSql;
}
/**
* Sets the client in the current class.
*
* @experimental
*
* @param client - The client connection to set.
*
* @returns The current instance with the new connection client.
*/
setClientInCurrentClass(client) {
return new this.constructor({ ...this.#initialArgs.data }, this.#initialArgs.dbCreds ? { ...this.#initialArgs.dbCreds } : undefined, { ...this.#initialArgs.options, client });
}
/**
* Sets the client in the base class.
*
* @experimental
*
* @param client - The client connection to set.
*
* @returns A new instance of the base class with the new connection client.
*/
setClientInBaseClass(client) {
return new BaseSequence({ ...this.#initialArgs.data }, this.#initialArgs.dbCreds ? { ...this.#initialArgs.dbCreds } : undefined, { ...this.#initialArgs.options, client });
}
async getCurrentValue() {
const query = `SELECT last_value FROM ${this.name}`;
const { rows } = await this.#executeSql({ query });
return rows[0] ? rows[0].last_value : null;
}
async getNextValue() {
const query = `SELECT nextval('${this.name}')`;
const { rows } = await this.#executeSql({ query });
if (!rows[0])
throw new Error("Could not get next value");
return rows[0].nextval;
}
async setValue(value) {
const query = `SELECT setval('${this.name}', $1)`;
await this.#executeSql({ query, values: [value] });
}
async incrementBy(value) {
const query = `SELECT setval('${this.name}', nextval('${this.name}') + $1)`;
await this.#executeSql({ query, values: [value] });
}
async restartWith(value) {
const query = `ALTER SEQUENCE ${this.name} RESTART WITH ${Number(value)}`;
await this.#executeSql({ query });
}
queryBuilder(options) {
const { client, isLoggerEnabled, logger, name } = options || {};
return new index_js_1.QueryBuilder(name ?? this.name, client ?? this.#executor, {
isLoggerEnabled: isLoggerEnabled ?? this.#isLoggerEnabled,
logger: logger ?? this.#logger,
});
}
// STATIC METHODS
static getStandardPool(creds, poolName) {
return connection.getStandardPool(creds, poolName);
}
static async removeStandardPool(creds, poolName) {
return connection.removeStandardPool(creds, poolName);
}
static getTransactionPool(creds, poolName) {
return connection.getTransactionPool(creds, poolName);
}
static async removeTransactionPool(creds, poolName) {
return connection.removeTransactionPool(creds, poolName);
}
}
exports.BaseSequence = BaseSequence;