UNPKG

@js-ak/db-manager

Version:
113 lines (112 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseSequence = void 0; const index_js_1 = require("../model/index.js"); /** * @experimental * * A generic class for handling sequences, which can be used with models that extend the `BaseSequence` model. * This class provides methods to get and set sequence values, increment sequences, and restart sequences. */ class BaseSequence { #name; /** * The model associated with this domain. */ model; /** * Creates an instance of `BaseSequence`. * * @param data - The data containing the model. * * @throws {Error} If the provided model does not extend the `BaseSequence` model. */ constructor(data) { if (!(data.model instanceof index_js_1.BaseSequence)) { throw new Error("You need pass data.model extended of PG.Model.BaseSequence"); } this.model = data.model; this.#name = this.model.name; } /** * Retrieves the name of the sequence. * * @returns The name of the sequence. */ get name() { return this.#name; } /** * Sets the pool client in the current class. * * @experimental * * @param client - The client connection to set. * * @returns A new instance of the current class with the updated client. */ setClientInCurrentClass(client) { return new this.constructor({ model: this.model.setClientInCurrentClass(client), }); } /** * Sets the pool client in the base class. * * @experimental * * @param client - The client connection to set. * * @returns A new instance of the BaseSequence class with the updated client. */ setClientInBaseClass(client) { return new BaseSequence({ model: this.model.setClientInBaseClass(client) }); } /** * Retrieves the current value of the sequence. * * @returns The current value of the sequence or null if no value is set. */ async getCurrentValue() { return this.model.getCurrentValue(); } /** * Retrieves the next value of the sequence. * * @returns The next value of the sequence. */ async getNextValue() { return this.model.getNextValue(); } /** * Increments the sequence by a given value. * * @param value - The value to increment the sequence by. * * @returns */ async incrementBy(value) { return this.model.incrementBy(value); } /** * Restarts the sequence with a given value. * * @param value - The value to restart the sequence with. * * @returns */ async restartWith(value) { return this.model.restartWith(value); } /** * Sets the sequence to a specific value. * * @param value - The value to set the sequence to. * * @returns */ async setValue(value) { return this.model.setValue(value); } } exports.BaseSequence = BaseSequence;