UNPKG

@js-ak/db-manager

Version:
200 lines (199 loc) 9.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseView = void 0; const index_js_1 = require("../model/index.js"); /** * A class representing a base view with generic type parameters for handling database operations. * * @experimental */ class BaseView { #name; #coreFields; /** * The model associated with this domain. */ model; /** * Initializes a new instance of the `BaseView` class. * * @param data - The domain data object containing the model. * * @throws {Error} If `data.model` is not an instance of `Model`. */ constructor(data) { if (!(data.model instanceof index_js_1.BaseView)) { throw new Error("You need pass data.model extended of PG.Model.BaseView"); } this.model = data.model; this.#name = this.model.name; this.#coreFields = this.model.coreFields; } /** * Gets the name of the database view. * * @returns The name of the view. */ get name() { return this.#name; } /** * Gets the fields of the database view. * * @returns An array of field names in the view. */ get coreFields() { return this.#coreFields; } /** * Compare query operations for the view. */ compareQuery = { /** * Compare query of `Retrieves an array of records based on the specified search parameters`. * * @param options - The options for retrieving records. * @param options.params - The search parameters to match records. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * @param [options.selected] - The fields to return for each matched record. * @param [options.pagination] - The pagination options. * @param [options.order] - The sorting options. * * @returns An object containing the SQL query string and the values for the parameters. */ getArrByParams: (options) => this.model.compareQuery.getArrByParams({ $and: options.params, $or: options.paramsOr }, options.selected, options.pagination, options.order), /** * Compare query of `Gets the count of records that match the specified search parameters`. * * @param options - The options for filtering records. * @param options.params - The search parameters to match records. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * * @returns An object containing the SQL query string and the values for the parameters. */ getCountByParams: (options) => this.model.compareQuery.getCountByParams({ $and: options.params, $or: options.paramsOr }), /** * Compare query of `Retrieves a single record based on the specified search parameters`. * * @param options - The options for retrieving the record. * @param options.params - The search parameters to match the record. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * @param [options.selected] - The fields to return for the matched record. * * @returns An object containing the SQL query string and the values for the parameters. */ getOneByParams: (options) => this.model.compareQuery.getOneByParams({ $and: options.params, $or: options.paramsOr }, options.selected), /** * Constructs a SQL stream query for selecting records based on the provided search parameters. * This version is optimized for large datasets where streaming is preferred over loading everything into memory. * * @param options - The options for retrieving records. * @param options.params - The search parameters to match records. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * @param [options.selected] - The fields to return for each matched record. * @param [options.pagination] - The pagination options. * @param [options.order] - The sorting options. * * @returns The SQL query and its parameter values for use with a streaming interface. */ streamArrByParams: (options) => this.model.compareQuery.streamArrByParams({ $and: options.params, $or: options.paramsOr }, options.selected, options.pagination, options.order), }; /** * 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 BaseView class with the updated client. */ setClientInBaseClass(client) { return new BaseView({ model: this.model.setClientInBaseClass(client) }); } /** * Retrieves an array of records based on the specified search parameters. * * @param options - The options for retrieving records. * @param options.params - The search parameters to match records. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * @param [options.selected] - The fields to return for each matched record. * @param [options.pagination] - The pagination options. * @param [options.order] - The sorting options. * @param options.order.orderBy - The field by which to sort the results. * @param options.order.ordering - The ordering direction (e.g., ASC, DESC). * * @returns A promise that resolves to an array of records with the selected fields. */ async getArrByParams(options) { return this.model.getArrByParams({ $and: options.params, $or: options.paramsOr }, options.selected, options.pagination, options.order); } /** * Gets the count of records that match the specified search parameters. * * @param options - The options for filtering records. * @param options.params - The search parameters to match records. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * * @returns A promise that resolves to the number of matching records. */ async getCountByParams(options) { return this.model.getCountByParams({ $and: options.params, $or: options.paramsOr }); } /** * Retrieves a single record based on the specified search parameters. * * @param options - The options for retrieving the record. * @param options.params - The search parameters to match the record. * @param [options.paramsOr] - An optional array of search parameters, where at least one must be matched. * @param [options.selected] - The fields to return for the matched record. * * @returns A promise that resolves to an object containing either a message (if not found) or the matched record. */ async getOneByParams(options) { const one = await this.model.getOneByParams({ $and: options.params, $or: options.paramsOr }, options.selected); if (!one) return { message: `Not found from ${this.model.name}` }; return { one }; } /** * Streams records from the database based on the specified search parameters. * * This method returns a readable stream of records that match the given filter conditions. * Useful for efficiently processing large datasets without loading them entirely into memory. * * @param options - The options for retrieving records. * @param options.params - The search parameters to match all (AND condition). * @param [options.paramsOr] - An optional array of search parameters where at least one must match (OR condition). * @param [options.selected] - The list of fields to include in each returned record. * @param [options.pagination] - The pagination options to control result limits and offsets. * @param [options.order] - The sorting rules for the result set. * @param options.order[].orderBy - The field to sort by. * @param options.order[].ordering - The sorting direction (`ASC` or `DESC`). * * @param [streamOptions] - Optional configuration for the stream behavior: * - `batchSize`: Number of rows fetched from the database per batch. * - `highWaterMark`: Maximum number of rows buffered in memory. * - `rowMode`: If set to `"array"`, rows will be returned as arrays instead of objects. * - `types`: Custom type parser map for Postgres types. * * @returns A readable stream emitting records of type `Pick<VG["CoreFields"], T>` on the `"data"` event. */ async streamArrByParams(options, streamOptions) { return this.model.streamArrByParams({ $and: options.params, $or: options.paramsOr }, options.selected, options.pagination, options.order, streamOptions); } } exports.BaseView = BaseView;