@js-ak/db-manager
Version:
153 lines (152 loc) • 5.31 kB
JavaScript
import * as connection from "../connection.js";
import { QueryBuilder } from "../query-builder/index.js";
import { setLoggerAndExecutor } from "../helpers/index.js";
/**
* @experimental
*/
export 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 = 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 = 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 = 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 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);
}
}